summaryrefslogtreecommitdiffstats
path: root/stdlib
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2023-06-28 21:26:33 -0700
committerKaz Kylheku <kaz@kylheku.com>2023-06-28 21:26:33 -0700
commit1c583965749a40cdbe15846c2487f32a426dcbeb (patch)
tree394bd3a195c221e3c885d474b036b52ebc4ac0e3 /stdlib
parent8ee92a458a4b854b4a7e726975c1197be877c500 (diff)
downloadtxr-1c583965749a40cdbe15846c2487f32a426dcbeb.tar.gz
txr-1c583965749a40cdbe15846c2487f32a426dcbeb.tar.bz2
txr-1c583965749a40cdbe15846c2487f32a426dcbeb.zip
New cached sorting functions.
These functions are useful when sorting a sequence using an expensive keyfun. * autoload.c (csort_set_entries, csort_instantiate): New static functions. (autlod_init): Register autoloading of csort module via new functions. * stdlib/csort.tl: New file. * tests/012/sort.tl: csort functions included in tests. * txr.1: Documented. * stdlib/doc-syms.tl: Updated.
Diffstat (limited to 'stdlib')
-rw-r--r--stdlib/csort.tl43
-rw-r--r--stdlib/doc-syms.tl4
2 files changed, 47 insertions, 0 deletions
diff --git a/stdlib/csort.tl b/stdlib/csort.tl
new file mode 100644
index 00000000..44715236
--- /dev/null
+++ b/stdlib/csort.tl
@@ -0,0 +1,43 @@
+;; Copyright 2023
+;; Kaz Kylheku <kaz@kylheku.com>
+;; Vancouver, Canada
+;; All rights reserved.
+;;
+;; Redistribution and use in source and binary forms, with or without
+;; modification, are permitted provided that the following conditions are met:
+;;
+;; 1. Redistributions of source code must retain the above copyright notice,
+;; this list of conditions and the following disclaimer.
+;;
+;; 2. Redistributions in binary form must reproduce the above copyright notice,
+;; this list of conditions and the following disclaimer in the documentation
+;; and/or other materials provided with the distribution.
+;;
+;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+;; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+;; ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+;; CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+;; ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+;; POSSIBILITY OF SUCH DAMAGE.
+
+(defmacro cached-sort-body (sort-fn)
+ ^(if (eq key-fun :)
+ (,sort-fn seq less-fun key-fun)
+ (,sort-fn seq less-fun (hash-map key-fun seq :eq-based))))
+
+(defun csort (seq : (less-fun :) (key-fun :))
+ (cached-sort-body sort))
+
+(defun cnsort (seq : (less-fun :) (key-fun :))
+ (cached-sort-body nsort))
+
+(defun cssort (seq : (less-fun :) (key-fun :))
+ (cached-sort-body ssort))
+
+(defun csnsort (seq : (less-fun :) (key-fun :))
+ (cached-sort-body snsort))
diff --git a/stdlib/doc-syms.tl b/stdlib/doc-syms.tl
index 4d4ee04d..47930ce8 100644
--- a/stdlib/doc-syms.tl
+++ b/stdlib/doc-syms.tl
@@ -350,6 +350,7 @@
("closure" "N-0216EF16")
("cmp-str" "N-0143A273")
("cmspar" "N-01B1B5DF")
+ ("cnsort" "N-02102493")
("coded-length" "N-0167F423")
("coll" "D-0058")
("collect" "D-000E")
@@ -447,6 +448,9 @@
("cs7" "N-01B1B5DF")
("cs8" "N-01B1B5DF")
("csize" "N-01B1B5DF")
+ ("csnsort" "N-02102493")
+ ("csort" "N-02102493")
+ ("cssort" "N-02102493")
("cstopb" "N-01B1B5DF")
("cum-norm-dist" "N-03AB449B")
("cxr" "N-01DA4F04")