aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaul Eggert2013-11-18 10:56:42 -0800
committerPaul Eggert2013-11-18 10:56:42 -0800
commitec2c4ee6d2cb9c5505f120229269941f064b23fa (patch)
tree9c0e0ba58f15f602fce7d349cfa89fc665728161
parent87d86601022feb7a330fc6344cc85ec65563c1b6 (diff)
downloademacs-ec2c4ee6d2cb9c5505f120229269941f064b23fa.tar.gz
emacs-ec2c4ee6d2cb9c5505f120229269941f064b23fa.zip
Improve API of recently-added bool vector functions.
The old API had (bool-vector-count-matches A B) and (bool-vector-count-matches-at A B I), which gave the misleading impression that the two functions were variants, one with a location I. The new API has (bool-vector-count-population A) and (bool-vector-count-consecutive A B I) to make the distinction clearer. The first function no longer has a B argument, since the caller can easily determine the number of nils if the length and number of ts is known. * src/data.c (Fbool_vector_count_population): Rename from bool_vector_count_matches, and accept just 1 argument. (Fbool_vector_count_consecutive): Rename from Fbool_vector_count_matches_at. * test/automated/data-tests.el: Adjust to API changes. Fixes: debbugs:15912
-rw-r--r--etc/NEWS4
-rw-r--r--src/ChangeLog14
-rw-r--r--src/data.c27
-rw-r--r--test/ChangeLog5
-rw-r--r--test/automated/data-tests.el46
5 files changed, 55 insertions, 41 deletions
diff --git a/etc/NEWS b/etc/NEWS
index 132c461b258..b68978b8ed9 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -736,8 +736,8 @@ frame.
736*** `bool-vector-set-difference' 736*** `bool-vector-set-difference'
737*** `bool-vector-not' 737*** `bool-vector-not'
738*** `bool-vector-subsetp' 738*** `bool-vector-subsetp'
739*** `bool-vector-count-matches' 739*** `bool-vector-count-consecutive'
740*** `bool-vector-count-matches-at' 740*** `bool-vector-count-population'
741 741
742** Comparison functions =, <, >, <=, >= now take many arguments. 742** Comparison functions =, <, >, <=, >= now take many arguments.
743 743
diff --git a/src/ChangeLog b/src/ChangeLog
index 21025c677fc..d6edcea4513 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,5 +1,19 @@
12013-11-18 Paul Eggert <eggert@cs.ucla.edu> 12013-11-18 Paul Eggert <eggert@cs.ucla.edu>
2 2
3 Improve API of recently-added bool vector functions (Bug#15912).
4 The old API had (bool-vector-count-matches A B)
5 and (bool-vector-count-matches-at A B I), which gave the
6 misleading impression that the two functions were variants, one
7 with a location I. The new API has (bool-vector-count-population A)
8 and (bool-vector-count-consecutive A B I) to make the distinction
9 clearer. The first function no longer has a B argument, since the
10 caller can easily determine the number of nils if the length and
11 number of ts is known.
12 * data.c (Fbool_vector_count_population): Rename from
13 bool_vector_count_matches, and accept just 1 argument.
14 (Fbool_vector_count_consecutive): Rename from
15 Fbool_vector_count_matches_at.
16
3 Always allocate at least one bits_word per bool vector. 17 Always allocate at least one bits_word per bool vector.
4 See Daniel Colascione in: 18 See Daniel Colascione in:
5 http://lists.gnu.org/archive/html/emacs-devel/2013-11/msg00518.html 19 http://lists.gnu.org/archive/html/emacs-devel/2013-11/msg00518.html
diff --git a/src/data.c b/src/data.c
index b8b0f248dfa..2c789f37431 100644
--- a/src/data.c
+++ b/src/data.c
@@ -3282,11 +3282,12 @@ Return the destination vector. */)
3282 return b; 3282 return b;
3283} 3283}
3284 3284
3285DEFUN ("bool-vector-count-matches", Fbool_vector_count_matches, 3285DEFUN ("bool-vector-count-population", Fbool_vector_count_population,
3286 Sbool_vector_count_matches, 2, 2, 0, 3286 Sbool_vector_count_population, 1, 1, 0,
3287 doc: /* Count how many elements in A equal B. 3287 doc: /* Count how many elements in A are t.
3288A must be a bool vector. B is a generalized bool. */) 3288A is a bool vector. To count A's nil elements, subtract the return
3289 (Lisp_Object a, Lisp_Object b) 3289value from A's length. */)
3290 (Lisp_Object a)
3290{ 3291{
3291 EMACS_INT count; 3292 EMACS_INT count;
3292 EMACS_INT nr_bits; 3293 EMACS_INT nr_bits;
@@ -3303,17 +3304,13 @@ A must be a bool vector. B is a generalized bool. */)
3303 for (i = 0; i < nwords; i++) 3304 for (i = 0; i < nwords; i++)
3304 count += count_one_bits_word (adata[i]); 3305 count += count_one_bits_word (adata[i]);
3305 3306
3306 if (NILP (b))
3307 count = nr_bits - count;
3308 return make_number (count); 3307 return make_number (count);
3309} 3308}
3310 3309
3311DEFUN ("bool-vector-count-matches-at", 3310DEFUN ("bool-vector-count-consecutive", Fbool_vector_count_consecutive,
3312 Fbool_vector_count_matches_at, 3311 Sbool_vector_count_consecutive, 3, 3, 0,
3313 Sbool_vector_count_matches_at, 3, 3, 0, 3312 doc: /* Count how many consecutive elements in A equal B starting at I.
3314 doc: /* Count how many consecutive elements in A equal B at i. 3313A is a bool vector, B is t or nil, and I is an index into A. */)
3315A must be a bool vector. B is a generalized boolean. i is an
3316index into the vector. */)
3317 (Lisp_Object a, Lisp_Object b, Lisp_Object i) 3314 (Lisp_Object a, Lisp_Object b, Lisp_Object i)
3318{ 3315{
3319 EMACS_INT count; 3316 EMACS_INT count;
@@ -3660,8 +3657,8 @@ syms_of_data (void)
3660 defsubr (&Sbool_vector_set_difference); 3657 defsubr (&Sbool_vector_set_difference);
3661 defsubr (&Sbool_vector_not); 3658 defsubr (&Sbool_vector_not);
3662 defsubr (&Sbool_vector_subsetp); 3659 defsubr (&Sbool_vector_subsetp);
3663 defsubr (&Sbool_vector_count_matches); 3660 defsubr (&Sbool_vector_count_consecutive);
3664 defsubr (&Sbool_vector_count_matches_at); 3661 defsubr (&Sbool_vector_count_population);
3665 3662
3666 set_symbol_function (Qwholenump, XSYMBOL (Qnatnump)->function); 3663 set_symbol_function (Qwholenump, XSYMBOL (Qnatnump)->function);
3667 3664
diff --git a/test/ChangeLog b/test/ChangeLog
index 79d9ab544e0..35a168ce0c4 100644
--- a/test/ChangeLog
+++ b/test/ChangeLog
@@ -1,3 +1,8 @@
12013-11-18 Paul Eggert <eggert@cs.ucla.edu>
2
3 Improve API of recently-added bool vector functions (Bug#15912).
4 * automated/data-tests.el: Adjust to API changes.
5
12013-11-16 Michael Albinus <michael.albinus@gmx.de> 62013-11-16 Michael Albinus <michael.albinus@gmx.de>
2 7
3 * automated/tramp-tests.el (tramp-test07-file-exists-p) 8 * automated/tramp-tests.el (tramp-test07-file-exists-p)
diff --git a/test/automated/data-tests.el b/test/automated/data-tests.el
index d79e1643848..f731b8bf1db 100644
--- a/test/automated/data-tests.el
+++ b/test/automated/data-tests.el
@@ -77,42 +77,40 @@
77;; Bool vector tests. Compactly represent bool vectors as hex 77;; Bool vector tests. Compactly represent bool vectors as hex
78;; strings. 78;; strings.
79 79
80(ert-deftest bool-vector-count-matches-all-0-nil () 80(ert-deftest bool-vector-count-population-all-0-nil ()
81 (cl-loop for sz in '(0 45 1 64 9 344) 81 (cl-loop for sz in '(0 45 1 64 9 344)
82 do (let* ((bv (make-bool-vector sz nil))) 82 do (let* ((bv (make-bool-vector sz nil)))
83 (should 83 (should
84 (eql 84 (zerop
85 (bool-vector-count-matches bv nil) 85 (bool-vector-count-population bv))))))
86 sz)))))
87 86
88(ert-deftest bool-vector-count-matches-all-0-t () 87(ert-deftest bool-vector-count-population-all-1-t ()
89 (cl-loop for sz in '(0 45 1 64 9 344) 88 (cl-loop for sz in '(0 45 1 64 9 344)
90 do (let* ((bv (make-bool-vector sz nil))) 89 do (let* ((bv (make-bool-vector sz t)))
91 (should 90 (should
92 (eql 91 (eql
93 (bool-vector-count-matches bv t) 92 (bool-vector-count-population bv)
94 0))))) 93 sz)))))
95 94
96(ert-deftest bool-vector-count-matches-1-nil () 95(ert-deftest bool-vector-count-population-1-nil ()
97 (let* ((bv (make-bool-vector 45 nil))) 96 (let* ((bv (make-bool-vector 45 nil)))
98 (aset bv 40 t) 97 (aset bv 40 t)
99 (aset bv 0 t) 98 (aset bv 0 t)
100 (should 99 (should
101 (eql 100 (eql
102 (bool-vector-count-matches bv t) 101 (bool-vector-count-population bv)
103 2))) 102 2))))
104 )
105 103
106(ert-deftest bool-vector-count-matches-1-t () 104(ert-deftest bool-vector-count-population-1-t ()
107 (let* ((bv (make-bool-vector 45 nil))) 105 (let* ((bv (make-bool-vector 45 t)))
108 (aset bv 40 t) 106 (aset bv 40 nil)
109 (aset bv 0 t) 107 (aset bv 0 nil)
110 (should 108 (should
111 (eql 109 (eql
112 (bool-vector-count-matches bv nil) 110 (bool-vector-count-population bv)
113 43)))) 111 43))))
114 112
115(defun mock-bool-vector-count-matches-at (a b i) 113(defun mock-bool-vector-count-consecutive (a b i)
116 (loop for i from i below (length a) 114 (loop for i from i below (length a)
117 while (eq (aref a i) b) 115 while (eq (aref a i) b)
118 sum 1)) 116 sum 1))
@@ -147,8 +145,8 @@
147 (nreverse nibbles) 145 (nreverse nibbles)
148 ""))) 146 "")))
149 147
150(defun test-bool-vector-count-matches-at-tc (desc) 148(defun test-bool-vector-count-consecutive-tc (desc)
151 "Run a test case for bool-vector-count-matches-at. 149 "Run a test case for bool-vector-count-consecutive.
152DESC is a string describing the test. It is a sequence of 150DESC is a string describing the test. It is a sequence of
153hexadecimal digits describing the bool vector. We exhaustively 151hexadecimal digits describing the bool vector. We exhaustively
154test all counts at all possible positions in the vector by 152test all counts at all possible positions in the vector by
@@ -158,8 +156,8 @@ comparing the subr with a much slower lisp implementation."
158 for lf in '(nil t) 156 for lf in '(nil t)
159 do (loop 157 do (loop
160 for pos from 0 upto (length bv) 158 for pos from 0 upto (length bv)
161 for cnt = (mock-bool-vector-count-matches-at bv lf pos) 159 for cnt = (mock-bool-vector-count-consecutive bv lf pos)
162 for rcnt = (bool-vector-count-matches-at bv lf pos) 160 for rcnt = (bool-vector-count-consecutive bv lf pos)
163 unless (eql cnt rcnt) 161 unless (eql cnt rcnt)
164 do (error "FAILED testcase %S %3S %3S %3S" 162 do (error "FAILED testcase %S %3S %3S %3S"
165 pos lf cnt rcnt))))) 163 pos lf cnt rcnt)))))
@@ -182,8 +180,8 @@ comparing the subr with a much slower lisp implementation."
182 "0000000000000000000000000" 180 "0000000000000000000000000"
183 "FFFFFFFFFFFFFFFF1")) 181 "FFFFFFFFFFFFFFFF1"))
184 182
185(ert-deftest bool-vector-count-matches-at () 183(ert-deftest bool-vector-count-consecutive ()
186 (mapc #'test-bool-vector-count-matches-at-tc 184 (mapc #'test-bool-vector-count-consecutive-tc
187 bool-vector-test-vectors)) 185 bool-vector-test-vectors))
188 186
189(defun test-bool-vector-apply-mock-op (mock a b c) 187(defun test-bool-vector-apply-mock-op (mock a b c)