summaryrefslogtreecommitdiff
path: root/lisp/gnus/gnus-range.el
blob: 2b9d7fac1db033d33f3303d285ce719f020a87f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
;;; gnus-range.el --- range and sequence functions for Gnus  -*- lexical-binding: t; -*-

;; Copyright (C) 1996-2022 Free Software Foundation, Inc.

;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
;; Keywords: news

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;;; Code:

;;; List and range functions

(require 'range)
(define-obsolete-function-alias 'gnus-range-normalize #'range-normalize "29.1")

(defun gnus-last-element (list)
  "Return last element of LIST."
  (while (cdr list)
    (setq list (cdr list)))
  (car list))
(make-obsolete 'gnus-last-element "use `car' of `last' instead." "27.1")

(define-obsolete-function-alias 'gnus-copy-sequence #'copy-tree "27.1")

;; We could be using `seq-difference' here, but it's much slower
;; on these data sets.  See bug#50877.
(defun gnus-set-difference (list1 list2)
  "Return a list of elements of LIST1 that do not appear in LIST2."
  (let ((hash2 (make-hash-table :test 'eq))
        (result nil))
    (dolist (elt list2) (puthash elt t hash2))
    (dolist (elt list1)
      (unless (gethash elt hash2)
        (setq result (cons elt result))))
    (nreverse result)))

(defun gnus-range-nconcat (&rest ranges)
  "Return a range comprising all the RANGES, which are pre-sorted.
RANGES will be destructively altered."
  (setq ranges (delete nil ranges))
  (let* ((result (range-normalize (pop ranges)))
	 (last (last result)))
    (dolist (range ranges)
      (setq range (range-normalize range))
      ;; Normalize the single-number case, so that we don't need to
      ;; special-case that so much.
      (when (numberp (car last))
	(setcar last (cons (car last) (car last))))
      (when (numberp (car range))
	(setcar range (cons (car range) (car range))))
      (if (= (1+ (cdar last)) (caar range))
	  (progn
	    (setcdr (car last) (cdar range))
	    (setcdr last (cdr range)))
	(setcdr last range)
	;; Denormalize back, since we couldn't join the ranges up.
	(when (= (caar range) (cdar range))
	  (setcar range (caar range)))
	(when (= (caar last) (cdar last))
	  (setcar last (caar last))))
      (setq last (last last)))
    (if (and (consp (car result))
	     (= (length result) 1))
	(car result)
      result)))

(define-obsolete-function-alias 'gnus-range-difference
  #'range-difference "29.1")

;;;###autoload
(defun gnus-sorted-difference (list1 list2)
  "Return a list of elements of LIST1 that do not appear in LIST2.
Both lists have to be sorted over <.
The tail of LIST1 is not copied."
  (let (out)
    (while (and list1 list2)
      (cond ((= (car list1) (car list2))
	     (setq list1 (cdr list1)
		   list2 (cdr list2)))
	    ((< (car list1) (car list2))
	     (setq out (cons (car list1) out))
	     (setq list1 (cdr list1)))
	    (t
	     (setq list2 (cdr list2)))))
    (nconc (nreverse out) list1)))

;;;###autoload
(defun gnus-sorted-ndifference (list1 list2)
  "Return a list of elements of LIST1 that do not appear in LIST2.
Both lists have to be sorted over <.
LIST1 is modified."
  (let* ((top (cons nil list1))
	 (prev top))
    (while (and list1 list2)
      (cond ((= (car list1) (car list2))
	     (setcdr prev (cdr list1))
	     (setq list1 (cdr list1)
		   list2 (cdr list2)))
	    ((< (car list1) (car list2))
	     (setq prev list1
		   list1 (cdr list1)))
	    (t
	     (setq list2 (cdr list2)))))
    (cdr top)))

;;;###autoload
(defun gnus-sorted-complement (list1 list2)
  "Return a list of elements that are in LIST1 or LIST2 but not both.
Both lists have to be sorted over <."
  (let (out)
    (if (or (null list1) (null list2))
	(or list1 list2)
      (while (and list1 list2)
	(cond ((= (car list1) (car list2))
	       (setq list1 (cdr list1)
		     list2 (cdr list2)))
	      ((< (car list1) (car list2))
	       (setq out (cons (car list1) out))
	       (setq list1 (cdr list1)))
	      (t
	       (setq out (cons (car list2) out))
	       (setq list2 (cdr list2)))))
      (nconc (nreverse out) (or list1 list2)))))

;;;###autoload
(defun gnus-intersection (list1 list2)
  (declare (obsolete seq-intersection "28.1"))
  (nreverse (seq-intersection list1 list2 #'eq)))

;;;###autoload
(defun gnus-sorted-intersection (list1 list2)
  "Return intersection of LIST1 and LIST2.
LIST1 and LIST2 have to be sorted over <."
  (let (out)
    (while (and list1 list2)
      (cond ((= (car list1) (car list2))
	     (setq out (cons (car list1) out)
		   list1 (cdr list1)
		   list2 (cdr list2)))
	    ((< (car list1) (car list2))
	     (setq list1 (cdr list1)))
	    (t
	     (setq list2 (cdr list2)))))
    (nreverse out)))

(define-obsolete-function-alias 'gnus-sorted-range-intersection
  #'range-intersection "29.1")

;;;###autoload
(defalias 'gnus-set-sorted-intersection #'gnus-sorted-nintersection)

;;;###autoload
(defun gnus-sorted-nintersection (list1 list2)
  "Return intersection of LIST1 and LIST2 by modifying cdr pointers of LIST1.
LIST1 and LIST2 have to be sorted over <."
  (let* ((top (cons nil list1))
	 (prev top))
    (while (and list1 list2)
      (cond ((= (car list1) (car list2))
	     (setq prev list1
		   list1 (cdr list1)
		   list2 (cdr list2)))
	    ((< (car list1) (car list2))
	     (setcdr prev (cdr list1))
	     (setq list1 (cdr list1)))
	    (t
	     (setq list2 (cdr list2)))))
    (setcdr prev nil)
    (cdr top)))

;;;###autoload
(defun gnus-sorted-union (list1 list2)
  "Return union of LIST1 and LIST2.
LIST1 and LIST2 have to be sorted over <."
  (let (out)
    (while (and list1 list2)
      (cond ((= (car list1) (car list2))
	     (setq out (cons (car list1) out)
		   list1 (cdr list1)
		   list2 (cdr list2)))
	    ((< (car list1) (car list2))
	     (setq out (cons (car list1) out)
		   list1 (cdr list1)))
	    (t
	     (setq out (cons (car list2) out)
		   list2 (cdr list2)))))
    (while list1
      (setq out (cons (car list1) out)
	    list1 (cdr list1)))
    (while list2
      (setq out (cons (car list2) out)
	    list2 (cdr list2)))
    (nreverse out)))

;;;###autoload
(defun gnus-sorted-nunion (list1 list2)
  "Return union of LIST1 and LIST2 by modifying cdr pointers of LIST1.
LIST1 and LIST2 have to be sorted over <."
  (let* ((top (cons nil list1))
	 (prev top))
    (while (and list1 list2)
      (cond ((= (car list1) (car list2))
	     (setq prev list1
		   list1 (cdr list1)
		   list2 (cdr list2)))
	    ((< (car list1) (car list2))
	     (setq prev list1
		   list1 (cdr list1)))
	    (t
	     (setcdr prev (list (car list2)))
	     (setq prev (cdr prev)
		   list2 (cdr list2))
	     (setcdr prev list1))))
    (while list2
      (setcdr prev (list (car list2)))
      (setq prev (cdr prev)
	    list2 (cdr list2)))
    (cdr top)))

(defun gnus-compress-sequence (numbers &optional always-list)
  "Convert sorted list of numbers to a list of ranges or a single range.
If ALWAYS-LIST is non-nil, this function will always release a list of
ranges."
  (if always-list
      (range-compress-list numbers)
    (range-denormalize (range-compress-list numbers))))

(defalias 'gnus-uncompress-sequence #'gnus-uncompress-range)
(define-obsolete-function-alias 'gnus-uncompress-range
  #'range-uncompress "29.1")

(define-obsolete-function-alias 'gnus-add-to-range
  #'range-add-list "29.1")

(define-obsolete-function-alias 'gnus-remove-from-range
  #'range-remove "29.1")

(define-obsolete-function-alias 'gnus-member-of-range #'range-member-p "29.1")

(define-obsolete-function-alias 'gnus-list-range-intersection
  #'range-list-intersection "29.1")

(defalias 'gnus-inverse-list-range-intersection #'range-list-difference)

(define-obsolete-function-alias 'gnus-list-range-difference
  #'range-list-difference "29.1")

(define-obsolete-function-alias 'gnus-range-length #'range-length "29.1")

(define-obsolete-function-alias 'gnus-range-add #'range-concat "29.1")

;;;###autoload
(defun gnus-add-to-sorted-list (list num)
  "Add NUM into sorted LIST by side effect."
  (let* ((top (cons nil list))
	 (prev top))
    (while (and list (< (car list) num))
      (setq prev list
	    list (cdr list)))
    (unless (eq (car list) num)
      (setcdr prev (cons num list)))
    (cdr top)))

(define-obsolete-function-alias 'gnus-range-map #'range-map "29.1")

(provide 'gnus-range)

;;; gnus-range.el ends here