Skip to content

Commit 10958fb

Browse files
committed
Add recursion to TextArray
1 parent 1c83cae commit 10958fb

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

ecr/run.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class Run {
4747

4848
///////////////////////////////////////////////////////////////////////
4949
// Parse text using a specified separator
50+
// This recurses through values and builds a tree structure
5051
int parse(Text* t, char separator, TextArray* array) {
5152
const char* content = t->getText();
5253
int n = 0;

ecr/text.h

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,16 @@ class Text {
157157
}
158158
};
159159

160+
class ListItem {
161+
162+
private:
163+
164+
165+
public:
166+
167+
168+
};
169+
160170
/*
161171
TextArray is a memory-efficient class for managing arrays of strings.
162172
A typically usage is to break a piece of text into lines and have them
@@ -167,9 +177,11 @@ class TextArray {
167177

168178
private:
169179
const char* name;
170-
int size = 0; // the number of items
171-
Text** array = nullptr; // the array of items
172-
LinkedList* list; // A list to hold new data items as they are added
180+
int size = 0; // the number of items
181+
Text** array = nullptr; // an array of Text objects
182+
TextArray** array2 = nullptr; // an array of TextArray objects
183+
LinkedList* list; // A list to hold new Text items as they are added
184+
LinkedList* list2; // A list to hold new TextArray items as they are added
173185

174186
public:
175187

@@ -180,7 +192,7 @@ class TextArray {
180192
}
181193

182194
///////////////////////////////////////////////////////////////////////
183-
// Get a specified item.
195+
// Get a specified Text.
184196
// If the index is greater than the array size but not greater than
185197
// the combined size of array and list, return the item from the list.
186198
Text* get(int n) {
@@ -193,6 +205,20 @@ class TextArray {
193205
return nullptr;
194206
}
195207

208+
///////////////////////////////////////////////////////////////////////
209+
// Get a specified TextArray.
210+
// If the index is greater than the array size but not greater than
211+
// the combined size of array and list, return the item from the list.
212+
TextArray* getArray(int n) {
213+
if (n < size) {
214+
return array2[n];
215+
}
216+
else if (n < size + list2->getSize()) {
217+
return (TextArray*)list2->get(n - size);
218+
}
219+
return nullptr;
220+
}
221+
196222
///////////////////////////////////////////////////////////////////////
197223
// Get the item whose index is passed in as a Text.
198224
Text* get(Text* t) {
@@ -209,12 +235,18 @@ class TextArray {
209235
// Add an item. This goes into the linked list.
210236
void add(Text* item) {
211237
list->add(item);
238+
list2->add(nullptr);
212239
}
213240

214241
void add(const char* item) {
215242
list->add(new Text(item));
216243
}
217244

245+
void add(TextArray* array) {
246+
list->add(nullptr);
247+
list2->add(array);
248+
}
249+
218250
///////////////////////////////////////////////////////////////////////
219251
// Test if this array contains the given text
220252
bool contains(Text* t) {
@@ -231,26 +263,32 @@ class TextArray {
231263
// Flatten this item by creating a single array to hold all the data.
232264
void flatten() {
233265
Text** oldArray = array;
266+
TextArray** oldArray2 = array2;
234267
int oldSize = size;
235268
// Create a new array big enough for the old array and the list
236269
int total = oldSize + list->getSize();
237270
if (total > 0) {
238271
array = new Text*[total];
272+
array2 = new TextArray*[total];
239273
// Copy the old array to the new
240274
size = 0;
241275
while (size < oldSize) {
242276
array[size] = oldArray[size];
277+
array2[size] = oldArray2[size];
243278
size++;
244279
}
245280
if (oldArray != nullptr) {
246281
delete oldArray;
282+
delete oldArray2;
247283
}
248284
// Copy the list to the new array
249285
int n = 0;
250286
while (n < list->getSize()) {
251-
array[size++] = (Text*)list->get(n++);
287+
array[size] = (Text*)list->get(n);
288+
array2[size++] = (TextArray*)list2->get(n++);
252289
}
253290
list->clear();
291+
list2->clear();
254292
}
255293
}
256294

@@ -275,21 +313,27 @@ class TextArray {
275313
TextArray(const char* name) {
276314
this->name = name;
277315
list = new LinkedList(name);
316+
list2 = new LinkedList(name);
278317
}
279318

280319
///////////////////////////////////////////////////////////////////////
281320
// Default constructor
282321
TextArray() {
283322
list = new LinkedList("<noname>");
323+
list2 = new LinkedList("<noname>");
284324
}
285325

286326
///////////////////////////////////////////////////////////////////////
287327
// Destructor
288328
~TextArray() {
289329
delete array;
290330
array = nullptr;
331+
delete array2;
332+
array2 = nullptr;
291333
delete list;
292334
list = nullptr;
335+
delete list2;
336+
list2 = nullptr;
293337
#if DESTROY
294338
print("TextArray: Delete %s\n", name);
295339
#endif

0 commit comments

Comments
 (0)