我正在寻找类似的东西#'delete-duplicates
,但我知道列表中的所有元素都已经排序,或者反向排序,或者至少排列使得重复项已经彼此相邻。我希望利用这些知识来确保执行速度与列表中元素数量的平方不成比例。#'maplist
用它来发展我自己的解决方案是微不足道的,但是语言中已经有一些东西了吗?重新发明轮子会很尴尬。
需要明确的是,对于较大长度的列表,我希望删除的运行时间与列表的长度成正比,而不是与该长度的平方成正比。这是我希望避免的行为:
1 (defun one-shot (cardinality)
2 (labels ((generate-list (the-count)
3 (let* ((the-list (make-list the-count)))
4 (do ((iterator 0 (1+ iterator)))
5 ((>= iterator the-count))
6 (setf (nth iterator the-list) iterator))
7 the-list)))
8 (let* ((given-list (generate-list cardinality))
9 (stripped-list)
10 (start-time)
11 (end-time))
12 (setf start-time (get-universal-time))
13 (setf stripped-list (delete-duplicates given-list :test #'eql))
14 (setf end-time (get-universal-time))
15 (princ "for n = ")
16 (princ cardinality)
17 (princ ", #'delete-duplicates took ")
18 (princ (- end-time start-time))
19 (princ " seconds")
20 (terpri))))
21 (one-shot 20000)
22 (one-shot 40000)
23 (one-shot 80000)
for n = 20000, #'delete-duplicates took 6 seconds
for n = 40000, #'delete-duplicates took 24 seconds
for n = 80000, #'delete-duplicates took 95 seconds