-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathjo_clojure_persistent.h
2830 lines (2516 loc) · 86.6 KB
/
jo_clojure_persistent.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
#pragma once
template<typename T>
struct jo_alloc_t;
struct jo_alloc_base_t {
virtual void free(void *n) = 0;
};
template<typename T>
struct jo_alloc_t_type {
struct header_t {
jo_alloc_base_t *owner;
unsigned idx;
unsigned next;
std::atomic<int> ref_count = 0;
unsigned canary;
} h;
char data[sizeof(T)];
jo_alloc_t_type() {}
jo_alloc_t_type(const jo_alloc_t_type &other) {}
jo_alloc_t_type(const jo_alloc_t_type &&other) {}
void add_ref() {
assert(h.canary == 0xDEADBEEF);
#if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_IX86))
++h.ref_count;
#else
h.ref_count.fetch_add(1, std::memory_order_release);
#endif
}
void release() {
h.owner->free(data);
}
};
template<typename T>
struct jo_shared_ptr_t;
// allocate specific types multi threaded very quickly.
template<typename T>
struct jo_alloc_t : jo_alloc_base_t {
typedef jo_alloc_t_type<T> T_t;
enum {
MAX_SECTORS = 256
};
const int NUM_SECTORS = jo_min(std::thread::hardware_concurrency(), MAX_SECTORS);
jo_pinned_vector<T_t> vec;
char pad[64];
// Make sure each atomic is on its own cache-line
struct {
std::atomic<unsigned long long> head;
char pad[64 - sizeof(std::atomic<unsigned long long>)];
} free_list[MAX_SECTORS];
jo_alloc_t() {
for (int i = 0; i < NUM_SECTORS; i++) {
free_list[i].head.store(0, std::memory_order_relaxed);
}
}
jo_alloc_t(const jo_alloc_t &other) {
for (int i = 0; i < NUM_SECTORS; i++) {
free_list[i].head.store(0, std::memory_order_relaxed);
}
}
T *alloc() {
unsigned sector = thread_id & (NUM_SECTORS-1);
for(int i = 0; i < 1; ++i) {
unsigned long long cnt_idx = free_list[sector].head.load();
unsigned long long n_idx = cnt_idx & 0xffffffff;
while (n_idx > 0) {
T_t *n = &vec[n_idx-1];
unsigned long long cnt = (cnt_idx + 0x100000000ull) & (-1ll << 32);
unsigned long long cnt_idx2 = n->h.next | cnt;
if(free_list[sector].head.compare_exchange_weak(cnt_idx, cnt_idx2, std::memory_order_relaxed)) {
assert(n->h.canary == 0);
n->h.canary = 0xDEADBEEF;
n->h.next = 0;
n->h.ref_count.store(0);
n->h.owner = this;
//assert(!memcmp(n->data, "\xcd\xcd\xcd\xcd", 4)); // DEBUG
return (T*)n->data;
}
n_idx = cnt_idx & 0xffffffff;
}
sector = jo_pcg32(&jo_rnd_state) & (NUM_SECTORS-1);
}
size_t idx = vec.push_back(std::move(T_t()));
T_t *n = &vec[idx];
n->h.canary = 0xDEADBEEF;
n->h.idx = idx+1;
n->h.next = 0;
n->h.ref_count.store(0);
n->h.owner = this;
return (T*)n->data;
}
template<typename...A>
T *emplace(A... args) {
return new(alloc()) T(args...);
}
void free(void *n) {
T_t *t = (T_t*)((char*)n - sizeof(t->h));
assert(t->h.canary == 0xDEADBEEF);
if(t->h.ref_count.fetch_sub(1, std::memory_order_acq_rel) == 1) {
assert(t->h.canary == 0xDEADBEEF);
t->h.canary = 0; // make sure it's not double free'd
t->h.owner = 0;
// call dtor if not pod type
if(!std::is_pod<T>::value) {
((T*)t->data)->~T();
}
//memset(t->data, 0xCD, sizeof(T)); // DEBUG
unsigned idx = t->h.idx;
unsigned sector = thread_id & (NUM_SECTORS-1);
unsigned long long old_h = free_list[sector].head.load(), new_h;
do {
t->h.next = old_h & 0xffffffff;
new_h = (old_h & 0xffffffff00000000) | idx;
} while(!free_list[sector].head.compare_exchange_weak(old_h, new_h, std::memory_order_relaxed));
}
}
};
template<typename T>
struct jo_shared_ptr_t {
typedef jo_alloc_t_type<T> T_t;
T* ptr;
jo_shared_ptr_t() : ptr(nullptr) {}
jo_shared_ptr_t(T* Ptr) : ptr(Ptr) {
if(ptr) ((T_t*)((char*)ptr - sizeof(typename T_t::header_t)))->add_ref();
}
jo_shared_ptr_t(const jo_shared_ptr_t& other) : ptr(other.ptr) {
if(ptr) ((T_t*)((char*)ptr - sizeof(typename T_t::header_t)))->add_ref();
}
jo_shared_ptr_t(jo_shared_ptr_t&& other) : ptr(other.ptr) {
other.ptr = nullptr;
}
jo_shared_ptr_t& operator=(const jo_shared_ptr_t& other) {
if (this != &other) {
if(other.ptr) ((T_t*)((char*)other.ptr - sizeof(typename T_t::header_t)))->add_ref();
if(ptr) ((T_t*)((char*)ptr - sizeof(typename T_t::header_t)))->release();
ptr = other.ptr;
}
return *this;
}
jo_shared_ptr_t& operator=(jo_shared_ptr_t&& other) {
if (this != &other) {
if(ptr) ((T_t*)((char*)ptr - sizeof(typename T_t::header_t)))->release();
ptr = other.ptr;
other.ptr = nullptr;
}
return *this;
}
~jo_shared_ptr_t() {
if(ptr) ((T_t*)((char*)ptr - sizeof(typename T_t::header_t)))->release();
ptr = nullptr;
}
T& operator*() { return *ptr; }
T* operator->() { return ptr; }
const T& operator*() const { return *ptr; }
const T* operator->() const { return ptr; }
bool operator==(const jo_shared_ptr_t& other) const { return ptr == other.ptr; }
bool operator!=(const jo_shared_ptr_t& other) const { return ptr != other.ptr; }
bool operator!() const { return ptr == nullptr; }
operator bool() const { return ptr != nullptr; }
//operator T&() { return *ptr; }
//operator T&() const { return *ptr; }
template<typename U> jo_shared_ptr_t<U> &cast() {
static_assert(std::is_base_of<T, U>::value || std::is_base_of<U, T>::value, "type parameter of this class must derive from T or U");
return (jo_shared_ptr_t<U>&)(*this);
}
template<typename U> const jo_shared_ptr_t<U> &cast() const {
static_assert(std::is_base_of<T, U>::value || std::is_base_of<U, T>::value, "type parameter of this class must derive from T or U");
return (const jo_shared_ptr_t<U>&)(*this);
}
};
#ifdef USE_64BIT_NODES
typedef long long node_idx_unsafe_t;
#else
typedef int node_idx_unsafe_t;
#endif
static inline void node_add_ref(node_idx_unsafe_t idx);
static inline void node_release(node_idx_unsafe_t idx);
struct node_idx_t {
node_idx_unsafe_t idx;
node_idx_t() : idx() {}
node_idx_t(node_idx_unsafe_t _idx) : idx(_idx) {
node_add_ref(idx);
}
~node_idx_t() {
node_release(idx);
}
node_idx_t(const node_idx_t& other) : idx(other.idx) {
node_add_ref(idx);
}
node_idx_t(node_idx_t&& other) : idx(other.idx) {
other.idx = 0;
}
node_idx_t& operator=(node_idx_unsafe_t _idx) {
if(idx != _idx) {
node_add_ref(_idx);
node_release(idx);
idx = _idx;
}
return *this;
}
node_idx_t& operator=(const node_idx_t& other) {
if(idx != other.idx) {
node_add_ref(other.idx);
node_release(idx);
idx = other.idx;
}
return *this;
}
node_idx_t& operator=(node_idx_t&& other) {
if(idx != other.idx) {
node_release(idx);
idx = other.idx;
other.idx = 0;
}
return *this;
}
operator node_idx_unsafe_t() const { return idx; }
bool operator==(const node_idx_t& other) const { return idx == other.idx; }
bool operator!=(const node_idx_t& other) const { return idx != other.idx; }
bool operator==(node_idx_unsafe_t _idx) const { return idx == _idx; }
bool operator!=(node_idx_unsafe_t _idx) const { return idx != _idx; }
#ifdef USE_64BIT_NODES
bool operator==(int _idx) const { return idx == _idx; }
bool operator!=(int _idx) const { return idx != _idx; }
#endif
};
struct atomic_node_idx_t {
std::atomic<node_idx_unsafe_t> idx;
atomic_node_idx_t() : idx() {}
~atomic_node_idx_t() {
node_release(idx.load());
}
atomic_node_idx_t(const atomic_node_idx_t& other) {
idx.store(other.idx.load());
node_add_ref(idx.load());
}
atomic_node_idx_t(const node_idx_t& other) {
idx.store(other.idx);
node_add_ref(idx);
}
atomic_node_idx_t(atomic_node_idx_t&& other) {
idx.store(other.idx.load());
other.idx.store(0);
}
atomic_node_idx_t& operator=(const node_idx_t& other) {
if(idx != other.idx) {
node_add_ref(other.idx);
node_release(idx);
idx.store(other.idx);
}
return *this;
}
atomic_node_idx_t& operator=(const atomic_node_idx_t& other) {
if(idx != other.idx) {
node_add_ref(other.idx);
node_release(idx);
idx.store(other.idx.load());
}
return *this;
}
atomic_node_idx_t& operator=(atomic_node_idx_t&& other) {
if(idx != other.idx) {
node_idx_unsafe_t tmp = idx.exchange(other.idx);
node_release(tmp);
other.idx = 0;
}
return *this;
}
operator node_idx_unsafe_t() const { return idx; }
bool operator==(const atomic_node_idx_t& other) const { return idx == other.idx; }
bool operator!=(const atomic_node_idx_t& other) const { return idx != other.idx; }
bool operator==(node_idx_unsafe_t _idx) const { return idx == _idx; }
bool operator!=(node_idx_unsafe_t _idx) const { return idx != _idx; }
#ifdef USE_64BIT_NODES
bool operator==(int _idx) const { return idx == _idx; }
bool operator!=(int _idx) const { return idx != _idx; }
#endif
node_idx_t load() const {
return idx.load(std::memory_order_relaxed);
}
void store(node_idx_t _idx) {
node_add_ref(_idx);
node_release(idx.exchange(_idx));
}
bool compare_exchange_strong(node_idx_unsafe_t expected, node_idx_unsafe_t desired) {
if(idx.compare_exchange_strong(expected, desired)) {
node_add_ref(desired);
node_release(expected);
return true;
}
return false;
}
bool compare_exchange_weak(node_idx_unsafe_t expected, node_idx_unsafe_t desired) {
if(idx.compare_exchange_weak(expected, desired)) {
node_add_ref(desired);
node_release(expected);
return true;
}
return false;
}
};
static node_idx_t new_node_int(long long i, int flags = 0);
static inline long long get_node_int(node_idx_t idx);
static bool node_sym_eq(node_idx_unsafe_t n1i, node_idx_unsafe_t n2i);
static bool node_eq(node_idx_t n1i, node_idx_t n2i);
static bool node_lt(node_idx_t n1i, node_idx_t n2i);
static bool node_lte(node_idx_t n1i, node_idx_t n2i);
size_t jo_hash_value(node_idx_t n);
struct jo_persistent_list;
typedef jo_persistent_list list_t;
typedef jo_alloc_t<list_t> list_alloc_t;
list_alloc_t list_alloc;
typedef jo_shared_ptr_t<list_t> list_ptr_t;
template<typename... A>
static list_ptr_t new_list(A... args) { return list_ptr_t(list_alloc.emplace(args...)); }
struct jo_persistent_list_node_t;
typedef jo_persistent_list_node_t list_node_t;
typedef jo_alloc_t<list_node_t> list_node_alloc_t;
list_node_alloc_t list_node_alloc;
typedef jo_shared_ptr_t<list_node_t> list_node_ptr_t;
template<typename... A>
static list_node_ptr_t new_list_node(A... args) { return list_node_ptr_t(list_node_alloc.emplace(args...)); }
struct jo_persistent_list_node_t {
typedef node_idx_t T;
T value;
list_node_ptr_t next;
jo_persistent_list_node_t() : value(), next() {}
jo_persistent_list_node_t(const T &value, list_node_ptr_t next) : value(value), next(next) {}
jo_persistent_list_node_t(const jo_persistent_list_node_t &other) : value(other.value), next(other.next) {}
jo_persistent_list_node_t &operator=(const list_node_ptr_t &other) {
value = other->value;
next = other->next;
return *this;
}
jo_persistent_list_node_t &operator=(list_node_ptr_t &&other) {
value = other->value;
next = other->next;
return *this;
}
bool operator==(const list_node_ptr_t &other) const { return value == other->value && next == other->next; }
bool operator!=(const list_node_ptr_t &other) const { return !(*this == other); }
};
// A persistent (non-destructive) linked list implementation.
struct jo_persistent_list : jo_object {
typedef node_idx_t T;
list_node_ptr_t head;
list_node_ptr_t tail;
size_t length;
jo_persistent_list() : head(nullptr), tail(nullptr), length(0) {}
jo_persistent_list(const jo_persistent_list &other) : head(other.head), tail(other.tail), length(other.length) {}
jo_persistent_list &operator=(const jo_persistent_list &other) {
head = other.head;
tail = other.tail;
length = other.length;
return *this;
}
~jo_persistent_list() {}
list_ptr_t cons(const T &value) const {
list_ptr_t copy = new_list(*this);
copy->head = new_list_node(value, copy->head);
if(!copy->tail) {
copy->tail = copy->head;
}
copy->length++;
return copy;
}
auto cons_inplace(const T &value) {
head = new_list_node(value, head);
if(!tail) {
tail = head;
}
length++;
return this;
}
// makes a new list in reverse order of the current one
list_ptr_t reverse() const {
list_ptr_t copy = new_list();
list_node_ptr_t cur = head;
while(cur) {
copy->head = new_list_node(cur->value, copy->head);
if(!copy->tail) {
copy->tail = copy->head;
}
cur = cur->next;
}
copy->length = length;
return copy;
}
list_ptr_t clone() const {
list_ptr_t copy = new_list();
list_node_ptr_t cur = head;
list_node_ptr_t cur_copy = nullptr;
list_node_ptr_t prev = nullptr;
copy->head = nullptr;
while(cur) {
cur_copy = new_list_node(cur->value, nullptr);
if(prev) {
prev->next = cur_copy;
} else {
copy->head = cur_copy;
}
prev = cur_copy;
cur = cur->next;
}
copy->tail = prev;
copy->length = length;
return copy;
}
// Clone up to and including the given index and link the rest of the list to the new list.
list_ptr_t clone(int depth) const {
list_ptr_t copy = new_list();
list_node_ptr_t cur = head;
list_node_ptr_t cur_copy = nullptr;
list_node_ptr_t prev = nullptr;
while(cur && depth >= 0) {
cur_copy = new_list_node(cur->value, nullptr);
if(prev) {
prev->next = cur_copy;
} else {
copy->head = cur_copy;
}
prev = cur_copy;
cur = cur->next;
depth--;
}
if(cur) {
prev->next = cur;
copy->tail = tail;
} else {
copy->tail = prev;
}
copy->length = length;
return copy;
}
list_ptr_t conj(const jo_persistent_list &other) const {
list_ptr_t copy = clone();
if(copy->tail) {
copy->tail->next = other.head;
} else {
copy->head = other.head;
}
copy->tail = other.tail;
copy->length += other.length;
return copy;
}
auto conj_inplace(const jo_persistent_list &other) {
if(tail) {
tail->next = other.head;
} else {
head = other.head;
}
tail = other.tail;
length += other.length;
return this;
}
// conj a value
list_ptr_t conj(const T &value) const {
list_ptr_t copy = clone();
if(copy->tail) {
copy->tail->next = new_list_node(value, nullptr);
copy->tail = copy->tail->next;
} else {
copy->head = new_list_node(value, nullptr);
copy->tail = copy->head;
}
copy->length++;
return copy;
}
auto conj_inplace(const T &value) {
if(tail) {
tail->next = new_list_node(value, nullptr);
tail = tail->next;
} else {
head = new_list_node(value, nullptr);
tail = head;
}
length++;
return this;
}
auto disj_inplace(const jo_persistent_list &other) {
list_node_ptr_t cur = head;
list_node_ptr_t prev = nullptr;
while(cur) {
if(other.contains(cur->value)) {
if(prev) {
prev->next = cur->next;
} else {
head = cur->next;
}
length--;
if(!cur->next) {
tail = prev;
}
} else {
prev = cur;
}
cur = cur->next;
}
return this;
}
list_ptr_t disj(const jo_persistent_list &other) const {
list_ptr_t copy = clone();
copy->disj_inplace(other);
return copy;
}
// disj with lambda
template<typename F>
auto disj_inplace(F f) {
list_node_ptr_t cur = head;
list_node_ptr_t prev = nullptr;
while(cur) {
if(f(cur->value)) {
if(prev) {
prev->next = cur->next;
} else {
head = cur->next;
}
length--;
if(!cur->next) {
tail = prev;
}
} else {
prev = cur;
}
cur = cur->next;
}
return this;
}
template<typename F>
list_ptr_t disj(F f) const {
list_ptr_t copy = clone();
copy->disj_inplace(f);
return copy;
}
list_ptr_t assoc(size_t index, const T &value) const {
list_ptr_t copy = new_list();
list_node_ptr_t cur = head;
list_node_ptr_t cur_copy = nullptr;
list_node_ptr_t prev = nullptr;
while(cur && index >= 0) {
cur_copy = new_list_node(cur->value, nullptr);
if(prev) {
prev->next = cur_copy;
} else {
copy->head = cur_copy;
}
prev = cur_copy;
cur = cur->next;
index--;
}
prev->value = value;
if(cur) {
prev->next = cur;
copy->tail = tail;
} else {
copy->tail = prev;
}
copy->length = length;
return copy;
}
list_ptr_t erase_node(list_node_ptr_t at) const {
list_ptr_t copy = new_list();
list_node_ptr_t cur = head;
list_node_ptr_t cur_copy = nullptr;
list_node_ptr_t prev = nullptr;
while(cur && cur != at) {
cur_copy = new_list_node(cur->value, nullptr);
if(prev) {
prev->next = cur_copy;
} else {
copy->head = cur_copy;
}
prev = cur_copy;
cur = cur->next;
}
if(cur) {
if(prev) {
prev->next = cur->next;
} else {
copy->head = cur->next;
}
copy->tail = tail;
} else {
copy->tail = prev;
}
copy->length = length-1;
return copy;
}
// pop off value from front
list_ptr_t pop() const {
list_ptr_t copy = new_list(*this);
if(head) {
copy->head = head->next;
if(!copy->head) {
copy->tail = nullptr;
}
copy->length = length - 1;
}
return copy;
}
const T &nth(int index) const {
list_node_ptr_t cur = head;
while(cur && index > 0) {
cur = cur->next;
index--;
}
return cur->value;
}
const T &operator[](int index) const {
return nth(index);
}
list_ptr_t rest() const {
list_ptr_t copy = new_list();
if(head) {
copy->head = head->next;
copy->tail = tail;
copy->length = length - 1;
}
return copy;
}
list_ptr_t first() const {
list_ptr_t copy = new_list();
if(head) {
copy->head = new_list_node(head->value, nullptr);
copy->tail = copy->head;
copy->length = 1;
}
return copy;
}
T first_value() const {
if(!head) {
return T();
}
return head->value;
}
T second_value() const {
if(!head || !head->next) {
return T();
}
return head->next->value;
}
T third_value() const {
if(!head || !head->next || !head->next->next) {
return T();
}
return head->next->next->value;
}
T last_value() const {
if(!tail) {
return T();
}
return tail->value;
}
list_ptr_t drop(int index) const {
list_ptr_t copy = new_list();
list_node_ptr_t cur = head;
while(cur && --index > 0) {
cur = cur->next;
}
if(cur) {
copy->head = cur->next;
copy->tail = tail;
copy->length = length - index;
}
return copy;
}
// push_back
auto push_back_inplace(const T &value) {
if(!head) {
head = new_list_node(value, nullptr);
tail = head;
} else {
tail->next = new_list_node(value, nullptr);
tail = tail->next;
}
length++;
return this;
}
list_ptr_t push_back(const T &value) const {
list_ptr_t copy = clone();
copy->push_back_inplace(value);
return copy;
}
// push_front inplace
auto push_front_inplace(const T &value) {
if(!head) {
head = new_list_node(value, nullptr);
tail = head;
} else {
list_node_ptr_t new_head = new_list_node(value, head);
head = new_head;
}
length++;
return this;
}
list_ptr_t push_front(const T &value) const {
list_ptr_t copy = new_list(*this);
copy->push_front_inplace(value);
return copy;
}
auto push_front_inplace(const T &value1, const T &value2) {
push_front_inplace(value2);
push_front_inplace(value1);
return this;
}
list_ptr_t push_front(const T &value1, const T &value2) const {
list_ptr_t copy = new_list(*this);
copy->push_front_inplace(value1, value2);
return copy;
}
auto push_front_inplace(const T &value1, const T &value2, const T &value3) {
push_front_inplace(value3);
push_front_inplace(value2);
push_front_inplace(value1);
return this;
}
list_ptr_t push_front(const T &value1, const T &value2, const T &value3) const {
list_ptr_t copy = new_list(*this);
copy->push_front_inplace(value1, value2, value3);
return copy;
}
auto push_front_inplace(const T &value1, const T &value2, const T &value3, const T &value4) {
push_front_inplace(value4);
push_front_inplace(value3);
push_front_inplace(value2);
push_front_inplace(value1);
return this;
}
list_ptr_t push_front(const T &value1, const T &value2, const T &value3, const T &value4) const {
list_ptr_t copy = new_list(*this);
copy->push_front_inplace(value1, value2, value3, value4);
return copy;
}
auto push_front_inplace(const T &value1, const T &value2, const T &value3, const T &value4, const T &value5) {
push_front_inplace(value5);
push_front_inplace(value4);
push_front_inplace(value3);
push_front_inplace(value2);
push_front_inplace(value1);
return this;
}
list_ptr_t push_front(const T &value1, const T &value2, const T &value3, const T &value4, const T &value5) const {
list_ptr_t copy = new_list(*this);
copy->push_front_inplace(value1, value2, value3, value4, value5);
return copy;
}
auto push_front_inplace(const T &value1, const T &value2, const T &value3, const T &value4, const T &value5, const T &value6) {
push_front_inplace(value6);
push_front_inplace(value5);
push_front_inplace(value4);
push_front_inplace(value3);
push_front_inplace(value2);
push_front_inplace(value1);
return this;
}
list_ptr_t push_front(const T &value1, const T &value2, const T &value3, const T &value4, const T &value5, const T &value6) const {
list_ptr_t copy = new_list(*this);
copy->push_front_inplace(value1, value2, value3, value4, value5, value6);
return copy;
}
list_ptr_t push_front(list_ptr_t other) const {
list_ptr_t copy = new_list(*this);
list_node_ptr_t cur = other->head;
while(cur) {
copy->push_front_inplace(cur->value);
cur = cur->next;
}
return copy;
}
auto pop_front_inplace() {
if(head) {
head = head->next;
if(!head) {
tail = nullptr;
}
length--;
}
return this;
}
auto pop_front_inplace(size_t num) {
while(num-- > 0 && head) {
head = head->next;
if(!head) {
tail = nullptr;
}
length--;
}
return this;
}
list_ptr_t pop_front() const {
list_ptr_t copy = new_list(*this);
copy->pop_front_inplace();
return copy;
}
list_ptr_t pop_front(size_t num) const {
list_ptr_t copy = new_list(*this);
copy->pop_front_inplace(num);
return copy;
}
list_ptr_t subvec(int start, int end) const {
list_node_ptr_t cur = head;
while(cur && start > 0) {
cur = cur->next;
start--;
}
list_ptr_t copy = new_list();
while(cur && end > start) {
copy->push_back_inplace(cur->value);
cur = cur->next;
end--;
}
return copy;
}
// pop_back
auto pop_back_inplace() {
if(head) {
if(head == tail) {
head = tail = nullptr;
} else {
list_node_ptr_t cur = head;
while(cur->next != tail) {
cur = cur->next;
}
tail = cur;
tail->next = nullptr;
}
length--;
}
return this;
}
list_ptr_t pop_back() const {
list_ptr_t copy = new_list(*this);
copy->pop_back_inplace();
return copy;
}
bool contains(const T &value) const {
list_node_ptr_t cur = head;
while(cur) {
if(cur->value == value) {
return true;
}
cur = cur->next;
}
return false;
}
// contains with lambda for comparison
template<typename F>
bool contains(const F &f) const {
list_node_ptr_t cur = head;
while(cur) {
if(f(cur->value)) {
return true;
}
cur = cur->next;
}
return false;
}
list_ptr_t erase(const T &value) {
list_node_ptr_t cur = head;
list_node_ptr_t prev = nullptr;
while(cur) {
if(cur->value == value) {
return erase_node(cur);
}
prev = cur;
cur = cur->next;
}
return this;
}
list_ptr_t take(int N) const {
list_ptr_t copy = new_list();
list_node_ptr_t cur = head;
while(cur && N > 0) {
copy->push_back_inplace(cur->value);
cur = cur->next;
N--;
}
return copy;
}
list_ptr_t take_last(int N) const {
return drop(length - N);
}
// return a random permutation of the elements of the list
list_ptr_t shuffle() const {
// convert to jo_vector
jo_vector<T> v;
list_node_ptr_t cur = head;
while(cur) {
v.push_back(cur->value);
cur = cur->next;
}
// shuffle
jo_random_shuffle(v.begin(), v.end());
// convert back to jo_persistent_list
list_ptr_t copy = new_list();
for(int i = 0; i < v.size(); i++) {
copy->push_back_inplace(v[i]);
}
return copy;
}
// return items with a random probability of p
list_ptr_t random_sample(float p) const {
if(p <= 0.0f) {
return new_list();
}