sortedstrings.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package stringutils2
  15. import (
  16. "sort"
  17. )
  18. type SSortedStrings []string
  19. func NewSortedStrings(strs []string) SSortedStrings {
  20. if strs == nil {
  21. return SSortedStrings{}
  22. }
  23. sort.Strings(strs)
  24. return SSortedStrings(strs)
  25. }
  26. func Append(ss SSortedStrings, ele ...string) SSortedStrings {
  27. ss = ss.Append(ele...)
  28. return ss
  29. }
  30. func (ss SSortedStrings) Append(ele ...string) SSortedStrings {
  31. if ss == nil {
  32. ss = NewSortedStrings([]string{})
  33. }
  34. for _, e := range ele {
  35. pos, find := ss.Index(e)
  36. if find {
  37. continue
  38. }
  39. ss = append(ss, e)
  40. copy(ss[pos+1:], ss[pos:])
  41. ss[pos] = e
  42. }
  43. return ss
  44. }
  45. func (ss SSortedStrings) Remove(ele ...string) SSortedStrings {
  46. if ss == nil {
  47. return ss
  48. }
  49. for _, e := range ele {
  50. pos, find := ss.Index(e)
  51. if !find {
  52. continue
  53. }
  54. if pos < len(ss)-1 {
  55. copy(ss[pos:], ss[pos+1:])
  56. }
  57. ss = ss[:len(ss)-1]
  58. }
  59. return ss
  60. }
  61. func (ss SSortedStrings) Index(needle string) (int, bool) {
  62. i := 0
  63. j := len(ss) - 1
  64. for i <= j {
  65. m := (i + j) / 2
  66. if ss[m] < needle {
  67. i = m + 1
  68. } else if ss[m] > needle {
  69. j = m - 1
  70. } else {
  71. return m, true
  72. }
  73. }
  74. return j + 1, false
  75. }
  76. func (ss SSortedStrings) Contains(needle string) bool {
  77. _, find := ss.Index(needle)
  78. return find
  79. }
  80. func (ss SSortedStrings) ContainsAny(needles ...string) bool {
  81. for i := range needles {
  82. _, find := ss.Index(needles[i])
  83. if find {
  84. return true
  85. }
  86. }
  87. return false
  88. }
  89. func (ss SSortedStrings) ContainsAll(needles ...string) bool {
  90. for i := range needles {
  91. _, find := ss.Index(needles[i])
  92. if !find {
  93. return false
  94. }
  95. }
  96. return true
  97. }
  98. func Contains(a, b SSortedStrings) bool {
  99. _, _, bNoA := Split(a, b)
  100. if len(bNoA) == 0 {
  101. return true
  102. } else {
  103. return false
  104. }
  105. }
  106. func Equals(a, b SSortedStrings) bool {
  107. aNoB, _, bNoA := Split(a, b)
  108. if len(aNoB) == 0 && len(bNoA) == 0 {
  109. return true
  110. } else {
  111. return false
  112. }
  113. }
  114. func Split(a, b SSortedStrings) (aNoB SSortedStrings, aAndB SSortedStrings, bNoA SSortedStrings) {
  115. a_b := make([]string, 0)
  116. b_a := make([]string, 0)
  117. anb := make([]string, 0)
  118. i := 0
  119. j := 0
  120. for i < len(a) && j < len(b) {
  121. if a[i] == b[j] {
  122. anb = append(anb, a[i])
  123. i += 1
  124. j += 1
  125. } else if a[i] < b[j] {
  126. a_b = append(a_b, a[i])
  127. i += 1
  128. } else if a[i] > b[j] {
  129. b_a = append(b_a, b[j])
  130. j += 1
  131. }
  132. }
  133. if i < len(a) {
  134. a_b = append(a_b, a[i:]...)
  135. }
  136. if j < len(b) {
  137. b_a = append(b_a, b[j:]...)
  138. }
  139. aNoB = SSortedStrings(a_b)
  140. aAndB = SSortedStrings(anb)
  141. bNoA = SSortedStrings(b_a)
  142. return
  143. }
  144. func Merge(a, b SSortedStrings) SSortedStrings {
  145. ret := make([]string, 0)
  146. i := 0
  147. j := 0
  148. for i < len(a) && j < len(b) {
  149. if a[i] == b[j] {
  150. ret = append(ret, a[i])
  151. i += 1
  152. j += 1
  153. } else if a[i] < b[j] {
  154. ret = append(ret, a[i])
  155. i += 1
  156. } else if a[i] > b[j] {
  157. ret = append(ret, b[j])
  158. j += 1
  159. }
  160. }
  161. if i < len(a) {
  162. ret = append(ret, a[i:]...)
  163. }
  164. if j < len(b) {
  165. ret = append(ret, b[j:]...)
  166. }
  167. return SSortedStrings(ret)
  168. }
  169. func Intersect(a, b SSortedStrings) SSortedStrings {
  170. ret := make([]string, 0)
  171. i := 0
  172. j := 0
  173. for i < len(a) && j < len(b) {
  174. if a[i] == b[j] {
  175. ret = append(ret, a[i])
  176. i += 1
  177. j += 1
  178. } else if a[i] < b[j] {
  179. i += 1
  180. } else if a[i] > b[j] {
  181. j += 1
  182. }
  183. }
  184. return SSortedStrings(ret)
  185. }