]> code.delx.au - gnu-emacs/commitdiff
Improve API of recently-added bool vector functions.
authorPaul Eggert <eggert@cs.ucla.edu>
Mon, 18 Nov 2013 18:56:42 +0000 (10:56 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Mon, 18 Nov 2013 18:56:42 +0000 (10:56 -0800)
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
etc/NEWS
src/ChangeLog
src/data.c
test/ChangeLog
test/automated/data-tests.el

index 132c461b25821876c4ad521e2a13de7c667ca215..b68978b8ed9a2823aca8fe71b2c5a926a2790081 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -736,8 +736,8 @@ frame.
 *** `bool-vector-set-difference'
 *** `bool-vector-not'
 *** `bool-vector-subsetp'
-*** `bool-vector-count-matches'
-*** `bool-vector-count-matches-at'
+*** `bool-vector-count-consecutive'
+*** `bool-vector-count-population'
 
 ** Comparison functions =, <, >, <=, >= now take many arguments.
 
index 21025c677fc21e0037376d98be348fcdf1050fb1..d6edcea4513ec3121d6901749604bb298ac4dd7f 100644 (file)
@@ -1,5 +1,19 @@
 2013-11-18  Paul Eggert  <eggert@cs.ucla.edu>
 
+       Improve API of recently-added bool vector functions (Bug#15912).
+       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.
+       * 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.
+
        Always allocate at least one bits_word per bool vector.
        See Daniel Colascione in:
        http://lists.gnu.org/archive/html/emacs-devel/2013-11/msg00518.html
index b8b0f248dfa7bb9d285bfef8efe7d0f180c0976c..2c789f374315eea792f93c4f95d2c70038e1b1dc 100644 (file)
@@ -3282,11 +3282,12 @@ Return the destination vector.  */)
   return b;
 }
 
-DEFUN ("bool-vector-count-matches", Fbool_vector_count_matches,
-       Sbool_vector_count_matches, 2, 2, 0,
-       doc: /* Count how many elements in A equal B.
-A must be a bool vector.  B is a generalized bool.  */)
-  (Lisp_Object a, Lisp_Object b)
+DEFUN ("bool-vector-count-population", Fbool_vector_count_population,
+       Sbool_vector_count_population, 1, 1, 0,
+       doc: /* Count how many elements in A are t.
+A is a bool vector.  To count A's nil elements, subtract the return
+value from A's length.  */)
+  (Lisp_Object a)
 {
   EMACS_INT count;
   EMACS_INT nr_bits;
@@ -3303,17 +3304,13 @@ A must be a bool vector.  B is a generalized bool.  */)
   for (i = 0; i < nwords; i++)
     count += count_one_bits_word (adata[i]);
 
-  if (NILP (b))
-    count = nr_bits - count;
   return make_number (count);
 }
 
-DEFUN ("bool-vector-count-matches-at",
-       Fbool_vector_count_matches_at,
-       Sbool_vector_count_matches_at, 3, 3, 0,
-       doc: /* Count how many consecutive elements in A equal B at i.
-A must be a bool vector.  B is a generalized boolean.  i is an
-index into the vector.  */)
+DEFUN ("bool-vector-count-consecutive", Fbool_vector_count_consecutive,
+       Sbool_vector_count_consecutive, 3, 3, 0,
+       doc: /* Count how many consecutive elements in A equal B starting at I.
+A is a bool vector, B is t or nil, and I is an index into A.  */)
   (Lisp_Object a, Lisp_Object b, Lisp_Object i)
 {
   EMACS_INT count;
@@ -3660,8 +3657,8 @@ syms_of_data (void)
   defsubr (&Sbool_vector_set_difference);
   defsubr (&Sbool_vector_not);
   defsubr (&Sbool_vector_subsetp);
-  defsubr (&Sbool_vector_count_matches);
-  defsubr (&Sbool_vector_count_matches_at);
+  defsubr (&Sbool_vector_count_consecutive);
+  defsubr (&Sbool_vector_count_population);
 
   set_symbol_function (Qwholenump, XSYMBOL (Qnatnump)->function);
 
index 79d9ab544e0bd31f9b83183edaade53ba69871fb..35a168ce0c43584bb277b8118e07db7962a125ce 100644 (file)
@@ -1,3 +1,8 @@
+2013-11-18  Paul Eggert  <eggert@cs.ucla.edu>
+
+       Improve API of recently-added bool vector functions (Bug#15912).
+       * automated/data-tests.el: Adjust to API changes.
+
 2013-11-16  Michael Albinus  <michael.albinus@gmx.de>
 
        * automated/tramp-tests.el (tramp-test07-file-exists-p)
index d79e16438482c7f0e677d943fa94a753edd29c07..f731b8bf1db012167d3d077c03fd7f3fadebdf1e 100644 (file)
 ;; Bool vector tests.  Compactly represent bool vectors as hex
 ;; strings.
 
-(ert-deftest bool-vector-count-matches-all-0-nil ()
+(ert-deftest bool-vector-count-population-all-0-nil ()
   (cl-loop for sz in '(0 45 1 64 9 344)
            do (let* ((bv (make-bool-vector sz nil)))
                 (should
-                 (eql
-                  (bool-vector-count-matches bv nil)
-                  sz)))))
+                 (zerop
+                  (bool-vector-count-population bv))))))
 
-(ert-deftest bool-vector-count-matches-all-0-t ()
+(ert-deftest bool-vector-count-population-all-1-t ()
   (cl-loop for sz in '(0 45 1 64 9 344)
-           do (let* ((bv (make-bool-vector sz nil)))
+           do (let* ((bv (make-bool-vector sz t)))
                 (should
                  (eql
-                  (bool-vector-count-matches bv t)
-                  0)))))
+                  (bool-vector-count-population bv)
+                  sz)))))
 
-(ert-deftest bool-vector-count-matches-1-nil ()
+(ert-deftest bool-vector-count-population-1-nil ()
   (let* ((bv (make-bool-vector 45 nil)))
     (aset bv 40 t)
     (aset bv 0 t)
     (should
      (eql
-      (bool-vector-count-matches bv t)
-      2)))
-  )
+      (bool-vector-count-population bv)
+      2))))
 
-(ert-deftest bool-vector-count-matches-1-t ()
-  (let* ((bv (make-bool-vector 45 nil)))
-    (aset bv 40 t)
-    (aset bv 0 t)
+(ert-deftest bool-vector-count-population-1-t ()
+  (let* ((bv (make-bool-vector 45 t)))
+    (aset bv 40 nil)
+    (aset bv 0 nil)
     (should
      (eql
-      (bool-vector-count-matches bv nil)
+      (bool-vector-count-population bv)
       43))))
 
-(defun mock-bool-vector-count-matches-at (a b i)
+(defun mock-bool-vector-count-consecutive (a b i)
   (loop for i from i below (length a)
         while (eq (aref a i) b)
         sum 1))
                (nreverse nibbles)
                "")))
 
-(defun test-bool-vector-count-matches-at-tc (desc)
-  "Run a test case for bool-vector-count-matches-at.
+(defun test-bool-vector-count-consecutive-tc (desc)
+  "Run a test case for bool-vector-count-consecutive.
 DESC is a string describing the test.  It is a sequence of
 hexadecimal digits describing the bool vector.  We exhaustively
 test all counts at all possible positions in the vector by
@@ -158,8 +156,8 @@ comparing the subr with a much slower lisp implementation."
      for lf in '(nil t)
      do (loop
          for pos from 0 upto (length bv)
-         for cnt = (mock-bool-vector-count-matches-at bv lf pos)
-         for rcnt = (bool-vector-count-matches-at bv lf pos)
+         for cnt = (mock-bool-vector-count-consecutive bv lf pos)
+         for rcnt = (bool-vector-count-consecutive bv lf pos)
          unless (eql cnt rcnt)
          do (error "FAILED testcase %S %3S %3S %3S"
                    pos lf cnt rcnt)))))
@@ -182,8 +180,8 @@ comparing the subr with a much slower lisp implementation."
   "0000000000000000000000000"
   "FFFFFFFFFFFFFFFF1"))
 
-(ert-deftest bool-vector-count-matches-at ()
-  (mapc #'test-bool-vector-count-matches-at-tc
+(ert-deftest bool-vector-count-consecutive ()
+  (mapc #'test-bool-vector-count-consecutive-tc
         bool-vector-test-vectors))
 
 (defun test-bool-vector-apply-mock-op (mock a b c)