pathelementmap.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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/v6/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. valueMap PathElementMap
  25. }
  26. func MakePathElementValueMap(size int) PathElementValueMap {
  27. return PathElementValueMap{
  28. valueMap: MakePathElementMap(size),
  29. }
  30. }
  31. type sortedPathElementValues []pathElementValue
  32. // Implement the sort interface; this would permit bulk creation, which would
  33. // be faster than doing it one at a time via Insert.
  34. func (spev sortedPathElementValues) Len() int { return len(spev) }
  35. func (spev sortedPathElementValues) Less(i, j int) bool {
  36. return spev[i].PathElement.Less(spev[j].PathElement)
  37. }
  38. func (spev sortedPathElementValues) Swap(i, j int) { spev[i], spev[j] = spev[j], spev[i] }
  39. // Insert adds the pathelement and associated value in the map.
  40. // If insert is called twice with the same PathElement, the value is replaced.
  41. func (s *PathElementValueMap) Insert(pe PathElement, v value.Value) {
  42. s.valueMap.Insert(pe, v)
  43. }
  44. // Get retrieves the value associated with the given PathElement from the map.
  45. // (nil, false) is returned if there is no such PathElement.
  46. func (s *PathElementValueMap) Get(pe PathElement) (value.Value, bool) {
  47. v, ok := s.valueMap.Get(pe)
  48. if !ok {
  49. return nil, false
  50. }
  51. return v.(value.Value), true
  52. }
  53. // PathElementValueMap is a map from PathElement to interface{}.
  54. type PathElementMap struct {
  55. members sortedPathElementValues
  56. }
  57. type pathElementValue struct {
  58. PathElement PathElement
  59. Value interface{}
  60. }
  61. func MakePathElementMap(size int) PathElementMap {
  62. return PathElementMap{
  63. members: make(sortedPathElementValues, 0, size),
  64. }
  65. }
  66. // Insert adds the pathelement and associated value in the map.
  67. // If insert is called twice with the same PathElement, the value is replaced.
  68. func (s *PathElementMap) Insert(pe PathElement, v interface{}) {
  69. loc := sort.Search(len(s.members), func(i int) bool {
  70. return !s.members[i].PathElement.Less(pe)
  71. })
  72. if loc == len(s.members) {
  73. s.members = append(s.members, pathElementValue{pe, v})
  74. return
  75. }
  76. if s.members[loc].PathElement.Equals(pe) {
  77. s.members[loc].Value = v
  78. return
  79. }
  80. s.members = append(s.members, pathElementValue{})
  81. copy(s.members[loc+1:], s.members[loc:])
  82. s.members[loc] = pathElementValue{pe, v}
  83. }
  84. // Get retrieves the value associated with the given PathElement from the map.
  85. // (nil, false) is returned if there is no such PathElement.
  86. func (s *PathElementMap) Get(pe PathElement) (interface{}, bool) {
  87. loc := sort.Search(len(s.members), func(i int) bool {
  88. return !s.members[i].PathElement.Less(pe)
  89. })
  90. if loc == len(s.members) {
  91. return nil, false
  92. }
  93. if s.members[loc].PathElement.Equals(pe) {
  94. return s.members[loc].Value, true
  95. }
  96. return nil, false
  97. }