1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <FL/Fl_Tree_Item_Array.H>
#include <FL/Fl_Tree_Item.H>
//////////////////////
// Fl_Tree_Item_Array.cxx
//////////////////////
//
// Fl_Tree -- This file is part of the Fl_Tree widget for FLTK
// Copyright (C) 2009-2010 by Greg Ercolano.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file. If this
// file is missing or damaged, see the license at:
//
// https://www.fltk.org/COPYING.php
//
// Please see the following page on how to report bugs and issues:
//
// https://www.fltk.org/bugs.php
//
/// Constructor; creates an empty array.
///
/// The optional 'chunksize' can be specified to optimize
/// memory allocation for potentially large arrays. Default chunksize is 10.
///
Fl_Tree_Item_Array::Fl_Tree_Item_Array(int new_chunksize) {
_items = 0;
_total = 0;
_size = 0;
_flags = 0;
_chunksize = new_chunksize;
}
/// Destructor. Calls each item's destructor, destroys internal _items array.
Fl_Tree_Item_Array::~Fl_Tree_Item_Array() {
clear();
}
/// Copy constructor. Makes new copy of array, with new instances of each item.
Fl_Tree_Item_Array::Fl_Tree_Item_Array(const Fl_Tree_Item_Array* o) {
_items = (Fl_Tree_Item**)malloc(o->_size * sizeof(Fl_Tree_Item*));
_total = 0;
_size = o->_size;
_chunksize = o->_chunksize;
_flags = o->_flags;
int t;
for (t =0; t<o->_total; t++ ) {
if ( _flags & MANAGE_ITEM ) {
_items[t] = new Fl_Tree_Item(o->_items[t]); // make new copy of item
++_total;
_items[t]->update_prev_next(t); // update uses _total's current value
} else {
_items[t] = o->_items[t]; // copy ptr only
++_total;
}
}
}
/// Clear the entire array.
///
/// Each item will be deleted (destructors will be called),
/// and the array will be cleared. total() will return 0.
///
void Fl_Tree_Item_Array::clear() {
if ( _items ) {
int t;
for (t =0; t<_total; t++ ) {
if ( _flags & MANAGE_ITEM )
{
delete _items[t];
_items[t] = 0;
}
}
free((void*)_items); _items = 0;
}
_total = _size = 0;
}
// Internal: Enlarge the items array.
//
// Adjusts size/items memory allocation as needed.
// Does NOT change total.
//
void Fl_Tree_Item_Array::enlarge(int count) {
int newtotal = _total + count; // new total
if ( newtotal >= _size ) { // more than we have allocated?
if ( (newtotal/150) > _chunksize ) _chunksize *= 10;
// Increase size of array
int newsize = _size + _chunksize;
Fl_Tree_Item **newitems = (Fl_Tree_Item**)malloc(newsize * sizeof(Fl_Tree_Item*));
if ( _items ) {
// Copy old array -> new, delete old
memmove(newitems, _items, _size * sizeof(Fl_Tree_Item*));
free((void*)_items); _items = 0;
}
// Adjust items/sizeitems
_items = newitems;
_size = newsize;
}
}
/// Insert an item at index position \p pos.
///
/// Handles enlarging array if needed, total increased by 1.
/// If \p pos \>= total(), the item is appended to the array.
/// If \p pos \< 0, the item is prepended (works like pos == 0).
///
void Fl_Tree_Item_Array::insert(int pos, Fl_Tree_Item *new_item) {
if (pos < 0)
pos = 0;
else if (pos > _total)
pos = _total;
enlarge(1);
// printf("*** POS=%d TOTAL-1=%d NITEMS=%d\n", pos, _total-1, (_total-pos));
if ( pos <= (_total - 1) ) { // need to move memory around?
int nitems = _total - pos;
memmove(&_items[pos+1], &_items[pos], sizeof(Fl_Tree_Item*) * nitems);
}
_items[pos] = new_item;
_total++;
if ( _flags & MANAGE_ITEM )
{
_items[pos]->update_prev_next(pos); // adjust item's prev/next and its neighbors
}
}
/// Add an item* to the end of the array.
///
/// Assumes the item was created with 'new', and will remain
/// allocated.. Fl_Tree_Item_Array will handle calling the
/// item's destructor when the array is cleared or the item remove()'ed.
///
void Fl_Tree_Item_Array::add(Fl_Tree_Item *val) {
insert(_total, val);
}
/// Replace the item at \p index with \p newitem.
///
/// Old item at index position will be destroyed,
/// and the new item will take it's place, and stitched into the linked list.
///
void Fl_Tree_Item_Array::replace(int index, Fl_Tree_Item *newitem) {
if ( _items[index] ) { // delete if non-zero
if ( _flags & MANAGE_ITEM )
// Destroy old item
delete _items[index];
}
_items[index] = newitem; // install new item
if ( _flags & MANAGE_ITEM )
{
// Restitch into linked list
_items[index]->update_prev_next(index);
}
}
/// Remove the item at \param[in] index from the array.
///
/// The item will be delete'd (if non-NULL), so its destructor will be called.
///
void Fl_Tree_Item_Array::remove(int index) {
if ( _items[index] ) { // delete if non-zero
if ( _flags & MANAGE_ITEM )
delete _items[index];
}
_items[index] = 0;
_total--;
int i;
for (i =index; i<_total; i++ ) { // reshuffle the array
_items[i] = _items[i+1];
}
if ( _flags & MANAGE_ITEM )
{
if ( index < _total ) { // removed item not last?
_items[index]->update_prev_next(index); // update next item's prev/next and neighbors
} else if ( ((index-1) >= 0) && // removed item IS last?
((index-1) < _total)) {
_items[index-1]->update_prev_next(index-1);// update prev item's prev/next and neighbors
}
}
}
/// Remove the item from the array.
///
/// \returns 0 if removed, or -1 if the item was not in the array.
///
int Fl_Tree_Item_Array::remove(Fl_Tree_Item *item) {
int t;
for (t =0; t<_total; t++ ) {
if ( item == _items[t] ) {
remove(t);
return(0);
}
}
return(-1);
}
/// Swap the two items at index positions \p ax and \p bx.
void Fl_Tree_Item_Array::swap(int ax, int bx) {
Fl_Tree_Item *asave = _items[ax];
_items[ax] = _items[bx];
_items[bx] = asave;
if ( _flags & MANAGE_ITEM )
{
// Adjust prev/next ptrs
_items[ax]->update_prev_next(ax);
_items[bx]->update_prev_next(bx);
}
}
/// Move item at 'from' to new position 'to' in the array.
/// Due to how the moving an item shuffles the array around,
/// a positional 'move' implies things that may not be obvious:
/// - When 'from' moved lower in tree, appears BELOW item that was at 'to'.
/// - When 'from' moved higher in tree, appears ABOVE item that was at 'to'.
///
/// \returns 0 on success, -1 on range error (e.g. if \p 'to' or \p 'from' out of range)
///
int Fl_Tree_Item_Array::move(int to, int from) {
if ( from == to ) return 0; // nop
if ( to<0 || to>=_total || from<0 || from>=_total ) return -1;
Fl_Tree_Item *item = _items[from];
// Remove item..
int t;
if ( from < to ) {
for (t =from; t<to && t<(_total+1); t++ )
_items[t] = _items[t+1];
} else {
for (t =from; t>to && t>0; t-- )
_items[t] = _items[t-1];
}
// Move to new position
_items[to] = item;
// Update all children
int r;
for (r =0; r<_total; r++ ) // XXX: excessive to do all children,
_items[r]->update_prev_next(r); // XXX: but avoids weird boundary issues
return 0;
}
/// Deparent item at \p 'pos' from our list of children.
/// Similar to a remove() without the destruction of the item.
/// This creates an orphaned item (still allocated, has no parent)
/// which soon after is typically reparented elsewhere.
///
/// \returns 0 on success, -1 on error (e.g. if \p 'pos' out of range)
///
int Fl_Tree_Item_Array::deparent(int pos) {
if ( pos>=_total || pos<0 ) return -1;
// Save item being deparented, and its two nearest siblings
Fl_Tree_Item *item = _items[pos];
Fl_Tree_Item *prev = item->prev_sibling();
Fl_Tree_Item *next = item->next_sibling();
// Remove from parent's list of children
_total -= 1;
int t;
for (t =pos; t<_total; t++ )
_items[t] = _items[t+1]; // delete, no destroy
// Now an orphan: remove association with old parent and siblings
item->update_prev_next(-1); // become an orphan
// Adjust bereaved siblings
if ( prev ) prev->update_prev_next(pos-1);
if ( next ) next->update_prev_next(pos);
return 0;
}
/// Reparent specified item as a child of ourself.
/// Typically 'newchild' was recently orphaned with deparent().
///
/// \returns 0 on success, -1 on error (e.g. if \p 'pos' out of range)
///
int Fl_Tree_Item_Array::reparent(Fl_Tree_Item *item, Fl_Tree_Item* newparent, int pos) {
if ( pos<0 || pos>_total ) return -1;
// Add item to new parent
enlarge(1);
_total += 1;
int t;
for (t =_total-1; t>pos; --t ) // shuffle array to make room for new entry
_items[t] = _items[t-1];
_items[pos] = item; // insert new entry
// Attach to new parent and siblings
_items[pos]->parent(newparent); // reparent (update_prev_next() needs this)
_items[pos]->update_prev_next(pos); // find new siblings
return 0;
}
|