| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
package arraylist |
| 11 |
|
| 12 |
import ( |
| 13 |
"fmt" |
| 14 |
"slices" |
| 15 |
"strings" |
| 16 |
|
| 17 |
"github.com/emirpasic/gods/v2/lists" |
| 18 |
"github.com/emirpasic/gods/v2/utils" |
| 19 |
) |
| 20 |
|
| 21 |
|
| 22 |
var _ lists.List[int] = (*List[int])(nil) |
| 23 |
|
| 24 |
|
| 25 |
type List[T comparable] struct { |
| 26 |
elements []T |
| 27 |
size int |
| 28 |
} |
| 29 |
|
| 30 |
const ( |
| 31 |
growthFactor = float32(2.0) |
| 32 |
shrinkFactor = float32(0.25) |
| 33 |
) |
| 34 |
|
| 35 |
|
| 36 |
func New[T comparable](values ...T) *List[T] { |
| 37 |
list := &List[T]{} |
| 38 |
if len(values) > 0 { |
| 39 |
list.Add(values...) |
| 40 |
} |
| 41 |
return list |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
func (list *List[T]) Add(values ...T) { |
| 46 |
list.growBy(len(values)) |
| 47 |
for _, value := range values { |
| 48 |
list.elements[list.size] = value |
| 49 |
list.size++ |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
func (list *List[T]) Get(index int) (T, bool) { |
| 56 |
|
| 57 |
if !list.withinRange(index) { |
| 58 |
var t T |
| 59 |
return t, false |
| 60 |
} |
| 61 |
|
| 62 |
return list.elements[index], true |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
func (list *List[T]) Remove(index int) { |
| 67 |
|
| 68 |
if !list.withinRange(index) { |
| 69 |
return |
| 70 |
} |
| 71 |
|
| 72 |
clear(list.elements[index : index+1]) |
| 73 |
copy(list.elements[index:], list.elements[index+1:list.size]) |
| 74 |
list.size-- |
| 75 |
|
| 76 |
list.shrink() |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
func (list *List[T]) Contains(values ...T) bool { |
| 84 |
|
| 85 |
for _, searchValue := range values { |
| 86 |
found := false |
| 87 |
for index := 0; index < list.size; index++ { |
| 88 |
if list.elements[index] == searchValue { |
| 89 |
found = true |
| 90 |
break |
| 91 |
} |
| 92 |
} |
| 93 |
if !found { |
| 94 |
return false |
| 95 |
} |
| 96 |
} |
| 97 |
return true |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
func (list *List[T]) Values() []T { |
| 102 |
newElements := make([]T, list.size, list.size) |
| 103 |
copy(newElements, list.elements[:list.size]) |
| 104 |
return newElements |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
func (list *List[T]) IndexOf(value T) int { |
| 109 |
if list.size == 0 { |
| 110 |
return -1 |
| 111 |
} |
| 112 |
for index, element := range list.elements { |
| 113 |
if element == value { |
| 114 |
return index |
| 115 |
} |
| 116 |
} |
| 117 |
return -1 |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
func (list *List[T]) Empty() bool { |
| 122 |
return list.size == 0 |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
func (list *List[T]) Size() int { |
| 127 |
return list.size |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
func (list *List[T]) Clear() { |
| 132 |
list.size = 0 |
| 133 |
list.elements = []T{} |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
func (list *List[T]) Sort(comparator utils.Comparator[T]) { |
| 138 |
if len(list.elements) < 2 { |
| 139 |
return |
| 140 |
} |
| 141 |
slices.SortFunc(list.elements[:list.size], comparator) |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
func (list *List[T]) Swap(i, j int) { |
| 146 |
if list.withinRange(i) && list.withinRange(j) { |
| 147 |
list.elements[i], list.elements[j] = list.elements[j], list.elements[i] |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
func (list *List[T]) Insert(index int, values ...T) { |
| 155 |
|
| 156 |
if !list.withinRange(index) { |
| 157 |
|
| 158 |
if index == list.size { |
| 159 |
list.Add(values...) |
| 160 |
} |
| 161 |
return |
| 162 |
} |
| 163 |
|
| 164 |
l := len(values) |
| 165 |
list.growBy(l) |
| 166 |
list.size += l |
| 167 |
copy(list.elements[index+l:], list.elements[index:list.size-l]) |
| 168 |
copy(list.elements[index:], values) |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
func (list *List[T]) Set(index int, value T) { |
| 175 |
|
| 176 |
if !list.withinRange(index) { |
| 177 |
|
| 178 |
if index == list.size { |
| 179 |
list.Add(value) |
| 180 |
} |
| 181 |
return |
| 182 |
} |
| 183 |
|
| 184 |
list.elements[index] = value |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
func (list *List[T]) String() string { |
| 189 |
str := "ArrayList\n" |
| 190 |
values := make([]string, 0, list.size) |
| 191 |
for _, value := range list.elements[:list.size] { |
| 192 |
values = append(values, fmt.Sprintf("%v", value)) |
| 193 |
} |
| 194 |
str += strings.Join(values, ", ") |
| 195 |
return str |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
func (list *List[T]) withinRange(index int) bool { |
| 200 |
return index >= 0 && index < list.size |
| 201 |
} |
| 202 |
|
| 203 |
func (list *List[T]) resize(cap int) { |
| 204 |
newElements := make([]T, cap, cap) |
| 205 |
copy(newElements, list.elements) |
| 206 |
list.elements = newElements |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
func (list *List[T]) growBy(n int) { |
| 211 |
|
| 212 |
currentCapacity := cap(list.elements) |
| 213 |
if list.size+n >= currentCapacity { |
| 214 |
newCapacity := int(growthFactor * float32(currentCapacity+n)) |
| 215 |
list.resize(newCapacity) |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
func (list *List[T]) shrink() { |
| 221 |
if shrinkFactor == 0.0 { |
| 222 |
return |
| 223 |
} |
| 224 |
|
| 225 |
currentCapacity := cap(list.elements) |
| 226 |
if list.size <= int(float32(currentCapacity)*shrinkFactor) { |
| 227 |
list.resize(list.size) |
| 228 |
} |
| 229 |
} |