Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / lib / DtSvc / include / codelibs / dynarray.h
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
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)
10  * any later version.
11  *
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
16  * details.
17  *
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
22  */
23 /*
24  * $TOG: dynarray.h /main/5 1999/10/15 17:22:58 mgreess $
25  *
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.
33  */
34 /* Handle dynamic arrays of arbitrary type and range. */
35
36 #ifndef __DYNARRAY_H_
37 #define __DYNARRAY_H_
38
39 #include <generic.h>
40 #if defined(USL) || defined(__uxp__) || defined(__osf__) || defined(sun)
41 #define _DELETE_ARRAY(sz) delete[]
42 #else
43 #define _DELETE_ARRAY(sz) delete[(sz)]
44 #endif
45
46
47 // this is used to create an ARRAY of a TYPE
48 #define declare_array(ARRAY, TYPE, BUMP) \
49 class ARRAY \
50 { \
51         long len; \
52         long max; \
53         TYPE *arr; \
54 protected: \
55         TYPE &bumpsize(long); \
56 public: \
57         ARRAY() { arr = 0; max = len = 0; } \
58         ARRAY(long siz) \
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; } \
71 };
72
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) \
78 { \
79         if (elt < 0) \
80             elt = 0; \
81         if (elt >= max) \
82         { \
83                 if (max <= 0) \
84                         max = 1; \
85                 long omax = max; \
86                 TYPE *narr = new TYPE[max = elt + (omax > BUMP ? BUMP : omax)]; \
87                 for (long i = 0; i < len; i++) \
88                         narr[i] = arr[i]; \
89                 _DELETE_ARRAY(omax) arr; \
90                 arr = narr; \
91         } \
92         if (elt >= len) \
93                 len = elt + 1; \
94         return arr[elt]; \
95 } \
96 ARRAY &ARRAY::operator=(const ARRAY &a) \
97 { \
98         if (&a == this) \
99             return *this; \
100         if (a.len > len) \
101                 bumpsize(a.len); \
102         len = a.len; \
103         for (long i = 0; i < len; i++) \
104                 arr[i] = a.arr[i]; \
105         return *this; \
106 } \
107 ARRAY::ARRAY(const ARRAY &t) \
108 { \
109         arr = 0; \
110         max = len = 0; \
111         *this = t; \
112 }
113
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)
120
121
122 // this is used to define a DYNARRAY of a TYPE
123 #define declare_dynarray(DYNARRAY, TYPE) \
124 class DYNARRAY \
125 { \
126         long low; \
127         long high; \
128         long min; \
129         long max; \
130         unsigned bump; \
131         TYPE *arr; \
132         TYPE *aptr; \
133         void init(long, long, unsigned); \
134         void chsize(long, long); \
135 public: \
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; } \
153 };
154
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) \
159 { \
160         if (len < 0) \
161             len = 0; \
162         high = start + len - 1; \
163         low = start; \
164         max = high; \
165         min = low; \
166         bump = bmp > 0 ? bmp : 1024; \
167         if (max < min) \
168             max = min; \
169         arr = new TYPE[max - min + 1]; \
170         aptr = arr - min; \
171 } \
172 void DYNARRAY::chsize(long lelt, long helt) \
173 { \
174         long nlow = lelt < low ? lelt : low; \
175         long nhigh = helt > high ? helt : high; \
176         if (nlow <= min || nhigh >= max) \
177         { \
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; \
188                 arr = narr; \
189                 aptr = naptr; \
190                 min = nmin; \
191                 max = nmax; \
192         } \
193         high = nhigh; \
194         low = nlow; \
195 } \
196 DYNARRAY &DYNARRAY::operator=(const DYNARRAY &a) \
197 { \
198         if (&a == this) \
199             return *this; \
200         if (a.low < low || a.high > high) \
201                 chsize(a.low, a.high); \
202         low = a.low; \
203         high = a.high; \
204         for (long i = a.low; i <= a.high; i++) \
205                 aptr[i] = a.aptr[i]; \
206         bump = a.bump; \
207         return *this; \
208 }
209
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)
214
215 // Predefined dynarrays for most common uses
216 /* declare_array(Charbuf, char, 256) */
217
218
219 #endif /* __DYNARRAY_H_ */