Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
satella
Manage
Activity
Members
Labels
Plan
Issues
1
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
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
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
public
satella
Commits
1efb5948
Commit
1efb5948
authored
7 years ago
by
Piotr Maślanka
Browse files
Options
Downloads
Patches
Plain Diff
heap fix
parent
48285374
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
satella/coding/structures.py
+11
-20
11 additions, 20 deletions
satella/coding/structures.py
tests/test_coding/test_structures.py
+7
-0
7 additions, 0 deletions
tests/test_coding/test_structures.py
with
18 additions
and
20 deletions
satella/coding/structures.py
+
11
−
20
View file @
1efb5948
...
...
@@ -184,20 +184,6 @@ class Heap(object):
while
heap
:
yield
heapq
.
heappop
(
heap
)
@returns_iterable
def
pop_less_than
(
self
,
less
):
"""
Return all elements less (sharp inequality) than particular value.
This changes state of the heap
:param less: value to compare against
:return: Iterator
"""
while
self
:
if
self
.
heap
[
0
]
<
less
:
return
yield
self
.
pop
()
@returns_iterable
def
iter_descending
(
self
):
"""
...
...
@@ -254,16 +240,21 @@ class TimeBasedHeap(Heap):
:param timestamp: timestamp for this item
:param item: object
"""
self
.
push
(
timestamp
,
item
)
self
.
push
(
(
timestamp
,
item
)
)
def
pop_less_than
(
self
,
timestamp
):
@returns_iterable
def
pop_less_than
(
self
,
less
):
"""
Return a
list of items with timestamps less than you
r value.
Return a
ll elements less (sharp inequality) than particula
r value.
Items will be removed from heap
:return: list of tuple(timestamp::float, item)
This changes state of the heap
:param less: value to compare against
:return: Iterator
"""
return
list
(
Heap
.
pop_less_than
(
self
,
timestamp
))
while
self
:
if
self
.
heap
[
0
]
<
less
:
return
yield
self
.
pop
()
def
remove
(
self
,
item
):
"""
...
...
This diff is collapsed.
Click to expand it.
tests/test_coding/test_structures.py
+
7
−
0
View file @
1efb5948
...
...
@@ -23,6 +23,13 @@ class TestTimeBasedHeap(unittest.TestCase):
class
TestHeap
(
unittest
.
TestCase
):
def
test_push
(
self
):
tbh
=
Heap
()
tbh
.
push
(
10
,
'
A
'
)
self
.
assertEquals
((
10
,
'
A
'
),
tbh
.
pop
())
tbh
.
push
((
10
,
'
A
'
))
self
.
assertEquals
((
10
,
'
A
'
),
tbh
.
pop
())
def
test_tbh_iter
(
self
):
tbh
=
Heap
()
...
...
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