Documentation ¶
Overview ¶
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:
This package follows the Crater Dog Technologies™ (craterdog) Go Coding Conventions located here:
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.
Index ¶
- type Accessible
- type ArrayClassLike
- type ArrayLike
- type AssociationClassLike
- type AssociationLike
- type Associative
- type Canonical
- type CatalogClassLike
- type CatalogLike
- type Collection
- type Expandable
- type Flexible
- type Limited
- type ListClassLike
- type ListLike
- type MapClassLike
- type MapLike
- type NotationClassLike
- type NotationLike
- type QueueClassLike
- type QueueLike
- type Searchable
- type Sequential
- type SetClassLike
- type SetLike
- type Sortable
- type StackClassLike
- type StackLike
- type Synchronized
- type Updatable
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Accessible ¶
type Accessible[V any] interface { // Methods GetValue(index int) V GetValues( first int, last int, ) Sequential[V] }
Accessible[V any] 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 ArrayClassLike ¶
type ArrayClassLike[V any] interface { // Constants Notation() NotationLike // Constructors MakeFromArray(values []V) ArrayLike[V] MakeFromSequence(values Sequential[V]) ArrayLike[V] MakeFromSize(size int) ArrayLike[V] MakeFromSource(source string) ArrayLike[V] }
ArrayClassLike[V any] is a class interface that defines the complete set of class constants, constructors and functions that must be supported by each concrete array-like class.
func Array ¶
func Array[V any](notation NotationLike) ArrayClassLike[V]
type ArrayLike ¶
type ArrayLike[V any] interface { // Abstractions Accessible[V] Sequential[V] Sortable[V] Updatable[V] }
ArrayLike[V any] is an instance interface that defines the complete set of instance attributes, abstractions and methods that must be supported by each instance of a concrete array-like class. 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 AssociationClassLike ¶
type AssociationClassLike[K comparable, V any] interface { // Constructors MakeWithAttributes( key K, value V, ) AssociationLike[K, V] }
AssociationClassLike[K comparable, V any] is a class interface that defines the complete set of class constants, constructors and functions that must be supported by each concrete association-like class.
func Association ¶
func Association[ K comparable, V any, ]() AssociationClassLike[K, V]
type AssociationLike ¶
type AssociationLike[K comparable, V any] interface { // Attributes GetClass() AssociationClassLike[K, V] GetKey() K GetValue() V SetValue(value V) }
AssociationLike[K comparable, V any] is an instance interface that defines the complete set of instance attributes, abstractions and methods that must be supported by each instance of a concrete association-like class.
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 Associative ¶
type Associative[K comparable, V any] 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, ) }
Associative[K comparable, V any] defines the set of method signatures that must be supported by all sequences of key-value associations.
type Canonical ¶
type Canonical interface { // Methods FormatCollection(collection Collection) string ParseSource(source string) Collection }
Canonical defines the set of method signatures that must be supported by all canonical notations.
type CatalogClassLike ¶
type CatalogClassLike[K comparable, V any] interface { // Constants Notation() NotationLike // 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) 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] }
CatalogClassLike[K comparable, V any] is a class interface that defines the complete set of class constants, constructors and functions that must be supported by each concrete catalog-like class.
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.
func Catalog ¶
func Catalog[K comparable, V any](notation NotationLike) CatalogClassLike[K, V]
type CatalogLike ¶
type CatalogLike[K comparable, V any] interface { // Attributes GetClass() CatalogClassLike[K, V] // Abstractions Associative[K, V] Sequential[AssociationLike[K, V]] Sortable[AssociationLike[K, V]] }
CatalogLike[K comparable, V any] is an instance interface that defines the complete set of instance attributes, abstractions and methods that must be supported by each instance of a concrete catalog-like class.
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 Collection ¶
type Collection any
Collection is a generic type representing any type of collections of values.
type Expandable ¶
type Expandable[V any] 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] }
Expandable[V any] defines the set of method signatures that must be supported by all sequences that allow new values to be appended, inserted and removed.
type Flexible ¶
type Flexible[V any] interface { // Methods AddValue(value V) AddValues(values Sequential[V]) RemoveAll() RemoveValue(value V) RemoveValues(values Sequential[V]) }
Flexible[V any] 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 Limited ¶
type Limited[V any] interface { // Methods AddValue(value V) RemoveAll() }
Limited[V any] 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 ListClassLike ¶
type ListClassLike[V any] interface { // Constants Notation() NotationLike // Constructors Make() ListLike[V] MakeFromArray(values []V) ListLike[V] MakeFromSequence(values Sequential[V]) ListLike[V] MakeFromSource(source string) ListLike[V] // Functions Concatenate( first ListLike[V], second ListLike[V], ) ListLike[V] }
ListClassLike[V any] is a class interface that defines the complete set of class constants, constructors and functions that must be supported by each concrete list-like class.
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.
func List ¶
func List[V any](notation NotationLike) ListClassLike[V]
type ListLike ¶
type ListLike[V any] interface { // Attributes GetClass() ListClassLike[V] // Abstractions Accessible[V] Expandable[V] Searchable[V] Sequential[V] Sortable[V] Updatable[V] }
ListLike[V any] is an instance interface that defines the complete set of instance attributes, abstractions and methods that must be supported by each instance of a concrete list-like class. 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 MapClassLike ¶
type MapClassLike[K comparable, V any] interface { // Constants Notation() NotationLike // 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) MapLike[K, V] }
MapClassLike[K comparable, V any] is a class interface that defines the complete set of class constants, constructors and functions that must be supported by each concrete map-like class.
func Map ¶
func Map[K comparable, V any](notation NotationLike) MapClassLike[K, V]
type MapLike ¶
type MapLike[K comparable, V any] interface { // Abstractions Associative[K, V] Sequential[AssociationLike[K, V]] }
MapLike[K comparable, V any] is an instance interface that defines the complete set of instance attributes, abstractions and methods that must be supported by each instance of a concrete map-like class. 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 NotationClassLike ¶
type NotationClassLike interface { // Constructors Make() NotationLike }
NotationClassLike is a class interface that defines the complete set of class constants, constructors and functions that must be supported by each concrete notation-like class.
type NotationLike ¶
type NotationLike interface { // Attributes GetClass() NotationClassLike // Abstractions Canonical }
NotationLike is an instance interface that defines the complete set of instance attributes, abstractions and methods that must be supported by each instance of a concrete notation-like class. 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 QueueClassLike ¶
type QueueClassLike[V any] interface { // Constants Notation() NotationLike DefaultCapacity() int // Constructors Make() QueueLike[V] MakeFromArray(values []V) QueueLike[V] MakeFromSequence(values Sequential[V]) QueueLike[V] MakeFromSource(source string) 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]] }
QueueClassLike[V any] is a class interface that defines the complete set of class constants, constructors and functions that must be supported by each concrete queue-like class.
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.
func Queue ¶
func Queue[V any](notation NotationLike) QueueClassLike[V]
type QueueLike ¶
type QueueLike[V any] interface { // Attributes GetClass() QueueClassLike[V] GetCapacity() int // Abstractions Limited[V] Sequential[V] // Methods CloseQueue() RemoveHead() ( head V, ok bool, ) }
QueueLike[V any] is an instance interface that defines the complete set of instance attributes, abstractions and methods that must be supported by each instance of a concrete queue-like class. 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 Searchable ¶
type Searchable[V any] interface { // Methods ContainsAll(values Sequential[V]) bool ContainsAny(values Sequential[V]) bool ContainsValue(value V) bool GetIndex(value V) int }
Searchable[V any] defines the set of method signatures that must be supported by all searchable sequences of values.
type Sequential ¶
type Sequential[V any] interface { // Methods AsArray() []V GetIterator() age.IteratorLike[V] GetSize() int IsEmpty() bool }
Sequential[V any] defines the set of method signatures that must be supported by all sequences of values.
type SetClassLike ¶
type SetClassLike[V any] interface { // Constants Notation() NotationLike // Constructors Make() SetLike[V] MakeFromArray(values []V) SetLike[V] MakeFromSequence(values Sequential[V]) SetLike[V] MakeFromSource(source string) SetLike[V] MakeWithCollator(collator age.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] }
SetClassLike[V any] is a class interface that defines the complete set of class constants, constructors and functions that must be supported by each concrete set-like class.
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.
func Set ¶
func Set[V any](notation NotationLike) SetClassLike[V]
type SetLike ¶
type SetLike[V any] interface { // Attributes GetClass() SetClassLike[V] GetCollator() age.CollatorLike[V] // Abstractions Accessible[V] Flexible[V] Searchable[V] Sequential[V] }
SetLike[V any] is an instance interface that defines the complete set of instance attributes, abstractions and methods that must be supported by each instance of a concrete set-like class. 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 Sortable ¶
type Sortable[V any] interface { // Methods ReverseValues() ShuffleValues() SortValues() SortValuesWithRanker(ranker age.RankingFunction[V]) }
Sortable[V any] defines the set of method signatures that must be supported by all sequences whose values may be reordered using various sorting algorithms.
type StackClassLike ¶
type StackClassLike[V any] interface { // Constants Notation() NotationLike DefaultCapacity() int // Constructors Make() StackLike[V] MakeFromArray(values []V) StackLike[V] MakeFromSequence(values Sequential[V]) StackLike[V] MakeFromSource(source string) StackLike[V] MakeWithCapacity(capacity int) StackLike[V] }
StackClassLike[V any] is a class interface that defines the complete set of class constants, constructors and functions that must be supported by each concrete stack-like class.
func Stack ¶
func Stack[V any](notation NotationLike) StackClassLike[V]
type StackLike ¶
type StackLike[V any] interface { // Attributes GetClass() StackClassLike[V] GetCapacity() int // Abstractions Limited[V] Sequential[V] // Methods RemoveTop() V }
StackLike[V any] is an instance interface that defines the complete set of instance attributes, abstractions and methods that must be supported by each instance of a concrete stack-like class. 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 Synchronized ¶
type Synchronized interface { // Methods Add(delta int) Done() Wait() }
Synchronized defines the set of method signatures that must be supported by all synchronized groups of threads.