source: tags/arb_5.3/ALIV3/a3_darray.cxx

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