Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
grumpy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
grumpy
Commits
f997f26d
Commit
f997f26d
authored
Feb 06, 2017
by
Garry Bullock
Committed by
Dylan Trotter
Feb 06, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding bisect module and tests from stdlib (#234)
parent
4c2da97c
Changes
3
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
479 additions
and
0 deletions
+479
-0
Makefile
Makefile
+1
-0
third_party/stdlib/bisect.py
third_party/stdlib/bisect.py
+92
-0
third_party/stdlib/test/test_bisect.py
third_party/stdlib/test/test_bisect.py
+386
-0
No files found.
Makefile
View file @
f997f26d
...
@@ -93,6 +93,7 @@ STDLIB_TESTS := \
...
@@ -93,6 +93,7 @@ STDLIB_TESTS := \
test
/test_slice
\
test
/test_slice
\
test
/test_string
\
test
/test_string
\
test
/test_md5
\
test
/test_md5
\
test
/test_bisect
\
threading_test
\
threading_test
\
time_test
\
time_test
\
types_test
\
types_test
\
...
...
third_party/stdlib/bisect.py
0 → 100644
View file @
f997f26d
"""Bisection algorithms."""
def
insort_right
(
a
,
x
,
lo
=
0
,
hi
=
None
):
"""Insert item x in list a, and keep it sorted assuming a is sorted.
If x is already in a, insert it to the right of the rightmost x.
Optional args lo (default 0) and hi (default len(a)) bound the
slice of a to be searched.
"""
if
lo
<
0
:
raise
ValueError
(
'lo must be non-negative'
)
if
hi
is
None
:
hi
=
len
(
a
)
while
lo
<
hi
:
mid
=
(
lo
+
hi
)
//
2
if
x
<
a
[
mid
]:
hi
=
mid
else
:
lo
=
mid
+
1
a
.
insert
(
lo
,
x
)
insort
=
insort_right
# backward compatibility
def
bisect_right
(
a
,
x
,
lo
=
0
,
hi
=
None
):
"""Return the index where to insert item x in list a, assuming a is sorted.
The return value i is such that all e in a[:i] have e <= x, and all e in
a[i:] have e > x. So if x already appears in the list, a.insert(x) will
insert just after the rightmost x already there.
Optional args lo (default 0) and hi (default len(a)) bound the
slice of a to be searched.
"""
if
lo
<
0
:
raise
ValueError
(
'lo must be non-negative'
)
if
hi
is
None
:
hi
=
len
(
a
)
while
lo
<
hi
:
mid
=
(
lo
+
hi
)
//
2
if
x
<
a
[
mid
]:
hi
=
mid
else
:
lo
=
mid
+
1
return
lo
bisect
=
bisect_right
# backward compatibility
def
insort_left
(
a
,
x
,
lo
=
0
,
hi
=
None
):
"""Insert item x in list a, and keep it sorted assuming a is sorted.
If x is already in a, insert it to the left of the leftmost x.
Optional args lo (default 0) and hi (default len(a)) bound the
slice of a to be searched.
"""
if
lo
<
0
:
raise
ValueError
(
'lo must be non-negative'
)
if
hi
is
None
:
hi
=
len
(
a
)
while
lo
<
hi
:
mid
=
(
lo
+
hi
)
//
2
if
a
[
mid
]
<
x
:
lo
=
mid
+
1
else
:
hi
=
mid
a
.
insert
(
lo
,
x
)
def
bisect_left
(
a
,
x
,
lo
=
0
,
hi
=
None
):
"""Return the index where to insert item x in list a, assuming a is sorted.
The return value i is such that all e in a[:i] have e < x, and all e in
a[i:] have e >= x. So if x already appears in the list, a.insert(x) will
insert just before the leftmost x already there.
Optional args lo (default 0) and hi (default len(a)) bound the
slice of a to be searched.
"""
if
lo
<
0
:
raise
ValueError
(
'lo must be non-negative'
)
if
hi
is
None
:
hi
=
len
(
a
)
while
lo
<
hi
:
mid
=
(
lo
+
hi
)
//
2
if
a
[
mid
]
<
x
:
lo
=
mid
+
1
else
:
hi
=
mid
return
lo
# Overwrite above definitions with a fast C implementation
# try:
# from _bisect import *
# except ImportError:
# pass
third_party/stdlib/test/test_bisect.py
0 → 100644
View file @
f997f26d
This diff is collapsed.
Click to expand it.
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