forked from benbjohnson/immutable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenhanced_builders.go
More file actions
571 lines (512 loc) · 15.8 KB
/
Copy pathenhanced_builders.go
File metadata and controls
571 lines (512 loc) · 15.8 KB
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
package immutable
import (
"cmp"
"iter"
)
// BatchListBuilder provides enhanced batch operations for efficient List construction.
// Optimized for bulk insertions with minimal allocations.
type BatchListBuilder[T any] struct {
list *List[T]
batchSize int
buffer []T
}
// NewBatchListBuilder returns a new batch-optimized list builder.
// batchSize determines the internal buffer size for batch operations.
func NewBatchListBuilder[T any](batchSize int) *BatchListBuilder[T] {
if batchSize <= 0 {
batchSize = 32 // default batch size
}
return &BatchListBuilder[T]{
list: NewList[T](),
batchSize: batchSize,
buffer: make([]T, 0, batchSize),
}
}
// Append adds a single value to the batch buffer.
// Values are flushed to the list when buffer reaches capacity.
func (b *BatchListBuilder[T]) Append(value T) {
b.buffer = append(b.buffer, value)
if len(b.buffer) >= b.batchSize {
b.Flush()
}
}
// AppendSlice adds multiple values efficiently.
// Automatically handles batching for optimal performance.
func (b *BatchListBuilder[T]) AppendSlice(values []T) {
for _, value := range values {
b.Append(value)
}
}
// Flush commits all buffered values to the underlying list.
func (b *BatchListBuilder[T]) Flush() {
if len(b.buffer) == 0 {
return
}
// Fast path: if underlying list is slice-backed, extend in one allocation.
// This may produce a slice node larger than listSliceThreshold. That is
// intentional: batch-built lists trade threshold compliance for build
// speed. The first subsequent Append/Prepend by a consumer will trigger
// a one-time O(n) trie conversion.
if sliceNode, ok := b.list.root.(*listSliceNode[T]); ok {
newLen := b.list.size + len(b.buffer)
newElements := make([]T, newLen)
copy(newElements, sliceNode.elements)
copy(newElements[b.list.size:], b.buffer)
b.list.root = &listSliceNode[T]{elements: newElements}
b.list.size = newLen
} else {
// Fallback: append one-by-one using mutable trie path
for _, value := range b.buffer {
b.list = b.list.append(value, true) // mutable for performance
}
}
// Clear buffer (reuse capacity)
b.buffer = b.buffer[:0]
}
// Reset clears the builder state while retaining buffer capacity.
func (b *BatchListBuilder[T]) Reset() {
b.list = NewList[T]()
b.buffer = b.buffer[:0]
}
// List returns the final list and invalidates the builder.
// Automatically flushes any remaining buffered values.
func (b *BatchListBuilder[T]) List() *List[T] {
b.Flush()
list := b.list
b.list = nil
return list
}
// Len returns the total number of elements (committed + buffered).
func (b *BatchListBuilder[T]) Len() int {
if b.list == nil {
return 0
}
return b.list.Len() + len(b.buffer)
}
// BatchMapBuilder provides enhanced batch operations for efficient Map construction.
type BatchMapBuilder[K comparable, V any] struct {
m *Map[K, V]
batchSize int
buffer []mapEntry[K, V]
}
// NewBatchMapBuilder returns a new batch-optimized map builder.
func NewBatchMapBuilder[K comparable, V any](hasher Hasher[K], batchSize int) *BatchMapBuilder[K, V] {
if batchSize <= 0 {
batchSize = 32
}
return &BatchMapBuilder[K, V]{
m: NewMap[K, V](hasher),
batchSize: batchSize,
buffer: make([]mapEntry[K, V], 0, batchSize),
}
}
// Set adds a key/value pair to the batch buffer.
func (b *BatchMapBuilder[K, V]) Set(key K, value V) {
b.buffer = append(b.buffer, mapEntry[K, V]{key: key, value: value})
if len(b.buffer) >= b.batchSize {
b.Flush()
}
}
// SetMap adds all entries from a regular Go map.
func (b *BatchMapBuilder[K, V]) SetMap(entries map[K]V) {
for k, v := range entries {
b.Set(k, v)
}
}
// Flush commits all buffered entries to the underlying map.
func (b *BatchMapBuilder[K, V]) Flush() {
if len(b.buffer) == 0 {
return
}
// Fast path: if map is empty, build an array node in one shot with last-write-wins semantics.
if b.m.root == nil {
var dedup []mapEntry[K, V]
if len(b.buffer) <= maxArrayMapSize {
// Tiny buffer: use slice-based last-occurrence dedup without maps.
for i := len(b.buffer) - 1; i >= 0; i-- {
key := b.buffer[i].key
found := false
for _, e := range dedup {
if b.m.hasher != nil {
if b.m.hasher.Equal(e.key, key) {
found = true
break
}
} else {
if any(e.key) == any(key) {
found = true
break
}
}
}
if !found {
dedup = append(dedup, b.buffer[i])
}
}
// Reverse to restore original order of last occurrences
for i, j := 0, len(dedup)-1; i < j; i, j = i+1, j-1 {
dedup[i], dedup[j] = dedup[j], dedup[i]
}
} else {
// Larger buffer: map-based dedup
seen := make(map[K]struct{}, len(b.buffer))
for i := len(b.buffer) - 1; i >= 0; i-- {
e := b.buffer[i]
if _, ok := seen[e.key]; ok {
continue
}
seen[e.key] = struct{}{}
dedup = append(dedup, e)
}
for i, j := 0, len(dedup)-1; i < j; i, j = i+1, j-1 {
dedup[i], dedup[j] = dedup[j], dedup[i]
}
}
// Ensure hasher is set for Get operations
if b.m.hasher == nil && len(dedup) > 0 {
b.m.hasher = NewHasher(dedup[0].key)
}
// Install as array node
b.m.size = len(dedup)
b.m.root = &mapArrayNode[K, V]{entries: dedup}
} else if arr, ok := b.m.root.(*mapArrayNode[K, V]); ok {
// Small-structure fast path: stay in array node if total entries remain under threshold.
// Build last-write-wins overrides and first-seen order for new keys (slice-based for tiny buffers).
// Stage last-occurrence per key as slice for tiny buffers; fallback to map for larger buffers.
var last []mapEntry[K, V]
if len(b.buffer) <= maxArrayMapSize {
for i := len(b.buffer) - 1; i >= 0; i-- {
e := b.buffer[i]
found := false
for _, le := range last {
if b.m.hasher != nil {
if b.m.hasher.Equal(le.key, e.key) {
found = true
break
}
} else {
if any(le.key) == any(e.key) {
found = true
break
}
}
}
if !found {
last = append(last, e)
}
}
// Reverse to keep first-seen order among last-occurrences
for i, j := 0, len(last)-1; i < j; i, j = i+1, j-1 {
last[i], last[j] = last[j], last[i]
}
} else {
seenNew := make(map[K]struct{}, len(b.buffer))
for i := len(b.buffer) - 1; i >= 0; i-- {
e := b.buffer[i]
if _, ok := seenNew[e.key]; ok {
continue
}
seenNew[e.key] = struct{}{}
last = append(last, e)
}
for i, j := 0, len(last)-1; i < j; i, j = i+1, j-1 {
last[i], last[j] = last[j], last[i]
}
}
// Track original keys
orig := make(map[K]struct{}, len(arr.entries))
for _, e := range arr.entries {
orig[e.key] = struct{}{}
}
// Copy existing and apply overrides from last
newEntries := make([]mapEntry[K, V], len(arr.entries))
copy(newEntries, arr.entries)
for i, e := range newEntries {
for _, le := range last {
// If key matches, override value
match := false
if b.m.hasher != nil {
match = b.m.hasher.Equal(e.key, le.key)
} else {
match = any(e.key) == any(le.key)
}
if match {
newEntries[i] = mapEntry[K, V]{key: e.key, value: le.value}
break
}
}
}
// Append truly new keys
toAppend := make([]mapEntry[K, V], 0)
for _, le := range last {
if _, existed := orig[le.key]; !existed {
toAppend = append(toAppend, le)
}
}
newCount := len(newEntries) + len(toAppend)
if newCount <= maxArrayMapSize {
newEntries = append(newEntries, toAppend...)
b.m.size = newCount
b.m.root = &mapArrayNode[K, V]{entries: newEntries}
} else {
// Fallback: set one-by-one using mutable path
for _, e := range b.buffer {
b.m = b.m.set(e.key, e.value, true)
}
}
} else {
// Fallback: set one-by-one using mutable path
for _, entry := range b.buffer {
b.m = b.m.set(entry.key, entry.value, true) // mutable for performance
}
}
// Clear buffer (reuse capacity)
b.buffer = b.buffer[:0]
}
// Reset clears the builder state while retaining buffer capacity.
func (b *BatchMapBuilder[K, V]) Reset() {
var hasher Hasher[K]
if b.m != nil {
hasher = b.m.hasher
}
b.m = NewMap[K, V](hasher)
b.buffer = b.buffer[:0]
}
// Map returns the final map and invalidates the builder.
func (b *BatchMapBuilder[K, V]) Map() *Map[K, V] {
b.Flush()
m := b.m
b.m = nil
return m
}
// Len returns the total number of entries (committed + buffered).
func (b *BatchMapBuilder[K, V]) Len() int {
if b.m == nil {
return 0
}
return b.m.Len() + len(b.buffer)
}
// StreamingListBuilder provides streaming operations with configurable flush triggers.
type StreamingListBuilder[T any] struct {
*BatchListBuilder[T]
autoFlushSize int
autoFlushEnabled bool
}
// NewStreamingListBuilder creates a builder with automatic flush capabilities.
func NewStreamingListBuilder[T any](batchSize, autoFlushSize int) *StreamingListBuilder[T] {
return &StreamingListBuilder[T]{
BatchListBuilder: NewBatchListBuilder[T](batchSize),
autoFlushSize: max(autoFlushSize, batchSize),
autoFlushEnabled: autoFlushSize > 0,
}
}
// Stream processes values through a streaming pipeline.
// Automatically flushes when size thresholds are reached.
func (b *StreamingListBuilder[T]) Stream(values <-chan T) {
for value := range values {
b.Append(value)
// Auto-flush when reaching threshold
if b.autoFlushEnabled && b.Len() >= b.autoFlushSize {
b.Flush()
}
}
}
// Filter processes values through a filter function before adding.
func (b *StreamingListBuilder[T]) Filter(values []T, filterFn func(T) bool) {
for _, value := range values {
if filterFn(value) {
b.Append(value)
}
}
}
// Transform processes values through a transformation function.
func (b *StreamingListBuilder[T]) Transform(values []T, transformFn func(T) T) {
for _, value := range values {
b.Append(transformFn(value))
}
}
// SortedBatchBuilder provides batch operations optimized for sorted data.
type SortedBatchBuilder[K cmp.Ordered, V any] struct {
sm *SortedMap[K, V]
batchSize int
buffer []mapEntry[K, V]
sorted bool // whether buffer is kept sorted
}
// NewSortedBatchBuilder creates a batch builder for sorted maps.
// If maintainSort is true, entries are kept sorted in the buffer for optimal insertion.
func NewSortedBatchBuilder[K cmp.Ordered, V any](comparer Comparer[K], batchSize int, maintainSort bool) *SortedBatchBuilder[K, V] {
if batchSize <= 0 {
batchSize = 32
}
return &SortedBatchBuilder[K, V]{
sm: NewSortedMap[K, V](comparer),
batchSize: batchSize,
buffer: make([]mapEntry[K, V], 0, batchSize),
sorted: maintainSort,
}
}
// Set adds a key/value pair, maintaining sort order if enabled.
func (b *SortedBatchBuilder[K, V]) Set(key K, value V) {
entry := mapEntry[K, V]{key: key, value: value}
if b.sorted && len(b.buffer) > 0 {
// Insert in sorted position
insertIdx := 0
for i, existing := range b.buffer {
if defaultCompare(key, existing.key) <= 0 {
insertIdx = i
break
}
insertIdx = i + 1
}
// Insert at correct position
b.buffer = append(b.buffer, mapEntry[K, V]{})
copy(b.buffer[insertIdx+1:], b.buffer[insertIdx:])
b.buffer[insertIdx] = entry
} else {
// Simple append
b.buffer = append(b.buffer, entry)
}
if len(b.buffer) >= b.batchSize {
b.Flush()
}
}
// Flush commits buffered entries to the sorted map.
func (b *SortedBatchBuilder[K, V]) Flush() {
if len(b.buffer) == 0 {
return
}
// Batch set all buffered entries
for _, entry := range b.buffer {
b.sm = b.sm.set(entry.key, entry.value, true)
}
// Clear buffer
b.buffer = b.buffer[:0]
}
// SortedMap returns the final sorted map.
func (b *SortedBatchBuilder[K, V]) SortedMap() *SortedMap[K, V] {
b.Flush()
sm := b.sm
b.sm = nil
return sm
}
// BatchSetBuilder provides enhanced batch operations for efficient Set construction.
type BatchSetBuilder[T comparable] struct {
mapBuilder *BatchMapBuilder[T, struct{}]
}
// NewBatchSetBuilder returns a new batch-optimized set builder.
func NewBatchSetBuilder[T comparable](hasher Hasher[T], batchSize int) *BatchSetBuilder[T] {
return &BatchSetBuilder[T]{
mapBuilder: NewBatchMapBuilder[T, struct{}](hasher, batchSize),
}
}
// Add inserts a value into the batch buffer.
func (b *BatchSetBuilder[T]) Add(value T) {
b.mapBuilder.Set(value, struct{}{})
}
// AddSlice adds multiple values efficiently.
func (b *BatchSetBuilder[T]) AddSlice(values []T) {
for _, value := range values {
b.Add(value)
}
}
// Flush commits all buffered values to the underlying set.
func (b *BatchSetBuilder[T]) Flush() {
b.mapBuilder.Flush()
}
// Set returns the final set and invalidates the builder.
func (b *BatchSetBuilder[T]) Set() *Set[T] {
m := b.mapBuilder.Map()
if m == nil {
return nil
}
return &Set[T]{m: m}
}
// Len returns the total number of elements (committed + buffered).
func (b *BatchSetBuilder[T]) Len() int {
return b.mapBuilder.Len()
}
// BatchSortedSetBuilder provides enhanced batch operations for efficient SortedSet construction.
type BatchSortedSetBuilder[T cmp.Ordered] struct {
sortedBuilder *SortedBatchBuilder[T, struct{}]
}
// NewBatchSortedSetBuilder returns a new batch-optimized sorted set builder.
func NewBatchSortedSetBuilder[T cmp.Ordered](comparer Comparer[T], batchSize int, maintainSort bool) *BatchSortedSetBuilder[T] {
return &BatchSortedSetBuilder[T]{
sortedBuilder: NewSortedBatchBuilder[T, struct{}](comparer, batchSize, maintainSort),
}
}
// Add inserts a value into the batch buffer, maintaining sort order if enabled.
func (b *BatchSortedSetBuilder[T]) Add(value T) {
b.sortedBuilder.Set(value, struct{}{})
}
// AddSlice adds multiple values efficiently.
func (b *BatchSortedSetBuilder[T]) AddSlice(values []T) {
for _, value := range values {
b.Add(value)
}
}
// Flush commits buffered values to the sorted set.
func (b *BatchSortedSetBuilder[T]) Flush() {
b.sortedBuilder.Flush()
}
// SortedSet returns the final sorted set.
func (b *BatchSortedSetBuilder[T]) SortedSet() *SortedSet[T] {
sm := b.sortedBuilder.SortedMap()
if sm == nil {
return nil
}
return &SortedSet[T]{m: sm}
}
// Len returns the total number of elements (committed + buffered).
func (b *BatchSortedSetBuilder[T]) Len() int {
return b.sortedBuilder.sm.Len() + len(b.sortedBuilder.buffer)
}
// StreamingMapBuilder provides streaming operations with configurable flush triggers for Maps.
type StreamingMapBuilder[K comparable, V any] struct {
*BatchMapBuilder[K, V]
autoFlushSize int
autoFlushEnabled bool
}
// NewStreamingMapBuilder creates a map builder with automatic flush capabilities.
func NewStreamingMapBuilder[K comparable, V any](hasher Hasher[K], batchSize, autoFlushSize int) *StreamingMapBuilder[K, V] {
return &StreamingMapBuilder[K, V]{
BatchMapBuilder: NewBatchMapBuilder[K, V](hasher, batchSize),
autoFlushSize: max(autoFlushSize, batchSize),
autoFlushEnabled: autoFlushSize > 0,
}
}
// Stream processes key/value pairs from an iterator.
// Use maps.All(m) to stream from a Go map, or provide any iter.Seq2 source.
func (b *StreamingMapBuilder[K, V]) Stream(seq iter.Seq2[K, V]) {
for key, value := range seq {
b.Set(key, value)
if b.autoFlushEnabled && b.Len() >= b.autoFlushSize {
b.Flush()
}
}
}
// Filter processes entries from an iterator, adding only those accepted by filterFn.
func (b *StreamingMapBuilder[K, V]) Filter(seq iter.Seq2[K, V], filterFn func(K, V) bool) {
for key, value := range seq {
if filterFn(key, value) {
b.Set(key, value)
}
}
}
// Transform processes entries from an iterator through a transformation function.
func (b *StreamingMapBuilder[K, V]) Transform(seq iter.Seq2[K, V], transformFn func(K, V) (K, V)) {
for key, value := range seq {
newKey, newValue := transformFn(key, value)
b.Set(newKey, newValue)
}
}
// SetMany adds multiple key/value pairs efficiently from a map.
func (b *StreamingMapBuilder[K, V]) SetMany(entries map[K]V) {
for key, value := range entries {
b.Set(key, value)
// Auto-flush when reaching threshold
if b.autoFlushEnabled && b.Len() >= b.autoFlushSize {
b.Flush()
}
}
}