1 ;;; gnus-range.el --- range and sequence functions for Gnus
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
30 (eval-when-compile (require 'cl
))
32 ;;; List and range functions
34 (defsubst gnus-range-normalize
(range)
36 If RANGE is a single range, return (RANGE). Otherwise, return RANGE."
37 (if (listp (cdr-safe range
)) range
(list range
)))
39 (defun gnus-last-element (list)
40 "Return last element of LIST."
42 (setq list
(cdr list
)))
45 (defun gnus-copy-sequence (list)
46 "Do a complete, total copy of a list."
49 (if (consp (car list
))
50 (push (gnus-copy-sequence (pop list
)) out
)
51 (push (pop list
) out
)))
53 (nconc (nreverse out
) list
)
56 (defun gnus-set-difference (list1 list2
)
57 "Return a list of elements of LIST1 that do not appear in LIST2."
58 (let ((list1 (copy-sequence list1
)))
60 (setq list1
(delq (car list2
) list1
))
61 (setq list2
(cdr list2
)))
64 (defun gnus-range-difference (range1 range2
)
65 "Return the range of elements in RANGE1 that do not appear in RANGE2.
66 Both ranges must be in ascending order."
67 (setq range1
(gnus-range-normalize range1
))
68 (setq range2
(gnus-range-normalize range2
))
69 (let* ((new-range (cons nil
(copy-sequence range1
)))
75 (min1 (if (numberp r1
) r1
(car r1
)))
76 (max1 (if (numberp r1
) r1
(cdr r1
)))
77 (min2 (if (numberp r2
) r2
(car r2
)))
78 (max2 (if (numberp r2
) r2
(cdr r2
))))
81 ;; Invalid range: may result from overlap condition (below)
82 ;; remove Invalid range
86 ;; Inefficient representation: may result from overlap condition (below)
87 (setcar (cdr r
) min1
))
89 ;; All done with range2
92 ;; No overlap: range1 preceeds range2
95 ;; No overlap: range2 preceeds range1
97 ((and (<= min2 min1
) (<= max1 max2
))
98 ;; Complete overlap: range1 removed
101 (setcdr r
(nconc (list (cons min1
(1- min2
)) (cons (1+ max2
) max1
)) (cddr r
)))))))
107 (defun gnus-sorted-difference (list1 list2
)
108 "Return a list of elements of LIST1 that do not appear in LIST2.
109 Both lists have to be sorted over <.
110 The tail of LIST1 is not copied."
112 (while (and list1 list2
)
113 (cond ((= (car list1
) (car list2
))
114 (setq list1
(cdr list1
)
116 ((< (car list1
) (car list2
))
117 (setq out
(cons (car list1
) out
))
118 (setq list1
(cdr list1
)))
120 (setq list2
(cdr list2
)))))
121 (nconc (nreverse out
) list1
)))
124 (defun gnus-sorted-ndifference (list1 list2
)
125 "Return a list of elements of LIST1 that do not appear in LIST2.
126 Both lists have to be sorted over <.
128 (let* ((top (cons nil list1
))
130 (while (and list1 list2
)
131 (cond ((= (car list1
) (car list2
))
132 (setcdr prev
(cdr list1
))
133 (setq list1
(cdr list1
)
135 ((< (car list1
) (car list2
))
139 (setq list2
(cdr list2
)))))
143 (defun gnus-sorted-complement (list1 list2
)
144 "Return a list of elements that are in LIST1 or LIST2 but not both.
145 Both lists have to be sorted over <."
147 (if (or (null list1
) (null list2
))
149 (while (and list1 list2
)
150 (cond ((= (car list1
) (car list2
))
151 (setq list1
(cdr list1
)
153 ((< (car list1
) (car list2
))
154 (setq out
(cons (car list1
) out
))
155 (setq list1
(cdr list1
)))
157 (setq out
(cons (car list2
) out
))
158 (setq list2
(cdr list2
)))))
159 (nconc (nreverse out
) (or list1 list2
)))))
162 (defun gnus-intersection (list1 list2
)
165 (when (memq (car list2
) list1
)
166 (setq result
(cons (car list2
) result
)))
167 (setq list2
(cdr list2
)))
171 (defun gnus-sorted-intersection (list1 list2
)
172 "Return intersection of LIST1 and LIST2.
173 LIST1 and LIST2 have to be sorted over <."
175 (while (and list1 list2
)
176 (cond ((= (car list1
) (car list2
))
177 (setq out
(cons (car list1
) out
)
180 ((< (car list1
) (car list2
))
181 (setq list1
(cdr list1
)))
183 (setq list2
(cdr list2
)))))
187 (defun gnus-sorted-range-intersection (range1 range2
)
188 "Return intersection of RANGE1 and RANGE2.
189 RANGE1 and RANGE2 have to be sorted over <."
192 (max1 (if (numberp min1
)
193 (if (numberp (cdr range1
))
195 (setq range1 nil
)) min1
)
197 (setq min1
(car min1
)))))
199 (max2 (if (numberp min2
)
200 (if (numberp (cdr range2
))
202 (setq range2 nil
)) min2
)
204 (setq min2
(car min2
))))))
205 (setq range1
(cdr range1
)
207 (while (and min1 min2
)
208 (cond ((< max1 min2
) ; range1 preceeds range2
209 (setq range1
(cdr range1
)
211 ((< max2 min1
) ; range2 preceeds range1
212 (setq range2
(cdr range2
)
214 (t ; some sort of overlap is occurring
215 (let ((min (max min1 min2
))
216 (max (min max1 max2
)))
217 (setq out
(if (= min max
)
219 (cons (cons min max
) out
))))
220 (if (< max1 max2
) ; range1 ends before range2
221 (setq min1 nil
) ; incr range1
222 (setq min2 nil
)))) ; incr range2
224 (setq min1
(car range1
)
225 max1
(if (numberp min1
) min1
(prog1 (cdr min1
) (setq min1
(car min1
))))
226 range1
(cdr range1
)))
228 (setq min2
(car range2
)
229 max2
(if (numberp min2
) min2
(prog1 (cdr min2
) (setq min2
(car min2
))))
230 range2
(cdr range2
))))
239 (defalias 'gnus-set-sorted-intersection
'gnus-sorted-nintersection
)
242 (defun gnus-sorted-nintersection (list1 list2
)
243 "Return intersection of LIST1 and LIST2 by modifying cdr pointers of LIST1.
244 LIST1 and LIST2 have to be sorted over <."
245 (let* ((top (cons nil list1
))
247 (while (and list1 list2
)
248 (cond ((= (car list1
) (car list2
))
252 ((< (car list1
) (car list2
))
253 (setcdr prev
(cdr list1
))
254 (setq list1
(cdr list1
)))
256 (setq list2
(cdr list2
)))))
261 (defun gnus-sorted-union (list1 list2
)
262 "Return union of LIST1 and LIST2.
263 LIST1 and LIST2 have to be sorted over <."
265 (while (and list1 list2
)
266 (cond ((= (car list1
) (car list2
))
267 (setq out
(cons (car list1
) out
)
270 ((< (car list1
) (car list2
))
271 (setq out
(cons (car list1
) out
)
274 (setq out
(cons (car list2
) out
)
275 list2
(cdr list2
)))))
277 (setq out
(cons (car list1
) out
)
280 (setq out
(cons (car list2
) out
)
285 (defun gnus-sorted-nunion (list1 list2
)
286 "Return union of LIST1 and LIST2 by modifying cdr pointers of LIST1.
287 LIST1 and LIST2 have to be sorted over <."
288 (let* ((top (cons nil list1
))
290 (while (and list1 list2
)
291 (cond ((= (car list1
) (car list2
))
295 ((< (car list1
) (car list2
))
299 (setcdr prev
(list (car list2
)))
300 (setq prev
(cdr prev
)
302 (setcdr prev list1
))))
304 (setcdr prev
(list (car list2
)))
305 (setq prev
(cdr prev
)
309 (defun gnus-compress-sequence (numbers &optional always-list
)
310 "Convert list of numbers to a list of ranges or a single range.
311 If ALWAYS-LIST is non-nil, this function will always release a list of
313 (let* ((first (car numbers
))
318 (if (not (listp (cdr numbers
)))
321 (cond ((= last
(car numbers
)) nil
) ;Omit duplicated number
322 ((= (1+ last
) (car numbers
)) ;Still in sequence
323 (setq last
(car numbers
)))
324 (t ;End of one sequence
326 (cons (if (= first last
) first
329 (setq first
(car numbers
))
330 (setq last
(car numbers
))))
331 (setq numbers
(cdr numbers
)))
332 (if (and (not always-list
) (null result
))
333 (if (= first last
) (list first
) (cons first last
))
334 (nreverse (cons (if (= first last
) first
(cons first last
))
337 (defalias 'gnus-uncompress-sequence
'gnus-uncompress-range
)
338 (defun gnus-uncompress-range (ranges)
339 "Expand a list of ranges into a list of numbers.
340 RANGES is either a single range on the form `(num . num)' or a list of
342 (let (first last result
)
346 ((not (listp (cdr ranges
)))
347 (setq first
(car ranges
))
348 (setq last
(cdr ranges
))
349 (while (<= first last
)
350 (setq result
(cons first result
))
351 (setq first
(1+ first
)))
355 (if (atom (car ranges
))
356 (when (numberp (car ranges
))
357 (setq result
(cons (car ranges
) result
)))
358 (setq first
(caar ranges
))
359 (setq last
(cdar ranges
))
360 (while (<= first last
)
361 (setq result
(cons first result
))
362 (setq first
(1+ first
))))
363 (setq ranges
(cdr ranges
)))
364 (nreverse result
)))))
366 (defun gnus-add-to-range (ranges list
)
367 "Return a list of ranges that has all articles from both RANGES and LIST.
368 Note: LIST has to be sorted over `<'."
370 (gnus-compress-sequence list t
)
371 (setq list
(copy-sequence list
))
372 (unless (listp (cdr ranges
))
373 (setq ranges
(list ranges
)))
375 ilist lowest highest temp
)
376 (while (and ranges list
)
378 (setq lowest
(or (and (atom (car ranges
)) (car ranges
))
380 (while (and list
(cdr list
) (< (cadr list
) lowest
))
381 (setq list
(cdr list
)))
382 (when (< (car ilist
) lowest
)
384 (setq list
(cdr list
))
386 (setq out
(nconc (gnus-compress-sequence ilist t
) out
)))
387 (setq highest
(or (and (atom (car ranges
)) (car ranges
))
389 (while (and list
(<= (car list
) highest
))
390 (setq list
(cdr list
)))
391 (setq ranges
(cdr ranges
)))
393 (setq out
(nconc (gnus-compress-sequence list t
) out
)))
394 (setq out
(sort out
(lambda (r1 r2
)
395 (< (or (and (atom r1
) r1
) (car r1
))
396 (or (and (atom r2
) r2
) (car r2
))))))
399 (if (atom (car ranges
))
401 (if (atom (cadr ranges
))
402 (when (= (1+ (car ranges
)) (cadr ranges
))
403 (setcar ranges
(cons (car ranges
)
405 (setcdr ranges
(cddr ranges
)))
406 (when (= (1+ (car ranges
)) (caadr ranges
))
407 (setcar (cadr ranges
) (car ranges
))
408 (setcar ranges
(cadr ranges
))
409 (setcdr ranges
(cddr ranges
)))))
411 (if (atom (cadr ranges
))
412 (when (= (1+ (cdar ranges
)) (cadr ranges
))
413 (setcdr (car ranges
) (cadr ranges
))
414 (setcdr ranges
(cddr ranges
)))
415 (when (= (1+ (cdar ranges
)) (caadr ranges
))
416 (setcdr (car ranges
) (cdadr ranges
))
417 (setcdr ranges
(cddr ranges
))))))
418 (setq ranges
(cdr ranges
)))
421 (defun gnus-remove-from-range (range1 range2
)
422 "Return a range that has all articles from RANGE2 removed from RANGE1.
423 The returned range is always a list. RANGE2 can also be a unsorted
424 list of articles. RANGE1 is modified by side effects, RANGE2 is not
426 (if (or (null range1
) (null range2
))
428 (let (out r1 r2 r1_min r1_max r2_min r2_max
429 (range2 (gnus-copy-sequence range2
)))
430 (setq range1
(if (listp (cdr range1
)) range1
(list range1
))
431 range2
(sort (if (listp (cdr range2
)) range2
(list range2
))
433 (< (if (consp e1
) (car e1
) e1
)
434 (if (consp e2
) (car e2
) e2
))))
437 r1_min
(if (consp r1
) (car r1
) r1
)
438 r1_max
(if (consp r1
) (cdr r1
) r1
)
439 r2_min
(if (consp r2
) (car r2
) r2
)
440 r2_max
(if (consp r2
) (cdr r2
) r2
))
441 (while (and range1 range2
)
442 (cond ((< r2_max r1_min
) ; r2 < r1
444 (setq r2
(car range2
)
445 r2_min
(if (consp r2
) (car r2
) r2
)
446 r2_max
(if (consp r2
) (cdr r2
) r2
)))
447 ((and (<= r2_min r1_min
) (<= r1_max r2_max
)) ; r2 overlap r1
449 (setq r1
(car range1
)
450 r1_min
(if (consp r1
) (car r1
) r1
)
451 r1_max
(if (consp r1
) (cdr r1
) r1
)))
452 ((and (<= r2_min r1_min
) (<= r2_max r1_max
)) ; r2 overlap min r1
454 (setq r1_min
(1+ r2_max
)
456 r2_min
(if (consp r2
) (car r2
) r2
)
457 r2_max
(if (consp r2
) (cdr r2
) r2
)))
458 ((and (<= r1_min r2_min
) (<= r2_max r1_max
)) ; r2 contained in r1
459 (if (eq r1_min
(1- r2_min
))
461 (push (cons r1_min
(1- r2_min
)) out
))
463 (if (< r2_max r1_max
) ; finished with r1?
464 (setq r1_min
(1+ r2_max
))
466 (setq r1
(car range1
)
467 r1_min
(if (consp r1
) (car r1
) r1
)
468 r1_max
(if (consp r1
) (cdr r1
) r1
)))
469 (setq r2
(car range2
)
470 r2_min
(if (consp r2
) (car r2
) r2
)
471 r2_max
(if (consp r2
) (cdr r2
) r2
)))
472 ((and (<= r2_min r1_max
) (<= r1_max r2_max
)) ; r2 overlap max r1
473 (if (eq r1_min
(1- r2_min
))
475 (push (cons r1_min
(1- r2_min
)) out
))
477 (setq r1
(car range1
)
478 r1_min
(if (consp r1
) (car r1
) r1
)
479 r1_max
(if (consp r1
) (cdr r1
) r1
)))
480 ((< r1_max r2_min
) ; r2 > r1
482 (if (eq r1_min r1_max
)
484 (push (cons r1_min r1_max
) out
))
485 (setq r1
(car range1
)
486 r1_min
(if (consp r1
) (car r1
) r1
)
487 r1_max
(if (consp r1
) (cdr r1
) r1
)))))
489 (if (eq r1_min r1_max
)
491 (push (cons r1_min r1_max
) out
))
494 (push (pop range1
) out
))
497 (defun gnus-member-of-range (number ranges
)
498 (if (not (listp (cdr ranges
)))
499 (and (>= number
(car ranges
))
500 (<= number
(cdr ranges
)))
503 (if (numberp (car ranges
))
504 (>= number
(car ranges
))
505 (>= number
(caar ranges
)))
507 (when (if (numberp (car ranges
))
508 (= number
(car ranges
))
509 (and (>= number
(caar ranges
))
510 (<= number
(cdar ranges
))))
512 (setq ranges
(cdr ranges
)))
515 (defun gnus-list-range-intersection (list ranges
)
516 "Return a list of numbers in LIST that are members of RANGES.
517 LIST is a sorted list."
518 (setq ranges
(gnus-range-normalize ranges
))
520 (while (setq number
(pop list
))
522 (if (numberp (car ranges
))
523 (< (car ranges
) number
)
524 (< (cdar ranges
) number
)))
525 (setq ranges
(cdr ranges
)))
527 (if (numberp (car ranges
))
528 (= (car ranges
) number
)
529 ;; (caar ranges) <= number <= (cdar ranges)
530 (>= number
(caar ranges
))))
531 (push number result
)))
534 (defalias 'gnus-inverse-list-range-intersection
'gnus-list-range-difference
)
536 (defun gnus-list-range-difference (list ranges
)
537 "Return a list of numbers in LIST that are not members of RANGES.
538 LIST is a sorted list."
539 (setq ranges
(gnus-range-normalize ranges
))
541 (while (setq number
(pop list
))
543 (if (numberp (car ranges
))
544 (< (car ranges
) number
)
545 (< (cdar ranges
) number
)))
546 (setq ranges
(cdr ranges
)))
547 (when (or (not ranges
)
548 (if (numberp (car ranges
))
549 (not (= (car ranges
) number
))
550 ;; not ((caar ranges) <= number <= (cdar ranges))
551 (< number
(caar ranges
))))
552 (push number result
)))
555 (defun gnus-range-length (range)
556 "Return the length RANGE would have if uncompressed."
560 ((not (listp (cdr range
)))
561 (- (cdr range
) (car range
) -
1))
564 (dolist (x range sum
)
566 (+ sum
(if (consp x
) (- (cdr x
) (car x
) -
1) 1))))))))
568 (defun gnus-sublist-p (list sublist
)
569 "Test whether all elements in SUBLIST are members of LIST."
572 (unless (memq (pop sublist
) list
)
577 (defun gnus-range-add (range1 range2
)
578 "Add RANGE2 to RANGE1 (nondestructively)."
579 (unless (listp (cdr range1
))
580 (setq range1
(list range1
)))
581 (unless (listp (cdr range2
))
582 (setq range2
(list range2
)))
583 (let ((item1 (pop range1
))
586 (while (or item1 item2
)
591 ((and (numberp item1
) (numberp item2
)) (< item1 item2
))
592 ((numberp item1
) (< item1
(car item2
)))
593 ((numberp item2
) (< (car item1
) item2
))
594 (t (< (car item1
) (car item2
)))))
597 (let ((tmp1 item
) (tmp2 (if selector item1 item2
)))
601 ((and (numberp tmp1
) (numberp tmp2
))
603 ((eq tmp1 tmp2
) tmp1
)
604 ((eq (1+ tmp1
) tmp2
) (cons tmp1 tmp2
))
605 ((eq (1+ tmp2
) tmp1
) (cons tmp2 tmp1
))
609 ((and (>= tmp1
(car tmp2
)) (<= tmp1
(cdr tmp2
))) tmp2
)
610 ((eq (1+ tmp1
) (car tmp2
)) (cons tmp1
(cdr tmp2
)))
611 ((eq (1- tmp1
) (cdr tmp2
)) (cons (car tmp2
) tmp1
))
615 ((and (>= tmp2
(car tmp1
)) (<= tmp2
(cdr tmp1
))) tmp1
)
616 ((eq (1+ tmp2
) (car tmp1
)) (cons tmp2
(cdr tmp1
)))
617 ((eq (1- tmp2
) (cdr tmp1
)) (cons (car tmp1
) tmp2
))
619 ((< (1+ (cdr tmp1
)) (car tmp2
)) nil
)
620 ((< (1+ (cdr tmp2
)) (car tmp1
)) nil
)
621 (t (cons (min (car tmp1
) (car tmp2
))
622 (max (cdr tmp1
) (cdr tmp2
))))))
624 (if item
(push item range
))
625 (if selector item1 item2
))))
627 (setq item1
(pop range1
))
628 (setq item2
(pop range2
))))
629 (if item
(push item range
))
633 (defun gnus-add-to-sorted-list (list num
)
634 "Add NUM into sorted LIST by side effect."
635 (let* ((top (cons nil list
))
637 (while (and list
(< (car list
) num
))
640 (unless (eq (car list
) num
)
641 (setcdr prev
(cons num list
)))
644 (defun gnus-range-map (func range
)
645 "Apply FUNC to each value contained by RANGE."
646 (setq range
(gnus-range-normalize range
))
648 (let ((span (pop range
)))
651 (let ((first (car span
))
653 (while (<= first last
)
655 (setq first
(1+ first
))))))))
657 (provide 'gnus-range
)
659 ;;; arch-tag: 4780bdd8-5a15-4aff-be28-18727895b6ad
660 ;;; gnus-range.el ends here