-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
47 lines (41 loc) · 1.18 KB
/
types.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
package util
import (
"github.com/fireflycons/generic_collections/collections"
"github.com/fireflycons/generic_collections/internal/local"
"github.com/fireflycons/generic_collections/internal/messages"
)
// Properties shared by all iterator types.
type IteratorBase[T any] struct {
Version int
NilElement collections.Element[T]
}
// Concrete representation of Element interface.
type ElementType[T any] struct {
Collection collections.Collection[T]
Version int
ValueP *T
local.InternalImpl
}
func NewElementType[T any](collection collections.Collection[T], val *T) *ElementType[T] {
return &ElementType[T]{
Collection: collection,
Version: GetVersion[T](collection),
ValueP: val,
}
}
func (e *ElementType[T]) Value() T {
if e.Version != GetVersion[T](e.Collection) {
panic(messages.COLLECTION_MODIFIED)
}
return *e.ValueP
}
func (e *ElementType[T]) ValuePtr() *T {
collectionType := e.Collection.Type()
if collectionType == collections.COLLECTION_HASHSET || collectionType == collections.COLLECTION_ORDEREDSET {
panic(messages.SET_POINTER_MODIFICATION)
}
if e.Version != GetVersion[T](e.Collection) {
panic(messages.COLLECTION_MODIFIED)
}
return e.ValueP
}