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 librararies 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. */
40 #if defined(USL) || defined(__uxp__) || defined(__osf__) || defined(sun)
41 #define _DELETE_ARRAY(sz) delete[]
43 #define _DELETE_ARRAY(sz) delete[(sz)]
47 // this is used to create an ARRAY of a TYPE
48 #define declare_array(ARRAY, TYPE, BUMP) \
55 TYPE &bumpsize(long); \
57 ARRAY() { arr = 0; max = len = 0; } \
59 { arr = 0; max = len = 0; if (siz > 0) bumpsize(siz-1); } \
60 ARRAY(const ARRAY &); \
61 ~ARRAY() { _DELETE_ARRAY(max) arr; } \
62 ARRAY &operator=(const ARRAY &); \
63 long size() const { return len; } \
64 void reset(long l = 0) { bumpsize(l); len = l; } \
65 TYPE &operator[](long e) \
66 { if (e < len) return arr[e]; else return bumpsize(e); } \
67 TYPE &elt(long e) const { return arr[e]; } \
68 TYPE &end() { return bumpsize(len); } \
69 TYPE *getarr() const { return arr; } \
70 TYPE *operator()() const { return arr; } \
73 // this implements an ARRAY of a TYPE
74 // - this must be done once and only once in the user code
75 // printf("0x%X: max=%d len=%d elt=%d\n", this, max, len, elt);
76 #define implement_array(ARRAY, TYPE, BUMP) \
77 TYPE &ARRAY::bumpsize(long elt) \
86 TYPE *narr = new TYPE[max = elt + (omax > BUMP ? BUMP : omax)]; \
87 for (long i = 0; i < len; i++) \
89 _DELETE_ARRAY(omax) arr; \
96 ARRAY &ARRAY::operator=(const ARRAY &a) \
103 for (long i = 0; i < len; i++) \
107 ARRAY::ARRAY(const ARRAY &t) \
114 // the user can also use these to define an array of any type
115 #define darray(TYPE) name2(TYPE,array)
116 #define darraydeclare(TYPE) declare_array(darray(TYPE), TYPE, 1024)
117 #define darrayimplement(TYPE) implement_array(darray(TYPE), TYPE, 1024)
118 #define darraydeclare2(TYPE,BUMP) declare_array(darray(TYPE), TYPE, BUMP)
119 #define darrayimplement2(TYPE,BUMP) implement_array(darray(TYPE), TYPE, BUMP)
122 // this is used to define a DYNARRAY of a TYPE
123 #define declare_dynarray(DYNARRAY, TYPE) \
133 void init(long, long, unsigned); \
134 void chsize(long, long); \
136 DYNARRAY(long l, long s = 0, unsigned b = 1024) { init(l, s, b); } \
137 DYNARRAY() { init(0, 0, 1024); } \
138 DYNARRAY(const DYNARRAY &t) \
139 { init(t.high - t.low + 1, t.low, t.bump); *this = t; } \
140 ~DYNARRAY() { _DELETE_ARRAY(max - min + 1) arr; } \
141 DYNARRAY &operator=(const DYNARRAY &); \
142 long size() const { return high - low + 1; } \
143 void reset(long len = 0, long st = 0) \
144 { chsize(st, st + len - 1); high = st + len - 1; low = st; } \
145 long smallest() const { return low; } \
146 long largest() const { return high; } \
147 TYPE &operator[](long e) \
148 { if (e <= low || e >= high) chsize(e,e); return aptr[e]; } \
149 TYPE &elt(long e) const { return aptr[e]; } \
150 TYPE &end() { return (*this)[largest() + 1]; } \
151 TYPE *getarr() const { return aptr; } \
152 TYPE *operator()() const { return aptr; } \
155 // this creates the code needed for a DYNARRAY of TYPE
156 // - this must be done once and only once in the user code
157 #define implement_dynarray(DYNARRAY, TYPE) \
158 void DYNARRAY::init(long len, long start, unsigned bmp) \
162 high = start + len - 1; \
166 bump = bmp > 0 ? bmp : 1024; \
169 arr = new TYPE[max - min + 1]; \
172 void DYNARRAY::chsize(long lelt, long helt) \
174 long nlow = lelt < low ? lelt : low; \
175 long nhigh = helt > high ? helt : high; \
176 if (nlow <= min || nhigh >= max) \
178 long nmin = nlow < min ? nlow : min; \
179 long nmax = nhigh > max ? nhigh : max; \
180 long m = max - min + 1; \
181 long nm = nmax - nmin + 1 + (m > bump ? bump : m); \
182 long nl = nhigh - nlow + 1; \
183 TYPE *narr = new TYPE[nm]; \
184 TYPE *naptr = narr - nmin; \
185 for (long i = low; i <= high; i++) \
186 naptr[i] = aptr[i]; \
187 _DELETE_ARRAY(m) arr; \
196 DYNARRAY &DYNARRAY::operator=(const DYNARRAY &a) \
200 if (a.low < low || a.high > high) \
201 chsize(a.low, a.high); \
204 for (long i = a.low; i <= a.high; i++) \
205 aptr[i] = a.aptr[i]; \
210 // the user can also use these to define arrays of any type
211 #define dynarray(TYPE) name2(TYPE,dynarray)
212 #define dynarraydeclare(TYPE) declare_dynarray(dynarray(TYPE), TYPE)
213 #define dynarrayimplement(TYPE) implement_dynarray(dynarray(TYPE), TYPE)
215 // Predefined dynarrays for most common uses
216 /* declare_array(Charbuf, char, 256) */
219 #endif /* __DYNARRAY_H_ */