Commit | Line | Data |
---|---|---|
936f52d6 C |
1 | // This may look like C code, but it is really -*- C++ -*- |
2 | /* | |
3 | Copyright (C) 1988 Free Software Foundation | |
4 | written by Doug Lea (dl@rocky.oswego.edu) | |
5 | ||
6 | This file is part of GNU CC. | |
7 | ||
8 | GNU CC is distributed in the hope that it will be useful, | |
9 | but WITHOUT ANY WARRANTY. No author or distributor | |
10 | accepts responsibility to anyone for the consequences of using it | |
11 | or for whether it serves any particular purpose or works at all, | |
12 | unless he says so in writing. Refer to the GNU CC General Public | |
13 | License for full details. | |
14 | ||
15 | Everyone is granted permission to copy, modify and redistribute | |
16 | GNU CC, but only under the conditions described in the | |
17 | GNU CC General Public License. A copy of this license is | |
18 | supposed to have been given to you along with GNU CC so you | |
19 | can know your rights and responsibilities. It should be in a | |
20 | file named COPYING. Among other things, the copyright notice | |
21 | and this notice must be preserved on all copies. | |
22 | */ | |
23 | ||
24 | ||
25 | #ifndef _<T>Vec_h | |
26 | #ifdef __GNUG__ | |
27 | #pragma once | |
28 | #pragma interface | |
29 | #endif | |
30 | #define _<T>Vec_h 1 | |
31 | ||
32 | #ifndef _<T>_typedefs | |
33 | #define _<T>_typedefs 1 | |
34 | typedef void (*<T>Procedure)(<T&>); | |
35 | typedef <T> (*<T>Mapper)(<T&>); | |
36 | typedef <T> (*<T>Combiner)(<T&>, <T&>); | |
37 | typedef int (*<T>Predicate)(<T&>); | |
38 | typedef int (*<T>Comparator)(<T&>, <T&>); | |
39 | #endif | |
40 | ||
41 | ||
42 | class <T>Vec | |
43 | { | |
44 | protected: | |
45 | int len; | |
46 | <T> *s; | |
47 | ||
48 | <T>Vec(int l, <T>* d); | |
49 | public: | |
50 | <T>Vec (); | |
51 | <T>Vec (int l); | |
52 | <T>Vec (int l, <T&> fill_value); | |
53 | <T>Vec (<T>Vec&); | |
54 | ~<T>Vec (); | |
55 | ||
56 | <T>Vec & operator = (<T>Vec & a); | |
57 | <T>Vec at(int from = 0, int n = -1); | |
58 | ||
59 | int capacity(); | |
60 | void resize(int newlen); | |
61 | ||
62 | <T>& operator [] (int n); | |
63 | <T>& elem(int n); | |
64 | ||
65 | friend <T>Vec concat(<T>Vec & a, <T>Vec & b); | |
66 | friend <T>Vec map(<T>Mapper f, <T>Vec & a); | |
67 | friend <T>Vec merge(<T>Vec & a, <T>Vec & b, <T>Comparator f); | |
68 | friend <T>Vec combine(<T>Combiner f, <T>Vec & a, <T>Vec & b); | |
69 | friend <T>Vec reverse(<T>Vec & a); | |
70 | ||
71 | void reverse(); | |
72 | void sort(<T>Comparator f); | |
73 | void fill(<T&> val, int from = 0, int n = -1); | |
74 | ||
75 | void apply(<T>Procedure f); | |
76 | <T> reduce(<T>Combiner f, <T&> base); | |
77 | int index(<T&> targ); | |
78 | ||
79 | friend int operator == (<T>Vec& a, <T>Vec& b); | |
80 | friend int operator != (<T>Vec& a, <T>Vec& b); | |
81 | ||
82 | void error(const char* msg); | |
83 | void range_error(); | |
84 | }; | |
85 | ||
86 | extern void default_<T>Vec_error_handler(const char*); | |
87 | extern one_arg_error_handler_t <T>Vec_error_handler; | |
88 | ||
89 | extern one_arg_error_handler_t | |
90 | set_<T>Vec_error_handler(one_arg_error_handler_t f); | |
91 | ||
92 | ||
93 | #if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES) | |
94 | ||
95 | inline <T>Vec::<T>Vec() | |
96 | { | |
97 | len = 0; s = 0; | |
98 | } | |
99 | ||
100 | inline <T>Vec::<T>Vec(int l) | |
101 | { | |
102 | s = new <T> [len = l]; | |
103 | } | |
104 | ||
105 | ||
106 | inline <T>Vec::<T>Vec(int l, <T>* d) :len(l), s(d) {} | |
107 | ||
108 | ||
109 | inline <T>Vec::~<T>Vec() | |
110 | { | |
111 | delete[len] s; | |
112 | } | |
113 | ||
114 | ||
115 | inline <T>& <T>Vec::operator [] (int n) | |
116 | { | |
117 | if ((unsigned)n >= len) | |
118 | range_error(); | |
119 | return s[n]; | |
120 | } | |
121 | ||
122 | inline <T>& <T>Vec::elem(int n) | |
123 | { | |
124 | return s[n]; | |
125 | } | |
126 | ||
127 | ||
128 | inline int <T>Vec::capacity() | |
129 | { | |
130 | return len; | |
131 | } | |
132 | ||
133 | ||
134 | ||
135 | inline int operator != (<T>Vec& a, <T>Vec& b) | |
136 | { | |
137 | return !(a == b); | |
138 | } | |
139 | ||
140 | ||
141 | #endif | |
142 | #endif |