Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
ba
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
markn92
ba
Commits
0e90839e
Commit
0e90839e
authored
5 years ago
by
markn92
Browse files
Options
Downloads
Patches
Plain Diff
adding priority queue implementation
parent
c55e382b
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
evrouting/utils.py
+44
-0
44 additions, 0 deletions
evrouting/utils.py
tests/test_utils.py
+33
-0
33 additions, 0 deletions
tests/test_utils.py
with
77 additions
and
0 deletions
evrouting/utils.py
0 → 100644
+
44
−
0
View file @
0e90839e
import
itertools
from
heapq
import
*
class
PriorityQueue
:
REMOVED
=
'
<removed-task>
'
# placeholder for a removed task
def
__init__
(
self
):
self
.
pq
=
[]
# list of entries arranged in a heap
self
.
entry_finder
=
{}
# mapping of tasks to entries
self
.
counter
=
itertools
.
count
()
# unique sequence count as tie break
def
insert
(
self
,
item
,
priority
=
0
):
"""
Add a new task or update the priority of an existing task
"""
if
item
in
self
.
entry_finder
:
self
.
remove_item
(
item
)
count
=
next
(
self
.
counter
)
entry
=
[
priority
,
count
,
item
]
self
.
entry_finder
[
item
]
=
entry
heappush
(
self
.
pq
,
entry
)
def
remove_item
(
self
,
item
):
"""
Mark an existing task as REMOVED. Raise KeyError if not found.
"""
entry
=
self
.
entry_finder
.
pop
(
item
)
entry
[
-
1
]
=
self
.
REMOVED
def
delete_min
(
self
):
"""
Remove and return the lowest priority task. Raise KeyError if empty.
"""
while
self
.
pq
:
priority
,
count
,
item
=
heappop
(
self
.
pq
)
if
item
is
not
self
.
REMOVED
:
del
self
.
entry_finder
[
item
]
return
item
raise
KeyError
(
'
pop from an empty priority queue
'
)
def
peak_min
(
self
):
"""
Return minimum item without removing it from the queue.
"""
while
self
.
pq
:
priority
,
count
,
item
=
self
.
pq
[
0
]
if
item
is
not
self
.
REMOVED
:
return
item
else
:
heappop
(
self
.
pq
)
raise
KeyError
(
'
Empty queue.
'
)
This diff is collapsed.
Click to expand it.
tests/test_utils.py
0 → 100644
+
33
−
0
View file @
0e90839e
import
pytest
from
evrouting.utils
import
PriorityQueue
@pytest.fixture
def
q
():
q
=
PriorityQueue
()
q
.
insert
(
'
high
'
,
3
)
q
.
insert
(
'
low
'
,
2
)
yield
q
del
q
class
TestProrityQueue
:
def
test_insert
(
self
,
q
):
assert
q
.
delete_min
()
==
'
low
'
assert
q
.
delete_min
()
==
'
high
'
with
pytest
.
raises
(
KeyError
):
q
.
delete_min
()
def
test_peak
(
self
,
q
):
assert
q
.
peak_min
()
==
'
low
'
assert
q
.
peak_min
()
==
'
low
'
# does not get removed
def
test_update
(
self
,
q
):
q
.
insert
(
'
high
'
,
1
)
assert
q
.
delete_min
()
==
'
high
'
assert
q
.
delete_min
()
==
'
low
'
with
pytest
.
raises
(
KeyError
):
q
.
delete_min
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment