|
| 1 | +/* |
| 2 | +Copyright 2024 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package bidirectionalmap |
| 18 | + |
| 19 | +import ( |
| 20 | + "k8s.io/utils/genericinterfaces" |
| 21 | + "k8s.io/utils/set" |
| 22 | +) |
| 23 | + |
| 24 | +// BidirectionalMap is a bidirectional map. |
| 25 | +type BidirectionalMap[X genericinterfaces.Ordered, Y genericinterfaces.Ordered] struct { |
| 26 | + right map[X]set.Set[Y] |
| 27 | + left map[Y]set.Set[X] |
| 28 | +} |
| 29 | + |
| 30 | +// NewBidirectionalMap creates a new BidirectionalMap. |
| 31 | +func NewBidirectionalMap[X genericinterfaces.Ordered, Y genericinterfaces.Ordered]() *BidirectionalMap[X, Y] { |
| 32 | + return &BidirectionalMap[X, Y]{ |
| 33 | + right: make(map[X]set.Set[Y]), |
| 34 | + left: make(map[Y]set.Set[X]), |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// InsertRight inserts a new item into the right map. |
| 39 | +func (bdm *BidirectionalMap[X, Y]) InsertRight(x X, y Y) bool { |
| 40 | + if bdm.right[x] == nil { |
| 41 | + bdm.right[x] = set.New[Y]() |
| 42 | + } |
| 43 | + if bdm.right[x].Has(y) { |
| 44 | + return false |
| 45 | + } |
| 46 | + if bdm.left[y] == nil { |
| 47 | + bdm.left[y] = set.New[X]() |
| 48 | + } |
| 49 | + bdm.right[x].Insert(y) |
| 50 | + bdm.left[y].Insert(x) |
| 51 | + return true |
| 52 | +} |
| 53 | + |
| 54 | +// InsertLeft inserts a new item into the left map. |
| 55 | +func (bdm *BidirectionalMap[X, Y]) InsertLeft(y Y, x X) bool { |
| 56 | + if bdm.left[y] == nil { |
| 57 | + bdm.left[y] = set.New[X]() |
| 58 | + } |
| 59 | + if bdm.left[y].Has(x) { |
| 60 | + return false |
| 61 | + } |
| 62 | + if bdm.right[x] == nil { |
| 63 | + bdm.right[x] = set.New[Y]() |
| 64 | + } |
| 65 | + bdm.right[x].Insert(y) |
| 66 | + bdm.left[y].Insert(x) |
| 67 | + return true |
| 68 | +} |
| 69 | + |
| 70 | +// GetRight returns a value from the right map. |
| 71 | +func (bdm *BidirectionalMap[X, Y]) GetRight(x X) set.Set[Y] { |
| 72 | + return bdm.right[x] |
| 73 | +} |
| 74 | + |
| 75 | +// GetLeft returns a value from left map. |
| 76 | +func (bdm *BidirectionalMap[X, Y]) GetLeft(y Y) set.Set[X] { |
| 77 | + return bdm.left[y] |
| 78 | +} |
| 79 | + |
| 80 | +// DeleteRightKey deletes the key from the right map and removes |
| 81 | +// the inverse mapping from the left map. |
| 82 | +func (bdm *BidirectionalMap[X, Y]) DeleteRightKey(x X) { |
| 83 | + if leftValues, ok := bdm.right[x]; ok { |
| 84 | + delete(bdm.right, x) |
| 85 | + for y := range leftValues { |
| 86 | + bdm.left[y].Delete(x) |
| 87 | + if bdm.left[y].Len() == 0 { |
| 88 | + delete(bdm.left, y) |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// DeleteLeftKey deletes the key from the left map and removes |
| 95 | +// the inverse mapping from the right map. |
| 96 | +func (bdm *BidirectionalMap[X, Y]) DeleteLeftKey(y Y) { |
| 97 | + if rightValues, ok := bdm.left[y]; ok { |
| 98 | + delete(bdm.left, y) |
| 99 | + for x := range rightValues { |
| 100 | + bdm.right[x].Delete(y) |
| 101 | + if bdm.right[x].Len() == 0 { |
| 102 | + delete(bdm.right, x) |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +// GetRightKeys returns the keys from the right map. |
| 109 | +func (bdm *BidirectionalMap[X, Y]) GetRightKeys() set.Set[X] { |
| 110 | + return set.KeySet[X](bdm.right) |
| 111 | +} |
| 112 | + |
| 113 | +// GetLeftKeys returns the keys from the left map. |
| 114 | +func (bdm *BidirectionalMap[X, Y]) GetLeftKeys() set.Set[Y] { |
| 115 | + return set.KeySet[Y](bdm.left) |
| 116 | +} |
0 commit comments