diff options
| author | Dmitry Antipov | 2014-08-29 15:02:56 +0400 |
|---|---|---|
| committer | Dmitry Antipov | 2014-08-29 15:02:56 +0400 |
| commit | dd958fb246a13c024ce58415c3de99f4390bcd77 (patch) | |
| tree | 3ef3b5be28a1702e7c5b5276dfd77503b0492061 | |
| parent | f0e709846e9a5a6b77a2f526fed37624771f19e6 (diff) | |
| download | emacs-dd958fb246a13c024ce58415c3de99f4390bcd77.tar.gz emacs-dd958fb246a13c024ce58415c3de99f4390bcd77.zip | |
* doc/lispref/lists.texi (Functions that Rearrange Lists): Remove
description of sort ...
* doc/lispref/sequences.texi (Sequence Functions): ... and generalize
it for sequences. Add an example.
* src/fns.c (Fsort): Use more natural Qsequencep error.
* test/automated/fns-tests.el (fns-tests-sort): Minor style rewrite.
| -rw-r--r-- | doc/lispref/ChangeLog | 7 | ||||
| -rw-r--r-- | doc/lispref/lists.texi | 68 | ||||
| -rw-r--r-- | doc/lispref/sequences.texi | 92 | ||||
| -rw-r--r-- | src/fns.c | 2 | ||||
| -rw-r--r-- | test/automated/fns-tests.el | 4 |
5 files changed, 102 insertions, 71 deletions
diff --git a/doc/lispref/ChangeLog b/doc/lispref/ChangeLog index 783be72e830..a9954d84658 100644 --- a/doc/lispref/ChangeLog +++ b/doc/lispref/ChangeLog | |||
| @@ -1,3 +1,10 @@ | |||
| 1 | 2014-08-29 Dmitry Antipov <dmantipov@yandex.ru> | ||
| 2 | |||
| 3 | * lists.texi (Functions that Rearrange Lists): Remove | ||
| 4 | description of sort ... | ||
| 5 | * sequences.texi (Sequence Functions): ... and generalize | ||
| 6 | it for sequences. Add an example. | ||
| 7 | |||
| 1 | 2014-08-28 Eli Zaretskii <eliz@gnu.org> | 8 | 2014-08-28 Eli Zaretskii <eliz@gnu.org> |
| 2 | 9 | ||
| 3 | * display.texi (Bidirectional Display): Update the Emacs's class | 10 | * display.texi (Bidirectional Display): Update the Emacs's class |
diff --git a/doc/lispref/lists.texi b/doc/lispref/lists.texi index f724d5bd902..21be5cca4fc 100644 --- a/doc/lispref/lists.texi +++ b/doc/lispref/lists.texi | |||
| @@ -1124,74 +1124,6 @@ each time you run it! Here is what happens: | |||
| 1124 | @end smallexample | 1124 | @end smallexample |
| 1125 | @end defun | 1125 | @end defun |
| 1126 | 1126 | ||
| 1127 | @defun sort list predicate | ||
| 1128 | @cindex stable sort | ||
| 1129 | @cindex sorting lists | ||
| 1130 | This function sorts @var{list} stably, though destructively, and | ||
| 1131 | returns the sorted list. It compares elements using @var{predicate}. A | ||
| 1132 | stable sort is one in which elements with equal sort keys maintain their | ||
| 1133 | relative order before and after the sort. Stability is important when | ||
| 1134 | successive sorts are used to order elements according to different | ||
| 1135 | criteria. | ||
| 1136 | |||
| 1137 | The argument @var{predicate} must be a function that accepts two | ||
| 1138 | arguments. It is called with two elements of @var{list}. To get an | ||
| 1139 | increasing order sort, the @var{predicate} should return non-@code{nil} if the | ||
| 1140 | first element is ``less than'' the second, or @code{nil} if not. | ||
| 1141 | |||
| 1142 | The comparison function @var{predicate} must give reliable results for | ||
| 1143 | any given pair of arguments, at least within a single call to | ||
| 1144 | @code{sort}. It must be @dfn{antisymmetric}; that is, if @var{a} is | ||
| 1145 | less than @var{b}, @var{b} must not be less than @var{a}. It must be | ||
| 1146 | @dfn{transitive}---that is, if @var{a} is less than @var{b}, and @var{b} | ||
| 1147 | is less than @var{c}, then @var{a} must be less than @var{c}. If you | ||
| 1148 | use a comparison function which does not meet these requirements, the | ||
| 1149 | result of @code{sort} is unpredictable. | ||
| 1150 | |||
| 1151 | The destructive aspect of @code{sort} is that it rearranges the cons | ||
| 1152 | cells forming @var{list} by changing @sc{cdr}s. A nondestructive sort | ||
| 1153 | function would create new cons cells to store the elements in their | ||
| 1154 | sorted order. If you wish to make a sorted copy without destroying the | ||
| 1155 | original, copy it first with @code{copy-sequence} and then sort. | ||
| 1156 | |||
| 1157 | Sorting does not change the @sc{car}s of the cons cells in @var{list}; | ||
| 1158 | the cons cell that originally contained the element @code{a} in | ||
| 1159 | @var{list} still has @code{a} in its @sc{car} after sorting, but it now | ||
| 1160 | appears in a different position in the list due to the change of | ||
| 1161 | @sc{cdr}s. For example: | ||
| 1162 | |||
| 1163 | @example | ||
| 1164 | @group | ||
| 1165 | (setq nums '(1 3 2 6 5 4 0)) | ||
| 1166 | @result{} (1 3 2 6 5 4 0) | ||
| 1167 | @end group | ||
| 1168 | @group | ||
| 1169 | (sort nums '<) | ||
| 1170 | @result{} (0 1 2 3 4 5 6) | ||
| 1171 | @end group | ||
| 1172 | @group | ||
| 1173 | nums | ||
| 1174 | @result{} (1 2 3 4 5 6) | ||
| 1175 | @end group | ||
| 1176 | @end example | ||
| 1177 | |||
| 1178 | @noindent | ||
| 1179 | @strong{Warning}: Note that the list in @code{nums} no longer contains | ||
| 1180 | 0; this is the same cons cell that it was before, but it is no longer | ||
| 1181 | the first one in the list. Don't assume a variable that formerly held | ||
| 1182 | the argument now holds the entire sorted list! Instead, save the result | ||
| 1183 | of @code{sort} and use that. Most often we store the result back into | ||
| 1184 | the variable that held the original list: | ||
| 1185 | |||
| 1186 | @example | ||
| 1187 | (setq nums (sort nums '<)) | ||
| 1188 | @end example | ||
| 1189 | |||
| 1190 | @xref{Sorting}, for more functions that perform sorting. | ||
| 1191 | See @code{documentation} in @ref{Accessing Documentation}, for a | ||
| 1192 | useful example of @code{sort}. | ||
| 1193 | @end defun | ||
| 1194 | |||
| 1195 | @node Sets And Lists | 1127 | @node Sets And Lists |
| 1196 | @section Using Lists as Sets | 1128 | @section Using Lists as Sets |
| 1197 | @cindex lists as sets | 1129 | @cindex lists as sets |
diff --git a/doc/lispref/sequences.texi b/doc/lispref/sequences.texi index 8f17862d427..d3a6792c1ba 100644 --- a/doc/lispref/sequences.texi +++ b/doc/lispref/sequences.texi | |||
| @@ -327,6 +327,98 @@ encouraged to treat strings as immutable. | |||
| 327 | 327 | ||
| 328 | @end defun | 328 | @end defun |
| 329 | 329 | ||
| 330 | @defun sort sequence predicate | ||
| 331 | @cindex stable sort | ||
| 332 | @cindex sorting lists | ||
| 333 | @cindex sorting vectors | ||
| 334 | This function sorts @var{sequence} stably. Note that this function doesn't work | ||
| 335 | for all sequences; it may be used only for lists and vectors. If @var{sequence} | ||
| 336 | is a list, it is modified destructively. This functions returns the sorted | ||
| 337 | @var{sequence} and compares elements using @var{predicate}. A stable sort is | ||
| 338 | one in which elements with equal sort keys maintain their relative order before | ||
| 339 | and after the sort. Stability is important when successive sorts are used to | ||
| 340 | order elements according to different criteria. | ||
| 341 | |||
| 342 | The argument @var{predicate} must be a function that accepts two | ||
| 343 | arguments. It is called with two elements of @var{sequence}. To get an | ||
| 344 | increasing order sort, the @var{predicate} should return non-@code{nil} if the | ||
| 345 | first element is ``less than'' the second, or @code{nil} if not. | ||
| 346 | |||
| 347 | The comparison function @var{predicate} must give reliable results for | ||
| 348 | any given pair of arguments, at least within a single call to | ||
| 349 | @code{sort}. It must be @dfn{antisymmetric}; that is, if @var{a} is | ||
| 350 | less than @var{b}, @var{b} must not be less than @var{a}. It must be | ||
| 351 | @dfn{transitive}---that is, if @var{a} is less than @var{b}, and @var{b} | ||
| 352 | is less than @var{c}, then @var{a} must be less than @var{c}. If you | ||
| 353 | use a comparison function which does not meet these requirements, the | ||
| 354 | result of @code{sort} is unpredictable. | ||
| 355 | |||
| 356 | The destructive aspect of @code{sort} for lists is that it rearranges the | ||
| 357 | cons cells forming @var{sequence} by changing @sc{cdr}s. A nondestructive | ||
| 358 | sort function would create new cons cells to store the elements in their | ||
| 359 | sorted order. If you wish to make a sorted copy without destroying the | ||
| 360 | original, copy it first with @code{copy-sequence} and then sort. | ||
| 361 | |||
| 362 | Sorting does not change the @sc{car}s of the cons cells in @var{sequence}; | ||
| 363 | the cons cell that originally contained the element @code{a} in | ||
| 364 | @var{sequence} still has @code{a} in its @sc{car} after sorting, but it now | ||
| 365 | appears in a different position in the list due to the change of | ||
| 366 | @sc{cdr}s. For example: | ||
| 367 | |||
| 368 | @example | ||
| 369 | @group | ||
| 370 | (setq nums '(1 3 2 6 5 4 0)) | ||
| 371 | @result{} (1 3 2 6 5 4 0) | ||
| 372 | @end group | ||
| 373 | @group | ||
| 374 | (sort nums '<) | ||
| 375 | @result{} (0 1 2 3 4 5 6) | ||
| 376 | @end group | ||
| 377 | @group | ||
| 378 | nums | ||
| 379 | @result{} (1 2 3 4 5 6) | ||
| 380 | @end group | ||
| 381 | @end example | ||
| 382 | |||
| 383 | @noindent | ||
| 384 | @strong{Warning}: Note that the list in @code{nums} no longer contains | ||
| 385 | 0; this is the same cons cell that it was before, but it is no longer | ||
| 386 | the first one in the list. Don't assume a variable that formerly held | ||
| 387 | the argument now holds the entire sorted list! Instead, save the result | ||
| 388 | of @code{sort} and use that. Most often we store the result back into | ||
| 389 | the variable that held the original list: | ||
| 390 | |||
| 391 | @example | ||
| 392 | (setq nums (sort nums '<)) | ||
| 393 | @end example | ||
| 394 | |||
| 395 | For the better understanding of what stable sort is, consider the following | ||
| 396 | vector example. After sorting, all items whose @code{car} is 8 are grouped | ||
| 397 | at the beginning of @code{vector}, but their relative order is preserved. | ||
| 398 | All items whose @code{car} is 9 are grouped at the end of @code{vector}, | ||
| 399 | but their relative order is also preserved: | ||
| 400 | |||
| 401 | @example | ||
| 402 | @group | ||
| 403 | (setq | ||
| 404 | vector | ||
| 405 | (vector '(8 . "xxx") '(9 . "aaa") '(8 . "bbb") '(9 . "zzz") | ||
| 406 | '(9 . "ppp") '(8 . "ttt") '(8 . "eee") '(9 . "fff"))) | ||
| 407 | @result{} [(8 . "xxx") (9 . "aaa") (8 . "bbb") (9 . "zzz") | ||
| 408 | (9 . "ppp") (8 . "ttt") (8 . "eee") (9 . "fff")] | ||
| 409 | @end group | ||
| 410 | @group | ||
| 411 | (sort vector (lambda (x y) (< (car x) (car y)))) | ||
| 412 | @result{} [(8 . "xxx") (8 . "bbb") (8 . "ttt") (8 . "eee") | ||
| 413 | (9 . "aaa") (9 . "zzz") (9 . "ppp") (9 . "fff")] | ||
| 414 | @end group | ||
| 415 | @end example | ||
| 416 | |||
| 417 | @xref{Sorting}, for more functions that perform sorting. | ||
| 418 | See @code{documentation} in @ref{Accessing Documentation}, for a | ||
| 419 | useful example of @code{sort}. | ||
| 420 | @end defun | ||
| 421 | |||
| 330 | @node Arrays | 422 | @node Arrays |
| 331 | @section Arrays | 423 | @section Arrays |
| 332 | @cindex array | 424 | @cindex array |
| @@ -1958,7 +1958,7 @@ if the first element should sort before the second. */) | |||
| 1958 | else if (VECTORP (seq)) | 1958 | else if (VECTORP (seq)) |
| 1959 | seq = sort_vector (seq, predicate); | 1959 | seq = sort_vector (seq, predicate); |
| 1960 | else if (!NILP (seq)) | 1960 | else if (!NILP (seq)) |
| 1961 | wrong_type_argument (Qarrayp, seq); | 1961 | wrong_type_argument (Qsequencep, seq); |
| 1962 | return seq; | 1962 | return seq; |
| 1963 | } | 1963 | } |
| 1964 | 1964 | ||
diff --git a/test/automated/fns-tests.el b/test/automated/fns-tests.el index a6c45443db6..9f55a873ea4 100644 --- a/test/automated/fns-tests.el +++ b/test/automated/fns-tests.el | |||
| @@ -113,8 +113,8 @@ | |||
| 113 | (should (equal | 113 | (should (equal |
| 114 | (sort | 114 | (sort |
| 115 | (vector | 115 | (vector |
| 116 | (cons 8 "xxx") (cons 9 "aaa") (cons 8 "bbb") (cons 9 "zzz") | 116 | '(8 . "xxx") '(9 . "aaa") '(8 . "bbb") '(9 . "zzz") |
| 117 | (cons 9 "ppp") (cons 8 "ttt") (cons 8 "eee") (cons 9 "fff")) | 117 | '(9 . "ppp") '(8 . "ttt") '(8 . "eee") '(9 . "fff")) |
| 118 | (lambda (x y) (< (car x) (car y)))) | 118 | (lambda (x y) (< (car x) (car y)))) |
| 119 | [(8 . "xxx") (8 . "bbb") (8 . "ttt") (8 . "eee") | 119 | [(8 . "xxx") (8 . "bbb") (8 . "ttt") (8 . "eee") |
| 120 | (9 . "aaa") (9 . "zzz") (9 . "ppp") (9 . "fff")]))) | 120 | (9 . "aaa") (9 . "zzz") (9 . "ppp") (9 . "fff")]))) |