[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dash 11907f4 145/316: Speed up `-uniq` with hash-table.
From: |
ELPA Syncer |
Subject: |
[elpa] externals/dash 11907f4 145/316: Speed up `-uniq` with hash-table. (#305) |
Date: |
Mon, 15 Feb 2021 15:57:45 -0500 (EST) |
branch: externals/dash
commit 11907f4592ff1813536366d54245d3ecf6b99198
Merge: 77f3bf4 a5706bb
Author: Matus Goljer <goljer@logio.cz>
Commit: GitHub <noreply@github.com>
Speed up `-uniq` with hash-table. (#305)
Speed up `-uniq` with hash-table.
---
dash.el | 19 ++++++++++++++++---
dev/examples.el | 14 +++++++++++++-
2 files changed, 29 insertions(+), 4 deletions(-)
diff --git a/dash.el b/dash.el
index 587c074..f48c517 100644
--- a/dash.el
+++ b/dash.el
@@ -2274,9 +2274,22 @@ The test for equality is done with `equal',
or with `-compare-fn' if that's non-nil.
Alias: `-uniq'"
- (let (result)
- (--each list (unless (-contains? result it) (!cons it result)))
- (nreverse result)))
+ ;; Implementation note: The speedup gained from hash table lookup
+ ;; starts to outweigh its overhead for lists of length greater than
+ ;; 32. See discussion in PR #305.
+ (let* ((len (length list))
+ (lut (and (> len 32)
+ ;; Check that `-compare-fn' is a valid hash-table
+ ;; lookup function or `nil'.
+ (memq -compare-fn '(nil equal eq eql))
+ (make-hash-table :test (or -compare-fn #'equal)
+ :size len))))
+ (if lut
+ (--filter (unless (gethash it lut)
+ (puthash it t lut))
+ list)
+ (--each list (unless (-contains? lut it) (!cons it lut)))
+ (nreverse lut))))
(defalias '-uniq '-distinct)
diff --git a/dev/examples.el b/dev/examples.el
index ff7a92d..7142370 100644
--- a/dev/examples.el
+++ b/dev/examples.el
@@ -692,7 +692,19 @@ new list."
(defexamples -distinct
(-distinct '()) => '()
- (-distinct '(1 2 2 4)) => '(1 2 4)))
+ (-distinct '(1 2 2 4)) => '(1 2 4)
+ (-distinct '(t t t)) => '(t)
+ (-distinct '(nil nil nil)) => '(nil)
+ (let ((-compare-fn nil))
+ (-distinct '((1) (2) (1) (1)))) => '((1) (2))
+ (let ((-compare-fn #'eq))
+ (-distinct '((1) (2) (1) (1)))) => '((1) (2) (1) (1))
+ (let ((-compare-fn #'eq))
+ (-distinct '(:a :b :a :a))) => '(:a :b)
+ (let ((-compare-fn #'eql))
+ (-distinct '(2.1 3.1 2.1 2.1))) => '(2.1 3.1)
+ (let ((-compare-fn #'string=))
+ (-distinct '(dash "dash" "ash" "cash" "bash"))) => '(dash "ash" "cash"
"bash")))
(def-example-group "Other list operations"
"Other list functions not fit to be classified elsewhere."
- [elpa] externals/dash 26f065f 129/316: Merge pull request #282 from yyoncho/anamorphic-doto, (continued)
- [elpa] externals/dash 26f065f 129/316: Merge pull request #282 from yyoncho/anamorphic-doto, ELPA Syncer, 2021/02/15
- [elpa] externals/dash 4abffdc 123/316: Update docs, ELPA Syncer, 2021/02/15
- [elpa] externals/dash 677c156 134/316: Merge pull request #290 from leungbk/rotate, ELPA Syncer, 2021/02/15
- [elpa] externals/dash 1549860 139/316: Merge pull request #296 from cireu/fix-hash-opt-expander, ELPA Syncer, 2021/02/15
- [elpa] externals/dash 93e0465 137/316: Remove dependecy `macroexp`, ELPA Syncer, 2021/02/15
- [elpa] externals/dash a358b79 143/316: Speed up `-uniq` with hash-table., ELPA Syncer, 2021/02/15
- [elpa] externals/dash 77f3bf4 142/316: Merge pull request #302 from bbatsov/patch-1, ELPA Syncer, 2021/02/15
- [elpa] externals/dash 38dc929 222/316: Fix, improve, and extend anaphoric folds, ELPA Syncer, 2021/02/15
- [elpa] externals/dash d308676 225/316: Fix signal argument and case where start is null, ELPA Syncer, 2021/02/15
- [elpa] externals/dash 994cda9 228/316: Simplify -cons-pair?, ELPA Syncer, 2021/02/15
- [elpa] externals/dash 11907f4 145/316: Speed up `-uniq` with hash-table. (#305),
ELPA Syncer <=
- [elpa] externals/dash ad21e13 146/316: Ignore all .elc and TAGS files, ELPA Syncer, 2021/02/15
- [elpa] externals/dash fae51b5 147/316: Make -inits not destroy its argument, ELPA Syncer, 2021/02/15
- [elpa] externals/dash ce1294b 152/316: Optimize non-destructive -inits, ELPA Syncer, 2021/02/15
- [elpa] externals/dash a743ae3 153/316: Merge pull request #313 from SwiftLawnGnome/master, ELPA Syncer, 2021/02/15
- [elpa] externals/dash 62707a6 154/316: Avoid interning unused symbols in destructuring, ELPA Syncer, 2021/02/15
- [elpa] externals/dash 9631947 155/316: Merge pull request #317 from cireu/feat/intern-soft, ELPA Syncer, 2021/02/15
- [elpa] externals/dash 0ee27a4 157/316: Clarify docs on -zip-pair future behaviour, ELPA Syncer, 2021/02/15
- [elpa] externals/dash 5f65fdf 159/316: Merge pull request #321 from wbolster/some-macro-indentation, ELPA Syncer, 2021/02/15
- [elpa] externals/dash 13e5366 161/316: Merge pull request #323 from tarsiiformes/typos, ELPA Syncer, 2021/02/15
- [elpa] externals/dash c645ed6 162/316: use bash for pre-commit script, ELPA Syncer, 2021/02/15