set.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package fieldpath
  14. import (
  15. "sort"
  16. "strings"
  17. )
  18. // Set identifies a set of fields.
  19. type Set struct {
  20. // Members lists fields that are part of the set.
  21. // TODO: will be serialized as a list of path elements.
  22. Members PathElementSet
  23. // Children lists child fields which themselves have children that are
  24. // members of the set. Appearance in this list does not imply membership.
  25. // Note: this is a tree, not an arbitrary graph.
  26. Children SetNodeMap
  27. }
  28. // NewSet makes a set from a list of paths.
  29. func NewSet(paths ...Path) *Set {
  30. s := &Set{}
  31. for _, p := range paths {
  32. s.Insert(p)
  33. }
  34. return s
  35. }
  36. // Insert adds the field identified by `p` to the set. Important: parent fields
  37. // are NOT added to the set; if that is desired, they must be added separately.
  38. func (s *Set) Insert(p Path) {
  39. if len(p) == 0 {
  40. // Zero-length path identifies the entire object; we don't
  41. // track top-level ownership.
  42. return
  43. }
  44. for {
  45. if len(p) == 1 {
  46. s.Members.Insert(p[0])
  47. return
  48. }
  49. s = s.Children.Descend(p[0])
  50. p = p[1:]
  51. }
  52. }
  53. // Union returns a Set containing elements which appear in either s or s2.
  54. func (s *Set) Union(s2 *Set) *Set {
  55. return &Set{
  56. Members: *s.Members.Union(&s2.Members),
  57. Children: *s.Children.Union(&s2.Children),
  58. }
  59. }
  60. // Intersection returns a Set containing leaf elements which appear in both s
  61. // and s2. Intersection can be constructed from Union and Difference operations
  62. // (example in the tests) but it's much faster to do it in one pass.
  63. func (s *Set) Intersection(s2 *Set) *Set {
  64. return &Set{
  65. Members: *s.Members.Intersection(&s2.Members),
  66. Children: *s.Children.Intersection(&s2.Children),
  67. }
  68. }
  69. // Difference returns a Set containing elements which:
  70. // * appear in s
  71. // * do not appear in s2
  72. //
  73. // In other words, for leaf fields, this acts like a regular set difference
  74. // operation. When non leaf fields are compared with leaf fields ("parents"
  75. // which contain "children"), the effect is:
  76. // * parent - child = parent
  77. // * child - parent = {empty set}
  78. func (s *Set) Difference(s2 *Set) *Set {
  79. return &Set{
  80. Members: *s.Members.Difference(&s2.Members),
  81. Children: *s.Children.Difference(s2),
  82. }
  83. }
  84. // Size returns the number of members of the set.
  85. func (s *Set) Size() int {
  86. return s.Members.Size() + s.Children.Size()
  87. }
  88. // Empty returns true if there are no members of the set. It is a separate
  89. // function from Size since it's common to check whether size > 0, and
  90. // potentially much faster to return as soon as a single element is found.
  91. func (s *Set) Empty() bool {
  92. if s.Members.Size() > 0 {
  93. return false
  94. }
  95. return s.Children.Empty()
  96. }
  97. // Has returns true if the field referenced by `p` is a member of the set.
  98. func (s *Set) Has(p Path) bool {
  99. if len(p) == 0 {
  100. // No one owns "the entire object"
  101. return false
  102. }
  103. for {
  104. if len(p) == 1 {
  105. return s.Members.Has(p[0])
  106. }
  107. var ok bool
  108. s, ok = s.Children.Get(p[0])
  109. if !ok {
  110. return false
  111. }
  112. p = p[1:]
  113. }
  114. }
  115. // Equals returns true if s and s2 have exactly the same members.
  116. func (s *Set) Equals(s2 *Set) bool {
  117. return s.Members.Equals(&s2.Members) && s.Children.Equals(&s2.Children)
  118. }
  119. // String returns the set one element per line.
  120. func (s *Set) String() string {
  121. elements := []string{}
  122. s.Iterate(func(p Path) {
  123. elements = append(elements, p.String())
  124. })
  125. return strings.Join(elements, "\n")
  126. }
  127. // Iterate calls f once for each field that is a member of the set (preorder
  128. // DFS). The path passed to f will be reused so make a copy if you wish to keep
  129. // it.
  130. func (s *Set) Iterate(f func(Path)) {
  131. s.iteratePrefix(Path{}, f)
  132. }
  133. func (s *Set) iteratePrefix(prefix Path, f func(Path)) {
  134. s.Members.Iterate(func(pe PathElement) { f(append(prefix, pe)) })
  135. s.Children.iteratePrefix(prefix, f)
  136. }
  137. // WithPrefix returns the subset of paths which begin with the given prefix,
  138. // with the prefix not included.
  139. func (s *Set) WithPrefix(pe PathElement) *Set {
  140. subset, ok := s.Children.Get(pe)
  141. if !ok {
  142. return NewSet()
  143. }
  144. return subset
  145. }
  146. // setNode is a pair of PathElement / Set, for the purpose of expressing
  147. // nested set membership.
  148. type setNode struct {
  149. pathElement PathElement
  150. set *Set
  151. }
  152. // SetNodeMap is a map of PathElement to subset.
  153. type SetNodeMap struct {
  154. members sortedSetNode
  155. }
  156. type sortedSetNode []setNode
  157. // Implement the sort interface; this would permit bulk creation, which would
  158. // be faster than doing it one at a time via Insert.
  159. func (s sortedSetNode) Len() int { return len(s) }
  160. func (s sortedSetNode) Less(i, j int) bool { return s[i].pathElement.Less(s[j].pathElement) }
  161. func (s sortedSetNode) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  162. // Descend adds pe to the set if necessary, returning the associated subset.
  163. func (s *SetNodeMap) Descend(pe PathElement) *Set {
  164. loc := sort.Search(len(s.members), func(i int) bool {
  165. return !s.members[i].pathElement.Less(pe)
  166. })
  167. if loc == len(s.members) {
  168. s.members = append(s.members, setNode{pathElement: pe, set: &Set{}})
  169. return s.members[loc].set
  170. }
  171. if s.members[loc].pathElement.Equals(pe) {
  172. return s.members[loc].set
  173. }
  174. s.members = append(s.members, setNode{})
  175. copy(s.members[loc+1:], s.members[loc:])
  176. s.members[loc] = setNode{pathElement: pe, set: &Set{}}
  177. return s.members[loc].set
  178. }
  179. // Size returns the sum of the number of members of all subsets.
  180. func (s *SetNodeMap) Size() int {
  181. count := 0
  182. for _, v := range s.members {
  183. count += v.set.Size()
  184. }
  185. return count
  186. }
  187. // Empty returns false if there's at least one member in some child set.
  188. func (s *SetNodeMap) Empty() bool {
  189. for _, n := range s.members {
  190. if !n.set.Empty() {
  191. return false
  192. }
  193. }
  194. return true
  195. }
  196. // Get returns (the associated set, true) or (nil, false) if there is none.
  197. func (s *SetNodeMap) Get(pe PathElement) (*Set, bool) {
  198. loc := sort.Search(len(s.members), func(i int) bool {
  199. return !s.members[i].pathElement.Less(pe)
  200. })
  201. if loc == len(s.members) {
  202. return nil, false
  203. }
  204. if s.members[loc].pathElement.Equals(pe) {
  205. return s.members[loc].set, true
  206. }
  207. return nil, false
  208. }
  209. // Equals returns true if s and s2 have the same structure (same nested
  210. // child sets).
  211. func (s *SetNodeMap) Equals(s2 *SetNodeMap) bool {
  212. if len(s.members) != len(s2.members) {
  213. return false
  214. }
  215. for i := range s.members {
  216. if !s.members[i].pathElement.Equals(s2.members[i].pathElement) {
  217. return false
  218. }
  219. if !s.members[i].set.Equals(s2.members[i].set) {
  220. return false
  221. }
  222. }
  223. return true
  224. }
  225. // Union returns a SetNodeMap with members that appear in either s or s2.
  226. func (s *SetNodeMap) Union(s2 *SetNodeMap) *SetNodeMap {
  227. out := &SetNodeMap{}
  228. i, j := 0, 0
  229. for i < len(s.members) && j < len(s2.members) {
  230. if s.members[i].pathElement.Less(s2.members[j].pathElement) {
  231. out.members = append(out.members, s.members[i])
  232. i++
  233. } else {
  234. if !s2.members[j].pathElement.Less(s.members[i].pathElement) {
  235. out.members = append(out.members, setNode{pathElement: s.members[i].pathElement, set: s.members[i].set.Union(s2.members[j].set)})
  236. i++
  237. } else {
  238. out.members = append(out.members, s2.members[j])
  239. }
  240. j++
  241. }
  242. }
  243. if i < len(s.members) {
  244. out.members = append(out.members, s.members[i:]...)
  245. }
  246. if j < len(s2.members) {
  247. out.members = append(out.members, s2.members[j:]...)
  248. }
  249. return out
  250. }
  251. // Intersection returns a SetNodeMap with members that appear in both s and s2.
  252. func (s *SetNodeMap) Intersection(s2 *SetNodeMap) *SetNodeMap {
  253. out := &SetNodeMap{}
  254. i, j := 0, 0
  255. for i < len(s.members) && j < len(s2.members) {
  256. if s.members[i].pathElement.Less(s2.members[j].pathElement) {
  257. i++
  258. } else {
  259. if !s2.members[j].pathElement.Less(s.members[i].pathElement) {
  260. res := s.members[i].set.Intersection(s2.members[j].set)
  261. if !res.Empty() {
  262. out.members = append(out.members, setNode{pathElement: s.members[i].pathElement, set: res})
  263. }
  264. i++
  265. }
  266. j++
  267. }
  268. }
  269. return out
  270. }
  271. // Difference returns a SetNodeMap with members that appear in s but not in s2.
  272. func (s *SetNodeMap) Difference(s2 *Set) *SetNodeMap {
  273. out := &SetNodeMap{}
  274. i, j := 0, 0
  275. for i < len(s.members) && j < len(s2.Children.members) {
  276. if s.members[i].pathElement.Less(s2.Children.members[j].pathElement) {
  277. out.members = append(out.members, setNode{pathElement: s.members[i].pathElement, set: s.members[i].set})
  278. i++
  279. } else {
  280. if !s2.Children.members[j].pathElement.Less(s.members[i].pathElement) {
  281. diff := s.members[i].set.Difference(s2.Children.members[j].set)
  282. // We aren't permitted to add nodes with no elements.
  283. if !diff.Empty() {
  284. out.members = append(out.members, setNode{pathElement: s.members[i].pathElement, set: diff})
  285. }
  286. i++
  287. }
  288. j++
  289. }
  290. }
  291. if i < len(s.members) {
  292. out.members = append(out.members, s.members[i:]...)
  293. }
  294. return out
  295. }
  296. // Iterate calls f for each PathElement in the set.
  297. func (s *SetNodeMap) Iterate(f func(PathElement)) {
  298. for _, n := range s.members {
  299. f(n.pathElement)
  300. }
  301. }
  302. func (s *SetNodeMap) iteratePrefix(prefix Path, f func(Path)) {
  303. for _, n := range s.members {
  304. pe := n.pathElement
  305. n.set.iteratePrefix(append(prefix, pe), f)
  306. }
  307. }