-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add generic implementation of bidirectional map
furthermore, this PR extracts out the generic interfaces we use in the generic set implementation so that we can reuse them in the bidirectional map.
- Loading branch information
Han Kang
committed
Mar 3, 2024
1 parent
e7106e6
commit 7a63a7b
Showing
7 changed files
with
201 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# See the OWNERS docs at https://go.k8s.io/owners | ||
|
||
reviewers: | ||
- logicalhan | ||
- thockin | ||
approvers: | ||
- logicalhan | ||
- thockin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package bidirectionalmap | ||
|
||
import ( | ||
"k8s.io/utils/genericinterfaces" | ||
"k8s.io/utils/set" | ||
) | ||
|
||
// BidirectionalMap is a bidirectional map. | ||
type BidirectionalMap[X genericinterfaces.Ordered, Y genericinterfaces.Ordered] struct { | ||
right map[X]set.Set[Y] | ||
left map[Y]set.Set[X] | ||
} | ||
|
||
// NewBidirectionalMap creates a new BidirectionalMap. | ||
func NewBidirectionalMap[X genericinterfaces.Ordered, Y genericinterfaces.Ordered]() *BidirectionalMap[X, Y] { | ||
return &BidirectionalMap[X, Y]{ | ||
right: make(map[X]set.Set[Y]), | ||
left: make(map[Y]set.Set[X]), | ||
} | ||
} | ||
|
||
// InsertRight inserts a new item into the right map. | ||
func (bdm *BidirectionalMap[X, Y]) InsertRight(x X, y Y) bool { | ||
if bdm.right[x] == nil { | ||
bdm.right[x] = set.New[Y]() | ||
} | ||
if bdm.right[x].Has(y) { | ||
return false | ||
} | ||
if bdm.left[y] == nil { | ||
bdm.left[y] = set.New[X]() | ||
} | ||
bdm.right[x].Insert(y) | ||
bdm.left[y].Insert(x) | ||
return true | ||
} | ||
|
||
// InsertLeft inserts a new item into the left map. | ||
func (bdm *BidirectionalMap[X, Y]) InsertLeft(y Y, x X) bool { | ||
if bdm.left[y] == nil { | ||
bdm.left[y] = set.New[X]() | ||
} | ||
if bdm.left[y].Has(x) { | ||
return false | ||
} | ||
if bdm.right[x] == nil { | ||
bdm.right[x] = set.New[Y]() | ||
} | ||
bdm.right[x].Insert(y) | ||
bdm.left[y].Insert(x) | ||
return true | ||
} | ||
|
||
// GetRight returns a value from the right map. | ||
func (bdm *BidirectionalMap[X, Y]) GetRight(x X) set.Set[Y] { | ||
return bdm.right[x] | ||
} | ||
|
||
// GetLeft returns a value from left map. | ||
func (bdm *BidirectionalMap[X, Y]) GetLeft(y Y) set.Set[X] { | ||
return bdm.left[y] | ||
} | ||
|
||
// DeleteRightKey deletes the key from the right map and removes | ||
// the inverse mapping from the left map. | ||
func (bdm *BidirectionalMap[X, Y]) DeleteRightKey(x X) { | ||
if leftValues, ok := bdm.right[x]; ok { | ||
delete(bdm.right, x) | ||
for y := range leftValues { | ||
bdm.left[y].Delete(x) | ||
if bdm.left[y].Len() == 0 { | ||
delete(bdm.left, y) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// DeleteLeftKey deletes the key from the left map and removes | ||
// the inverse mapping from the right map. | ||
func (bdm *BidirectionalMap[X, Y]) DeleteLeftKey(y Y) { | ||
if rightValues, ok := bdm.left[y]; ok { | ||
delete(bdm.left, y) | ||
for x := range rightValues { | ||
bdm.right[x].Delete(y) | ||
if bdm.right[x].Len() == 0 { | ||
delete(bdm.right, x) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// GetRightKeys returns the keys from the right map. | ||
func (bdm *BidirectionalMap[X, Y]) GetRightKeys() set.Set[X] { | ||
return set.KeySet[X](bdm.right) | ||
} | ||
|
||
// GetLeftKeys returns the keys from the left map. | ||
func (bdm *BidirectionalMap[X, Y]) GetLeftKeys() set.Set[Y] { | ||
return set.KeySet[Y](bdm.left) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
Copyright 2020 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package bidirectionalmap | ||
|
||
import "testing" | ||
|
||
func TestMultipleInserts(t *testing.T) { | ||
bidimap := NewBidirectionalMap[string, string]() | ||
bidimap.InsertRight("r1", "l1") | ||
bidimap.InsertRight("r1", "l2") | ||
if bidimap.GetRight("r1").Len() != 2 { | ||
t.Errorf("GetRight('r1').Len() == %d, expected 2", bidimap.GetRight("r1").Len()) | ||
} | ||
if bidimap.GetLeft("l2").Len() != 1 { | ||
t.Errorf("GetLeft('l2').Len() == %d, expected 1", bidimap.GetLeft("l2").Len()) | ||
} | ||
bidimap.InsertLeft("l2", "r2") | ||
if bidimap.GetLeft("l2").Len() != 2 { | ||
t.Errorf("GetLeft('l2').Len() == %d, expected 2", bidimap.GetLeft("l2").Len()) | ||
} | ||
r2Len := bidimap.GetRight("r2").Len() | ||
if r2Len != 1 { | ||
t.Errorf("GetRight('r2').Len() == %d, expected 1", r2Len) | ||
} | ||
bidimap.DeleteRightKey("r2") | ||
if bidimap.GetRight("r2") != nil { | ||
t.Errorf("GetRight('r2') should be nil") | ||
} | ||
if bidimap.GetLeft("l2").Len() != 1 { | ||
t.Errorf("GetLeft('l2').Len() == %d, expected 1", bidimap.GetLeft("l2").Len()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# See the OWNERS docs at https://go.k8s.io/owners | ||
|
||
reviewers: | ||
- logicalhan | ||
- thockin | ||
approvers: | ||
- logicalhan | ||
- thockin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters