source: tags/initial/ALIV3/a3_darray.cxx

Last change on this file was 2, checked in by oldcode, 24 years ago

Initial revision

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.3 KB
Line 
1// -----------------------------------------------------------------------------
2//      Include-Dateien
3// -----------------------------------------------------------------------------
4
5#include <string.h>
6#include <stdlib.h>
7#include <fstream.h>
8
9#include "a3_darray.hxx"
10
11// -----------------------------------------------------------------------------
12        void DArray::Init ( int num,
13                                            int inc,
14                                            int del )
15// -----------------------------------------------------------------------------
16{
17        elements  = num;
18        nextfree  = 0;
19        increment = inc;
20        free      = del;
21        array     = new vp [elements];
22
23        memset(array,0,elements * sizeof(vp));
24}
25
26// -----------------------------------------------------------------------------
27        void DArray::Grow ( void )
28// -----------------------------------------------------------------------------
29{
30        vp  *save = array;
31        int  anz  = elements,
32                 next = nextfree;
33       
34        Init(elements + increment,increment,free);
35
36        nextfree = next;
37       
38        memcpy(array,save,anz * sizeof(vp));
39       
40        delete save;
41}
42
43// -----------------------------------------------------------------------------
44        void DArray::Shrink ( int num )
45// -----------------------------------------------------------------------------
46{
47        vp  *save = array;
48       
49        Init(num,increment,free);
50
51        nextfree = num;
52       
53        memcpy(array,save,num * sizeof(vp));
54       
55        delete save;
56}
57
58// -----------------------------------------------------------------------------
59        DArray::DArray ( void )
60// -----------------------------------------------------------------------------
61{
62        Init(DARRAY_SIZE,DARRAY_INC,0);
63}
64
65// -----------------------------------------------------------------------------
66        DArray::DArray ( int num )
67// -----------------------------------------------------------------------------
68{
69        if (num > 0) Init(num,DARRAY_INC,0);
70        else             Init(DARRAY_SIZE,DARRAY_INC,0);
71}
72
73// -----------------------------------------------------------------------------
74        DArray::DArray ( int num,
75                                         int inc,
76                                         int del )
77// -----------------------------------------------------------------------------
78{
79        del = !!del;
80       
81        if (num > 0)
82        {
83                if (inc > 0) Init(num,inc,del);
84                else             Init(num,DARRAY_INC,del);
85        }
86        else
87        {
88                if (inc > 0) Init(DARRAY_SIZE,inc,del);
89                else             Init(DARRAY_SIZE,DARRAY_INC,del);
90        }
91}
92
93// -----------------------------------------------------------------------------
94        DArray::DArray ( DArray &other )
95// -----------------------------------------------------------------------------
96{
97        Init(other.elements,other.increment,other.free);
98        nextfree = other.nextfree;
99        memcpy(array,other.array,elements * sizeof(vp));
100}
101       
102// -----------------------------------------------------------------------------
103        DArray::~DArray ( void )
104// -----------------------------------------------------------------------------
105{
106        Clear();
107}
108
109// -----------------------------------------------------------------------------
110        int DArray::Add ( vp elem )
111// -----------------------------------------------------------------------------
112{
113        int index = nextfree;
114       
115        if (nextfree == elements) Grow();
116       
117        array[nextfree++] = elem;
118       
119        return index;
120}
121
122// -----------------------------------------------------------------------------
123        int DArray::Set ( vp  elem,
124                                          int index )
125// -----------------------------------------------------------------------------
126{
127        int pos = -1;
128       
129        if (index >= 0)
130        {
131                pos = index;
132               
133                while (pos >= elements) Grow();
134
135                array[pos] = elem;
136               
137                nextfree = pos + 1;
138        }
139       
140        return pos;
141}
142
143// -----------------------------------------------------------------------------
144        int DArray::Del ( int index )
145// -----------------------------------------------------------------------------
146{
147        int error = 0;
148
149        if ((index < 0) || (index >= elements)) error = 1;
150        else
151        {
152                if (free && array[index]) delete array[index];
153               
154                array[index] = NULL;
155               
156                if (nextfree == (index - 1)) nextfree = index;
157        }
158       
159        return error;
160}
161
162// -----------------------------------------------------------------------------
163        int DArray::Elements ( void )
164// -----------------------------------------------------------------------------
165{
166        return elements;
167}
168       
169// -----------------------------------------------------------------------------
170        vp DArray::operator [] ( int index )
171// -----------------------------------------------------------------------------
172{
173        vp elem = NULL;
174       
175        if ((index >= 0) && (index < elements)) elem = array[index];
176
177        return elem;
178}
179
180// -----------------------------------------------------------------------------
181        DArray &DArray::operator = ( DArray &other )
182// -----------------------------------------------------------------------------
183{
184        if (this != &other)
185        {
186                Init(other.elements,other.increment,other.free);
187                nextfree = other.nextfree;
188                null     = other.null;
189                memcpy(array,other.array,elements * sizeof(vp));
190        }
191
192        return *this;
193}
194
195// -----------------------------------------------------------------------------
196        void DArray::Sort ( cmpfunc cmp )
197// -----------------------------------------------------------------------------
198{
199        if (elements > 1)
200        {
201                int count = 0,
202                        anz       = elements;
203
204                if (!null)
205                {
206                        int sum = 0,
207                                tmp;
208                       
209                        for (tmp = 0;tmp < anz;tmp++) if (array[tmp]) sum++;
210                       
211                        if (sum != anz)
212                        {
213                                vp *help = new vp [sum];
214
215                                anz = elements = nextfree = sum;
216                               
217                                sum = 0;
218                               
219                                for (tmp = 0;tmp < anz;tmp++) if (array[tmp]) help[sum++] = array[tmp];
220
221                                delete array;
222                               
223                                array = help;
224                        }
225                }
226               
227                qsort(array,anz,sizeof(vp),cmp);
228                int source;
229                int dest = 1;
230                vp lastelem = array[0];
231       
232                for (source = 1; source < anz; source++) {
233                        if (array[source] != lastelem){
234                                array[dest++] = lastelem = array[source];
235                        }
236                }
237                anz = dest;
238       
239                if (anz < elements) Shrink(anz);
240        }
241}
242
243// -----------------------------------------------------------------------------
244        void DArray::Clear ( void )
245// -----------------------------------------------------------------------------
246{
247        if (free) 
248        {
249                int count = elements;
250               
251                while (count--) if (array[count]) delete array[count];
252        }
253
254        delete array;
255
256        array = NULL;
257}
258
259// -----------------------------------------------------------------------------
260        void DArray::Dump ( dumpfunc edump )
261// -----------------------------------------------------------------------------
262{
263        int count = 0;
264       
265        cout << "\nelements  = " << elements;
266        cout << "\nnextfree  = " << nextfree;
267        cout << "\nincrement = " << increment;
268       
269        cout << "\n\n";
270
271        while (edump && (count < elements)) edump(array[count++]);
272
273        cout << "\n\n";
274}
Note: See TracBrowser for help on using the repository browser.