Debian Codegraph @

lists/arraylist/arraylist.go

raw symbols SCIP
1 // Copyright (c) 2015, Emir Pasic. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Package arraylist implements the array list.
6 //
7 // Structure is not thread safe.
8 //
9 // Reference: https://en.wikipedia.org/wiki/List_%28abstract_data_type%29
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 // Assert List implementation
22 var _ lists.List[int] = (*List[int])(nil)
23
24 // List holds the elements in a slice
25 type List[T comparable] struct {
26 elements []T
27 size int
28 }
29
30 const (
31 growthFactor = float32(2.0) // growth by 100%
32 shrinkFactor = float32(0.25) // shrink when size is 25% of capacity (0 means never shrink)
33 )
34
35 // New instantiates a new list and adds the passed values, if any, to the list
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 // Add appends a value at the end of the list
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 // Get returns the element at index.
54 // Second return parameter is true if index is within bounds of the array and array is not empty, otherwise false.
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 // Remove removes the element at the given index from the list.
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]) // shift to the left by one (slow operation, need ways to optimize this)
74 list.size--
75
76 list.shrink()
77 }
78
79 // Contains checks if elements (one or more) are present in the set.
80 // All elements have to be present in the set for the method to return true.
81 // Performance time complexity of n^2.
82 // Returns true if no arguments are passed at all, i.e. set is always super-set of empty set.
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 // Values returns all elements in the list.
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 // IndexOf returns index of provided element
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 // Empty returns true if list does not contain any elements.
121 func (list *List[T]) Empty() bool {
122 return list.size == 0
123 }
124
125 // Size returns number of elements within the list.
126 func (list *List[T]) Size() int {
127 return list.size
128 }
129
130 // Clear removes all elements from the list.
131 func (list *List[T]) Clear() {
132 list.size = 0
133 list.elements = []T{}
134 }
135
136 // Sort sorts values (in-place) using.
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 // Swap swaps the two values at the specified positions.
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 // Insert inserts values at specified index position shifting the value at that position (if any) and any subsequent elements to the right.
152 // Does not do anything if position is negative or bigger than list's size
153 // Note: position equal to list's size is valid, i.e. append.
154 func (list *List[T]) Insert(index int, values ...T) {
155
156 if !list.withinRange(index) {
157 // Append
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 // Set the value at specified index
172 // Does not do anything if position is negative or bigger than list's size
173 // Note: position equal to list's size is valid, i.e. append.
174 func (list *List[T]) Set(index int, value T) {
175
176 if !list.withinRange(index) {
177 // Append
178 if index == list.size {
179 list.Add(value)
180 }
181 return
182 }
183
184 list.elements[index] = value
185 }
186
187 // String returns a string representation of container
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 // Check that the index is within bounds of the list
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 // Expand the array if necessary, i.e. capacity will be reached if we add n elements
210 func (list *List[T]) growBy(n int) {
211 // When capacity is reached, grow by a factor of growthFactor and add number of elements
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 // Shrink the array if necessary, i.e. when size is shrinkFactor percent of current capacity
220 func (list *List[T]) shrink() {
221 if shrinkFactor == 0.0 {
222 return
223 }
224 // Shrink when size is at shrinkFactor * capacity
225 currentCapacity := cap(list.elements)
226 if list.size <= int(float32(currentCapacity)*shrinkFactor) {
227 list.resize(list.size)
228 }
229 }