Debian Codegraph @

lists/singlylinkedlist/singlylinkedlist.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 singlylinkedlist implements the singly-linked list.
6 //
7 // Structure is not thread safe.
8 //
9 // Reference: https://en.wikipedia.org/wiki/List_%28abstract_data_type%29
10 package singlylinkedlist
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, where each element points to the next element
25 type List[T comparable] struct {
26 first *element[T]
27 last *element[T]
28 size int
29 }
30
31 type element[T comparable] struct {
32 value T
33 next *element[T]
34 }
35
36 // New instantiates a new list and adds the passed values, if any, to the list
37 func New[T comparable](values ...T) *List[T] {
38 list := &List[T]{}
39 if len(values) > 0 {
40 list.Add(values...)
41 }
42 return list
43 }
44
45 // Add appends a value (one or more) at the end of the list (same as Append())
46 func (list *List[T]) Add(values ...T) {
47 for _, value := range values {
48 newElement := &element[T]{value: value}
49 if list.size == 0 {
50 list.first = newElement
51 list.last = newElement
52 } else {
53 list.last.next = newElement
54 list.last = newElement
55 }
56 list.size++
57 }
58 }
59
60 // Append appends a value (one or more) at the end of the list (same as Add())
61 func (list *List[T]) Append(values ...T) {
62 list.Add(values...)
63 }
64
65 // Prepend prepends a values (or more)
66 func (list *List[T]) Prepend(values ...T) {
67 // in reverse to keep passed order i.e. ["c","d"] -> Prepend(["a","b"]) -> ["a","b","c",d"]
68 for v := len(values) - 1; v >= 0; v-- {
69 newElement := &element[T]{value: values[v], next: list.first}
70 list.first = newElement
71 if list.size == 0 {
72 list.last = newElement
73 }
74 list.size++
75 }
76 }
77
78 // Get returns the element at index.
79 // Second return parameter is true if index is within bounds of the array and array is not empty, otherwise false.
80 func (list *List[T]) Get(index int) (T, bool) {
81
82 if !list.withinRange(index) {
83 var t T
84 return t, false
85 }
86
87 element := list.first
88 for e := 0; e != index; e, element = e+1, element.next {
89 }
90
91 return element.value, true
92 }
93
94 // Remove removes the element at the given index from the list.
95 func (list *List[T]) Remove(index int) {
96
97 if !list.withinRange(index) {
98 return
99 }
100
101 if list.size == 1 {
102 list.Clear()
103 return
104 }
105
106 var beforeElement *element[T]
107 element := list.first
108 for e := 0; e != index; e, element = e+1, element.next {
109 beforeElement = element
110 }
111
112 if element == list.first {
113 list.first = element.next
114 }
115 if element == list.last {
116 list.last = beforeElement
117 }
118 if beforeElement != nil {
119 beforeElement.next = element.next
120 }
121
122 element = nil
123
124 list.size--
125 }
126
127 // Contains checks if values (one or more) are present in the set.
128 // All values have to be present in the set for the method to return true.
129 // Performance time complexity of n^2.
130 // Returns true if no arguments are passed at all, i.e. set is always super-set of empty set.
131 func (list *List[T]) Contains(values ...T) bool {
132
133 if len(values) == 0 {
134 return true
135 }
136 if list.size == 0 {
137 return false
138 }
139 for _, value := range values {
140 found := false
141 for element := list.first; element != nil; element = element.next {
142 if element.value == value {
143 found = true
144 break
145 }
146 }
147 if !found {
148 return false
149 }
150 }
151 return true
152 }
153
154 // Values returns all elements in the list.
155 func (list *List[T]) Values() []T {
156 values := make([]T, list.size, list.size)
157 for e, element := 0, list.first; element != nil; e, element = e+1, element.next {
158 values[e] = element.value
159 }
160 return values
161 }
162
163 // IndexOf returns index of provided element
164 func (list *List[T]) IndexOf(value T) int {
165 if list.size == 0 {
166 return -1
167 }
168 for index, element := range list.Values() {
169 if element == value {
170 return index
171 }
172 }
173 return -1
174 }
175
176 // Empty returns true if list does not contain any elements.
177 func (list *List[T]) Empty() bool {
178 return list.size == 0
179 }
180
181 // Size returns number of elements within the list.
182 func (list *List[T]) Size() int {
183 return list.size
184 }
185
186 // Clear removes all elements from the list.
187 func (list *List[T]) Clear() {
188 list.size = 0
189 list.first = nil
190 list.last = nil
191 }
192
193 // Sort sort values (in-place) using.
194 func (list *List[T]) Sort(comparator utils.Comparator[T]) {
195
196 if list.size < 2 {
197 return
198 }
199
200 values := list.Values()
201 slices.SortFunc(values, comparator)
202
203 list.Clear()
204
205 list.Add(values...)
206
207 }
208
209 // Swap swaps values of two elements at the given indices.
210 func (list *List[T]) Swap(i, j int) {
211 if list.withinRange(i) && list.withinRange(j) && i != j {
212 var element1, element2 *element[T]
213 for e, currentElement := 0, list.first; element1 == nil || element2 == nil; e, currentElement = e+1, currentElement.next {
214 switch e {
215 case i:
216 element1 = currentElement
217 case j:
218 element2 = currentElement
219 }
220 }
221 element1.value, element2.value = element2.value, element1.value
222 }
223 }
224
225 // Insert inserts values at specified index position shifting the value at that position (if any) and any subsequent elements to the right.
226 // Does not do anything if position is negative or bigger than list's size
227 // Note: position equal to list's size is valid, i.e. append.
228 func (list *List[T]) Insert(index int, values ...T) {
229
230 if !list.withinRange(index) {
231 // Append
232 if index == list.size {
233 list.Add(values...)
234 }
235 return
236 }
237
238 list.size += len(values)
239
240 var beforeElement *element[T]
241 foundElement := list.first
242 for e := 0; e != index; e, foundElement = e+1, foundElement.next {
243 beforeElement = foundElement
244 }
245
246 if foundElement == list.first {
247 oldNextElement := list.first
248 for i, value := range values {
249 newElement := &element[T]{value: value}
250 if i == 0 {
251 list.first = newElement
252 } else {
253 beforeElement.next = newElement
254 }
255 beforeElement = newElement
256 }
257 beforeElement.next = oldNextElement
258 } else {
259 oldNextElement := beforeElement.next
260 for _, value := range values {
261 newElement := &element[T]{value: value}
262 beforeElement.next = newElement
263 beforeElement = newElement
264 }
265 beforeElement.next = oldNextElement
266 }
267 }
268
269 // Set value at specified index
270 // Does not do anything if position is negative or bigger than list's size
271 // Note: position equal to list's size is valid, i.e. append.
272 func (list *List[T]) Set(index int, value T) {
273
274 if !list.withinRange(index) {
275 // Append
276 if index == list.size {
277 list.Add(value)
278 }
279 return
280 }
281
282 foundElement := list.first
283 for e := 0; e != index; {
284 e, foundElement = e+1, foundElement.next
285 }
286 foundElement.value = value
287 }
288
289 // String returns a string representation of container
290 func (list *List[T]) String() string {
291 str := "SinglyLinkedList\n"
292 values := []string{}
293 for element := list.first; element != nil; element = element.next {
294 values = append(values, fmt.Sprintf("%v", element.value))
295 }
296 str += strings.Join(values, ", ")
297 return str
298 }
299
300 // Check that the index is within bounds of the list
301 func (list *List[T]) withinRange(index int) bool {
302 return index >= 0 && index < list.size
303 }