-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLLClasses.d
6074 lines (5880 loc) · 151 KB
/
LLClasses.d
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
/*
LLClasses is licenced under the terms of the MIT License (MIT)
Copyright (c) 2013 Basile Burg
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
module LLClasses;
import core.exception;
import core.stdc.string;
import core.memory : GC;
import std.stdio, std.string, std.c.stdlib;
import std.traits, std.demangle, std.conv, std.range;
version (Windows)
{
import core.sys.windows.windows, std.windows.syserror, std.c.windows.windows;
immutable newlineA = "\r\n";
// LS separator (std.uni) is not widely supported.
immutable newlineW = "\r\n"w;
immutable newlineD = "\r\n"d;
//
immutable AltnewlineA = "\n";
// LS separator (std.uni) is not widely supported.
immutable AltnewlineW = "\n"w;
immutable AltnewlineD = "\n"d;
//
immutable IsWin = true;
immutable IsPosix = false;
alias HANDLE FileHandle;
alias HANDLE SystemHandle;
const READ_WRITE = GENERIC_WRITE | GENERIC_READ;
const FSCTL_UNLOCK_VOLUME = 0x0009001c;
const FSCTL_LOCK_VOLUME = 0x00090018;
const IOCTL_DISK_GET_DRIVE_GEOMETRY = 0x0070000;
const IOCTL_DISK_GET_DRIVE_GEOMETRY_EX = 0x00700a0;
const IOCTL_STORAGE_QUERY_PROPERTY = 0x002D1400;
enum MEDIA_TYPE {
Unknown = 0x00,
F5_1Pt2_512 = 0x01,
F3_1Pt44_512 = 0x02,
F3_2Pt88_512 = 0x03,
F3_20Pt8_512 = 0x04,
F3_720_512 = 0x05,
F5_360_512 = 0x06,
F5_320_512 = 0x07,
F5_320_1024 = 0x08,
F5_180_512 = 0x09,
F5_160_512 = 0x0a,
RemovableMedia = 0x0b,
FixedMedia = 0x0c,
F3_120M_512 = 0x0d,
F3_640_512 = 0x0e,
F5_640_512 = 0x0f,
F5_720_512 = 0x10,
F3_1Pt2_512 = 0x11,
F3_1Pt23_1024 = 0x12,
F5_1Pt23_1024 = 0x13,
F3_128Mb_512 = 0x14,
F3_230Mb_512 = 0x15,
F8_256_128 = 0x16,
F3_200Mb_512 = 0x17,
F3_240M_512 = 0x18,
F3_32M_512 = 0x19
};
enum PARTITION_STYLE
{
PARTITION_STYLE_MBR = 0,
PARTITION_STYLE_GPT = 1,
PARTITION_STYLE_RAW = 2
};
enum STORAGE_PROPERTY_ID
{
StorageDeviceProperty = 0, // Indicates that the caller is querying for the device descriptor.
StorageAdapterProperty = 1, // Indicates that the caller is querying for the adapter descriptor.
StorageDeviceIdProperty = 2, // Indicates that the caller is querying for the device identifiers provided with the SCSI vital product data pages.
StorageDeviceUniqueIdProperty = 3, // Indicates that the caller is querying for the unique device identifiers. Vista/Server2008 or +.
StorageDeviceWriteCacheProperty = 4, // Indicates that the caller is querying for the write cache property. Vista/Server2008 or +.
StorageMiniportProperty = 5, // Indicates that the caller is querying for the miniport driver descriptor. Vista/Server2008 or +.
StorageAccessAlignmentProperty = 6, // Indicates that the caller is querying for the access alignment descriptor. Vista/Server2008 or +.
StorageDeviceSeekPenaltyProperty = 7, // Indicates that the caller is querying for the seek penalty descriptor. 7/Server2008R2 or +.
StorageDeviceTrimProperty = 8, // Indicates that the caller is querying for the trim descriptor. 7/Server2008R2 or +.
StorageDeviceWriteAggregationProperty = 9, // Indicates that the caller is querying for the write aggregation descriptor. 8/Server2012 or +.
StorageDeviceDeviceTelemetryProperty = 10, // This value is reserved. 8/Server2012 or +.
StorageDeviceLBProvisioningProperty = 11, // Indicates that the caller is querying for the logical block provisioning descriptor, usually to detect whether the storage system uses thin provisioning. 8/Server2012 or +.
StorageDevicePowerProperty = 12, // Indicates that the caller is querying for the power disk drive descriptor. 8/Server2012 or +.
StorageDeviceCopyOffloadProperty = 13, // Indicates that the caller is querying for the write offload descriptor. 8/Server2012 or +.
StorageDeviceResiliencyProperty = 14 // Indicates that the caller is querying for the device resiliency descriptor. 8/Server2012 or +.
};
alias STORAGE_PROPERTY_ID* PSTORAGE_PROPERTY_ID;
enum STORAGE_QUERY_TYPE
{
PropertyStandardQuery = 0, // Instructs the driver to return an appropriate descriptor.
PropertyExistsQuery = 1, // Instructs the driver to report whether the descriptor is supported.
PropertyMaskQuery = 2, // Not currently supported. Do not use.
PropertyQueryMaxDefined = 3 // Specifies the upper limit of the list of query types. This is used to validate the query type.
};
alias STORAGE_QUERY_TYPE* PSTORAGE_QUERY_TYPE;
struct GUID
{
DWORD Data1;
WORD Data2;
WORD Data3;
BYTE Data4[8];
}
struct DISK_GEOMETRY
{
LARGE_INTEGER Cylinders;
MEDIA_TYPE MediaType;
DWORD TracksPerCylinder;
DWORD SectorsPerTrack;
DWORD BytesPerSector;
};
struct DISK_GEOMETRY_EX {
DISK_GEOMETRY Geometry;
LARGE_INTEGER DiskSize;
BYTE Data[1];
};
struct DISK_PARTITION_INFO {
DWORD SizeOfPartitionInfo;
PARTITION_STYLE PartitionStyle;
union {
struct Mbr
{
DWORD Signature;
};
struct Gpt
{
GUID DiskId;
};
};
};
alias DISK_PARTITION_INFO* PDISK_PARTITION_INFO;
alias DISK_GEOMETRY_EX* PDISK_GEOMETRY_EX;
struct STORAGE_ACCESS_ALIGNMENT_DESCRIPTOR
{
DWORD Version;
DWORD Size;
DWORD BytesPerCacheLine;
DWORD BytesOffsetForCacheAlignment;
DWORD BytesPerLogicalSector;
DWORD BytesPerPhysicalSector;
DWORD BytesOffsetForSectorAlignment;
};
alias STORAGE_ACCESS_ALIGNMENT_DESCRIPTOR* PSTORAGE_ACCESS_ALIGNMENT_DESCRIPTOR;
struct STORAGE_PROPERTY_QUERY
{
STORAGE_PROPERTY_ID PropertyId;
STORAGE_QUERY_TYPE QueryType;
ubyte AdditionalParameters[1];
}
alias STORAGE_PROPERTY_QUERY* PSTORAGE_PROPERTY_QUERY;
extern (Windows)
{
export BOOL SetEndOfFile(in HANDLE hFile);
export BOOL GetDiskFreeSpaceA(
in LPCTSTR lpRootPathName,
LPDWORD lpSectorsPerCluster,
LPDWORD lpBytesPerSector,
LPDWORD lpNumberOfFreeClusters,
LPDWORD lpTotalNumberOfClusters);
export BOOL DeviceIoControl(
in HANDLE hDevice,
in DWORD dwIoControlCode,
in LPVOID lpInBuffer,
in DWORD nInBufferSize,
LPVOID lpOutBuffer,
in DWORD nOutBufferSize,
LPDWORD lpBytesReturned,
OVERLAPPED* lpOverlapped);
}
}
version (Posix)
{
import core.sys.posix.fcntl, core.sys.posix.unistd;
import core.sys.posix.sys.statvfs;
immutable newlineA = "\n";
// LS separator (std.uni) is not widely supported.
immutable newlineW = "\n"w;
immutable newlineD = "\n"d;
//
immutable AltnewlineA = "\r\n";
// LS separator (std.uni) is not widely supported.
immutable AltnewlineW = "\r\n"w;
immutable AltnewlineD = "\r\n"d;
//
immutable IsWin = false;
immutable IsPosix = true;
alias int FileHandle;
alias int SystemHandle;
}
version (linux) version (X86_64) version = linux64;
alias void delegate(Object aNotifier) nothrow dNotification;
// generate simple accessors for the dNotification: http://dpaste.dzfl.pl/d9bcb560
/**
* this class allocator/deallocator is globally implemented in each super class
* and can be globally activated using the version specifier "-version=uncollectclasses"
*/
mixin template mUncollectedClass()
{
deprecated new(size_t sz)
{
auto p = malloc(sz);
if (!p) throw new OutOfMemoryError();
return p;
}
deprecated delete(void* p)
{
if (p) free(p);
}
}
mixin template mConditionallyUncollected()
{
version(uncollectclasses) mixin UncollectedClass;
}
/**
* A simple Object which is not collected by the GC.
* It's not affected by the global "uncollectclasses" version specifier.
*/
class cUncollected
{
mixin mUncollectedClass;
unittest
{
auto foo = new cUncollected;
scope(exit) delete foo;
assert( GC.addrOf(&foo) == null );
}
}
/**
* An extended, still unspecialised, Object.
*
* Extensions:
* - interface utilities: IsImplementator(), QueryInterface().
* - serializable: includes an empty, overridable, implementation of ISearializable.
* - properties: Tag, Opaque and ptr
*/
class cObjectEx: iSerializable
{
private
{
int fTag;
void* fOpaque;
}
/**
* Returns the true "&this" pointer.
* Only valid in the class scope (?).
*/
protected void* ptr()
{
return cast(void*) this;
}
public
{
mixin mConditionallyUncollected;
/**
* Returns true if this implements the interface I.
*/
bool IsImplementator(I)()
{
return (cast(I) this) !is null;
}
/**
* Returns the interface I.
*/
I QueryInterface(I)()
{
return cast(I) this;
}
/**
* Any descendant can overrides this method in order to be read/written.
*/
void DeclareProperties(cMasterSerializer aSerializer)
{
aSerializer.AddProperty!int(intprop(&Tag, &Tag, "Tag"));
}
bool IsSerializationRecursive()
{
return true;
}
bool IsReference(iSerializable aSerializable)
{
return false;
}
@property
{
/**
* integer tag. not set to size_t because it's serialized.
*/
int Tag(){return fTag;}
/// ditto
void Tag(int aValue){fTag = aValue;}
/**
* Opaque data.
*/
void* TagPointer(){return fOpaque;}
/// ditto
void TagPointer(void* aValue){fOpaque = aValue;}
}
version(unittest)
{
void TestThisPtr(cObjectEx* aExtPtr)
{
assert(cast(size_t)aExtPtr == cast(size_t)ptr);
}
}
}
unittest
{
interface ITest
{
bool Bar();
}
interface ITest2
{
bool Baz();
}
class Foo: cObjectEx, ITest
{
bool Bar(){return true;}
}
auto foo = new Foo;
assert(foo.IsImplementator!ITest());
assert(!foo.IsImplementator!ITest2());
auto bar = foo.QueryInterface!ITest();
assert(bar !is null);
assert(bar.Bar());
assert(foo.IsImplementator!iSerializable());
foo.TestThisPtr(cast(cObjectEx*)foo);
writeln("cObjectEx passed the tests");
}
}
/**
* Parametrized array.
*
* sTypedArray(T) implements a single-dimension array of uncollected memory.
* It internally pre-allocates the memory to minimize the reallocations fingerprint.
*
* Its layout differs from standard D's dynamic arrays and they cannot be casted as T[].
*/
struct sTypedArray(T)
{
private
{
size_t fLength;
static void* fElems;
static uint fGranularity;
size_t fBlockCount;
void SetLength(size_t aLength)
{
size_t lBlockCount = ((aLength * T.sizeof) / fGranularity) + 1;
if (fBlockCount != lBlockCount)
{
fBlockCount = lBlockCount;
fElems = cast(T*) realloc(cast(void*) fElems, fGranularity * fBlockCount);
if (fElems == null)
{
throw new OutOfMemoryError();
}
}
fLength = aLength;
}
}
protected
{
void Grow()
{
SetLength(fLength + 1);
}
void Shrink()
{
SetLength(fLength - 1);
}
}
public
{
static this()
{
fGranularity = 4096;
fElems = malloc(fGranularity);
if (!fElems)
{
throw new OutOfMemoryError();
}
}
~this()
{
std.c.stdlib.free(fElems);
}
this(T[] someElement)
{
if (someElement.length == 0) return;
SetLength(someElement.length);
for (size_t i; i<fLength; i++)
{
*cast(T*) (fElems + i * T.sizeof) = someElement[i];
}
}
/**
* Indicates the memory allocation block-size.
*/
uint Granurality()
{
return fGranularity;
}
/**
* Sets the memory allocation block-size.
* aValue should be set to 16 or 4096 (the default).
*/
void Granularity(uint aValue)
{
if (fGranularity == aValue) return;
if (aValue < T.sizeof)
{
aValue = 16 * T.sizeof;
}
if (aValue < 16)
{
aValue = 16;
}
while (fGranularity % 16 != 0)
{
aValue--;
}
fGranularity = aValue;
SetLength(fLength);
}
/**
* Indicates how many block contains the array.
*/
size_t BlockCount()
{
return fBlockCount;
}
/**
* Element count.
*/
size_t Length()
{
return fLength;
}
/// ditto
void Length(size_t aLength)
{
if (aLength == fLength) return;
SetLength(aLength);
}
/**
* Pointer to the first element.
* Initially valid, thus cannot be used to
* determine if the array is empty.
*/
void* ptr()
{
return fElems;
}
/**
* Class operators
*/
T opIndex(size_t i)
{
return *cast(T*) (fElems + i * T.sizeof);
}
/// ditto
void opIndexAssign(T aItem, size_t i)
{
*cast(T*) (fElems + i * T.sizeof) = aItem;
}
/// ditto
int opApply(int delegate(ref T) dg)
{
int result = 0;
for (ptrdiff_t i = 0; i < fLength; i++)
{
result = dg(*cast(T*) (fElems + i * T.sizeof));
if (result) break;
}
return result;
}
/// ditto
int opApplyReverse(int delegate(ref T) dg)
{
int result = 0;
for (ptrdiff_t i = fLength-1; i >= 0; i--)
{
result = dg(*cast(T*) (fElems + i * T.sizeof));
if (result) break;
}
return result;
}
/// ditto
void opAssign(T[] someElements)
{
Length = someElements.length;
for (ptrdiff_t i = 0; i < someElements.length; i++)
{
*cast(T*) (fElems + i * T.sizeof) = someElements[i];
}
}
}
}
final private class cTypedArrayTester
{
unittest
{
sTypedArray!int intarr;
intarr.Length = 2;
intarr[0] = 8;
intarr[1] = 9;
assert( intarr[0] == 8);
assert( intarr[1] == 9);
auto floatarr = sTypedArray!float ([0.0f, 0.1f, 0.2f, 0.3f, 0.4f]);
assert( floatarr.Length == 5);
assert( floatarr[0] == 0.0f);
assert( floatarr[1] == 0.1f);
assert( floatarr[2] == 0.2f);
assert( floatarr[3] == 0.3f);
assert( floatarr[4] == 0.4f);
int i;
foreach(float aflt; floatarr)
{
float v = i * 0.1f;
assert( aflt == v);
i++;
}
foreach_reverse(float aflt; floatarr)
{
i--;
float v = i * 0.1f;
assert( aflt == v);
}
intarr.Length = 10_000_000;
intarr[intarr.Length-1] = cast(int) intarr.Length-1;
assert(intarr[intarr.Length-1] == intarr.Length-1);
intarr.Length = 10;
intarr.Length = 10_000_000;
intarr[intarr.Length-1] = cast(int) intarr.Length-1;
assert(intarr[intarr.Length-1] == intarr.Length-1);
writeln("sTypedArray(T) passed the tests");
}
}
enum eListChangeKind {ckAdd,ckInsert,ckRemove,ckExtract,ckExchange};
/**
* cList interface.
*/
abstract class cList(T)
{
alias void delegate(Object aNotifier, eListChangeKind aChangeKind, T* anItem) dListNotification;
/**
* Virtual method responsible for "cleaning" the items when
* the property MustCleanItems is set to true.
*/
protected void Cleanup();
/**
* Returns the index of aItem if found otherwise returns -1.
* Comparison is pointer-based (heap address).
*/
ptrdiff_t IndexOf(T* aItem);
/**
* Returns the index of the first item whose value compares to aItem
* if found otherwise returns -1.
* Comparison is value-based (dereference of heap address as T)
*/
ptrdiff_t FindValue(T aItem);
/**
* Add an item to the back of the list and returns its index.
* If the property AllowDup is set to false and if aItem is already
* held within the list, returns -1.
*/
ptrdiff_t Add(T* aItem);
/**
* Add someItem to the list.
*/
void AddSome(T someItem[]);
/**
* Insert aItem at position aIndex.
*/
void Insert(ptrdiff_t aIndex, T* aItem);
/**
* Remove the item located at aIndex.
*/
void Remove(ptrdiff_t aIndex);
/**
* Remove aItem if it's held within the list.
*/
void Remove(T* aItem);
/**
* Removes and returns the item located at aIndex.
*/
T* Extract(ptrdiff_t aIndex);
/**
* Removes and extracts aItem if it's held within the list.
*/
T* Extract(T* aItem);
/**
* Exchange the items located at aIndex1 and at aIndex2.
*/
void Exchange(ptrdiff_t aIndex1, ptrdiff_t aIndex2);
/**
* Exchange aItem1 and aItem2 position if both are held within the list.
*/
void Exchange(T* aItem1, T* aItem2);
/**
* class operators
*/
T* opIndex(ptrdiff_t i);
/// ditto
void opIndexAssign(T* aItem, ptrdiff_t i);
/// ditto
int opApply(int delegate(T*) dg);
/// ditto
int opApplyReverse(int delegate(T*) dg);
/**
* Returns the count of items held within the list.
*/
ptrdiff_t Count();
/**
* Defines if an item can stand more than once within the list.
*/
void AllowDup(bool aValue);
/// ditto
bool AllowDup();
/**
* Clear the items.
*/
void Clear();
/**
* Defines if the items must be cleaned when the list is destroyed.
* Depending on the context, cleaning denotes freeing memory or destroying some objects.
*/
bool MustCleanItems();
void MustCleanItems(bool aValue);
/**
* Returns the first item (front).
*/
T* First();
/**
* Returns the last item (back).
*/
T* Last();
/**
* Iterate through the Items, from the first to the last and
* call the callback aClbck for each item.
* Similar to a foreach() loop with an additional "context identifier"
* (allowing to use the same callback for different purposes)
* and an user parameter.
*/
void Iterate(size_t aReason, void delegate(size_t aReason, T* aItem, void* aUserDt, out bool doStop) aClbck, void* aUserDt = null);
/**
* Iterate through the items, in reverse order.
*/
void IterateReverse(size_t aReason, void delegate(size_t aReason, T* aItem, void* aUserDt, out bool doStop) aClbck, void* aUserDt = null);
/**
* Sort the items with a typed comparison callback
*/
void Sort(bool delegate(T* Item1, T* Item2) aCompareClbck);
/**
* The OnChange property can be assigned to get informed for each list change.
*/
dListNotification OnChange();
/// ditto
void OnChange(dListNotification aValue);
}
/**
* Parametrized "static" list.
*
* This list is fast for adding (not for inserting) and iterating through the items
* but not for being randomly reorganized. Thus once the items added, they
* shouldn't be removed (except from the very last item) or exchanged
* but they should rather remain "static".
*
* Its items are stored as pointers in an uncollected sTypedArray.
*/
class cStaticList(T): cList!T
{
private
{
sTypedArray!(T*) fItems;
bool fAllowDup;
bool fCleanupItems;
dListNotification fOnChange;
}
protected
{
override void Cleanup(){};
}
public
{
mixin mConditionallyUncollected;
this()
{
fAllowDup = true;
}
~this()
{
if (fCleanupItems) Cleanup;
}
final override ptrdiff_t IndexOf(T* aItem)
{
for (ptrdiff_t i; i < fItems.Length; i++)
{
if (fItems[i] == aItem) return i;
}
return -1;
}
final override ptrdiff_t FindValue(T aItem)
{
for (ptrdiff_t i; i < fItems.Length; i++)
{
if (*fItems[i] == aItem) return i;
}
return -1;
}
final override ptrdiff_t Add(T* aItem)
{
if ((!fAllowDup) && (IndexOf(aItem) != -1)) return -1;
fItems.Grow;
size_t lIndex = fItems.Length-1;
fItems[lIndex] = aItem;
if (fOnChange) fOnChange(this,eListChangeKind.ckAdd,aItem);
return lIndex;
}
final override void AddSome(T someItem[])
{
for(uint i = 0; i < someItem.length; i++ )
{
Add(&someItem[i]);
}
}
final override void Insert(ptrdiff_t aIndex, T* aItem)
{
if ((!fAllowDup) && (IndexOf(aItem) != -1)) return;
fItems.Grow;
for (size_t i = fItems.Length; i > aIndex; i--)
{
fItems[i] = fItems[i-1];
}
fItems[aIndex] = aItem;
if (fOnChange) fOnChange(this,eListChangeKind.ckInsert,aItem);
}
final override void Remove(T* aItem)
{
auto lIndex = IndexOf(aItem);
if (lIndex == -1) return;
for (size_t i = lIndex; i < fItems.Length-1; i++)
{
fItems[i] = fItems[i+1];
}
fItems.Shrink;
if (fOnChange) fOnChange(this,eListChangeKind.ckRemove,aItem);
}
final override void Remove(ptrdiff_t aIndex)
{
auto lItem = fItems[aIndex];
for (size_t i = aIndex; i < fItems.Length-1; i++)
{
fItems[i] = fItems[i+1];
}
fItems.Shrink;
if (fOnChange) fOnChange(this,eListChangeKind.ckRemove,lItem);
}
final override T* Extract(T* aItem)
{
auto lIndex = IndexOf(aItem);
if (lIndex == -1) return null;
Remove(lIndex);
return aItem;
}
final override T* Extract(ptrdiff_t aIndex)
{
auto lRes = fItems[aIndex];
for (size_t i = aIndex; i < fItems.Length-1; i++)
{
fItems[i] = fItems[i+1];
}
fItems.Shrink;
if (fOnChange) fOnChange(this,eListChangeKind.ckExtract,lRes);
return lRes;
}
final override void Exchange(ptrdiff_t aIndex1, ptrdiff_t aIndex2)
{
auto lCopy = fItems[aIndex1];
fItems[aIndex1] = fItems[aIndex2];
fItems[aIndex2] = lCopy;
if (fOnChange) fOnChange(this,eListChangeKind.ckExchange,null);
}
final override void Exchange(T* aItem1, T* aItem2)
{
auto lIndex1 = IndexOf(aItem1);
if (lIndex1 == -1) return;
auto lIndex2 = IndexOf(aItem2);
if (lIndex2 == -1) return;
Exchange(lIndex1,lIndex2);
}
final override void Clear()
{
fItems.Length = 0;
if (fOnChange) fOnChange(this,eListChangeKind.ckRemove,null);
}
final override void Iterate(size_t aReason, void delegate(size_t aReason, T* aItem, void* aUserDt, out bool doStop) aClbck, void* aUserDt = null)
{
bool doStop;
for (ptrdiff_t i = 0; i < fItems.Length; i++)
{
aClbck(aReason, fItems[i], aUserDt, doStop);
if (doStop) break;
}
}
final override void IterateReverse(size_t aReason, void delegate(size_t aReason, T* aItem, void* aUserDt, out bool doStop) aClbck, void* aUserDt = null)
{
bool doStop;
for (ptrdiff_t i = fItems.Length -1; i >= 0; i--)
{
aClbck(aReason, fItems[i], aUserDt, doStop);
if (doStop) break;
}
}
final override T* opIndex(ptrdiff_t i)
{
return fItems[i];
}
final override void opIndexAssign(T* aItem, ptrdiff_t i)
{
if ((!fAllowDup) && (IndexOf(aItem) != -1)) return;
fItems[i] = aItem;
}
final override int opApply(int delegate(T*) dg)
{
int result = 0;
for (ptrdiff_t i = 0; i < fItems.Length; i++)
{
result = dg(fItems[i]);
if (result) break;
}
return result;
}
final override int opApplyReverse(int delegate(T*) dg)
{
int result = 0;
for (ptrdiff_t i = fItems.Length-1; i >= 0; i--)
{
result = dg(fItems[i]);
if (result)
break;
}
return result;
}
final override T* First()
{
if (fItems.Length == 0) return null;
return fItems[0];
}
final override T* Last()
{
if (fItems.Length == 0) return null;
return fItems[fItems.Length-1];
}
final override ptrdiff_t Count()
{
return fItems.Length;
}
final override bool AllowDup()
{
return fAllowDup;
}
final override void AllowDup(bool aValue)
{
if (fAllowDup == aValue) return;
fAllowDup = aValue;
if (!fAllowDup)
{
size_t lAddr1,lAddr2;
for (size_t i = 0; i < fItems.Length; i++)
{
lAddr1 = cast(size_t) &(*fItems[i]);
for (size_t j = fItems.Length-1; j > i; j--)
{
lAddr2 = cast(size_t) &(*fItems[j]);
if (lAddr1 - lAddr2 == 0) Remove(j);
}
}
}
}
final override bool MustCleanItems()
{
return fCleanupItems;
}
final override void MustCleanItems(bool aValue)
{
fCleanupItems = aValue;
}
final override void Sort(bool delegate(T* Item1, T* Item2) aCompareClbck)
{
}
final override dListNotification OnChange() {return fOnChange;}
final override void OnChange(dListNotification aValue){fOnChange = aValue;}
}
}
final private class StaticListTester
{
unittest
{
struct Foo{int a,b,c;}
Foo Foos[1000];
auto FooList = new cStaticList!Foo;
FooList.AddSome(Foos);
assert( FooList.Count == Foos.length);
assert( FooList.IndexOf( &Foos[500] ) == 500);
assert( FooList.IndexOf( &Foos[999] ) == 999);
assert( *FooList[246] == Foos[246]);
FooList.AddSome(Foos);
assert( FooList.Count == Foos.length * 2);
FooList.Remove(1500);
assert( FooList.Count == Foos.length * 2 -1);
assert( *FooList[1500] == Foos[501]);
FooList.AllowDup = false;
assert( FooList.Count == Foos.length);
FooList.Insert(1, &Foos[5]);
assert( FooList.Count == Foos.length);
FooList.AllowDup = true;
FooList.Insert(1, &Foos[5]);
assert( *FooList[0] == Foos[0]);
assert( *FooList[1] == Foos[5]);
assert( *FooList[2] == Foos[1]);
FooList.Clear;
FooList.AddSome(Foos);
FooList.Insert(0,&Foos[999]);
assert( *FooList[0] == Foos[999]);
assert( *FooList.First == Foos[999]);
assert( *FooList[1] == Foos[0]);
FooList.Clear;
assert( FooList.Count == 0);
FooList.AddSome(Foos);
FooList.Exchange(0,999);
assert( *FooList.First == Foos[999]);
assert( *FooList.Last == Foos[0]);
assert( *FooList[0] == Foos[999]);
assert( *FooList[999] == Foos[0]);
FooList.Exchange(&Foos[0],&Foos[999]);
assert( *FooList[0] == Foos[0]);
assert( *FooList[999] == Foos[999]);
int i;
void IteratorClbck1(size_t aReason, Foo* aItem, void* aUserDt, out bool doStop)
{
aItem.a = i;
i++;