4 * Purpose: A general purpose resizable array.
5 * Author: Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
6 * Created: January 1996
8 * Dependencies: None (almost)
10 * Copyright: 1996, Alejandro Aguilar Sierra
13 * You are free to use and modify this code under the terms of
14 * the GNU General Public Licence version 2 or later.
20 #define byte unsigned char
23 /** A resizable array.
24 A general purpose resizable array.
25 @author Alejandro Aguilar Sierra
42 LyxArrayBase(int size = ARRAY_STEP);
44 LyxArrayBase(LyxArrayBase const &);
48 /// Constructs a new array with dx elements starting at pos
49 LyxArrayBase & operator= (LyxArrayBase const &);
52 int empty() const { return (last == 0); }
55 int Last() { return last; }
57 /// Fills with 0 the entire array and set last to 0
60 /// Make the allocated memory fit the needed size
63 /// Remove dx elements from position pos. Don't changes the size
64 void Remove(int pos, int dx);
66 /// Merge dx elements from array a at pos. Changes the size if necessary.
67 void Merge(LyxArrayBase * a, int pos, int dx);
69 /// Same as Merge but doesn't changes the size (dangerous)
70 void MergeF(LyxArrayBase * a, int pos, int dx);
72 /// Copy dx byts from an array at position pos
73 void Copy(void *, int pos, int dx);
75 /// Constructs a new array with dx elements starting at pos
76 LyxArrayBase * Extract(int pos, int dx);
78 /// Insert a character at position pos
79 void Insert(int pos, byte);
81 /// Insert a string of lenght dx at position pos
82 void Insert(int pos, byte *, int dx);
84 /// Constructs a new array with dx elements starting at pos
85 byte operator[](const int);
89 void Resize(int newsize);
91 bool Move(int p, int shift);
95 /// Last position inserted.
97 /// Max size of the array.
101 friend class MathedIter;
106 /************************ Inline functions *****************************/
109 void LyxArrayBase::Init()
111 memset(bf, 0, maxsize);
115 inline // Hmmm, Hp-UX's CC can't handle this inline. Asger.
116 void LyxArrayBase::Resize(int newsize)
118 if (newsize<ARRAY_MIN_SIZE)
119 newsize = ARRAY_MIN_SIZE;
120 newsize += ARRAY_STEP - (newsize % ARRAY_STEP);
121 byte *nwbf = new byte[newsize];
122 if (last >= newsize) last = newsize-1;
124 memcpy(nwbf, bf, last);
131 LyxArrayBase::LyxArrayBase(int size)
133 maxsize = (size<ARRAY_MIN_SIZE) ? ARRAY_MIN_SIZE: size;
134 bf = new byte[maxsize]; // this leaks
139 LyxArrayBase::~LyxArrayBase()
145 LyxArrayBase::LyxArrayBase(const LyxArrayBase& a)
148 bf = new byte[maxsize];
149 memcpy(&bf[0], &a.bf[0], maxsize);
154 LyxArrayBase& LyxArrayBase::operator= (const LyxArrayBase& a)
158 memcpy(&bf[0], &a.bf[0], maxsize);
164 bool LyxArrayBase::Move(int p, int shift)
168 if (last+shift>= maxsize) {
169 Resize(last + shift);
171 memmove(&bf[p+shift], &bf[p], last-p);
180 void LyxArrayBase::Fit()
186 void LyxArrayBase::Remove(int pos, int dx)
192 void LyxArrayBase::Merge(LyxArrayBase *a, int p, int dx)
195 memcpy(&bf[p], &a->bf[0], dx);
199 void LyxArrayBase::MergeF(LyxArrayBase *a, int p, int dx)
201 memcpy(&bf[p], &a->bf[0], dx);
205 void LyxArrayBase::Copy(void *a, int p, int dx)
207 memcpy(&bf[p], a, dx);
211 LyxArrayBase *LyxArrayBase::Extract(int, int dx)
213 LyxArrayBase *a = new LyxArrayBase(dx);
214 a->Merge(this, 0, dx);
219 byte LyxArrayBase::operator[](const int i)
226 void LyxArrayBase::Insert(int pos, byte c)
228 if (pos<0) pos = last;
230 Resize(maxsize+ARRAY_STEP);