single.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. package lists
  2. import (
  3. "surdeus.su/core/gods"
  4. "sort"
  5. "fmt"
  6. )
  7. // Linked list X .
  8. // The package implements better variation of
  9. // linked list than in standard library since it uses
  10. // the new conception of generics.
  11. // The type represents singly linked list data structure.
  12. type sLinkedList[V any] struct {
  13. // First empty element (not used to store values).
  14. // For fast pushing.
  15. before *sElement[V]
  16. // Points to the last for fast appending.
  17. last *sElement[V]
  18. // Length.
  19. ln int
  20. }
  21. // The type represents element of the linked list.
  22. type sElement[V any] struct {
  23. next *sElement[V]
  24. value V
  25. }
  26. func newSingly[V any](values ...V) *sLinkedList[V] {
  27. ret := &sLinkedList[V]{
  28. before: &sElement[V]{},
  29. last: nil,
  30. ln: 0,
  31. }
  32. ret.Add(values...)
  33. return ret
  34. }
  35. // Returns new empty linked list storing the V type.
  36. func NewSingly[V any](values ...V) List[V] {
  37. return newSingly[V](values...)
  38. }
  39. func (ll *sLinkedList[V]) Empty() bool {
  40. return ll.ln == 0
  41. }
  42. func (ll *sLinkedList[V]) Size() int {
  43. return ll.ln
  44. }
  45. func (ll *sLinkedList[V]) Clear() {
  46. buf := newSingly[V]()
  47. *ll = *buf
  48. }
  49. func (ll *sLinkedList[V]) Len() int {
  50. return ll.ln
  51. }
  52. // Get the index-indexed element itself.
  53. func (ll *sLinkedList[V]) getEl(index int) *sElement[V] {
  54. if ll.ln <= index || index < 0 {
  55. panic(gods.IndexRangeErr)
  56. }
  57. p := ll.before
  58. for i := 0; i <= index; i++ {
  59. p = p.next
  60. }
  61. return p
  62. }
  63. // Get the value of index-indexed element.
  64. func (ll *sLinkedList[V]) Get(index int) V {
  65. return ll.getEl(index).value
  66. }
  67. // Set the new value in i-indexed element.
  68. func (ll *sLinkedList[V]) Set(i int, v V) {
  69. el := ll.getEl(i)
  70. el.value = v
  71. }
  72. // Insert the V value before the i-th element.
  73. func (ll *sLinkedList[V]) InsB(index int, values ...V) {
  74. if index == 0 {
  75. ll.Put(values...)
  76. return
  77. }
  78. el := ll.getEl(index - 1)
  79. for _, v := range values {
  80. el.next = &sElement[V]{
  81. value: v,
  82. next: el.next,
  83. }
  84. el = el.next
  85. }
  86. ll.ln += len(values)
  87. }
  88. func (ll *sLinkedList[V]) InsA(index int, values ...V) {
  89. if index == ll.ln-1 {
  90. ll.Add(values...)
  91. return
  92. }
  93. el := ll.getEl(index)
  94. for _, v := range values {
  95. el.next = &sElement[V]{
  96. value: v,
  97. next: el.next,
  98. }
  99. el = el.next
  100. }
  101. ll.ln += len(values)
  102. }
  103. func (ll *sLinkedList[V]) Swap(i1, i2 int) {
  104. if i1 == i2 {
  105. return
  106. }
  107. el1 := ll.getEl(i1)
  108. el2 := ll.getEl(i2)
  109. el1.value, el2.value =
  110. el2.value, el1.value
  111. }
  112. func (ll *sLinkedList[V]) Del(i int) {
  113. if i == 0 {
  114. ll.before.next =
  115. ll.before.next.next
  116. ll.ln--
  117. return
  118. }
  119. el1 := ll.getEl(i - 1)
  120. if i == ll.ln-1 {
  121. el1.next = nil
  122. } else {
  123. el2 := ll.getEl(i + 1)
  124. el1.next = el2
  125. }
  126. ll.ln--
  127. }
  128. func (ll *sLinkedList[V]) Put(values ...V) {
  129. ln := len(values)
  130. for i := ln - 1; i >= 0; i-- {
  131. ll.Push(values[i])
  132. }
  133. }
  134. func (ll *sLinkedList[V]) Pop() V {
  135. el := ll.before.next
  136. if el == nil {
  137. panic(gods.IndexRangeErr)
  138. }
  139. ll.before.next = el.next
  140. ll.ln--
  141. return el.value
  142. }
  143. func (ll *sLinkedList[V]) Push(v V) {
  144. prevNext := ll.before.next
  145. nextNext := &sElement[V]{
  146. next: prevNext,
  147. value: v,
  148. }
  149. ll.before.next = nextNext
  150. ll.ln++
  151. if ll.ln == 1 {
  152. ll.last = ll.before.next
  153. }
  154. }
  155. // Append to the end of the list.
  156. func (ll *sLinkedList[V]) Add(values ...V) {
  157. for _, value := range values {
  158. ll.gappend(value)
  159. }
  160. }
  161. func (ll *sLinkedList[V]) gappend(v V) {
  162. if ll.ln == 0 {
  163. ll.Push(v)
  164. return
  165. }
  166. last := &sElement[V]{
  167. next: nil,
  168. value: v,
  169. }
  170. lastBuf := ll.last
  171. lastBuf.next = last
  172. ll.last = last
  173. ll.ln++
  174. }
  175. // Returns the first element of the linked list.
  176. func (ll *sLinkedList[V]) First() *sElement[V] {
  177. return ll.before.next
  178. }
  179. // Get elements value.
  180. func (ll *sElement[V]) Value() V {
  181. return ll.value
  182. }
  183. // Returns the next element. If the returned value == nil,
  184. // then it is the last element.
  185. func (ll *sElement[V]) Next() *sElement[V] {
  186. return ll.next
  187. }
  188. // Returns the last element.
  189. func (ll *sLinkedList[V]) Last() *sElement[V] {
  190. return ll.last
  191. }
  192. // Returns a channel with values ordered as in list.
  193. func (ll *sLinkedList[V]) Chan() chan V {
  194. chn := make(chan V)
  195. go func() {
  196. el := ll.before
  197. for el.next != nil {
  198. el = el.next
  199. chn <- el.value
  200. }
  201. close(chn)
  202. }()
  203. return chn
  204. }
  205. // Returns slice of values in the list ordered as in the list.
  206. func (ll *sLinkedList[V]) Values() []V {
  207. buf := make([]V, ll.Len())
  208. i := 0
  209. el := ll.before
  210. for el.next != nil {
  211. el = el.next
  212. buf[i] = el.value
  213. i++
  214. }
  215. return buf
  216. }
  217. func (ll *sLinkedList[V]) String() string {
  218. return fmt.Sprintf("%v", ll.Values())
  219. }
  220. func (ll *sLinkedList[V]) Sort(fn gods.LessFunc[V]) {
  221. sort.Sort(gods.CustomSort[V]{
  222. CustomSorter: ll,
  223. LessFunc: fn,
  224. })
  225. }