Revert "libede: fixed freeing wrong memory problem. this was the real cause of null...
[ede] / explorer / src / Structures / ArrayFactoredGrowth.ec
1 public import "ecere"
2
3 import "ArrayUtilities"
4
5 private:
6
7 define array = ((ArrayImpl)this).a;
8
9 class ArrayImpl
10 {
11    uint _size;
12    uint _count;
13    uint _factor;
14    Class type;
15    byte * a;
16 };
17
18 public class RedjArray
19 {
20    uint _size;
21    uint _count;
22    uint _factor;
23
24    ~RedjArray()
25    {
26       delete array;
27    }
28
29    //virtual void FreeItem(void *);
30
31 public:   
32    Class type;
33    property uint count
34    {
35       set
36       {
37          if(value != _count)
38          {
39             int newsize = (value / _factor + 1) * _factor;
40             if(newsize != _size)
41                size = value ? newsize : 0;
42             _count = value;
43          }
44       }
45       get { return _count; }
46    }
47    property uint size
48    {
49       set
50       {
51          if(value != _size)
52          {
53             if(array)
54             {
55                if(value)
56                   array = renew array byte[value * sizeoftype];
57                else
58                   delete array;
59             }
60             else if(value)
61                array = new byte[value * sizeoftype];
62             _size = value;
63          }
64       }
65       get { return _size; }
66    }
67    property uint growingFactor { set { _factor = value; } get { return _factor; } }
68    property void * data
69    {
70       set
71       {
72          memcpy(array, value, _size * sizeoftype);
73       }
74    }
75    void Append(int n)
76    {
77       count += n;
78    }
79    void Insert(uint position, int n)
80    {
81       Append(n);
82       if(position < _count - 1)
83          MoveBytes(array + (position + n) * sizeoftype, array + position * sizeoftype, (_count - position - n) * sizeoftype);
84    }
85    void Trim(int n)
86    {
87       count -= n;
88    }
89    void Remove(uint position, int n)
90    {
91       if(position + n - 1 < _count - 1)
92          MoveBytes(array + position * sizeoftype, array + (position + n) * sizeoftype, (_count - position - n) * sizeoftype);
93       Trim(n);
94    }
95 };
96
97 public class IntArray : RedjArray
98 {
99    type = class(int);
100 public:
101    int * const _;
102    uint * Add(int item)
103    {
104       uint pos = _count;
105       Append(1);
106       _[pos] = item;
107       return &_[pos];
108    }
109    uint * AddBefore(uint position, int item)
110    {
111       Insert(position, 1);
112       _[position] = item;
113       return &_[position];
114    }
115 }
116
117 public class UintArray : RedjArray
118 {
119    type = class(uint);
120 public:
121    uint * const _;
122    uint * Add(uint item)
123    {
124       uint pos = _count;
125       Append(1);
126       _[pos] = item;
127       return &_[pos];
128    }
129    uint * AddBefore(uint position, uint item)
130    {
131       Insert(position, 1);
132       _[position] = item;
133       return &_[position];
134    }
135 }
136
137 public class StringArray : RedjArray
138 {
139    type = class(String);
140 public:
141    String * const _;
142    String * Add(String item)
143    {
144       uint pos = _count;
145       Append(1);
146       _[pos] = item;
147       return &_[pos];
148    }
149    String * AddBefore(uint position, String item)
150    {
151       Insert(position, 1);
152       _[position] = item;
153       return &_[position];
154    }
155    void Clear()
156    {
157       int c;
158       for(c = 0; c < _count; c++)
159          delete _[c];
160    }
161 }