set.go 7.7 KB

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