2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
24 * $TOG: dynarray.h /main/5 1999/10/15 17:22:58 mgreess $
26 * (c) Copyright 1996 Digital Equipment Corporation.
27 * (c) Copyright 1993,1994,1996 Hewlett-Packard Company.
28 * (c) Copyright 1993,1994,1996 International Business Machines Corp.
29 * (c) Copyright 1993,1994,1996 Sun Microsystems, Inc.
30 * (c) Copyright 1993,1994,1996 Novell, Inc.
31 * (c) Copyright 1996 FUJITSU LIMITED.
32 * (c) Copyright 1996 Hitachi.
34 /* Handle dynamic arrays of arbitrary type and range. */
39 #if !defined(linux) && !defined(CSRG_BASED) && !defined(sun)
42 #if defined(sun) || defined(linux) || defined(CSRG_BASED)
43 #define _DELETE_ARRAY(sz) delete[]
45 #define _DELETE_ARRAY(sz) delete[(sz)]
49 // this is used to create an ARRAY of a TYPE
50 #define declare_array(ARRAY, TYPE, BUMP) \
57 TYPE &bumpsize(long); \
59 ARRAY() { arr = 0; max = len = 0; } \
61 { arr = 0; max = len = 0; if (siz > 0) bumpsize(siz-1); } \
62 ARRAY(const ARRAY &); \
63 ~ARRAY() { _DELETE_ARRAY(max) arr; } \
64 ARRAY &operator=(const ARRAY &); \
65 long size() const { return len; } \
66 void reset(long l = 0) { bumpsize(l); len = l; } \
67 TYPE &operator[](long e) \
68 { if (e < len) return arr[e]; else return bumpsize(e); } \
69 TYPE &elt(long e) const { return arr[e]; } \
70 TYPE &end() { return bumpsize(len); } \
71 TYPE *getarr() const { return arr; } \
72 TYPE *operator()() const { return arr; } \
75 // this implements an ARRAY of a TYPE
76 // - this must be done once and only once in the user code
77 // printf("0x%X: max=%d len=%d elt=%d\n", this, max, len, elt);
78 #define implement_array(ARRAY, TYPE, BUMP) \
79 TYPE &ARRAY::bumpsize(long elt) \
88 TYPE *narr = new TYPE[max = elt + (omax > BUMP ? BUMP : omax)]; \
89 for (long i = 0; i < len; i++) \
91 _DELETE_ARRAY(omax) arr; \
98 ARRAY &ARRAY::operator=(const ARRAY &a) \
105 for (long i = 0; i < len; i++) \
109 ARRAY::ARRAY(const ARRAY &t) \
116 // the user can also use these to define an array of any type
117 #define darray(TYPE) name2(TYPE,array)
118 #define darraydeclare(TYPE) declare_array(darray(TYPE), TYPE, 1024)
119 #define darrayimplement(TYPE) implement_array(darray(TYPE), TYPE, 1024)
120 #define darraydeclare2(TYPE,BUMP) declare_array(darray(TYPE), TYPE, BUMP)
121 #define darrayimplement2(TYPE,BUMP) implement_array(darray(TYPE), TYPE, BUMP)
124 // this is used to define a DYNARRAY of a TYPE
125 #define declare_dynarray(DYNARRAY, TYPE) \
135 void init(long, long, unsigned); \
136 void chsize(long, long); \
138 DYNARRAY(long l, long s = 0, unsigned b = 1024) { init(l, s, b); } \
139 DYNARRAY() { init(0, 0, 1024); } \
140 DYNARRAY(const DYNARRAY &t) \
141 { init(t.high - t.low + 1, t.low, t.bump); *this = t; } \
142 ~DYNARRAY() { _DELETE_ARRAY(max - min + 1) arr; } \
143 DYNARRAY &operator=(const DYNARRAY &); \
144 long size() const { return high - low + 1; } \
145 void reset(long len = 0, long st = 0) \
146 { chsize(st, st + len - 1); high = st + len - 1; low = st; } \
147 long smallest() const { return low; } \
148 long largest() const { return high; } \
149 TYPE &operator[](long e) \
150 { if (e <= low || e >= high) chsize(e,e); return aptr[e]; } \
151 TYPE &elt(long e) const { return aptr[e]; } \
152 TYPE &end() { return (*this)[largest() + 1]; } \
153 TYPE *getarr() const { return aptr; } \
154 TYPE *operator()() const { return aptr; } \
157 // this creates the code needed for a DYNARRAY of TYPE
158 // - this must be done once and only once in the user code
159 #define implement_dynarray(DYNARRAY, TYPE) \
160 void DYNARRAY::init(long len, long start, unsigned bmp) \
164 high = start + len - 1; \
168 bump = bmp > 0 ? bmp : 1024; \
171 arr = new TYPE[max - min + 1]; \
174 void DYNARRAY::chsize(long lelt, long helt) \
176 long nlow = lelt < low ? lelt : low; \
177 long nhigh = helt > high ? helt : high; \
178 if (nlow <= min || nhigh >= max) \
180 long nmin = nlow < min ? nlow : min; \
181 long nmax = nhigh > max ? nhigh : max; \
182 long m = max - min + 1; \
183 long nm = nmax - nmin + 1 + (m > bump ? bump : m); \
184 long nl = nhigh - nlow + 1; \
185 TYPE *narr = new TYPE[nm]; \
186 TYPE *naptr = narr - nmin; \
187 for (long i = low; i <= high; i++) \
188 naptr[i] = aptr[i]; \
189 _DELETE_ARRAY(m) arr; \
198 DYNARRAY &DYNARRAY::operator=(const DYNARRAY &a) \
202 if (a.low < low || a.high > high) \
203 chsize(a.low, a.high); \
206 for (long i = a.low; i <= a.high; i++) \
207 aptr[i] = a.aptr[i]; \
212 // the user can also use these to define arrays of any type
213 #define dynarray(TYPE) name2(TYPE,dynarray)
214 #define dynarraydeclare(TYPE) declare_dynarray(dynarray(TYPE), TYPE)
215 #define dynarrayimplement(TYPE) implement_dynarray(dynarray(TYPE), TYPE)
217 // Predefined dynarrays for most common uses
218 /* declare_array(Charbuf, char, 256) */
221 #endif /* __DYNARRAY_H_ */