gitee.com/sy_183/go-common@v1.0.5-0.20231205030221-958cfe129b47/container/linked-list.go (about) 1 package container 2 3 type LinkedListEntry[E any] struct { 4 elem E 5 next *LinkedListEntry[E] 6 prev *LinkedListEntry[E] 7 } 8 9 func NewLinkedListEntry[E any](e E) *LinkedListEntry[E] { 10 return &LinkedListEntry[E]{elem: e} 11 } 12 13 func (e *LinkedListEntry[E]) Value() E { 14 return e.elem 15 } 16 17 func (e *LinkedListEntry[E]) Set(elem E) { 18 e.elem = elem 19 } 20 21 func (e *LinkedListEntry[E]) Next() *LinkedListEntry[E] { 22 if e.next == nil || e.next.next == nil { 23 return nil 24 } 25 return e.next 26 } 27 28 func (e *LinkedListEntry[E]) Prev() *LinkedListEntry[E] { 29 if e.prev == nil || e.prev.prev == nil { 30 return nil 31 } 32 return e.prev 33 } 34 35 func (e *LinkedListEntry[E]) Removed() bool { 36 return e.next == nil && e.prev == nil 37 } 38 39 type LinkedList[E any] struct { 40 entries []*LinkedListEntry[E] 41 head *LinkedListEntry[E] 42 tail *LinkedListEntry[E] 43 } 44 45 func NewLinkedList[E any](entries int) *LinkedList[E] { 46 l := &LinkedList[E]{ 47 entries: make([]*LinkedListEntry[E], entries), 48 head: new(LinkedListEntry[E]), 49 tail: new(LinkedListEntry[E]), 50 } 51 l.init() 52 return l 53 } 54 55 func (l *LinkedList[E]) init() { 56 rawEntries := make([]LinkedListEntry[E], len(l.entries)) 57 for i := range rawEntries { 58 l.entries[i] = &rawEntries[i] 59 } 60 l.entries = l.entries[:0] 61 l.head.next = l.tail 62 l.tail.prev = l.head 63 } 64 65 func (l *LinkedList[E]) Init(entries int) { 66 *l = LinkedList[E]{ 67 entries: make([]*LinkedListEntry[E], entries), 68 head: new(LinkedListEntry[E]), 69 tail: new(LinkedListEntry[E]), 70 } 71 l.init() 72 } 73 74 func (l *LinkedList[E]) IsNil() bool { 75 return l.head == nil 76 } 77 78 func (l *LinkedList[E]) Len() int { 79 return len(l.entries) 80 } 81 82 func (l *LinkedList[E]) Empty() bool { 83 return len(l.entries) == 0 84 } 85 86 func (l *LinkedList[E]) Head() (e E, exist bool) { 87 return l.head.next.elem, len(l.entries) != 0 88 } 89 90 func (l *LinkedList[E]) HeadEntry() *LinkedListEntry[E] { 91 if len(l.entries) == 0 { 92 return nil 93 } 94 return l.head.next 95 } 96 97 func (l *LinkedList[E]) Tail() (e E, exist bool) { 98 return l.tail.prev.elem, len(l.entries) != 0 99 } 100 101 func (l *LinkedList[E]) TailEntry() *LinkedListEntry[E] { 102 if len(l.entries) == 0 { 103 return nil 104 } 105 return l.tail.prev 106 } 107 108 func (l *LinkedList[E]) Range(f func(elem E) bool) bool { 109 var next *LinkedListEntry[E] 110 for cur := l.head.next; cur.next != nil; cur = next { 111 next = cur.next 112 if !f(cur.elem) { 113 return false 114 } 115 } 116 return true 117 } 118 119 func (l *LinkedList[E]) RangeEntries(f func(e *LinkedListEntry[E]) bool) bool { 120 var next *LinkedListEntry[E] 121 for cur := l.head.next; cur.next != nil; cur = next { 122 next = cur.next 123 if !f(cur) { 124 return false 125 } 126 } 127 return true 128 } 129 130 func (l *LinkedList[E]) Elems() (elems []E) { 131 for cur := l.head.next; cur.next != nil; cur = cur.next { 132 elems = append(elems, cur.elem) 133 } 134 return 135 } 136 137 func (l *LinkedList[E]) grow() int { 138 el := len(l.entries) 139 if el == cap(l.entries) { 140 l.entries = append(l.entries, nil) 141 l.entries = l.entries[:cap(l.entries)] 142 rawEntries := make([]LinkedListEntry[E], cap(l.entries)-el) 143 for i := range rawEntries { 144 l.entries[i+el] = &rawEntries[i] 145 } 146 l.entries = l.entries[:el] 147 } 148 return el 149 } 150 151 func (l *LinkedList[E]) newEntry(elem E) (e *LinkedListEntry[E]) { 152 el := l.grow() 153 l.entries = l.entries[:el+1] 154 e = l.entries[el] 155 e.elem = elem 156 return l.entries[el] 157 } 158 159 func (l *LinkedList[E]) freeEntry(e *LinkedListEntry[E]) { 160 el := len(l.entries) 161 l.entries[el-1] = e 162 l.entries = l.entries[:el-1] 163 } 164 165 func (l *LinkedList[E]) addLink(e, p *LinkedListEntry[E]) { 166 e.prev = p 167 e.next = p.next 168 p.next.prev = e 169 p.next = e 170 } 171 172 func (l *LinkedList[E]) removeLink(e *LinkedListEntry[E]) { 173 e.prev.next = e.next 174 e.next.prev = e.prev 175 e.next, e.prev = nil, nil 176 } 177 178 func (l *LinkedList[E]) insert(e, p *LinkedListEntry[E]) { 179 l.addLink(e, p) 180 } 181 182 func (l *LinkedList[E]) remove(e *LinkedListEntry[E]) { 183 l.removeLink(e) 184 l.freeEntry(e) 185 } 186 187 func (l *LinkedList[E]) Push(e E) { 188 l.insert(l.newEntry(e), l.tail.prev) 189 } 190 191 func (l *LinkedList[E]) Unshift(e E) { 192 l.insert(l.newEntry(e), l.head) 193 } 194 195 func (l *LinkedList[E]) InsertBack(e E, ref *LinkedListEntry[E]) { 196 l.insert(l.newEntry(e), ref) 197 } 198 199 func (l *LinkedList[E]) InsertFront(e E, ref *LinkedListEntry[E]) { 200 l.insert(l.newEntry(e), ref.prev) 201 } 202 203 func (l *LinkedList[E]) RemoveEntry(e *LinkedListEntry[E]) bool { 204 if e.Removed() { 205 return false 206 } 207 l.remove(e) 208 return true 209 } 210 211 func (l *LinkedList[E]) Shift() (e E, exist bool) { 212 if len(l.entries) == 0 { 213 return 214 } 215 entry := l.head.next 216 e, exist = entry.elem, true 217 l.remove(entry) 218 return 219 } 220 221 func (l *LinkedList[E]) Pop() (e E, exist bool) { 222 if len(l.entries) == 0 { 223 return 224 } 225 entry := l.tail.prev 226 e, exist = entry.elem, true 227 l.remove(entry) 228 return 229 } 230 231 func (l *LinkedList[E]) Clear() { 232 for _, e := range l.entries { 233 e.next, e.prev = nil, nil 234 } 235 l.entries = l.entries[:0] 236 l.head.next = l.tail 237 l.tail.prev = l.head 238 }