Small elisp library for efficiently maintaining a sorted list
. Internally, this uses an AVL-tree to store the cells of the list, but the sorted list is also available as a regular lisp list (that is the point). Note that because of the high constant factor of AVL-trees, it only makes sense to use this data structure if there are many lookups / updates and the list is large enough.
;; create a sorted list from a regular list, providing a comparison function implementing the sort order.
(setq mysortlist (sorted-list-create '(4 3 5 10 7) '<))
(sorted-list-member-p mysortlist 5) ;; runs in O(log n)
(pp (sorted-list-list mysortlist)) ;; get the underlying sorted lisp list
(sorted-list-insert mysortlist 1) ;; runs in O(log n)
(pp (sorted-list-list mysortlist)) ;; get the underlying sorted lisp list
(sorted-list-delete mysortlist 5) ;; runs in O(log n)
(pp (sorted-list-list mysortlist)) ;; get the underlying sorted lisp list
;; benchmark searching the last element in a list (1 ... 300000) using sorted list vs. regular list
(setq mysortlist (sorted-list-create (number-sequence 1 300000) '<))
(setq myunsortlist (number-sequence 1 300000))
(benchmark 10
'(dotimes (i 1000) (sorted-list-member-p mysortlist 299999)))
;; Elapsed time: 0.065488s
(benchmark 10
'(dotimes (i 1000) (member 299999 myunsortlist)))
;; Elapsed time: 15.424318s
Using use-package with quelpa.
(use-package
:quelpa ((sorted-list
:fetcher github
:repo "lordpretzel/sorted-list")
:upgrade t)
)
Using use-package with straight.el
(use-package sorted-list
:straight (sorted-list :type git :host github :repo "lordpretzel/sorted-list")
Alternatively, install from source. First, clone the source code:
cd MY-PATH
git clone https://github.com/lordpretzel/sorted-list.git
Now, from Emacs execute:
M-x package-install-file RET MY-PATH/sorted-list
Alternatively to the second step, add this to your Symbol’s value as variable is void: .emacs file:
(add-to-list 'load-path "MY-PATH/sorted-list")
(require 'sorted-list)