summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org/gruf/go-structr/timeline.go
blob: 0014e69eeb7424a8dcb8061dda611a8830058b1d (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
package structr

import (
	"cmp"
	"os"
	"reflect"
	"slices"
	"sync"
	"unsafe"
)

// Direction defines a direction
// to iterate entries in a Timeline.
type Direction bool

const (
	// Asc = ascending, i.e. bottom-up.
	Asc = Direction(true)

	// Desc = descending, i.e. top-down.
	Desc = Direction(false)
)

// TimelineConfig defines config vars for initializing a Timeline{}.
type TimelineConfig[StructType any, PK cmp.Ordered] struct {

	// Copy provides a means of copying
	// timelined values, to ensure returned values
	// do not share memory with those in timeline.
	Copy func(StructType) StructType

	// Invalidate is called when timelined
	// values are invalidated, either as passed
	// to Insert(), or by calls to Invalidate().
	Invalidate func(StructType)

	// PKey defines the generic parameter StructType's
	// field to use as the primary key for this cache.
	// It must be ordered so that the timeline can
	// maintain correct sorting of inserted values.
	//
	// Field selection logic follows the same path as
	// with IndexConfig{}.Fields. Noting that in this
	// case only a single field is permitted, though
	// it may be nested, and as described above the
	// type must conform to cmp.Ordered.
	PKey IndexConfig

	// Indices defines indices to create
	// in the Timeline for the receiving
	// generic struct type parameter.
	Indices []IndexConfig
}

// Timeline provides an ordered-list like cache of structures,
// with automated indexing and invalidation by any initialization
// defined combination of fields. The list order is maintained
// according to the configured struct primary key.
type Timeline[StructType any, PK cmp.Ordered] struct {

	// hook functions.
	invalid func(StructType)
	copy    func(StructType) StructType

	// main underlying
	// timeline list.
	//
	// where:
	// - head = top = largest
	// - tail = btm = smallest
	list list

	// contains struct field information of
	// the field used as the primary key for
	// this timeline. it can also be found
	// under indices[0]
	pkey pkey_field

	// indices used in storing passed struct
	// types by user defined sets of fields.
	indices []Index

	// protective mutex, guards:
	// - Timeline{}.*
	// - Index{}.data
	mutex sync.Mutex
}

// Init initializes the timeline with given configuration
// including struct fields to index, and necessary fns.
func (t *Timeline[T, PK]) Init(config TimelineConfig[T, PK]) {
	rt := reflect.TypeOf((*T)(nil)).Elem()

	if len(config.Indices) == 0 {
		panic("no indices provided")
	}

	if config.Copy == nil {
		panic("copy function must be provided")
	}

	// Safely copy over
	// provided config.
	t.mutex.Lock()
	defer t.mutex.Unlock()

	// The first index is created from PKey,
	// other indices are created as expected.
	t.indices = make([]Index, len(config.Indices)+1)
	t.indices[0].ptr = unsafe.Pointer(t)
	t.indices[0].init(rt, config.PKey, 0)
	if len(t.indices[0].fields) > 1 {
		panic("primary key must contain only 1 field")
	}
	for i, cfg := range config.Indices {
		t.indices[i+1].ptr = unsafe.Pointer(t)
		t.indices[i+1].init(rt, cfg, 0)
	}

	// Extract pkey details from index.
	field := t.indices[0].fields[0]
	t.pkey = pkey_field{
		rtype:   field.rtype,
		offsets: field.offsets,
		likeptr: field.likeptr,
	}

	// Copy over remaining.
	t.copy = config.Copy
	t.invalid = config.Invalidate
}

// Index selects index with given name from timeline, else panics.
func (t *Timeline[T, PK]) Index(name string) *Index {
	for i, idx := range t.indices {
		if idx.name == name {
			return &(t.indices[i])
		}
	}
	panic("unknown index: " + name)
}

// Select allows you to retrieve a slice of values, in order, from the timeline.
// This slice is defined by the minimum and maximum primary key parameters, up to
// a given length in size. The direction in which you select will determine which
// of the min / max primary key values is used as the *cursor* to begin the start
// of the selection, and which is used as the *boundary* to mark the end, if set.
// In either case, the length parameter is always optional.
//
// dir = Asc  : cursors up from 'max' (required), with boundary 'min' (optional).
// dir = Desc : cursors down from 'min' (required), with boundary 'max' (optional).
func (t *Timeline[T, PK]) Select(min, max *PK, length *int, dir Direction) (values []T) {

	// Acquire lock.
	t.mutex.Lock()

	// Check init'd.
	if t.copy == nil {
		t.mutex.Unlock()
		panic("not initialized")
	}

	switch dir {
	case Asc:
		// Verify args.
		if min == nil {
			t.mutex.Unlock()
			panic("min must be provided when selecting asc")
		}

		// Select determined values ASCENDING.
		values = t.select_asc(*min, max, length)

	case Desc:
		// Verify args.
		if max == nil {
			t.mutex.Unlock()
			panic("max must be provided when selecting asc")
		}

		// Select determined values DESCENDING.
		values = t.select_desc(min, *max, length)
	}

	// Done with lock.
	t.mutex.Unlock()

	return values
}

// Insert will insert the given values into the timeline,
// calling any set invalidate hook on each inserted value.
// Returns current list length after performing inserts.
func (t *Timeline[T, PK]) Insert(values ...T) int {

	// Acquire lock.
	t.mutex.Lock()

	// Check init'd.
	if t.copy == nil {
		t.mutex.Unlock()
		panic("not initialized")
	}

	// Allocate a slice of our value wrapping struct type.
	with_keys := make([]value_with_pk[T, PK], len(values))
	if len(with_keys) != len(values) {
		panic(assert("BCE"))
	}

	// Range the provided values.
	for i, value := range values {

		// Create our own copy
		// of value to work with.
		value = t.copy(value)

		// Take ptr to the value copy.
		vptr := unsafe.Pointer(&value)

		// Extract primary key from vptr.
		kptr := extract_pkey(vptr, t.pkey)

		var pkey PK
		if kptr != nil {
			// Cast as PK type.
			pkey = *(*PK)(kptr)
		} else {
			// Use zero value pointer.
			kptr = unsafe.Pointer(&pkey)
		}

		// Append wrapped value to slice with
		// the acquire pointers and primary key.
		with_keys[i] = value_with_pk[T, PK]{
			k: pkey,
			v: value,

			kptr: kptr,
			vptr: vptr,
		}
	}

	var last *list_elem

	// BEFORE inserting the prepared slice of value copies w/ primary
	// keys, sort them by their primary key, ascending. This permits
	// us to re-use the 'last' timeline position as next insert cursor.
	// Otherwise we would have to iterate from 'head' every single time.
	slices.SortFunc(with_keys, func(a, b value_with_pk[T, PK]) int {
		const k = +1
		switch {
		case a.k < b.k:
			return +k
		case b.k < a.k:
			return -k
		default:
			return 0
		}
	})

	// Store each value in the timeline,
	// updating the last used list element
	// each time so we don't have to iter
	// down from head on every single store.
	for _, value := range with_keys {
		last = t.store_one(last, value)
	}

	// Get func ptrs.
	invalid := t.invalid

	// Get length AFTER
	// insert to return.
	len := t.list.len

	// Done with lock.
	t.mutex.Unlock()

	if invalid != nil {
		// Pass all invalidated values
		// to given user hook (if set).
		for _, value := range values {
			invalid(value)
		}
	}

	return len
}

// Invalidate invalidates all entries stored in index under given keys.
// Note that if set, this will call the invalidate hook on each value.
func (t *Timeline[T, PK]) Invalidate(index *Index, keys ...Key) {
	if index == nil {
		panic("no index given")
	} else if index.ptr != unsafe.Pointer(t) {
		panic("invalid index for timeline")
	}

	// Acquire lock.
	t.mutex.Lock()

	// Preallocate expected ret slice.
	values := make([]T, 0, len(keys))

	for i := range keys {
		// Delete all items under key from index, collecting
		// value items and dropping them from all their indices.
		index.delete(keys[i].key, func(item *indexed_item) {

			// Cast to *actual* timeline item.
			t_item := to_timeline_item(item)

			if value, ok := item.data.(T); ok {
				// No need to copy, as item
				// being deleted from cache.
				values = append(values, value)
			}

			// Delete item.
			t.delete(t_item)
		})
	}

	// Get func ptrs.
	invalid := t.invalid

	// Done with lock.
	t.mutex.Unlock()

	if invalid != nil {
		// Pass all invalidated values
		// to given user hook (if set).
		for _, value := range values {
			invalid(value)
		}
	}
}

// Range will range over all values in the timeline in given direction.
// dir = Asc  : ranges from the bottom-up.
// dir = Desc : ranges from the top-down.
//
// Please note that the entire Timeline{} will be locked for the duration of the range
// operation, i.e. from the beginning of the first yield call until the end of the last.
func (t *Timeline[T, PK]) Range(dir Direction) func(yield func(index int, value T) bool) {
	return func(yield func(int, T) bool) {
		if t.copy == nil {
			panic("not initialized")
		} else if yield == nil {
			panic("nil func")
		}

		// Acquire lock.
		t.mutex.Lock()
		defer t.mutex.Unlock()

		var i int
		switch dir {

		case Asc:
			// Iterate through linked list from bottom (i.e. tail).
			for prev := t.list.tail; prev != nil; prev = prev.prev {

				// Extract item from list element.
				item := (*timeline_item)(prev.data)

				// Create copy of item value.
				value := t.copy(item.data.(T))

				// Pass to given function.
				if !yield(i, value) {
					break
				}

				// Iter
				i++
			}

		case Desc:
			// Iterate through linked list from top (i.e. head).
			for next := t.list.head; next != nil; next = next.next {

				// Extract item from list element.
				item := (*timeline_item)(next.data)

				// Create copy of item value.
				value := t.copy(item.data.(T))

				// Pass to given function.
				if !yield(i, value) {
					break
				}

				// Iter
				i++
			}
		}
	}
}

// RangeUnsafe is functionally similar to Range(), except it does not pass *copies* of
// data. It allows you to operate on the data directly and modify it. As such it can also
// be more performant to use this function, even for read-write operations.
//
// Please note that the entire Timeline{} will be locked for the duration of the range
// operation, i.e. from the beginning of the first yield call until the end of the last.
func (t *Timeline[T, PK]) RangeUnsafe(dir Direction) func(yield func(index int, value T) bool) {
	return func(yield func(int, T) bool) {
		if t.copy == nil {
			panic("not initialized")
		} else if yield == nil {
			panic("nil func")
		}

		// Acquire lock.
		t.mutex.Lock()
		defer t.mutex.Unlock()

		var i int
		switch dir {

		case Asc:
			// Iterate through linked list from bottom (i.e. tail).
			for prev := t.list.tail; prev != nil; prev = prev.prev {

				// Extract item from list element.
				item := (*timeline_item)(prev.data)

				// Pass to given function.
				if !yield(i, item.data.(T)) {
					break
				}

				// Iter
				i++
			}

		case Desc:
			// Iterate through linked list from top (i.e. head).
			for next := t.list.head; next != nil; next = next.next {

				// Extract item from list element.
				item := (*timeline_item)(next.data)

				// Pass to given function.
				if !yield(i, item.data.(T)) {
					break
				}

				// Iter
				i++
			}
		}
	}
}

// RangeKeys will iterate over all values for given keys in the given index.
//
// Please note that the entire Timeline{} will be locked for the duration of the range
// operation, i.e. from the beginning of the first yield call until the end of the last.
func (t *Timeline[T, PK]) RangeKeys(index *Index, keys ...Key) func(yield func(T) bool) {
	return func(yield func(T) bool) {
		if t.copy == nil {
			panic("not initialized")
		} else if index == nil {
			panic("no index given")
		} else if index.ptr != unsafe.Pointer(t) {
			panic("invalid index for timeline")
		} else if yield == nil {
			panic("nil func")
		}

		// Acquire lock.
		t.mutex.Lock()
		defer t.mutex.Unlock()

		for _, key := range keys {
			var done bool

			// Iterate over values in index under key.
			index.get(key.key, func(i *indexed_item) {

				// Cast to timeline_item type.
				item := to_timeline_item(i)

				// Create copy of item value.
				value := t.copy(item.data.(T))

				// Pass val to yield function.
				done = done || !yield(value)
			})

			if done {
				break
			}
		}
	}
}

// RangeKeysUnsafe is functionally similar to RangeKeys(), except it does not pass *copies*
// of data. It allows you to operate on the data directly and modify it. As such it can also
// be more performant to use this function, even for read-write operations.
//
// Please note that the entire Timeline{} will be locked for the duration of the range
// operation, i.e. from the beginning of the first yield call until the end of the last.
func (t *Timeline[T, PK]) RangeKeysUnsafe(index *Index, keys ...Key) func(yield func(T) bool) {
	return func(yield func(T) bool) {
		if t.copy == nil {
			panic("not initialized")
		} else if index == nil {
			panic("no index given")
		} else if index.ptr != unsafe.Pointer(t) {
			panic("invalid index for timeline")
		} else if yield == nil {
			panic("nil func")
		}

		// Acquire lock.
		t.mutex.Lock()
		defer t.mutex.Unlock()

		for _, key := range keys {
			var done bool

			// Iterate over values in index under key.
			index.get(key.key, func(i *indexed_item) {

				// Cast to timeline_item type.
				item := to_timeline_item(i)

				// Pass value data to yield function.
				done = done || !yield(item.data.(T))
			})

			if done {
				break
			}
		}
	}
}

// Trim will remove entries from the timeline in given
// direction, ensuring timeline is no larger than 'max'.
// If 'max' >= t.Len(), this function is a no-op.
// dir = Asc  : trims from the bottom-up.
// dir = Desc : trims from the top-down.
func (t *Timeline[T, PK]) Trim(max int, dir Direction) {
	// Acquire lock.
	t.mutex.Lock()

	// Calculate number to drop.
	diff := t.list.len - int(max)
	if diff <= 0 {

		// Trim not needed.
		t.mutex.Unlock()
		return
	}

	switch dir {
	case Asc:
		// Iterate over 'diff' items
		// from bottom of timeline list.
		for range diff {

			// Get bottom list elem.
			bottom := t.list.tail
			if bottom == nil {

				// reached
				// end.
				break
			}

			// Drop bottom-most item from timeline.
			item := (*timeline_item)(bottom.data)
			t.delete(item)
		}

	case Desc:
		// Iterate over 'diff' items
		// from top of timeline list.
		for range diff {

			// Get top list elem.
			top := t.list.head
			if top == nil {

				// reached
				// end.
				break
			}

			// Drop top-most item from timeline.
			item := (*timeline_item)(top.data)
			t.delete(item)
		}
	}

	// Compact index data stores.
	for _, idx := range t.indices {
		(&idx).data.Compact()
	}

	// Done with lock.
	t.mutex.Unlock()
}

// Clear empties the timeline by calling .TrimBottom(0, Down).
func (t *Timeline[T, PK]) Clear() { t.Trim(0, Desc) }

// Len returns the current length of cache.
func (t *Timeline[T, PK]) Len() int {
	t.mutex.Lock()
	l := t.list.len
	t.mutex.Unlock()
	return l
}

// Debug returns debug stats about cache.
func (t *Timeline[T, PK]) Debug() map[string]any {
	m := make(map[string]any, 2)
	t.mutex.Lock()
	m["list"] = t.list.len
	indices := make(map[string]any, len(t.indices))
	m["indices"] = indices
	for _, idx := range t.indices {
		var n uint64
		for _, l := range idx.data.m {
			n += uint64(l.len)
		}
		indices[idx.name] = n
	}
	t.mutex.Unlock()
	return m
}

func (t *Timeline[T, PK]) select_asc(min PK, max *PK, length *int) (values []T) {
	// Iterate through linked list
	// from bottom (i.e. tail), asc.
	prev := t.list.tail

	// Iterate from 'prev' up, skipping all
	// entries with pkey below cursor 'min'.
	for ; prev != nil; prev = prev.prev {
		item := (*timeline_item)(prev.data)
		pkey := *(*PK)(item.pk)

		// Check below min.
		if pkey <= min {
			continue
		}

		// Reached
		// cursor.
		break
	}

	if prev == nil {
		// No values
		// remaining.
		return
	}

	// Optimized switch case to handle
	// each set of argument combinations
	// separately, in order to minimize
	// number of checks during loops.
	switch {

	case length != nil && max != nil:
		// Deref arguments.
		length := *length
		max := *max

		// Optimistic preallocate slice.
		values = make([]T, 0, length)

		// Both a length and maximum were given,
		// select from cursor until either reached.
		for ; prev != nil; prev = prev.prev {
			item := (*timeline_item)(prev.data)
			pkey := *(*PK)(item.pk)

			// Check above max.
			if pkey >= max {
				break
			}

			// Append value copy.
			value := item.data.(T)
			value = t.copy(value)
			values = append(values, value)

			// Check if length reached.
			if len(values) >= length {
				break
			}
		}

	case length != nil:
		// Deref length.
		length := *length

		// Optimistic preallocate slice.
		values = make([]T, 0, length)

		// Only a length was given, select
		// from cursor until length reached.
		for ; prev != nil; prev = prev.prev {
			item := (*timeline_item)(prev.data)

			// Append value copy.
			value := item.data.(T)
			value = t.copy(value)
			values = append(values, value)

			// Check if length reached.
			if len(values) >= length {
				break
			}
		}

	case max != nil:
		// Deref min.
		max := *max

		// Only a maximum was given, select
		// from cursor until max is reached.
		for ; prev != nil; prev = prev.prev {
			item := (*timeline_item)(prev.data)
			pkey := *(*PK)(item.pk)

			// Check above max.
			if pkey >= max {
				break
			}

			// Append value copy.
			value := item.data.(T)
			value = t.copy(value)
			values = append(values, value)
		}

	default:
		// No maximum or length were given,
		// ALL from cursor need selecting.
		for ; prev != nil; prev = prev.prev {
			item := (*timeline_item)(prev.data)

			// Append value copy.
			value := item.data.(T)
			value = t.copy(value)
			values = append(values, value)
		}
	}

	return
}

func (t *Timeline[T, PK]) select_desc(min *PK, max PK, length *int) (values []T) {
	// Iterate through linked list
	// from top (i.e. head), desc.
	next := t.list.head

	// Iterate from 'next' down, skipping
	// all entries with pkey above cursor 'max'.
	for ; next != nil; next = next.next {
		item := (*timeline_item)(next.data)
		pkey := *(*PK)(item.pk)

		// Check above max.
		if pkey >= max {
			continue
		}

		// Reached
		// cursor.
		break
	}

	if next == nil {
		// No values
		// remaining.
		return
	}

	// Optimized switch case to handle
	// each set of argument combinations
	// separately, in order to minimize
	// number of checks during loops.
	switch {

	case length != nil && min != nil:
		// Deref arguments.
		length := *length
		min := *min

		// Optimistic preallocate slice.
		values = make([]T, 0, length)

		// Both a length and minimum were given,
		// select from cursor until either reached.
		for ; next != nil; next = next.next {
			item := (*timeline_item)(next.data)
			pkey := *(*PK)(item.pk)

			// Check below min.
			if pkey <= min {
				break
			}

			// Append value copy.
			value := item.data.(T)
			value = t.copy(value)
			values = append(values, value)

			// Check if length reached.
			if len(values) >= length {
				break
			}
		}

	case length != nil:
		// Deref length.
		length := *length

		// Optimistic preallocate slice.
		values = make([]T, 0, length)

		// Only a length was given, select
		// from cursor until length reached.
		for ; next != nil; next = next.next {
			item := (*timeline_item)(next.data)

			// Append value copy.
			value := item.data.(T)
			value = t.copy(value)
			values = append(values, value)

			// Check if length reached.
			if len(values) >= length {
				break
			}
		}

	case min != nil:
		// Deref min.
		min := *min

		// Only a minimum was given, select
		// from cursor until minimum reached.
		for ; next != nil; next = next.next {
			item := (*timeline_item)(next.data)
			pkey := *(*PK)(item.pk)

			// Check below min.
			if pkey <= min {
				break
			}

			// Append value copy.
			value := item.data.(T)
			value = t.copy(value)
			values = append(values, value)
		}

	default:
		// No minimum or length were given,
		// ALL from cursor need selecting.
		for ; next != nil; next = next.next {
			item := (*timeline_item)(next.data)

			// Append value copy.
			value := item.data.(T)
			value = t.copy(value)
			values = append(values, value)
		}
	}

	return
}

// value_with_pk wraps an incoming value type, with
// its extracted primary key, and pointers to both.
// this encompasses all arguments related to a value
// required by store_one(), simplifying some logic.
//
// with all the primary keys extracted, it also
// makes it much easier to sort input before insert.
type value_with_pk[T any, PK comparable] struct {
	k PK // primary key value
	v T  // value copy

	kptr unsafe.Pointer // primary key ptr
	vptr unsafe.Pointer // value copy ptr
}

func (t *Timeline[T, PK]) store_one(last *list_elem, value value_with_pk[T, PK]) *list_elem {
	// NOTE: the value passed here should
	// already be a copy of the original.

	// Alloc new index item.
	t_item := new_timeline_item()
	if cap(t_item.indexed) < len(t.indices) {

		// Preallocate item indices slice to prevent Go auto
		// allocating overlying large slices we don't need.
		t_item.indexed = make([]*index_entry, 0, len(t.indices))
	}

	// Set item value data.
	t_item.data = value.v
	t_item.pk = value.kptr

	// Get zero'th index, i.e.
	// the primary key index.
	idx0 := (&t.indices[0])

	// Acquire key buf.
	buf := new_buffer()

	// Calculate index key from already extracted
	// primary key, checking for zero return value.
	partptrs := []unsafe.Pointer{value.kptr}
	key := idx0.key(buf, partptrs)
	if key == "" { // i.e. (!allow_zero && pkey == zero)
		free_timeline_item(t_item)
		free_buffer(buf)
		return last
	}

	// Convert to indexed_item pointer.
	i_item := from_timeline_item(t_item)

	if last == nil {
		// No previous element was provided, this is
		// first insert, we need to work from head.

		// Check for emtpy head.
		if t.list.head == nil {

			// The easiest case, this will
			// be the first item in list.
			t.list.push_front(&t_item.elem)
			last = t.list.head // return value
			goto indexing
		}

		// Extract head item and its primary key.
		headItem := (*timeline_item)(t.list.head.data)
		headPK := *(*PK)(headItem.pk)
		if value.k > headPK {

			// Another easier case, this also
			// will be the first item in list.
			t.list.push_front(&t_item.elem)
			last = t.list.head // return value
			goto indexing
		}

		// Check (and drop) if pkey is a collision!
		if value.k == headPK && is_unique(idx0.flags) {
			free_timeline_item(t_item)
			free_buffer(buf)
			return t.list.head
		}

		// Set last = head.next
		// as next to work from.
		last = t.list.head.next
	}

	// Iterate through list from head
	// to find location. Optimized into two
	// cases to minimize loop CPU cycles.
	if is_unique(idx0.flags) {
		for next := last; //
		next != nil; next = next.next {

			// Extract item and it's primary key.
			nextItem := (*timeline_item)(next.data)
			nextPK := *(*PK)(nextItem.pk)

			// If pkey smaller than
			// cursor's, keep going.
			if value.k < nextPK {
				continue
			}

			// Check (and drop) if
			// pkey is a collision!
			if value.k == nextPK {
				free_timeline_item(t_item)
				free_buffer(buf)
				return next
			}

			// New pkey is larger than cursor,
			// insert into list just before it.
			t.list.insert(&t_item.elem, next.prev)
			last = next // return value
			goto indexing
		}
	} else {
		for next := last; //
		next != nil; next = next.next {

			// Extract item and it's primary key.
			nextItem := (*timeline_item)(next.data)
			nextPK := *(*PK)(nextItem.pk)

			// If pkey smaller than
			// cursor's, keep going.
			if value.k < nextPK {
				continue
			}

			// New pkey is larger than cursor,
			// insert into list just before it.
			t.list.insert(&t_item.elem, next.prev)
			last = next // return value
			goto indexing
		}
	}

	// We reached the end of the
	// list, insert at tail pos.
	t.list.push_back(&t_item.elem)
	last = t.list.tail // return value
	goto indexing

indexing:
	// Append already-extracted
	// primary key to 0th index.
	_ = idx0.add(key, i_item)

	// Insert item into each of indices.
	for i := 1; i < len(t.indices); i++ {

		// Get current index ptr.
		idx := (&t.indices[i])

		// Extract fields comprising index key from value.
		parts := extract_fields(value.vptr, idx.fields)

		// Calculate this index key,
		// checking for zero values.
		key := idx.key(buf, parts)
		if key == "" {
			continue
		}

		// Add this item to index,
		// checking for collisions.
		if !idx.add(key, i_item) {

			// This key already appears
			// in this unique index. So
			// drop new timeline item.
			t.delete(t_item)
			free_buffer(buf)
			return last
		}
	}

	// Done with bufs.
	free_buffer(buf)
	return last
}

func (t *Timeline[T, PK]) delete(i *timeline_item) {
	for len(i.indexed) != 0 {
		// Pop last indexed entry from list.
		entry := i.indexed[len(i.indexed)-1]
		i.indexed[len(i.indexed)-1] = nil
		i.indexed = i.indexed[:len(i.indexed)-1]

		// Get entry's index.
		index := entry.index

		// Drop this index_entry.
		index.delete_entry(entry)
	}

	// Drop from main list.
	t.list.remove(&i.elem)

	// Free unused item.
	free_timeline_item(i)
}

type timeline_item struct {
	indexed_item

	// retains fast ptr access
	// to primary key value of
	// above indexed_item{}.data
	pk unsafe.Pointer

	// check bits always all set
	// to 1. used to ensure cast
	// from indexed_item to this
	// type was originally a
	// timeline_item to begin with.
	ck uint
}

func init() {
	// ensure the embedded indexed_item struct is ALWAYS at zero offset.
	// we rely on this to allow a ptr to one to be a ptr to either of them.
	const off = unsafe.Offsetof(timeline_item{}.indexed_item)
	if off != 0 {
		panic(assert("offset_of(timeline_item{}.indexed_item) = 0"))
	}
}

// from_timeline_item converts a timeline_item ptr to indexed_item, given the above init() guarantee.
func from_timeline_item(item *timeline_item) *indexed_item {
	return (*indexed_item)(unsafe.Pointer(item))
}

// to_timeline_item converts an indexed_item ptr to timeline_item, given the above init() guarantee.
// NOTE THIS MUST BE AN indexed_item THAT WAS INITIALLY CONVERTED WITH from_timeline_item().
func to_timeline_item(item *indexed_item) *timeline_item {
	to := (*timeline_item)(unsafe.Pointer(item))
	if to.ck != ^uint(0) {
		// ensure check bits are set indicating
		// it was a timeline_item originally.
		panic(assert("check bits are set"))
	}
	return to
}

var timeline_item_pool sync.Pool

// new_timeline_item returns a new prepared timeline_item.
func new_timeline_item() *timeline_item {
	v := timeline_item_pool.Get()
	if v == nil {
		i := new(timeline_item)
		i.elem.data = unsafe.Pointer(i)
		i.ck = ^uint(0)
		v = i
	}
	item := v.(*timeline_item)
	return item
}

// free_timeline_item releases the timeline_item.
func free_timeline_item(item *timeline_item) {
	if len(item.indexed) > 0 ||
		item.elem.next != nil ||
		item.elem.prev != nil {
		msg := assert("item not in use")
		os.Stderr.WriteString(msg + "\n")
		return
	}
	item.data = nil
	item.pk = nil
	timeline_item_pool.Put(item)
}