-
Notifications
You must be signed in to change notification settings - Fork 0
/
Package.go
836 lines (728 loc) · 22.7 KB
/
Package.go
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
/*
................................................................................
. Copyright (c) 2009-2024 Crater Dog Technologies. All Rights Reserved. .
................................................................................
. DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. .
. .
. This code is free software; you can redistribute it and/or modify it under .
. the terms of The MIT License (MIT), as published by the Open Source .
. Initiative. (See https://opensource.org/license/MIT) .
................................................................................
*/
/*
Package "collection" defines a set of simple, pragmatic abstract types and
interfaces for Go based collections of values. It also provide an efficient and
compact implementation of the following collection classes based on these
abstractions:
- Array (extended Go array)
- Map (extended Go map)
- List (a sortable list)
- Catalog (a sortable map)
- Set (an ordered set)
- Stack (a LIFO)
- Queue (a blocking FIFO)
For detailed documentation on this package refer to the wiki:
- https://github.com/craterdog/go-collection-framework/wiki
This package follows the Crater Dog Technologies™ (craterdog) Go Coding
Conventions located here:
- https://github.com/craterdog/go-model-framework/wiki
Additional implementations of the classes provided by this package can be
developed and used seamlessly since the interface definitions only depend on
other interfaces and primitive types; and the class implementations only depend
on interfaces, not on each other.
*/
package collection
// Types
/*
Collection is a generic type representing any type of collections of values.
*/
type Collection any
/*
Key is a generic type representing any type of associative key.
*/
type Key any
/*
Value is a generic type representing any type of value.
*/
type Value any
// Functionals
/*
RankingFunction[V Value] defines the signature for any function that can
determine the relative ordering of two values. The result must be one of the
following:
-1: The first value is less than the second value.
0: The first value is equal to the second value.
1: The first value is more than the second value.
The meaning of "less" and "more" is determined by the specific function that
implements this signature.
*/
type RankingFunction[V Value] func(
first V,
second V,
) int
// Aspects
/*
Accessible[V Value] defines the set of method signatures that must be
supported by all sequences whose values can be accessed using indices. The
indices of an accessible sequence are ORDINAL rather than ZERO based—which
never really made sense except for pointer offsets. What is the "zeroth
value" anyway? It's the "first value", right? So we start fresh...
This approach allows for positive indices starting at the beginning of the
sequence, and negative indices starting at the end of the sequence as follows:
1 2 3 N
[value 1] . [value 2] . [value 3] ... [value N]
-N -(N-1) -(N-2) -1
Notice that because the indices are ordinal based, the positive and negative
indices are symmetrical.
*/
type Accessible[V Value] interface {
// Methods
GetValue(index int) V
GetValues(
first int,
last int,
) Sequential[V]
}
/*
Associative[K Key, V Value] defines the set of method signatures that must be
supported by all sequences of key-value associations.
*/
type Associative[K Key, V Value] interface {
// Methods
GetKeys() Sequential[K]
GetValue(key K) V
GetValues(keys Sequential[K]) Sequential[V]
RemoveAll()
RemoveValue(key K) V
RemoveValues(keys Sequential[K]) Sequential[V]
SetValue(
key K,
value V,
)
}
/*
Canonical defines the set of method signatures that must be supported by all
canonical notations.
*/
type Canonical interface {
// Methods
FormatCollection(collection Collection) string
ParseSource(source string) Collection
}
/*
Expandable[V Value] defines the set of method signatures that must be supported
by all sequences that allow new values to be appended, inserted and removed.
*/
type Expandable[V Value] interface {
// Methods
AppendValue(value V)
AppendValues(values Sequential[V])
InsertValue(
slot int,
value V,
)
InsertValues(
slot int,
values Sequential[V],
)
RemoveAll()
RemoveValue(index int) V
RemoveValues(
first int,
last int,
) Sequential[V]
}
/*
Flexible[V Value] defines the set of method signatures that must be supported by
all sequences of values that allow new values to be added and existing values to
be removed.
*/
type Flexible[V Value] interface {
// Methods
AddValue(value V)
AddValues(values Sequential[V])
RemoveAll()
RemoveValue(value V)
RemoveValues(values Sequential[V])
}
/*
Limited[V Value] defines the set of method signatures that must be supported by
all sequences of values that allow new values to be added and limit the total
number of values in the sequence.
*/
type Limited[V Value] interface {
// Methods
AddValue(value V)
RemoveAll()
}
/*
Searchable[V Value] defines the set of method signatures that must be supported
by all searchable sequences of values.
*/
type Searchable[V Value] interface {
// Methods
ContainsAll(values Sequential[V]) bool
ContainsAny(values Sequential[V]) bool
ContainsValue(value V) bool
GetIndex(value V) int
}
/*
Sequential[V Value] defines the set of method signatures that must be supported
by all sequences of values.
*/
type Sequential[V Value] interface {
// Methods
AsArray() []V
GetIterator() IteratorLike[V]
GetSize() int
IsEmpty() bool
}
/*
Sortable[V Value] defines the set of method signatures that must be supported by
all sequences whose values may be reordered using various sorting algorithms.
*/
type Sortable[V Value] interface {
// Methods
ReverseValues()
ShuffleValues()
SortValues()
SortValuesWithRanker(ranker RankingFunction[V])
}
/*
Synchronized defines the set of method signatures that must be supported by all
synchronized groups of threads.
*/
type Synchronized interface {
// Methods
Add(delta int)
Done()
Wait()
}
/*
Systematic[V Value] defines the set of method signatures that must be supported
by all systematic sorting agents.
*/
type Systematic[V Value] interface {
// Methods
ReverseValues(values []V)
ShuffleValues(values []V)
SortValues(values []V)
}
/*
Updatable[V Value] defines the set of method signatures that must be supported
by all updatable sequences of values.
*/
type Updatable[V Value] interface {
// Methods
SetValue(
index int,
value V,
)
SetValues(
index int,
values Sequential[V],
)
}
// Classes
/*
ArrayClassLike[V Value] defines the set of class constants, constructors and
functions that must be supported by all array-class-like classes.
*/
type ArrayClassLike[V Value] interface {
// Constructors
MakeFromArray(values []V) ArrayLike[V]
MakeFromSequence(values Sequential[V]) ArrayLike[V]
MakeFromSize(size int) ArrayLike[V]
MakeFromSource(
source string,
notation NotationLike,
) ArrayLike[V]
}
/*
AssociationClassLike[K Key, V Value] defines the set of class constants,
constructors and functions that must be supported by all
association-class-like classes.
*/
type AssociationClassLike[K Key, V Value] interface {
// Constructors
MakeWithAttributes(
key K,
value V,
) AssociationLike[K, V]
}
/*
CatalogClassLike[K comparable, V Value] defines the set of class constants,
constructors and functions that must be supported by all catalog-class-like
classes. The following functions are supported:
Extract() returns a new catalog containing only the associations that are in
the specified catalog that have the specified keys. The associations in the
resulting catalog will be in the same order as the specified keys.
Merge() returns a new catalog containing all of the associations that are in
the specified Catalogs in the order that they appear in each catalog. If a
key is present in both Catalogs, the value of the key from the second
catalog takes precedence.
*/
type CatalogClassLike[K comparable, V Value] interface {
// Constructors
Make() CatalogLike[K, V]
MakeFromArray(associations []AssociationLike[K, V]) CatalogLike[K, V]
MakeFromMap(associations map[K]V) CatalogLike[K, V]
MakeFromSequence(associations Sequential[AssociationLike[K, V]]) CatalogLike[K, V]
MakeFromSource(
source string,
notation NotationLike,
) CatalogLike[K, V]
// Functions
Extract(
catalog CatalogLike[K, V],
keys Sequential[K],
) CatalogLike[K, V]
Merge(
first CatalogLike[K, V],
second CatalogLike[K, V],
) CatalogLike[K, V]
}
/*
CollatorClassLike[V Value] defines the set of class constants, constructors and
functions that must be supported by all collator-class-like classes.
*/
type CollatorClassLike[V Value] interface {
// Constants
DefaultMaximum() int
// Constructors
Make() CollatorLike[V]
MakeWithMaximum(maximum int) CollatorLike[V]
}
/*
FormatterClassLike defines the set of class constants, constructors and
functions that must be supported by all formatter-class-like classes.
*/
type FormatterClassLike interface {
// Constants
DefaultMaximum() int
// Constructors
Make() FormatterLike
MakeWithMaximum(maximum int) FormatterLike
}
/*
IteratorClassLike[V Value] defines the set of class constants, constructors and
functions that must be supported by all iterator-class-like classes.
*/
type IteratorClassLike[V Value] interface {
// Constructors
MakeFromSequence(values Sequential[V]) IteratorLike[V]
}
/*
ListClassLike[V Value] defines the set of class constants, constructors and
functions that must be supported by all list-class-like classes. The following
functions are supported:
Concatenate() combines two lists into a new list containing all values in both
lists. The order of the values in each list is preserved in the new list.
*/
type ListClassLike[V Value] interface {
// Constructors
Make() ListLike[V]
MakeFromArray(values []V) ListLike[V]
MakeFromSequence(values Sequential[V]) ListLike[V]
MakeFromSource(
source string,
notation NotationLike,
) ListLike[V]
// Functions
Concatenate(
first ListLike[V],
second ListLike[V],
) ListLike[V]
}
/*
MapClassLike[K comparable, V Value] defines the set of class constants,
constructors and functions that must be supported by all map-class-like
classes.
*/
type MapClassLike[K comparable, V Value] interface {
// Constructors
Make() MapLike[K, V]
MakeFromArray(associations []AssociationLike[K, V]) MapLike[K, V]
MakeFromMap(associations map[K]V) MapLike[K, V]
MakeFromSequence(associations Sequential[AssociationLike[K, V]]) MapLike[K, V]
MakeFromSource(
source string,
notation NotationLike,
) MapLike[K, V]
}
/*
NotationClassLike defines the set of class constants, constructors and
functions that must be supported by all notation-class-like classes.
*/
type NotationClassLike interface {
// Constructors
Make() NotationLike
}
/*
ParserClassLike defines the set of class constants, constructors and functions
that must be supported by all parser-class-like classes.
*/
type ParserClassLike interface {
// Constructors
Make() ParserLike
}
/*
QueueClassLike[V Value] defines the set of class constants, constructors and
functions that must be supported by all queue-class-like classes. The following
functions are supported:
Fork() connects the output of the specified input Queue with a number of new
output queues specified by the size parameter and returns a sequence of the new
output queues. Each value added to the input queue will be added automatically
to ALL of the output queues. This pattern is useful when a set of DIFFERENT
operations needs to occur for every value and each operation can be done in
parallel.
Join() connects the outputs of the specified sequence of input queues with a new
output queue returns the new output queue. Each value removed from each input
queue will automatically be added to the output queue. This pattern is useful
when the results of the processing with a Split() function need to be
consolidated into a single queue.
Split() connects the output of the specified input Queue with the number of
output queues specified by the size parameter and returns a sequence of the new
output queues. Each value added to the input queue will be added automatically
to ONE of the output queues. This pattern is useful when a SINGLE operation
needs to occur for each value and the operation can be done on the values in
parallel. The results can then be consolidated later on using the Join()
function.
*/
type QueueClassLike[V Value] interface {
// Constants
DefaultCapacity() int
// Constructors
Make() QueueLike[V]
MakeFromArray(values []V) QueueLike[V]
MakeFromSequence(values Sequential[V]) QueueLike[V]
MakeFromSource(
source string,
notation NotationLike,
) QueueLike[V]
MakeWithCapacity(capacity int) QueueLike[V]
// Functions
Fork(
group Synchronized,
input QueueLike[V],
size int,
) Sequential[QueueLike[V]]
Join(
group Synchronized,
inputs Sequential[QueueLike[V]],
) QueueLike[V]
Split(
group Synchronized,
input QueueLike[V],
size int,
) Sequential[QueueLike[V]]
}
/*
SetClassLike[V Value] defines the set of class constants, constructors and
functions that must be supported by all set-class-like classes. The following
functions are supported:
And() returns a new set containing the values that are both of the specified
sets.
Or() returns a new set containing the values that are in either of the specified
sets.
Sans() returns a new set containing the values that are in the first specified
set but not in the second specified set.
Xor() returns a new set containing the values that are in the first specified
set or the second specified set but not both.
*/
type SetClassLike[V Value] interface {
// Constructors
Make() SetLike[V]
MakeFromArray(values []V) SetLike[V]
MakeFromSequence(values Sequential[V]) SetLike[V]
MakeFromSource(
source string,
notation NotationLike,
) SetLike[V]
MakeWithCollator(collator CollatorLike[V]) SetLike[V]
// Functions
And(
first SetLike[V],
second SetLike[V],
) SetLike[V]
Or(
first SetLike[V],
second SetLike[V],
) SetLike[V]
Sans(
first SetLike[V],
second SetLike[V],
) SetLike[V]
Xor(
first SetLike[V],
second SetLike[V],
) SetLike[V]
}
/*
SorterClassLike[V Value] defines the set of class constants, constructors and
functions that must be supported by all sorter-class-like classes.
*/
type SorterClassLike[V Value] interface {
// Constants
DefaultRanker() RankingFunction[V]
// Constructors
Make() SorterLike[V]
MakeWithRanker(ranker RankingFunction[V]) SorterLike[V]
}
/*
StackClassLike[V Value] defines the set of class constants, constructors and
functions that must be supported by all stack-class-like classes.
*/
type StackClassLike[V Value] interface {
// Constants
DefaultCapacity() int
// Constructors
Make() StackLike[V]
MakeFromArray(values []V) StackLike[V]
MakeFromSequence(values Sequential[V]) StackLike[V]
MakeFromSource(
source string,
notation NotationLike,
) StackLike[V]
MakeWithCapacity(capacity int) StackLike[V]
}
// Instances
/*
ArrayLike[V Value] defines the set of abstractions and methods that must be
supported by all array-like instances. An array-like class maintains a fixed
length indexed sequence of values. Each value is associated with an implicit
positive integer index. An array-like class uses ORDINAL based indexing rather
than the more common—and nonsensical—ZERO based indexing scheme (see the
description of what this means in the Accessible interface definition).
This type is parameterized as follows:
- V is any type of value.
This type essentially provides a higher level abstraction for the primitive Go
array type.
*/
type ArrayLike[V Value] interface {
// Abstractions
Accessible[V]
Sequential[V]
Sortable[V]
Updatable[V]
}
/*
AssociationLike[K Key, V Value] defines the set of abstractions and methods that
must be supported by all association-like instances. An association-like class
maintains information about a key-value association.
This type is parameterized as follows:
- K is a primitive type of key.
- V is any type of value.
This type is used by catalog-like instances to maintain their associations.
*/
type AssociationLike[K Key, V Value] interface {
// Attributes
GetKey() K
GetValue() V
SetValue(value V)
}
/*
CatalogLike[K Key, V Value] defines the set of abstractions and methods that
must be supported by all catalog-like instances. A catalog-like class maintains
a sequence of key-value associations.
This type is parameterized as follows:
- K is a primitive type of key.
- V is any type of entity.
A catalog-like class can use any association-like class key-value association.
*/
type CatalogLike[K Key, V Value] interface {
// Abstractions
Associative[K, V]
Sequential[AssociationLike[K, V]]
Sortable[AssociationLike[K, V]]
}
/*
CollatorLike[V Value] defines the set of abstractions and methods that must be
supported by all collator-like instances. A collator-like class is capable of
comparing and ranking two values of any type.
*/
type CollatorLike[V Value] interface {
// Attributes
GetDepth() int
GetMaximum() int
// Methods
CompareValues(
first V,
second V,
) bool
RankValues(
first V,
second V,
) int
}
/*
FormatterLike defines the set of abstractions and methods that must be supported
by all formatter-like instances.
*/
type FormatterLike interface {
// Attributes
GetDepth() int
GetMaximum() int
// Methods
FormatCollection(collection Collection) string
}
/*
IteratorLike[V Value] defines the set of abstractions and methods that must be
supported by all iterator-like instances. An iterator-like class can be used to
move forward and backward over the values in a sequence. It implements the Gang
of Four (GoF) Iterator Design Pattern:
- https://en.wikipedia.org/wiki/Iterator_pattern
A iterator agent locks into the slots that reside between each value in the
sequence:
[value 1] . [value 2] . [value 3] ... [value N]
^ ^ ^ ^
slot 0 slot 1 slot 2 slot N
It moves from slot to slot and has access to the values (if they exist) on each
side of the slot. At each slot an iterator has access to the previous value
and next value in the sequence (assuming they exist). The slot at the start of
the sequence has no PREVIOUS value, and the slot at the end of the sequence has
no NEXT value.
This type is parameterized as follows:
- V is any type of value.
An iterator-like class is supported by all collection types.
*/
type IteratorLike[V Value] interface {
// Methods
GetNext() V
GetPrevious() V
GetSlot() int
HasNext() bool
HasPrevious() bool
ToEnd()
ToSlot(slot int)
ToStart()
}
/*
ListLike[V Value] defines the set of abstractions and methods that must be
supported by all list-like instances. A list-like class maintains a dynamic
sequence of values which can grow or shrink as needed. Each value is associated
with an implicit positive integer index. An array-like class uses ORDINAL based
indexing rather than the more common—and nonsensical—ZERO based indexing scheme
(see the description of what this means in the Accessible interface definition).
This type is parameterized as follows:
- V is any type of value.
All comparison and ranking of values in the sequence is done using the default
collator.
*/
type ListLike[V Value] interface {
// Abstractions
Accessible[V]
Expandable[V]
Searchable[V]
Sequential[V]
Sortable[V]
Updatable[V]
}
/*
MapLike[K Key, V Value] defines the set of abstractions and methods that must be
supported by all map-like instances. A map-like class extends the primitive Go
map type and maintains a sequence of key-value associations. The order of the
key-value associations in a primitive Go map is random, even for two Go maps
containing the same key-value associations.
This type is parameterized as follows:
- K is a primitive type of key.
- V is any type of entity.
A map-like class can use any association-like class key-value association.
*/
type MapLike[K Key, V Value] interface {
// Abstractions
Associative[K, V]
Sequential[AssociationLike[K, V]]
}
/*
NotationLike defines the set of abstractions and methods that must be supported
by all notation-like instances. A notation-like class can be used to parse and
format collections using a canonical notation like XML, JSON and CDCN
(Crater Dog Collection Notation™).
*/
type NotationLike interface {
// Abstractions
Canonical
}
/*
ParserLike defines the set of abstractions and methods that must be supported by
all parser-like instances.
*/
type ParserLike interface {
// Methods
ParseSource(source string) Collection
}
/*
QueueLike[V Value] defines the set of abstractions and methods that must be
supported by all queue-like instances. A queue-like class implements FIFO
(i.e. first-in-first-out) semantics.
This type is parameterized as follows:
- V is any type of value.
A queue-like class is generally used by multiple go-routines at the same time
and therefore enforces synchronized access. A queue-like class enforces a
maximum length and will block on attempts to add a value it is full. It will
also block on attempts to remove a value when it is empty.
*/
type QueueLike[V Value] interface {
// Attributes
GetCapacity() int
// Abstractions
Limited[V]
Sequential[V]
// Methods
CloseQueue()
RemoveHead() (
head V,
ok bool,
)
}
/*
SetLike[V Value] defines the set of abstractions and methods that must be
supported by all set-like instances. A set-like class maintains an ordered
sequence of values which can grow or shrink as needed.
This type is parameterized as follows:
- V is any type of value.
The order of the values is determined by a configurable CollatorLike[V] agent.
*/
type SetLike[V Value] interface {
// Attributes
GetCollator() CollatorLike[V]
// Abstractions
Accessible[V]
Flexible[V]
Searchable[V]
Sequential[V]
}
/*
SorterLike[V Value] defines the set of abstractions and methods that must be
supported by all sorter-like instances. A sorter-like class implements a
specific sorting algorithm.
This type is parameterized as follows:
- V is any type of value.
A sorter-like class uses a ranking function to order the values. If no ranking
function is specified the values are sorted into their natural order.
*/
type SorterLike[V Value] interface {
// Attributes
GetRanker() RankingFunction[V]
// Abstractions
Systematic[V]
}
/*
StackLike[V Value] defines the set of abstractions and methods that must be
supported by all stack-like instances. A stack-like class implements LIFO
(i.e. last-in-first-out) semantics.
This type is parameterized as follows:
- V is any type of value.
A stack-like class enforces a maximum depth and will panic if that depth is
exceeded. It will also panic on attempts to remove a value when it is empty.
*/
type StackLike[V Value] interface {
// Attributes
GetCapacity() int
// Abstractions
Limited[V]
Sequential[V]
// Methods
RemoveTop() V
}