list.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. package concurrent
  2. import (
  3. "sync"
  4. )
  5. func initElem(v interface{}) *Element {
  6. return &Element{
  7. m: new(sync.RWMutex),
  8. val: v,
  9. }
  10. }
  11. type Element struct {
  12. m *sync.RWMutex
  13. prev *Element
  14. next *Element
  15. val interface{}
  16. }
  17. func (s *Element) SetVal(val interface{}) {
  18. s.m.Lock()
  19. s.val = val
  20. s.m.Unlock()
  21. }
  22. func (s *Element) Prev() (elem *Element) {
  23. s.m.RLock()
  24. elem = s.prev
  25. s.m.RUnlock()
  26. return
  27. }
  28. func (s *Element) Next() (elem *Element) {
  29. s.m.RLock()
  30. elem = s.next
  31. s.m.RUnlock()
  32. return
  33. }
  34. func (s *Element) Val() (val interface{}) {
  35. s.m.RLock()
  36. val = s.val
  37. s.m.RUnlock()
  38. return
  39. }
  40. func (s *Element) destroy() {
  41. s.m.Lock()
  42. if s.prev != nil {
  43. s.prev.next = s.next
  44. }
  45. if s.next != nil {
  46. s.next.prev = s.prev
  47. }
  48. s.prev, s.next = nil, nil
  49. s.m.Unlock()
  50. }
  51. func (s *Element) setNext(elem *Element) {
  52. s.m.Lock()
  53. if s.next != nil {
  54. elem.next, s.next.prev = s.next, elem
  55. }
  56. s.next, elem.prev = elem, s
  57. s.m.Unlock()
  58. }
  59. func (s *Element) setPrev(elem *Element) {
  60. s.m.Lock()
  61. if s.prev != nil {
  62. elem.prev, s.prev.next = s.prev, elem
  63. }
  64. s.prev, elem.next = elem, s
  65. s.m.Unlock()
  66. }
  67. func ListFromSlise(sl []interface{}) *List {
  68. l := NewList()
  69. for _, val := range sl {
  70. l.PushBack(val)
  71. }
  72. return l
  73. }
  74. func NewList() *List {
  75. return &List{
  76. m: new(sync.RWMutex),
  77. }
  78. }
  79. type List struct {
  80. m *sync.RWMutex
  81. first *Element
  82. last *Element
  83. size int
  84. }
  85. func (s *List) PushBackIfNotExists(v interface{}) (elem *Element, added bool) {
  86. s.m.Lock()
  87. defer s.m.Unlock()
  88. if s.search(v) == nil {
  89. added = true
  90. elem = s.pushBack(v)
  91. }
  92. return
  93. }
  94. func (s *List) PushBack(v interface{}) (elem *Element) {
  95. s.m.Lock()
  96. elem = s.pushBack(v)
  97. s.m.Unlock()
  98. return
  99. }
  100. func (s *List) pushBack(v interface{}) *Element {
  101. elem := initElem(v)
  102. if s.first == nil {
  103. s.first, s.last = elem, elem
  104. } else {
  105. s.last.setNext(elem)
  106. s.last = elem
  107. }
  108. s.size++
  109. return elem
  110. }
  111. func (s *List) PushFront(v interface{}) (elem *Element) {
  112. s.m.Lock()
  113. elem = s.pushFront(v)
  114. s.m.Unlock()
  115. return
  116. }
  117. func (s *List) PushFrontIfNotExists(v interface{}) (elem *Element, added bool) {
  118. s.m.Lock()
  119. defer s.m.Unlock()
  120. if s.search(v) == nil {
  121. added = true
  122. elem = s.pushFront(v)
  123. }
  124. return
  125. }
  126. func (s *List) pushFront(v interface{}) *Element {
  127. elem := initElem(v)
  128. if s.first == nil {
  129. s.first, s.last = elem, elem
  130. } else {
  131. s.first.setPrev(elem)
  132. s.first = elem
  133. }
  134. s.size++
  135. return elem
  136. }
  137. func (s *List) Remove(elem *Element) {
  138. if elem == nil {
  139. return
  140. }
  141. s.m.Lock()
  142. if elem == s.first {
  143. s.first = elem.next
  144. }
  145. if elem == s.last {
  146. s.last = elem.prev
  147. }
  148. elem.destroy()
  149. s.size--
  150. s.m.Unlock()
  151. }
  152. func (s *List) Size() (size int) {
  153. s.m.RLock()
  154. size = s.size
  155. s.m.RUnlock()
  156. return
  157. }
  158. func (s *List) First() (elem *Element) {
  159. s.m.RLock()
  160. elem = s.first
  161. s.m.RUnlock()
  162. return
  163. }
  164. func (s *List) Last() (elem *Element) {
  165. s.m.RLock()
  166. elem = s.last
  167. s.m.RUnlock()
  168. return
  169. }
  170. func (s *List) Index(index int) (elem *Element) {
  171. i := 0
  172. elem = s.First()
  173. for elem != nil && i < index {
  174. elem, i = elem.Next(), i+1
  175. }
  176. return
  177. }
  178. func (s *List) Slice() []interface{} {
  179. s.m.RLock()
  180. f, res := s.first, make([]interface{}, 0, s.size)
  181. s.m.RUnlock()
  182. for f != nil {
  183. res, f = append(res, f.Val()), f.Next()
  184. }
  185. return res
  186. }
  187. func (s *List) search(val interface{}) *Element {
  188. for f := s.first; f != nil; f = f.Next() {
  189. if f.Val() == val {
  190. return f
  191. }
  192. }
  193. return nil
  194. }
  195. func (s *List) Search(val interface{}) *Element {
  196. for f := s.First(); f != nil; f = f.Next() {
  197. if f.Val() == val {
  198. return f
  199. }
  200. }
  201. return nil
  202. }
  203. func (s *List) Each(call func(*Element) bool) {
  204. for f := s.First(); f != nil; f = f.Next() {
  205. if !call(f) {
  206. return
  207. }
  208. }
  209. }