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: (c) 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 /*@Doc: A resizable array
24 Why is it called "LyXArrayBase" if it is generic? (Lgb)
25 Initially I thought it could be the base class for both mathed's
26 and LyX' kernels data buffer. (Ale)
42 LyxArrayBase(int size=ARRAY_STEP);
44 LyxArrayBase(const LyxArrayBase&);
50 int Empty() { return (last==0); }
53 int Last() { return last; }
55 /// Fills with 0 the entire array and set last to 0
58 /// Make the allocated memory fit the needed size
61 /// Remove dx elements from position pos. Don't changes the size
62 void Remove(int pos, int dx);
64 /// Merge dx elements from array a at pos. Changes the size if necessary.
65 void Merge(LyxArrayBase *a, int pos, int dx);
67 /// Same as Merge but doesn't changes the size (dangerous)
68 void MergeF(LyxArrayBase *a, int pos, int dx);
70 /// Copy dx byts from an array at position pos
71 void Copy(void *, int pos, int dx);
73 /// Constructs a new array with dx elements starting at pos
74 LyxArrayBase* Extract(int pos, int dx);
76 /// Insert a character at position pos
77 void Insert(int pos, byte);
79 /// Insert a string of lenght dx at position pos
80 void Insert(int pos, byte *, int dx);
82 /// Constructs a new array with dx elements starting at pos
83 byte operator[](const int);
85 /// Constructs a new array with dx elements starting at pos
86 LyxArrayBase& operator=(const LyxArrayBase&);
90 void Resize(int newsize);
92 bool Move(int p, int shift);
96 /// Last position inserted.
98 /// Max size of the array.
102 friend class MathedIter;
107 /************************ Inline functions *****************************/
110 void LyxArrayBase::Init()
112 memset(bf, 0, maxsize);
116 inline // Hmmm, Hp-UX's CC can't handle this inline. Asger.
117 void LyxArrayBase::Resize(int newsize)
119 if (newsize<ARRAY_MIN_SIZE)
120 newsize = ARRAY_MIN_SIZE;
121 newsize += ARRAY_STEP - (newsize % ARRAY_STEP);
122 byte *nwbf = new byte[newsize];
123 if (last >= newsize) last = newsize-1;
125 memcpy(nwbf, bf, last);
132 LyxArrayBase::LyxArrayBase(int size)
134 maxsize = (size<ARRAY_MIN_SIZE) ? ARRAY_MIN_SIZE: size;
135 bf = new byte[maxsize]; // this leaks
140 LyxArrayBase::~LyxArrayBase()
146 LyxArrayBase::LyxArrayBase(const LyxArrayBase& a)
149 bf = new byte[maxsize];
150 memcpy(&bf[0], &a.bf[0], maxsize);
155 LyxArrayBase& LyxArrayBase::operator=(const LyxArrayBase& a)
159 memcpy(&bf[0], &a.bf[0], maxsize);
165 bool LyxArrayBase::Move(int p, int shift)
169 if (last+shift>=maxsize) {
170 Resize(last + shift);
172 memmove(&bf[p+shift], &bf[p], last-p);
181 void LyxArrayBase::Fit()
187 void LyxArrayBase::Remove(int pos, int dx)
193 void LyxArrayBase::Merge(LyxArrayBase *a, int p, int dx)
196 memcpy(&bf[p], &a->bf[0], dx);
200 void LyxArrayBase::MergeF(LyxArrayBase *a, int p, int dx)
202 memcpy(&bf[p], &a->bf[0], dx);
206 void LyxArrayBase::Copy(void *a, int p, int dx)
208 memcpy(&bf[p], a, dx);
212 LyxArrayBase *LyxArrayBase::Extract(int, int dx)
214 LyxArrayBase *a = new LyxArrayBase(dx);
215 a->Merge(this, 0, dx);
220 byte LyxArrayBase::operator[](const int i)
227 void LyxArrayBase::Insert(int pos, byte c)
229 if (pos<0) pos = last;
231 Resize(maxsize+ARRAY_STEP);