本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

229 lines
5.8KB

  1. // Copyright (c) 2015, Emir Pasic. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package arraylist implements the array list.
  5. //
  6. // Structure is not thread safe.
  7. //
  8. // Reference: https://en.wikipedia.org/wiki/List_%28abstract_data_type%29
  9. package arraylist
  10. import (
  11. "fmt"
  12. "strings"
  13. "github.com/emirpasic/gods/lists"
  14. "github.com/emirpasic/gods/utils"
  15. )
  16. func assertListImplementation() {
  17. var _ lists.List = (*List)(nil)
  18. }
  19. // List holds the elements in a slice
  20. type List struct {
  21. elements []interface{}
  22. size int
  23. }
  24. const (
  25. growthFactor = float32(2.0) // growth by 100%
  26. shrinkFactor = float32(0.25) // shrink when size is 25% of capacity (0 means never shrink)
  27. )
  28. // New instantiates a new list and adds the passed values, if any, to the list
  29. func New(values ...interface{}) *List {
  30. list := &List{}
  31. if len(values) > 0 {
  32. list.Add(values...)
  33. }
  34. return list
  35. }
  36. // Add appends a value at the end of the list
  37. func (list *List) Add(values ...interface{}) {
  38. list.growBy(len(values))
  39. for _, value := range values {
  40. list.elements[list.size] = value
  41. list.size++
  42. }
  43. }
  44. // Get returns the element at index.
  45. // Second return parameter is true if index is within bounds of the array and array is not empty, otherwise false.
  46. func (list *List) Get(index int) (interface{}, bool) {
  47. if !list.withinRange(index) {
  48. return nil, false
  49. }
  50. return list.elements[index], true
  51. }
  52. // Remove removes the element at the given index from the list.
  53. func (list *List) Remove(index int) {
  54. if !list.withinRange(index) {
  55. return
  56. }
  57. list.elements[index] = nil // cleanup reference
  58. copy(list.elements[index:], list.elements[index+1:list.size]) // shift to the left by one (slow operation, need ways to optimize this)
  59. list.size--
  60. list.shrink()
  61. }
  62. // Contains checks if elements (one or more) are present in the set.
  63. // All elements have to be present in the set for the method to return true.
  64. // Performance time complexity of n^2.
  65. // Returns true if no arguments are passed at all, i.e. set is always super-set of empty set.
  66. func (list *List) Contains(values ...interface{}) bool {
  67. for _, searchValue := range values {
  68. found := false
  69. for _, element := range list.elements {
  70. if element == searchValue {
  71. found = true
  72. break
  73. }
  74. }
  75. if !found {
  76. return false
  77. }
  78. }
  79. return true
  80. }
  81. // Values returns all elements in the list.
  82. func (list *List) Values() []interface{} {
  83. newElements := make([]interface{}, list.size, list.size)
  84. copy(newElements, list.elements[:list.size])
  85. return newElements
  86. }
  87. //IndexOf returns index of provided element
  88. func (list *List) IndexOf(value interface{}) int {
  89. if list.size == 0 {
  90. return -1
  91. }
  92. for index, element := range list.elements {
  93. if element == value {
  94. return index
  95. }
  96. }
  97. return -1
  98. }
  99. // Empty returns true if list does not contain any elements.
  100. func (list *List) Empty() bool {
  101. return list.size == 0
  102. }
  103. // Size returns number of elements within the list.
  104. func (list *List) Size() int {
  105. return list.size
  106. }
  107. // Clear removes all elements from the list.
  108. func (list *List) Clear() {
  109. list.size = 0
  110. list.elements = []interface{}{}
  111. }
  112. // Sort sorts values (in-place) using.
  113. func (list *List) Sort(comparator utils.Comparator) {
  114. if len(list.elements) < 2 {
  115. return
  116. }
  117. utils.Sort(list.elements[:list.size], comparator)
  118. }
  119. // Swap swaps the two values at the specified positions.
  120. func (list *List) Swap(i, j int) {
  121. if list.withinRange(i) && list.withinRange(j) {
  122. list.elements[i], list.elements[j] = list.elements[j], list.elements[i]
  123. }
  124. }
  125. // Insert inserts values at specified index position shifting the value at that position (if any) and any subsequent elements to the right.
  126. // Does not do anything if position is negative or bigger than list's size
  127. // Note: position equal to list's size is valid, i.e. append.
  128. func (list *List) Insert(index int, values ...interface{}) {
  129. if !list.withinRange(index) {
  130. // Append
  131. if index == list.size {
  132. list.Add(values...)
  133. }
  134. return
  135. }
  136. l := len(values)
  137. list.growBy(l)
  138. list.size += l
  139. copy(list.elements[index+l:], list.elements[index:list.size-l])
  140. copy(list.elements[index:], values)
  141. }
  142. // Set the value at specified index
  143. // Does not do anything if position is negative or bigger than list's size
  144. // Note: position equal to list's size is valid, i.e. append.
  145. func (list *List) Set(index int, value interface{}) {
  146. if !list.withinRange(index) {
  147. // Append
  148. if index == list.size {
  149. list.Add(value)
  150. }
  151. return
  152. }
  153. list.elements[index] = value
  154. }
  155. // String returns a string representation of container
  156. func (list *List) String() string {
  157. str := "ArrayList\n"
  158. values := []string{}
  159. for _, value := range list.elements[:list.size] {
  160. values = append(values, fmt.Sprintf("%v", value))
  161. }
  162. str += strings.Join(values, ", ")
  163. return str
  164. }
  165. // Check that the index is within bounds of the list
  166. func (list *List) withinRange(index int) bool {
  167. return index >= 0 && index < list.size
  168. }
  169. func (list *List) resize(cap int) {
  170. newElements := make([]interface{}, cap, cap)
  171. copy(newElements, list.elements)
  172. list.elements = newElements
  173. }
  174. // Expand the array if necessary, i.e. capacity will be reached if we add n elements
  175. func (list *List) growBy(n int) {
  176. // When capacity is reached, grow by a factor of growthFactor and add number of elements
  177. currentCapacity := cap(list.elements)
  178. if list.size+n >= currentCapacity {
  179. newCapacity := int(growthFactor * float32(currentCapacity+n))
  180. list.resize(newCapacity)
  181. }
  182. }
  183. // Shrink the array if necessary, i.e. when size is shrinkFactor percent of current capacity
  184. func (list *List) shrink() {
  185. if shrinkFactor == 0.0 {
  186. return
  187. }
  188. // Shrink when size is at shrinkFactor * capacity
  189. currentCapacity := cap(list.elements)
  190. if list.size <= int(float32(currentCapacity)*shrinkFactor) {
  191. list.resize(list.size)
  192. }
  193. }
上海开阖软件有限公司 沪ICP备12045867号-1