pathelementmap.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. "sigs.k8s.io/structured-merge-diff/v4/value"
  17. )
  18. // PathElementValueMap is a map from PathElement to value.Value.
  19. //
  20. // TODO(apelisse): We have multiple very similar implementation of this
  21. // for PathElementSet and SetNodeMap, so we could probably share the
  22. // code.
  23. type PathElementValueMap struct {
  24. members sortedPathElementValues
  25. }
  26. func MakePathElementValueMap(size int) PathElementValueMap {
  27. return PathElementValueMap{
  28. members: make(sortedPathElementValues, 0, size),
  29. }
  30. }
  31. type pathElementValue struct {
  32. PathElement PathElement
  33. Value value.Value
  34. }
  35. type sortedPathElementValues []pathElementValue
  36. // Implement the sort interface; this would permit bulk creation, which would
  37. // be faster than doing it one at a time via Insert.
  38. func (spev sortedPathElementValues) Len() int { return len(spev) }
  39. func (spev sortedPathElementValues) Less(i, j int) bool {
  40. return spev[i].PathElement.Less(spev[j].PathElement)
  41. }
  42. func (spev sortedPathElementValues) Swap(i, j int) { spev[i], spev[j] = spev[j], spev[i] }
  43. // Insert adds the pathelement and associated value in the map.
  44. func (s *PathElementValueMap) Insert(pe PathElement, v value.Value) {
  45. loc := sort.Search(len(s.members), func(i int) bool {
  46. return !s.members[i].PathElement.Less(pe)
  47. })
  48. if loc == len(s.members) {
  49. s.members = append(s.members, pathElementValue{pe, v})
  50. return
  51. }
  52. if s.members[loc].PathElement.Equals(pe) {
  53. return
  54. }
  55. s.members = append(s.members, pathElementValue{})
  56. copy(s.members[loc+1:], s.members[loc:])
  57. s.members[loc] = pathElementValue{pe, v}
  58. }
  59. // Get retrieves the value associated with the given PathElement from the map.
  60. // (nil, false) is returned if there is no such PathElement.
  61. func (s *PathElementValueMap) Get(pe PathElement) (value.Value, bool) {
  62. loc := sort.Search(len(s.members), func(i int) bool {
  63. return !s.members[i].PathElement.Less(pe)
  64. })
  65. if loc == len(s.members) {
  66. return nil, false
  67. }
  68. if s.members[loc].PathElement.Equals(pe) {
  69. return s.members[loc].Value, true
  70. }
  71. return nil, false
  72. }