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
31 typedef std::vector<byte> buffer_type;
44 LyxArrayBase(int size = ARRAY_STEP);
47 int empty() const { return (last == 0); }
50 int Last() { return last; }
52 /// Make the allocated memory fit the needed size
55 /// Remove dx elements from position pos. Don't changes the size
56 void Remove(int pos, int dx);
58 /// Merge dx elements from array a at pos. Changes the size if necessary.
59 void Merge(LyxArrayBase * a, int pos, int dx);
61 /// Same as Merge but doesn't changes the size (dangerous)
62 void MergeF(LyxArrayBase * a, int pos, int dx);
64 /// Copy dx byts from an array at position pos
65 void Copy(void *, int pos, int dx);
67 /// Constructs a new array with dx elements starting at pos
68 LyxArrayBase * Extract(int pos, int dx);
70 /// Insert a character at position pos
71 void Insert(int pos, byte);
73 /// Constructs a new array with dx elements starting at pos
74 byte operator[](const int);
78 void Resize(int newsize);
80 bool Move(int p, int shift);
84 /// Last position inserted.
86 /// Max size of the array.
90 friend class MathedIter;
95 /************************ Inline functions *****************************/
97 inline // Hmmm, Hp-UX's CC can't handle this inline. Asger.
98 void LyxArrayBase::Resize(int newsize)
100 if (newsize<ARRAY_MIN_SIZE)
101 newsize = ARRAY_MIN_SIZE;
102 newsize += ARRAY_STEP - (newsize % ARRAY_STEP);
104 if (last >= newsize) last = newsize-1;
110 LyxArrayBase::LyxArrayBase(int size)
112 maxsize = (size<ARRAY_MIN_SIZE) ? ARRAY_MIN_SIZE: size;
118 bool LyxArrayBase::Move(int p, int shift)
122 if (last + shift >= maxsize) {
123 Resize(last + shift);
125 memmove(&bf[p + shift], &bf[p], last - p);
134 void LyxArrayBase::Fit()
140 void LyxArrayBase::Remove(int pos, int dx)
146 void LyxArrayBase::Merge(LyxArrayBase * a, int p, int dx)
149 memcpy(&bf[p], &a->bf[0], dx);
153 void LyxArrayBase::MergeF(LyxArrayBase * a, int p, int dx)
155 memcpy(&bf[p], &a->bf[0], dx);
159 void LyxArrayBase::Copy(void * a, int p, int dx)
161 memcpy(&bf[p], a, dx);
165 LyxArrayBase * LyxArrayBase::Extract(int, int dx)
167 LyxArrayBase * a = new LyxArrayBase(dx);
168 a->Merge(this, 0, dx);
173 byte LyxArrayBase::operator[](const int i)
180 void LyxArrayBase::Insert(int pos, byte c)
182 if (pos < 0) pos = last;
184 Resize(maxsize + ARRAY_STEP);