Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3-pq
Commits
17f800dd
Commit
17f800dd
authored
Nov 01, 2019
by
Dirk McCormick
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: Remove()
parent
0c9cb1e2
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
28 additions
and
3 deletions
+28
-3
pq.go
pq.go
+6
-3
pq_test.go
pq_test.go
+22
-0
No files found.
pq.go
View file @
17f800dd
...
...
@@ -11,13 +11,12 @@ type PQ interface {
Pop
()
Elem
// Peek returns the highest priority Elem in PQ (without removing it).
Peek
()
Elem
// Remove removes the item at the given index from the PQ.
Remove
(
index
int
)
Elem
// Len returns the number of elements in the PQ.
Len
()
int
// Update `fixes` the PQ.
Update
(
index
int
)
// TODO explain why this interface should not be extended
// It does not support Remove. This is because...
}
// Elem describes elements that can be added to the PQ. Clients must implement
...
...
@@ -65,6 +64,10 @@ func (w *wrapper) Peek() Elem {
return
w
.
heapinterface
.
elems
[
0
]
.
(
Elem
)
}
func
(
w
*
wrapper
)
Remove
(
index
int
)
Elem
{
return
heap
.
Remove
(
&
w
.
heapinterface
,
index
)
.
(
Elem
)
}
func
(
w
*
wrapper
)
Update
(
index
int
)
{
heap
.
Fix
(
&
w
.
heapinterface
,
index
)
}
...
...
pq_test.go
View file @
17f800dd
...
...
@@ -67,6 +67,28 @@ func TestCorrectnessOfPop(t *testing.T) {
}
}
func
TestRemove
(
t
*
testing
.
T
)
{
q
:=
New
(
PriorityComparator
)
tasks
:=
[]
TestElem
{
{
Key
:
"a"
,
Priority
:
9
},
{
Key
:
"b"
,
Priority
:
4
},
{
Key
:
"c"
,
Priority
:
3
},
}
for
i
:=
range
tasks
{
q
.
Push
(
&
tasks
[
i
])
}
removed
:=
q
.
Remove
(
1
)
.
(
*
TestElem
)
if
q
.
Len
()
!=
2
{
t
.
Fatal
(
"expected item to have been removed"
)
}
if
q
.
Pop
()
.
(
*
TestElem
)
.
Key
==
removed
.
Key
{
t
.
Fatal
(
"Remove() returned wrong element"
)
}
if
q
.
Pop
()
.
(
*
TestElem
)
.
Key
==
removed
.
Key
{
t
.
Fatal
(
"Remove() returned wrong element"
)
}
}
func
TestUpdate
(
t
*
testing
.
T
)
{
t
.
Log
(
`
Add 3 elements.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment