1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
|
package collection
import (
"bytes"
"errors"
"fmt"
"strings"
"sync"
)
// LinkedList encapsulates a list where each entry is aware of only the next entry in the list.
type LinkedList struct {
first *llNode
last *llNode
length uint
key sync.RWMutex
}
type llNode struct {
payload interface{}
next *llNode
}
// Comparator is a function which evaluates two values to determine their relation to one another.
// - Zero is returned when `a` and `b` are equal.
// - Positive numbers are returned when `a` is greater than `b`.
// - Negative numbers are returned when `a` is less than `b`.
type Comparator func(a, b interface{}) (int, error)
// A collection of errors that may be thrown by functions in this file.
var (
ErrUnexpectedType = errors.New("value was of an unexpected type")
)
// NewLinkedList instantiates a new LinkedList with the entries provided.
func NewLinkedList(entries ...interface{}) *LinkedList {
list := &LinkedList{}
for _, entry := range entries {
list.AddBack(entry)
}
return list
}
// AddBack creates an entry in the LinkedList that is logically at the back of the list.
func (list *LinkedList) AddBack(entry interface{}) {
toAppend := &llNode{
payload: entry,
}
list.key.Lock()
defer list.key.Unlock()
list.length++
if list.first == nil {
list.first = toAppend
list.last = toAppend
return
}
list.last.next = toAppend
list.last = toAppend
}
// AddFront creates an entry in the LinkedList that is logically at the front of the list.
func (list *LinkedList) AddFront(entry interface{}) {
toAppend := &llNode{
payload: entry,
}
list.key.Lock()
defer list.key.Unlock()
list.length++
toAppend.next = list.first
if list.first == nil {
list.last = toAppend
}
list.first = toAppend
}
// Enumerate creates a new instance of Enumerable which can be executed on.
func (list *LinkedList) Enumerate(cancel <-chan struct{}) Enumerator {
retval := make(chan interface{})
go func() {
list.key.RLock()
defer list.key.RUnlock()
defer close(retval)
current := list.first
for current != nil {
select {
case retval <- current.payload:
break
case <-cancel:
return
}
current = current.next
}
}()
return retval
}
// Get finds the value from the LinkedList.
// pos is expressed as a zero-based index begining from the 'front' of the list.
func (list *LinkedList) Get(pos uint) (interface{}, bool) {
list.key.RLock()
defer list.key.RUnlock()
node, ok := get(list.first, pos)
if ok {
return node.payload, true
}
return nil, false
}
// IsEmpty tests the list to determine if it is populate or not.
func (list *LinkedList) IsEmpty() bool {
list.key.RLock()
defer list.key.RUnlock()
return list.first == nil
}
// Length returns the number of elements present in the LinkedList.
func (list *LinkedList) Length() uint {
list.key.RLock()
defer list.key.RUnlock()
return list.length
}
// PeekBack returns the entry logicall stored at the back of the list without removing it.
func (list *LinkedList) PeekBack() (interface{}, bool) {
list.key.RLock()
defer list.key.RUnlock()
if list.last == nil {
return nil, false
}
return list.last.payload, true
}
// PeekFront returns the entry logically stored at the front of this list without removing it.
func (list *LinkedList) PeekFront() (interface{}, bool) {
list.key.RLock()
defer list.key.RUnlock()
if list.first == nil {
return nil, false
}
return list.first.payload, true
}
// RemoveFront returns the entry logically stored at the front of this list and removes it.
func (list *LinkedList) RemoveFront() (interface{}, bool) {
list.key.Lock()
defer list.key.Unlock()
if list.first == nil {
return nil, false
}
retval := list.first.payload
list.first = list.first.next
list.length--
if 0 == list.length {
list.last = nil
}
return retval, true
}
// RemoveBack returns the entry logically stored at the back of this list and removes it.
func (list *LinkedList) RemoveBack() (interface{}, bool) {
list.key.Lock()
defer list.key.Unlock()
if list.last == nil {
return nil, false
}
retval := list.last.payload
list.length--
if list.length == 0 {
list.first = nil
} else {
node, _ := get(list.first, list.length-1)
node.next = nil
}
return retval, true
}
// Sort rearranges the positions of the entries in this list so that they are
// ascending.
func (list *LinkedList) Sort(comparator Comparator) error {
list.key.Lock()
defer list.key.Unlock()
var err error
list.first, err = mergeSort(list.first, comparator)
if err != nil {
return err
}
list.last = findLast(list.first)
return err
}
// Sorta rearranges the position of string entries in this list so that they
// are ascending.
func (list *LinkedList) Sorta() error {
list.key.Lock()
defer list.key.Unlock()
var err error
list.first, err = mergeSort(list.first, func(a, b interface{}) (int, error) {
castA, ok := a.(string)
if !ok {
return 0, ErrUnexpectedType
}
castB, ok := b.(string)
if !ok {
return 0, ErrUnexpectedType
}
return strings.Compare(castA, castB), nil
})
list.last = findLast(list.first)
return err
}
// Sorti rearranges the position of integer entries in this list so that they
// are ascending.
func (list *LinkedList) Sorti() (err error) {
list.key.Lock()
defer list.key.Unlock()
list.first, err = mergeSort(list.first, func(a, b interface{}) (int, error) {
castA, ok := a.(int)
if !ok {
return 0, ErrUnexpectedType
}
castB, ok := b.(int)
if !ok {
return 0, ErrUnexpectedType
}
return castA - castB, nil
})
if err != nil {
return
}
list.last = findLast(list.first)
return
}
// String prints upto the first fifteen elements of the list in string format.
func (list *LinkedList) String() string {
list.key.RLock()
defer list.key.RUnlock()
builder := bytes.NewBufferString("[")
current := list.first
for i := 0; i < 15 && current != nil; i++ {
builder.WriteString(fmt.Sprintf("%v ", current.payload))
current = current.next
}
if current == nil || current.next == nil {
builder.Truncate(builder.Len() - 1)
} else {
builder.WriteString("...")
}
builder.WriteRune(']')
return builder.String()
}
// Swap switches the positions in which two values are stored in this list.
// x and y represent the indexes of the items that should be swapped.
func (list *LinkedList) Swap(x, y uint) error {
list.key.Lock()
defer list.key.Unlock()
var xNode, yNode *llNode
if temp, ok := get(list.first, x); ok {
xNode = temp
} else {
return fmt.Errorf("index out of bounds 'x', wanted less than %d got %d", list.length, x)
}
if temp, ok := get(list.first, y); ok {
yNode = temp
} else {
return fmt.Errorf("index out of bounds 'y', wanted less than %d got %d", list.length, y)
}
temp := xNode.payload
xNode.payload = yNode.payload
yNode.payload = temp
return nil
}
// ToSlice converts the contents of the LinkedList into a slice.
func (list *LinkedList) ToSlice() []interface{} {
return list.Enumerate(nil).ToSlice()
}
func findLast(head *llNode) *llNode {
if head == nil {
return nil
}
current := head
for current.next != nil {
current = current.next
}
return current
}
func get(head *llNode, pos uint) (*llNode, bool) {
for i := uint(0); i < pos; i++ {
if head == nil {
return nil, false
}
head = head.next
}
return head, true
}
// merge takes two sorted lists and merges them into one sorted list.
// Behavior is undefined when you pass a non-sorted list as `left` or `right`
func merge(left, right *llNode, comparator Comparator) (first *llNode, err error) {
curLeft := left
curRight := right
var last *llNode
appendResults := func(updated *llNode) {
if last == nil {
last = updated
} else {
last.next = updated
last = last.next
}
if first == nil {
first = last
}
}
for curLeft != nil && curRight != nil {
var res int
if res, err = comparator(curLeft.payload, curRight.payload); nil != err {
break // Don't return, stitch the remaining elements back on.
} else if res < 0 {
appendResults(curLeft)
curLeft = curLeft.next
} else {
appendResults(curRight)
curRight = curRight.next
}
}
if curLeft != nil {
appendResults(curLeft)
}
if curRight != nil {
appendResults(curRight)
}
return
}
func mergeSort(head *llNode, comparator Comparator) (*llNode, error) {
if head == nil {
return nil, nil
}
left, right := split(head)
repair := func(left, right *llNode) *llNode {
lastLeft := findLast(left)
lastLeft.next = right
return left
}
var err error
if left != nil && left.next != nil {
left, err = mergeSort(left, comparator)
if err != nil {
return repair(left, right), err
}
}
if right != nil && right.next != nil {
right, err = mergeSort(right, comparator)
if err != nil {
return repair(left, right), err
}
}
return merge(left, right, comparator)
}
// split breaks a list in half.
func split(head *llNode) (left, right *llNode) {
left = head
if head == nil || head.next == nil {
return
}
right = head
sprinter := head
prev := head
for sprinter != nil && sprinter.next != nil {
prev = right
right = right.next
sprinter = sprinter.next.next
}
prev.next = nil
return
}
|