-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator.go
140 lines (112 loc) · 2.95 KB
/
iterator.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
/*
................................................................................
. 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 agent
import (
fmt "fmt"
syn "sync"
)
// CLASS ACCESS
// Reference
var iteratorClass = map[string]any{}
var iteratorMutex syn.Mutex
// Function
func Iterator[V Value]() IteratorClassLike[V] {
// Generate the name of the bound class type.
var class IteratorClassLike[V]
var name = fmt.Sprintf("%T", class)
// Check for existing bound class type.
iteratorMutex.Lock()
var value = iteratorClass[name]
switch actual := value.(type) {
case *iteratorClass_[V]:
// This bound class type already exists.
class = actual
default:
// Add a new bound class type.
class = &iteratorClass_[V]{
// This class does not define any constants.
}
iteratorClass[name] = class
}
iteratorMutex.Unlock()
// Return a reference to the bound class type.
return class
}
// CLASS METHODS
// Target
type iteratorClass_[V Value] struct {
// This class does not define any constants.
}
// Constructors
func (c *iteratorClass_[V]) MakeFromArray(values []V) IteratorLike[V] {
var size = len(values)
return &iterator_[V]{
size_: size,
values_: values,
}
}
// INSTANCE METHODS
// Target
type iterator_[V Value] struct {
class_ IteratorClassLike[V]
size_ int // So we can safely cache the size.
slot_ int // The initial slot is zero.
values_ []V // The Go array of values is immutable.
}
// Attributes
func (v *iterator_[V]) GetClass() IteratorClassLike[V] {
return v.class_
}
// Public
func (v *iterator_[V]) GetNext() V {
var result V
if v.slot_ < v.size_ {
v.slot_ = v.slot_ + 1
result = v.values_[v.slot_-1] // convert to ZERO based indexing
}
return result
}
func (v *iterator_[V]) GetPrevious() V {
var result V
if v.slot_ > 0 {
result = v.values_[v.slot_-1] // convert to ZERO based indexing
v.slot_ = v.slot_ - 1
}
return result
}
func (v *iterator_[V]) GetSlot() int {
return v.slot_
}
func (v *iterator_[V]) HasNext() bool {
return v.slot_ < v.size_
}
func (v *iterator_[V]) HasPrevious() bool {
return v.slot_ > 0
}
func (v *iterator_[V]) ToEnd() {
v.slot_ = v.size_
}
func (v *iterator_[V]) ToSlot(slot int) {
if slot > v.size_ {
slot = v.size_
}
if slot < -v.size_ {
slot = -v.size_
}
if slot < 0 {
slot = slot + v.size_ + 1
}
v.slot_ = slot
}
func (v *iterator_[V]) ToStart() {
v.slot_ = 0
}