| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 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 |
|
| 22 |
var _ lists.List[any] = (*List[any])(nil) |
| 23 |
|
| 24 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 62 |
func (list *List[T]) Append(values ...T) { |
| 63 |
list.Add(values...) |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
func (list *List[T]) Prepend(values ...T) { |
| 68 |
|
| 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 |
|
| 83 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 196 |
func (list *List[T]) Empty() bool { |
| 197 |
return list.size == 0 |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
func (list *List[T]) Size() int { |
| 202 |
return list.size |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
func (list *List[T]) Clear() { |
| 207 |
list.size = 0 |
| 208 |
list.first = nil |
| 209 |
list.last = nil |
| 210 |
} |
| 211 |
|
| 212 |
|
| 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 |
|
| 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 |
|
| 245 |
|
| 246 |
|
| 247 |
func (list *List[T]) Insert(index int, values ...T) { |
| 248 |
|
| 249 |
if !list.withinRange(index) { |
| 250 |
|
| 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 |
|
| 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 |
|
| 303 |
|
| 304 |
|
| 305 |
func (list *List[T]) Set(index int, value T) { |
| 306 |
|
| 307 |
if !list.withinRange(index) { |
| 308 |
|
| 309 |
if index == list.size { |
| 310 |
list.Add(value) |
| 311 |
} |
| 312 |
return |
| 313 |
} |
| 314 |
|
| 315 |
var foundElement *element[T] |
| 316 |
|
| 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 |
|
| 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 |
|
| 345 |
func (list *List[T]) withinRange(index int) bool { |
| 346 |
return index >= 0 && index < list.size |
| 347 |
} |