Debian Codegraph @

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