-
Notifications
You must be signed in to change notification settings - Fork 1
/
list.h
4045 lines (3777 loc) · 145 KB
/
list.h
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Evo C++ Library
/* Copyright 2019 Justin Crowell
Distributed under the BSD 2-Clause License -- see included file LICENSE.txt for details.
*/
///////////////////////////////////////////////////////////////////////////////
/** \file list.h Evo List container. */
#pragma once
#ifndef INCL_evo_list_h
#define INCL_evo_list_h
#include "impl/container.h"
#include "impl/iter.h"
// Disable certain MSVC warnings for this file
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable:4458)
#endif
namespace evo {
/** \addtogroup EvoContainers */
//@{
///////////////////////////////////////////////////////////////////////////////
/** Sequential list container with random access.
\tparam T Item type to use
\tparam TSize Size type to use for size/index values (must be unsigned integer) -- default: SizeT
\par Features
- Similar to STL `vector`
- Items are stored sequentially in memory as a dynamic array -- random access uses constant time
- Preallocates extra memory when buffer grows -- see capacity(), resize(), capacity(Size)
- No memory allocated by new empty list
.
- Some methods have a read-only (const) version and modifier version with suffix "M" -- example: item() and itemM()
- operator[]() is read-only -- use operator()() for mutable access
- Supports efficient use as stack or queue -- see add(const Item&), pop(), popq()
- Advanced methods have "adv" prefix -- these allow some intrusive control
.
- \ref Sharing "Sharing" and \ref Slicing "Slicing" make for simple and efficient copying/splitting
- \b Caution: Copying from a raw pointer will use \ref UnsafePtrRef "Unsafe Pointer Referencing"
.
C++11:
- Range-based for loop -- see \ref StlCompatibility
\code
List<int> list;
for (auto num : list.asconst()) {
}
\endcode
- Initialization lists
\code
List<int> list = {1, 2, 3};
List<String> strlist = {"foo", "bar"};
\endcode
- Move semantics
\par Iterators
- List<>::Iter -- Read-Only Iterator (IteratorRa)
- List<>::IterM -- Mutable Iterator (IteratorRa)
.
\par Constructors
- List()
- List(const ValEmpty&)
- List(const ListType&)
- List(const ListType&,Key,Key)
- List(const ListBaseType&,Key,Key)
- List(const Item*,Size)
- List(const PtrBase<Item>&,Size)
- List(std::initializer_list<T>) [C++11]
- List(ListType&&) [C++11]
.
\par Read Access
- asconst()
- size()
- null(), empty()
- capacity()
- shared()
- data()
- item()
- operator[]()
- first(), last(), iend()
- hash()
- compare()
- operator==()
- operator!=()
- starts(ItemVal) const
- starts(const Item*,Size) const
- ends(ItemVal) const
- ends(const Item*,Size) const
- cbegin(), cend()
- begin() const, end() const
- find()
- findr()
- findany()
- findanyr()
- contains(ItemVal) const
- contains(const Item*,Size) const
- splitat(Key,T1&,T2&) const
- splitat(Key,T1&) const
- splitat(Key,ValNull,T2&) const
.
\par Slicing
- slice(Key)
- slice(Key,Size), slice2()
- truncate()
- triml(), trimr()
- splitat_setl(Key), splitat_setl(Key,T2&)
- splitat_setr(Key), splitat_setr(Key,T1&)
- pop(), popq()
- unslice()
.
\par Modifiers
- dataM()
- itemM()
- operator()()
- firstM(), lastM()
- begin(), end()
- resize()
- reserve(), compact()
- capacity(Size)
- capacitymin(), capacitymax()
- unshare()
- set()
- set(const ListType&)
- set(const ListType&,Key,Key)
- set(const ListBaseType&,Key,Key)
- set(const Item*,Size)
- set(const PtrBase<Item>&,Size)
- set2()
- setempty()
- clear()
- operator=(const ListType& data)
- operator=(ListType&&) [C++11]
- operator=(const ValNull&)
- operator=(const ValEmpty&)
- copy(const ListBaseType&)
- copy(const Item*,Size)
- fill()
- add(const Item&)
- add(const ListBaseType&)
- add(const Item*,Size)
- addnew(), addmin()
- operator<<(const Item&)
- operator<<(const ListBaseType&)
- operator<<(const ValNull&)
- operator<<(const ValEmpty&)
- prepend(const Item&)
- prepend(const ListBaseType&)
- prepend(const Item*,Size)
- prependnew()
- insert(Key,const Item&)
- insert(Key,const ListBaseType&)
- insert(Key,const Item*,Size)
- insertnew()
- pop(T&)
- pop(T&,Key)
- popq(T&)
- remove()
- replace()
- move(Key,Key)
- move(Key,ListType&,Key,Size)
- swap(Key,Key)
- swap(ListType&)
- reverse()
.
\par Advanced
- advItem(Key)
- advFirst(), advLast()
- advResize()
- advBuffer(Size)
- advBuffer()
- advSize()
- advWrite()
- advWriteDone()
- advEdit()
- advEditDone()
- advAdd()
- advPrepend()
- advInsert()
- advRemove()
- advSwap()
.
\par Example
\code
#include <evo/list.h>
#include <evo/io.h>
using namespace evo;
static Console& c = con();
int main() {
// Create number list, add some numbers
List<int> list;
list.add(1);
list << 2 << 3;
// operator[] provides read-only (const) access
int value = list[0];
//list[0] = 0; // Error: operator[] is read-only (const)
// operator() provides write (mutable) access
list(0) = 0; // Allowed: operator() is mutable
// Iterate and modify items (mutable)
for (List<int>::IterM iter(list); iter; ++iter)
(*iter) += 10; // Parenthesis not required, only used to workaround doxygen formatting bug
// Iterate and print items (read-only)
for (List<int>::Iter iter(list); iter; ++iter)
c.out << "Item: " << *iter << NL;
return 0;
}
\endcode
Output:
\code{.unparsed}
Item: 10
Item: 12
Item: 13
\endcode
*/
template<class T,class TSize=SizeT>
class List : public ListBase<T,TSize> {
protected:
using ListBase<T,TSize>::data_; // Slice data pointer, NULL if null, EVO_PEMPTY if empty (null/empty: buffer unused, no reference)
using ListBase<T,TSize>::size_; // Slice size, 0 if empty
public:
EVO_CONTAINER_TYPE;
typedef TSize Size; ///< %List size integer type
typedef Size Key; ///< %Key type (item index)
typedef T Value; ///< %Value type (same as Item)
typedef T Item; ///< %Item type (same as Value)
typedef typename DataCopy<T>::PassType ItemVal; ///< %Item type as parameter (POD types passed by value, otherwise by const-ref)
typedef List<T,Size> ThisType; ///< This list type
typedef List<T,Size> ListType; ///< %List type for parameters
typedef ListBase<T,Size> ListBaseType; ///< %List base type for any Evo list
// Iterator support types
/** \cond impl */
typedef Key IterKey;
typedef T IterItem;
/** \endcond */
typedef typename IteratorRa<ThisType>::Const Iter; ///< Iterator (const) - IteratorRa
typedef IteratorRa<ThisType> IterM; ///< Iterator (mutable) - IteratorRa
/** %Edit buffer for advEdit().
- This holds buffer state during an edit operation
- This cleans up if edit is incomplete so will not leak memory
- See advEdit()
*/
struct Edit {
T* ptr; ///< Data pointer, use to write to buffer
Size size; ///< Data size, update after write
void* header; ///< Internal buffer data, do not modify
/** Constructor. */
Edit() : ptr(NULL), size(0), header(NULL)
{ }
/** Destructor, frees buffer if needed. */
~Edit() {
if (header != NULL)
::free(header);
}
/** Clear data and free buffer. */
void clear() {
if (header != NULL) {
::free(header);
header = NULL;
}
ptr = NULL;
size = 0;
}
/** Write (copy) data from source.
- This appends to end of edit data (ptr+size) and updates size member
- \b Caution: This assumes destination items are uninitialized (only matters for non-POD types)
- \b Caution: Results are undefined if copying from buffer being edited in-place (advEdit() returned false)
- \b Caution: Results are undefined if writing after end of buffer
.
\param src Source to copy from
\param start Start position to copy from
\param count Item count to copy, ALL for all
\return Actual item count written, 0 for none
*/
Size write(const ListBaseType& src, Size start=0, Size count=ALL) {
if (start < src.size_ && count > 0) {
const Size maxcount = src.size_ - start;
if (count > maxcount)
count = maxcount;
DataInit<Item>::init(ptr + size, src.data_, count);
size += count;
} else
count = 0;
return count;
}
/** Write (copy) data from buffer.
- This appends to end of edit data (ptr+size) and updates size member
- \b Caution: This assumes destination items are uninitialized (only matters for non-POD types)
- \b Caution: Results are undefined if copying from buffer being edited in-place (advEdit() returned false)
- \b Caution: Results are undefined if writing after end of buffer
.
\param data Data pointer to copy from
\param count Item count to copy
\return Actual item count written (same as count)
*/
Size write(const Item* data, Size count) {
if (count > 0) {
assert( data != NULL );
DataInit<Item>::init(ptr + size, data, count);
size += count;
}
return count;
}
};
/** Default constructor sets as null. */
List() {
data_ = NULL;
size_ = 0;
#if EVO_LIST_OPT_REFTERM
terminated_ = false;
#endif
}
/** Constructor sets as empty but not null.
\param val vEMPTY
*/
explicit List(const ValEmpty& val) {
EVO_PARAM_UNUSED(val);
data_ = EVO_PEMPTY;
size_ = 0;
#if EVO_LIST_OPT_REFTERM
terminated_ = false;
#endif
}
/** Copy constructor.
- Makes shared copy if possible -- see \ref Sharing "Sharing"
.
\param data Data to copy
*/
List(const ListType& data) {
data_ = NULL;
size_ = 0;
#if EVO_LIST_OPT_REFTERM
terminated_ = false;
#endif
ref(data);
}
/** Extended copy constructor.
- Makes shared copy if possible -- see \ref Sharing "Sharing"
.
\param data Data to copy
\param index Start index of data to copy, END to set as empty
\param size Size as item count, ALL for all from index
*/
List(const ListType& data, Key index, Key size=ALL) {
data_ = NULL;
size_ = 0;
#if EVO_LIST_OPT_REFTERM
terminated_ = false;
#endif
ref(data, index, size);
}
/** Constructor to copy sublist data.
- For best performance (and less safety) reference sublist instead with set(const Item*,Size)
.
\param data Data to copy
\param index Start index of data to reference, END to set as empty
\param size Size as item count, ALL for all from index
*/
List(const ListBaseType& data, Key index=0, Key size=ALL) {
size_ = 0;
#if EVO_LIST_OPT_REFTERM
terminated_ = false;
#endif
if (data.data_ == NULL) {
data_ = NULL;
} else if (index < data.size_) {
const Size max_size = data.size_ - index;
if (size > max_size)
size = max_size;
if (size > 0)
copy(data.data_+index, size);
else
data_ = EVO_PEMPTY;
} else
data_ = EVO_PEMPTY;
}
/** Constructor to copy sublist data.
- For best performance (and less safety) reference sublist instead with set(const Item*,Size)
.
\param data Data pointer to copy from, NULL to set as null
\param index Start index of data to reference, END to set as empty
\param size Size as item count, ALL for all from index
*/
List(const ListBaseType* data, Key index=0, Key size=ALL) {
size_ = 0;
#if EVO_LIST_OPT_REFTERM
terminated_ = false;
#endif
if (data == NULL || data->data_ == NULL) {
data_ = NULL;
} else if (index < data->size_) {
const Size max_size = data->size_ - index;
if (size > max_size)
size = max_size;
if (size > 0)
copy(data->data_+index, size);
else
data_ = EVO_PEMPTY;
} else
data_ = EVO_PEMPTY;
}
/** Constructor for data pointer.
- \b Caution: Uses \ref UnsafePtrRef "Unsafe Pointer Referencing"
.
\param data Data pointer to use
\param size Data size as item count
*/
List(const Item* data, Size size) {
data_ = NULL;
size_ = 0;
#if EVO_LIST_OPT_REFTERM
terminated_ = false;
#endif
if (data != NULL)
ref(data, size);
}
/** Constructor to copy from managed pointer.
\param data Data pointer to use
\param size Data size as item count
*/
List(const PtrBase<Item>& data, Size size) {
data_ = NULL;
size_ = 0;
#if EVO_LIST_OPT_REFTERM
terminated_ = false;
#endif
if (data.ptr_ != NULL)
copy(data.ptr_, size);
}
#if defined(EVO_CPP11)
/** Sequence constructor (C++11).
\param init Initializer list, passed as comma-separated values in braces `{ }`
*/
List(std::initializer_list<T> init) : List() {
assert( init.size() < IntegerT<Size>::MAX );
modAppend(NULL, (Size)init.size());
T* item = data_;
for (auto& val : init)
new(item++) T(val);
}
/** Move constructor (C++11).
\param src Source to move
*/
List(ListType&& src) {
::memcpy(this, &src, sizeof(ListType));
::memset(&src, 0, sizeof(ListType));
}
/** Move assignment operator (C++11).
\param src Source to move
\return This
*/
ListType& operator=(ListType&& src) {
clear();
capacity(0);
::memcpy(this, &src, sizeof(ListType));
::memset(&src, 0, sizeof(ListType));
return *this;
}
#endif
/** Explicitly use a const reference to this.
- This is useful to force using this as const without casting
.
\return This
*/
const ListType& asconst() const {
return *this;
}
// SET
/** Assignment operator.
- Makes shared copy if possible -- see \ref Sharing "Sharing"
.
\param data Data to copy
\return This
*/
ListType& operator=(const ListType& data)
{ return set(data); }
/** Assignment operator to copy sublist.
- For best performance (and less safety) reference sublist instead with set(const Item*,Size)
.
\param data Data to copy
\return This
*/
ListType& operator=(const ListBaseType& data) {
if (data.data_ == NULL)
set();
else if (data.size_ > 0)
copy(data.data_, data.size_);
else
setempty();
return *this;
}
/** Assignment operator to set as null and empty.
- Same as set()
.
Example:
\code
list = vNULL;
\endcode
\return This
*/
ListType& operator=(const ValNull&)
{ return set(); }
/** Assignment operator to set as empty but not null.
Example:
\code
list = vEMPTY;
\endcode
\return This
*/
ListType& operator=(const ValEmpty&)
{ clear(); data_ = EVO_PEMPTY; return *this; }
/** Clear by removing all items.
- Does not set as null -- null status is unchanged
- Append operators can be chained\n
Example:
\code
// Clear character list and append two characters
list.clear() << 'a' << 'b';
\endcode
.
\return This
*/
ListType& clear() {
if (data_ > EVO_PEMPTY) {
if (buf_.ptr != NULL) {
assert( buf_.header != NULL );
if (buf_.header->refs > 1) {
// Detach from shared
--buf_.header->refs;
buf_.header = NULL;
buf_.ptr = NULL;
data_ = EVO_PEMPTY;
} else if (buf_.header->used > 0) {
// Clear buffer, leave buffer for later use
assert( buf_.header->refs == 1 );
DataInit<T>::uninit(buf_.ptr, buf_.header->used);
buf_.header->used = 0;
data_ = buf_.ptr;
}
} else {
data_ = EVO_PEMPTY;
#if !EVO_LIST_OPT_LAZYBUF
// Lazy buffer disabled
assert( buf_.header == NULL );
#endif
}
size_ = 0;
#if EVO_LIST_OPT_REFTERM
terminated_ = false;
#endif
}
#if !EVO_LIST_OPT_LAZYBUF
// Lazy buffer disabled
assert( (buf_.header != NULL) == (buf_.ptr != NULL) );
#endif
return *this;
}
/** %Set as null and empty.
- Append operators can be chained\n
Example:
\code
// Clear character list and append two characters
list.set() << 'a' << 'b';
\endcode
.
\return This
*/
ListType& set()
{ clear(); data_ = NULL; return *this; }
/** %Set from data pointer.
- \b Caution: Uses \ref UnsafePtrRef "Unsafe Pointer Referencing"
.
\param data Data pointer to use
\param size Data size as item count
\return This
*/
ListType& set(const Item* data, Size size)
{ ref(data, size); return *this; }
/** %Set from managed data pointer.
- \b Caution: Uses \ref UnsafePtrRef "Unsafe Pointer Referencing"
.
\param data Data pointer to copy
\param size Data size as item count
*/
ListType& set(const PtrBase<Item>& data, Size size) {
if (data.ptr_ == NULL)
set();
else
copy(data.ptr_, size);
return *this;
}
/** %Set from another list.
- Makes shared copy if possible -- see \ref Sharing "Sharing"
.
\param data Data to copy
\return This
*/
ListType& set(const ListType& data)
{ ref(data); return *this; }
/** %Set from subset of another list.
- Makes shared copy if possible -- see \ref Sharing "Sharing"
.
\param data Data to copy
\param index Start index of data, END to set as empty
\param size Data size as item count, ALL for all from index
\return This
*/
ListType& set(const ListType& data, Key index, Key size=ALL)
{ ref(data, index, size); return *this; }
/** %Set as copy of sublist.
- For best performance (and less safety) reference sublist instead with set(const Item*,Size)
.
\param data Data to copy
\param index Start index of sublist data, END to set as empty
\param size Data size as item count, ALL for all from index
\return This
*/
ListType& set(const ListBaseType& data, Key index=0, Key size=ALL) {
if (data.data_ == NULL)
set();
else if (index < data.size_) {
const Size max_size = data.size_ - index;
if (size > max_size)
size = max_size;
if (size > 0)
copy(data.data_+index, size);
else
setempty();
} else
setempty();
return *this;
}
/** %Set from subset of another list using start/end positions.
- Makes shared copy if possible -- see \ref Sharing "Sharing"
- If index2 < index1 then index2 will be set to index1 (empty sublist)
.
\param data Data to copy
\param index1 Start index of new slice, END to slice all items from beginning
\param index2 End index of new slice (this item not included), END for all after index1
\return This
*/
ListType& set2(const ListType& data, Key index1, Key index2)
{ ref(data, index1, (index1<index2?index2-index1:0)); return *this; }
/** %Set as copy of sublist using start/end positions.
- For best performance (and less safety) reference sublist instead with set(const Item*,Size)
.
\param data Data to copy
\param index1 Start index of sublist data, END to set as empty
\param index2 End index of sublist data (this item not included), END for all after index1
\return This
*/
ListType& set2(const ListBaseType& data, Key index1, Key index2) {
if (data.data_ == NULL) {
set();
} else {
if (index2 > data.size_)
index2 = data.size_;
if (index1 < data.size_ && index2 > index1)
copy(data.data_+index1, (index2-index1));
else
setempty();
}
return *this;
}
/** %Set as empty but not null.
- Append operators can be chained\n
Example:
\code
// Set as empty character list then append two characters
list.setempty() << 'a' << 'b';
\endcode
.
\return This
*/
ListType& setempty()
{ clear(); data_ = EVO_PEMPTY; return *this; }
// INFO
/** Get whether null.
- Always empty when null
.
\return Whether null
*/
bool null() const
{ return (data_ == NULL); }
/** Get whether empty.
- Empty when size() is 0
.
\return Whether empty
*/
bool empty() const
{ return (size_ == 0); }
/** Get size.
\return Size as item count
*/
Size size() const
{ return size_; }
/** Get whether shared.
- Data is shared when referencing external data or buffer is allocated and shared (reference count > 1)
.
\return Whether shared
*/
bool shared() const
{ return (buf_.ptr == NULL ? (size_ > 0) : (buf_.header->refs > 1)); }
/** Get capacity.
- This gets the total size of currently allocated buffer
- Data still may be referenced without a buffer so this could return less than size()
- Buffer may be allocated (by previous use) even though not currently used
.
\return Capacity as item count, 0 if no buffer allocated
*/
Size capacity() const
{ return (buf_.header == NULL ? 0 : buf_.header->size); }
/** Get data pointer (const).
- \b Caution: May return an invalid non-NULL pointer if empty
- \b Caution: Calling any modifier/mutable method like unshare() after this may (will) invalidate the returned pointer
.
\return Data pointer as read-only, NULL if null, may be invalid if empty (const)
*/
const Item* data() const
{ return data_; }
/** Get item at position (const).
- \b Caution: Results are undefined if index is out of bounds
.
\param index Item index
\return Given item as read-only (const)
*/
const Item& operator[](Key index) const
{ assert( index < size_ ); return data_[index]; }
/** Get item at position (const).
- \b Caution: Results are undefined if index is out of bounds
.
\param index Item index
\return Given item as read-only (const)
*/
const Item& item(Key index) const
{ assert( index < size_ ); return data_[index]; }
/** Get first item (const).
\return First item pointer, NULL if empty
*/
const Item* first() const
{ return (size_ > 0 ? data_ : NULL); }
/** Get last item (const).
\return Last item pointer, NULL if empty
*/
const Item* last() const
{ return (size_ > 0 ? data_+size_-1 : NULL); }
/** Get index from last item using offset.
- This simplifies math when computing an index from last item
- This uses the formula: iend = size - 1 - offset
.
\param offset Offset from end, 0 for last item, 1 for second-last, etc
\return Resulting index, END if offset out of bounds
*/
Key iend(Size offset=0) const
{ return (offset < size_ ? size_-1-offset : END); }
/** Get data hash value.
\param seed Seed value for hashing multiple values, 0 if none
\return Hash value
*/
ulong hash(ulong seed=0) const
{ return DataHash<T>::hash(data_, size_, seed); }
// COMPARE
/** Comparison.
\param data Data to compare to
\return Result (<0 if this is less, 0 if equal, >0 if this is greater)
*/
int compare(const ListBaseType& data) const {
int result;
if (data_ == NULL)
result = (data.data_ == NULL ? 0 : -1);
else if (data.data_ == NULL)
result = 1;
else
result = DataCompare<T>::compare(data_, size_, data.data_, data.size_);
return result;
}
/** Equality operator.
\param data Data to compare to
\return Whether equal
*/
bool operator==(const ListBaseType& data) const {
bool result;
if (data_ == NULL)
result = (data.data_ == NULL);
else if (data.data_ == NULL || size_ != data.size_)
result = false;
else if (size_ == 0 || data_ == data.data_) {
result = true;
} else
result = DataEqual<T>::equal(data_, data.data_, data.size_);
return result;
}
/** Inequality operator.
\param data Data to compare to
\return Whether inequal
*/
bool operator!=(const ListBaseType& data) const {
bool result;
if (data_ == NULL)
result = (data.data_ != NULL);
else if (data.data_ == NULL || size_ != data.size_)
result = true;
else if (size_ == 0 || data_ == data.data_) {
result = false;
} else
result = !DataEqual<T>::equal(data_, data.data_, data.size_);
return result;
}
/** Check if this starts with given item.
- Uses item %operator==() for comparisons
.
\param item Item to check
\return Whether starts with item
*/
bool starts(ItemVal item) const
{ return (size_ > 0 && data_[0] == item); }
/** Check if starts with given items.
- Uses item %operator==() for comparisons
.
\param items Items to check
\param size Size as item count to check
\return Whether starts with items
*/
bool starts(const Item* items, Size size) const
{ return (size > 0 && size_ >= size && DataEqual<T>::equal(data_, items, size)); }
/** Check if this starts with given items.
- Uses item %operator==() for comparisons
.
\param items Items to check
\return Whether starts with items
*/
bool starts(const ListBaseType& items) const
{ return (items.size_ > 0 && size_ >= items.size_ && DataEqual<T>::equal(data_, items.data_, items.size_)); }
/** Check if this ends with given item.
- Uses item %operator==() for comparisons
.
\param item Item to check
\return Whether ends with item
*/
bool ends(ItemVal item) const
{ return (size_ > 0 && data_[size_-1] == item); }
/** Check if this ends with given items.
- Uses item %operator==() for comparisons
.
\param items Items to check
\param size Item count to check
\return Whether ends with items
*/
bool ends(const Item* items, Size size) const
{ return (size > 0 && size_ >= size && DataEqual<T>::equal(data_+(size_-size), items, size)); }
/** Check if this ends with given items.
- Uses item %operator==() for comparisons
.
\param items Items to check
\return Whether ends with items
*/
bool ends(const ListBaseType& items) const
{ return (items.size_ > 0 && size_ >= items.size_ && DataEqual<T>::equal(data_+(size_-items.size_), items.data_, items.size_)); }
// FIND
/** Get iterator at first item (const).
- This allows compatibility with range-based for loops and other libraries, otherwise use container Iter directly
.
\return Iterator at first item, or at end position if empty
\see Iter, cend(), begin(), end()
*/
Iter cbegin() const
{ return Iter(*this); }
/** Get iterator at end (const).
- This allows compatibility with range-based for loops and other libraries, otherwise use container Iter directly
- This really just creates an empty iterator
.
\return Iterator at end position
\see Iter, cbegin(), begin(), end()
*/
Iter cend() const
{ return Iter(); }
/** Get iterator at first item (mutable).
- This allows compatibility with range-based for loops and other libraries, otherwise use container Iter directly
- cbegin() is more efficient, since this effectively calls unshare() to make items mutable
.
\return Iterator at first item, or at end position if empty
\see IterM, end(), cbegin(), cend()
*/
IterM begin()
{ return IterM(*this); }
/** Get iterator at first item (const).
- This allows compatibility with range-based for loops and other libraries, otherwise use container Iter directly
.
\return Iterator at first item, or at end position if empty
\see end() const, cbegin()
*/
Iter begin() const
{ return Iter(*this); }
/** Get iterator at end.
- This allows compatibility with range-based for loops and other libraries, otherwise use container Iter directly
- This really just creates an empty iterator
.
\return Iterator at end position
\see IterM, begin(), cbegin(), cend()
*/
IterM end()
{ return IterM(); }
/** Get iterator at end (const).
- This allows compatibility with range-based for loops and other libraries, otherwise use container Iter directly
- This really just creates an empty iterator
.
\return Iterator at end position
\see begin() const, cend()
*/
Iter end() const