-
Notifications
You must be signed in to change notification settings - Fork 20
/
bisect.lisp
84 lines (78 loc) · 3.53 KB
/
bisect.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
(defpackage :cp/bisect
(:use :cl)
(:export #:bisect-left #:bisect-right))
(in-package :cp/bisect)
(declaim (inline bisect-left))
(defun bisect-left (target value &key (start 0) end (order #'<) (key #'identity))
"TARGET := vector | function (taking an integer argument)
ORDER := strict order
Analogue of lower_bound() of C++ or bisect_left() of Python: Returns the least
index (or input) i such that TARGET[i] >= VALUE, where '>=' is the complement of
ORDER. In other words, this function returns the leftmost index at which VALUE
can be inserted with keeping the order. Therefore, TARGET must be monotonically
non-decreasing with respect to ORDER.
- This function returns END if VALUE exceeds TARGET[END-1].
- The range [START, END) is half-open.
- END must be explicitly specified if TARGET is function.
- KEY is applied to each element of TARGET before comparison."
(declare (integer start)
((or null integer) end))
(macrolet
((frob (accessor &optional declaration)
`(labels
((%bisect-left (ng ok)
;; TARGET[OK] >= VALUE always holds (assuming
;; TARGET[END] = +infinity)
,@(when declaration (list declaration))
(if (<= (- ok ng) 1)
ok
(let ((mid (ash (+ ng ok) -1)))
(if (funcall order (funcall key (,accessor target mid)) value)
(%bisect-left mid ok)
(%bisect-left ng mid))))))
(assert (<= start end))
(%bisect-left (- start 1) end))))
(etypecase target
(vector
(let ((end (or end (length target))))
(frob aref (declare ((integer -1 (#.most-positive-fixnum)) ng ok)))))
(function
(assert end () "Requires END argument if TARGET is a function.")
(frob funcall)))))
(declaim (inline bisect-right))
(defun bisect-right (target value &key (start 0) end (order #'<) (key #'identity))
"TARGET := vector | function (taking an integer argument)
ORDER := strict order
Analogue of upper_bound() of C++ or bisect_right() of Python: Returns the least
index (or input) i such that TARGET[i] > VALUE. In other words, this function
returns the rightmost index at which VALUE can be inserted with keeping the
order. Therefore, TARGET must be monotonically non-decreasing with respect to
ORDER.
- This function returns END if VALUE >= TARGET[END-1].
- The range [START, END) is half-open.
- END must be explicitly specified if TARGET is function.
- KEY is applied to each element of TARGET before comparison."
(declare (integer start)
((or null integer) end))
(macrolet
((frob (accessor &optional declaration)
`(labels
((%bisect-right (ng ok)
;; TARGET[OK] > VALUE always holds (assuming
;; TARGET[END] = +infinity)
,@(when declaration (list declaration))
(if (<= (- ok ng) 1)
ok
(let ((mid (ash (+ ng ok) -1)))
(if (funcall order value (funcall key (,accessor target mid)))
(%bisect-right ng mid)
(%bisect-right mid ok))))))
(assert (<= start end))
(%bisect-right (- start 1) end))))
(etypecase target
(vector
(let ((end (or end (length target))))
(frob aref (declare ((integer -1 (#.array-dimension-limit)) ng ok)))))
(function
(assert end () "Requires END argument if TARGET is a function.")
(frob funcall)))))