File: FXObjectList.cpp

package info (click to toggle)
gogglesmm 1.2.5-6
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 16,812 kB
  • sloc: cpp: 231,960; ansic: 893; xml: 222; makefile: 33
file content (442 lines) | stat: -rw-r--r-- 10,542 bytes parent folder | download | duplicates (2)
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/********************************************************************************
*                                                                               *
*                            O b j e c t   L i s t                              *
*                                                                               *
*********************************************************************************
* Copyright (C) 1997,2022 by Jeroen van der Zijp.   All Rights Reserved.        *
*********************************************************************************
* This library is free software; you can redistribute it and/or modify          *
* it under the terms of the GNU Lesser General Public License as published by   *
* the Free Software Foundation; either version 3 of the License, or             *
* (at your option) any later version.                                           *
*                                                                               *
* This library is distributed in the hope that it will be useful,               *
* but WITHOUT ANY WARRANTY; without even the implied warranty of                *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                 *
* GNU Lesser General Public License for more details.                           *
*                                                                               *
* You should have received a copy of the GNU Lesser General Public License      *
* along with this program.  If not, see <http://www.gnu.org/licenses/>          *
********************************************************************************/
#include "xincs.h"
#include "fxver.h"
#include "fxdefs.h"
#include "FXArray.h"
#include "FXHash.h"
#include "FXStream.h"
#include "FXObject.h"
#include "FXStream.h"
#include "FXElement.h"
#include "FXObjectList.h"

/*
  Notes:
  - A list of pointers to objects.
  - The list may be serialized; this means contents of all objects referenced
    from the list may be saved or loaded through the serialization mechanism.
  - Serialization is limited to 2^31 objects only, due to serialization format
    using a 32-bit int for compatibility with 32 bit systems.
*/


// Size rounded up to nearest multiple of ROUNDVAL
#define ROUNDVAL    16

// Round up to nearest ROUNDVAL
#define ROUNDUP(n)  (((n)+ROUNDVAL-1)&-ROUNDVAL)

// Special empty object list value
#define EMPTY       (const_cast<FXObject**>((FXObject *const *)(__objectlist__empty__+1)))

using namespace FX;

/*******************************************************************************/

namespace FX {


// Empty object list valie
extern const FXival __objectlist__empty__[];
const FXival __objectlist__empty__[2]={0,0};


// Change number of items in list
FXbool FXObjectList::no(FXival num){
  if(__likely(num!=no())){
    if(0<num){
      FXptr p;
      if(ptr!=EMPTY){
        if(__unlikely((p=::realloc(((FXival*)ptr)-1,sizeof(FXival)+sizeof(FXObject*)*ROUNDUP(num)))==nullptr)) return false;
        }
      else{
        if(__unlikely((p=::malloc(sizeof(FXival)+sizeof(FXObject*)*ROUNDUP(num)))==nullptr)) return false;
        }
      ptr=(FXObject**)(((FXival*)p)+1);
      *(((FXival*)ptr)-1)=num;
      }
    else{
      if(ptr!=EMPTY){
        ::free(((FXival*)ptr)-1);
        ptr=EMPTY;
        }
      }
    }
  return true;
  }


// Default constructor
FXObjectList::FXObjectList():ptr(EMPTY){
  }


// Copy constructor
FXObjectList::FXObjectList(const FXObjectList& other):ptr(EMPTY){
  FXival num=other.no();
  if(__likely(0<num && no(num))){
    copyElms(ptr,other.ptr,num);
    }
  }


// Construct and init with single object
FXObjectList::FXObjectList(FXObject* object):ptr(EMPTY){
  if(__likely(no(1))){
    ptr[0]=object;
    }
  }


// Construct and init with n copies of object
FXObjectList::FXObjectList(FXObject* object,FXival n):ptr(EMPTY){
  if(__likely(0<n && no(n))){
    fillElms(ptr,object,n);
    }
  }


// Construct and init with list of objects
FXObjectList::FXObjectList(FXObject** objects,FXival n):ptr(EMPTY){
  if(__likely(0<n && no(n))){
    copyElms(ptr,objects,n);
    }
  }


// Assignment operator
FXObjectList& FXObjectList::operator=(const FXObjectList& other){
  if(__likely(ptr!=other.ptr && no(other.no()))){
    copyElms(ptr,other.ptr,other.no());
    }
  return *this;
  }


// Adopt objects from src, leaving src empty
FXObjectList& FXObjectList::adopt(FXObjectList& other){
  if(__likely(ptr!=other.ptr)){
    swap(ptr,other.ptr);
    other.clear();
    }
  return *this;
  }


// Find object in list, searching forward; return position or -1
FXival FXObjectList::find(const FXObject* object,FXival pos) const {
  FXival p=FXMAX(0,pos);
  while(p<no()){
    if(ptr[p]==object){ return p; }
    ++p;
    }
  return -1;
  }


// Find object in list, searching backward; return position or -1
FXival FXObjectList::rfind(const FXObject* object,FXival pos) const {
  FXival p=FXMIN(pos,no()-1);
  while(0<=p){
    if(ptr[p]==object){ return p; }
    --p;
    }
  return -1;
  }


// Assign object p to list
FXbool FXObjectList::assign(FXObject* object){
  if(__likely(no(1))){
    ptr[0]=object;
    return true;
    }
  return false;
  }


// Assign n copies of object to list
FXbool FXObjectList::assign(FXObject* object,FXival n){
  if(__likely(no(n))){
    fillElms(ptr,object,n);
    return true;
    }
  return false;
  }


// Assign n objects to list
FXbool FXObjectList::assign(FXObject** objects,FXival n){
  if(__likely(no(n))){
    moveElms(ptr,objects,n);
    return true;
    }
  return false;
  }


// Assign objects to list
FXbool FXObjectList::assign(const FXObjectList& objects){
  return assign(objects.ptr,objects.no());
  }


// Insert an object
FXbool FXObjectList::insert(FXival pos,FXObject* object){
  FXival num=no();
  if(__likely(no(num+1))){
    moveElms(ptr+pos+1,ptr+pos,num-pos);
    ptr[pos]=object;
    return true;
    }
  return false;
  }


// Insert n copies of object at specified position
FXbool FXObjectList::insert(FXival pos,FXObject* object,FXival n){
  FXival num=no();
  if(__likely(no(num+n))){
    moveElms(ptr+pos+n,ptr+pos,num-pos);
    fillElms(ptr+pos,object,n);
    return true;
    }
  return false;
  }


// Insert n objects at specified position
FXbool FXObjectList::insert(FXival pos,FXObject** objects,FXival n){
  FXival num=no();
  if(__likely(no(num+n))){
    moveElms(ptr+pos+n,ptr+pos,num-pos);
    copyElms(ptr+pos,objects,n);
    return true;
    }
  return false;
  }


// Insert objects at specified position
FXbool FXObjectList::insert(FXival pos,const FXObjectList& objects){
  return insert(pos,objects.ptr,objects.no());
  }


// Prepend an object
FXbool FXObjectList::prepend(FXObject* object){
  FXival num=no();
  if(__likely(no(num+1))){
    moveElms(ptr+1,ptr,num);
    ptr[0]=object;
    return true;
    }
  return false;
  }


// Prepend n copies of object
FXbool FXObjectList::prepend(FXObject* object,FXival n){
  FXival num=no();
  if(__likely(no(num+n))){
    moveElms(ptr+n,ptr,num);
    fillElms(ptr,object,n);
    return true;
    }
  return false;
  }


// Prepend n objects
FXbool FXObjectList::prepend(FXObject** objects,FXival n){
  FXival num=no();
  if(__likely(no(num+n))){
    moveElms(ptr+n,ptr,num);
    copyElms(ptr,objects,n);
    return true;
    }
  return false;
  }


// Prepend objects
FXbool FXObjectList::prepend(const FXObjectList& objects){
  return prepend(objects.ptr,objects.no());
  }


// Append an object
FXbool FXObjectList::append(FXObject* object){
  FXival num=no();
  if(__likely(no(num+1))){
    ptr[num]=object;
    return true;
    }
  return false;
  }


// Append n copies of object
FXbool FXObjectList::append(FXObject* object,FXival n){
  FXival num=no();
  if(__likely(no(num+n))){
    fillElms(ptr+num,object,n);
    return true;
    }
  return false;
  }


// Append n objects
FXbool FXObjectList::append(FXObject** objects,FXival n){
  FXival num=no();
  if(__likely(no(num+n))){
    copyElms(ptr+num,objects,n);
    return true;
    }
  return false;
  }


// Append objects
FXbool FXObjectList::append(const FXObjectList& objects){
  return append(objects.ptr,objects.no());
  }


// Replace element
FXbool FXObjectList::replace(FXival pos,FXObject* object){
  ptr[pos]=object;
  return true;
  }


// Replaces the m objects at pos with n copies of object
FXbool FXObjectList::replace(FXival pos,FXival m,FXObject* object,FXival n){
  FXival num=no();
  if(__unlikely(m<n)){
    if(__unlikely(!no(num-m+n))) return false;
    moveElms(ptr+pos+n,ptr+pos+m,num-pos-n);
    }
  else if(__unlikely(m>n)){
    moveElms(ptr+pos+n,ptr+pos+m,num-pos-m);
    if(__unlikely(!no(num-m+n))) return false;
    }
  fillElms(ptr+pos,object,n);
  return true;
  }


// Replaces the m objects at pos with n objects
FXbool FXObjectList::replace(FXival pos,FXival m,FXObject** objects,FXival n){
  FXival num=no();
  if(__unlikely(m<n)){
    if(__unlikely(!no(num-m+n))) return false;
    moveElms(ptr+pos+n,ptr+pos+m,num-pos-n);
    }
  else if(__unlikely(m>n)){
    moveElms(ptr+pos+n,ptr+pos+m,num-pos-m);
    if(__unlikely(!no(num-m+n))) return false;
    }
  copyElms(ptr+pos,objects,n);
  return true;
  }


// Replace the m objects at pos with objects
FXbool FXObjectList::replace(FXival pos,FXival m,const FXObjectList& objects){
  return replace(pos,m,objects.ptr,objects.no());
  }


// Remove object at pos
FXbool FXObjectList::erase(FXival pos){
  FXival num=no();
  moveElms(ptr+pos,ptr+pos+1,num-pos-1);
  return no(num-1);
  }


// Remove n objects at pos
FXbool FXObjectList::erase(FXival pos,FXival n){
  FXival num=no();
  moveElms(ptr+pos,ptr+pos+n,num-n-pos);
  return no(num-n);
  }


// Remove object
FXbool FXObjectList::remove(const FXObject* object){
  FXival pos;
  if(0<=(pos=find(object))){
    return erase(pos);
    }
  return false;
  }


// Push object to end
FXbool FXObjectList::push(FXObject* object){
  return append(object);
  }


// Pop object from end
FXbool FXObjectList::pop(){
  return no(no()-1);
  }


// Clear the list
FXbool FXObjectList::clear(){
  return no(0);
  }


// Save to stream; children may be NULL
void FXObjectList::save(FXStream& store) const {
  FXint num;
  num=no();
  store << num;
  for(FXint i=0; i<num; i++){
    store << ptr[i];
    }
  }


// Load from stream; children may be NULL
void FXObjectList::load(FXStream& store){
  FXint num;
  store >> num;
  if(!no(num)) return;
  for(FXint i=0; i<num; i++){
    store >> ptr[i];
    }
  }


// Free up nicely
FXObjectList::~FXObjectList(){
  clear();
  }

}