1 ;;; gnus-range.el --- range and sequence functions for Gnus
3 ;; Copyright (C) 1996-2011 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 (eval-when-compile (require 'cl
))
29 ;;; List and range functions
31 (defsubst gnus-range-normalize
(range)
33 If RANGE is a single range, return (RANGE). Otherwise, return RANGE."
34 (if (listp (cdr-safe range
)) range
(list range
)))
36 (defun gnus-last-element (list)
37 "Return last element of LIST."
39 (setq list
(cdr list
)))
42 (defun gnus-copy-sequence (list)
43 "Do a complete, total copy of a list."
46 (if (consp (car list
))
47 (push (gnus-copy-sequence (pop list
)) out
)
48 (push (pop list
) out
)))
50 (nconc (nreverse out
) list
)
53 (defun gnus-set-difference (list1 list2
)
54 "Return a list of elements of LIST1 that do not appear in LIST2."
55 (let ((list1 (copy-sequence list1
)))
57 (setq list1
(delq (car list2
) list1
))
58 (setq list2
(cdr list2
)))
61 (defun gnus-range-nconcat (&rest ranges
)
62 "Return a range comprising all the RANGES, which are pre-sorted.
63 RANGES will be destructively altered."
64 (setq ranges
(delete nil ranges
))
65 (let* ((result (gnus-range-normalize (pop ranges
)))
67 (dolist (range ranges
)
68 (setq range
(gnus-range-normalize range
))
69 ;; Normalize the single-number case, so that we don't need to
70 ;; special-case that so much.
71 (when (numberp (car last
))
72 (setcar last
(cons (car last
) (car last
))))
73 (when (numberp (car range
))
74 (setcar range
(cons (car range
) (car range
))))
75 (if (= (1+ (cdar last
)) (caar range
))
77 (setcdr (car last
) (cdar range
))
78 (setcdr last
(cdr range
)))
80 ;; Denormalize back, since we couldn't join the ranges up.
81 (when (= (caar range
) (cdar range
))
82 (setcar range
(caar range
)))
83 (when (= (caar last
) (cdar last
))
84 (setcar last
(caar last
))))
85 (setq last
(last last
)))
86 (if (and (consp (car result
))
87 (= (length result
) 1))
91 (defun gnus-range-difference (range1 range2
)
92 "Return the range of elements in RANGE1 that do not appear in RANGE2.
93 Both ranges must be in ascending order."
94 (setq range1
(gnus-range-normalize range1
))
95 (setq range2
(gnus-range-normalize range2
))
96 (let* ((new-range (cons nil
(copy-sequence range1
)))
102 (min1 (if (numberp r1
) r1
(car r1
)))
103 (max1 (if (numberp r1
) r1
(cdr r1
)))
104 (min2 (if (numberp r2
) r2
(car r2
)))
105 (max2 (if (numberp r2
) r2
(cdr r2
))))
108 ;; Invalid range: may result from overlap condition (below)
109 ;; remove Invalid range
113 ;; Inefficient representation: may result from overlap condition (below)
114 (setcar (cdr r
) min1
))
116 ;; All done with range2
119 ;; No overlap: range1 precedes range2
122 ;; No overlap: range2 precedes range1
124 ((and (<= min2 min1
) (<= max1 max2
))
125 ;; Complete overlap: range1 removed
128 (setcdr r
(nconc (list (cons min1
(1- min2
)) (cons (1+ max2
) max1
)) (cddr r
)))))))
134 (defun gnus-sorted-difference (list1 list2
)
135 "Return a list of elements of LIST1 that do not appear in LIST2.
136 Both lists have to be sorted over <.
137 The tail of LIST1 is not copied."
139 (while (and list1 list2
)
140 (cond ((= (car list1
) (car list2
))
141 (setq list1
(cdr list1
)
143 ((< (car list1
) (car list2
))
144 (setq out
(cons (car list1
) out
))
145 (setq list1
(cdr list1
)))
147 (setq list2
(cdr list2
)))))
148 (nconc (nreverse out
) list1
)))
151 (defun gnus-sorted-ndifference (list1 list2
)
152 "Return a list of elements of LIST1 that do not appear in LIST2.
153 Both lists have to be sorted over <.
155 (let* ((top (cons nil list1
))
157 (while (and list1 list2
)
158 (cond ((= (car list1
) (car list2
))
159 (setcdr prev
(cdr list1
))
160 (setq list1
(cdr list1
)
162 ((< (car list1
) (car list2
))
166 (setq list2
(cdr list2
)))))
170 (defun gnus-sorted-complement (list1 list2
)
171 "Return a list of elements that are in LIST1 or LIST2 but not both.
172 Both lists have to be sorted over <."
174 (if (or (null list1
) (null list2
))
176 (while (and list1 list2
)
177 (cond ((= (car list1
) (car list2
))
178 (setq list1
(cdr list1
)
180 ((< (car list1
) (car list2
))
181 (setq out
(cons (car list1
) out
))
182 (setq list1
(cdr list1
)))
184 (setq out
(cons (car list2
) out
))
185 (setq list2
(cdr list2
)))))
186 (nconc (nreverse out
) (or list1 list2
)))))
189 (defun gnus-intersection (list1 list2
)
192 (when (memq (car list2
) list1
)
193 (setq result
(cons (car list2
) result
)))
194 (setq list2
(cdr list2
)))
198 (defun gnus-sorted-intersection (list1 list2
)
199 "Return intersection of LIST1 and LIST2.
200 LIST1 and LIST2 have to be sorted over <."
202 (while (and list1 list2
)
203 (cond ((= (car list1
) (car list2
))
204 (setq out
(cons (car list1
) out
)
207 ((< (car list1
) (car list2
))
208 (setq list1
(cdr list1
)))
210 (setq list2
(cdr list2
)))))
214 (defun gnus-sorted-range-intersection (range1 range2
)
215 "Return intersection of RANGE1 and RANGE2.
216 RANGE1 and RANGE2 have to be sorted over <."
219 (max1 (if (numberp min1
)
220 (if (numberp (cdr range1
))
222 (setq range1 nil
)) min1
)
224 (setq min1
(car min1
)))))
226 (max2 (if (numberp min2
)
227 (if (numberp (cdr range2
))
229 (setq range2 nil
)) min2
)
231 (setq min2
(car min2
))))))
232 (setq range1
(cdr range1
)
234 (while (and min1 min2
)
235 (cond ((< max1 min2
) ; range1 precedes range2
236 (setq range1
(cdr range1
)
238 ((< max2 min1
) ; range2 precedes range1
239 (setq range2
(cdr range2
)
241 (t ; some sort of overlap is occurring
242 (let ((min (max min1 min2
))
243 (max (min max1 max2
)))
244 (setq out
(if (= min max
)
246 (cons (cons min max
) out
))))
247 (if (< max1 max2
) ; range1 ends before range2
248 (setq min1 nil
) ; incr range1
249 (setq min2 nil
)))) ; incr range2
251 (setq min1
(car range1
)
252 max1
(if (numberp min1
) min1
(prog1 (cdr min1
) (setq min1
(car min1
))))
253 range1
(cdr range1
)))
255 (setq min2
(car range2
)
256 max2
(if (numberp min2
) min2
(prog1 (cdr min2
) (setq min2
(car min2
))))
257 range2
(cdr range2
))))
266 (defalias 'gnus-set-sorted-intersection
'gnus-sorted-nintersection
)
269 (defun gnus-sorted-nintersection (list1 list2
)
270 "Return intersection of LIST1 and LIST2 by modifying cdr pointers of LIST1.
271 LIST1 and LIST2 have to be sorted over <."
272 (let* ((top (cons nil list1
))
274 (while (and list1 list2
)
275 (cond ((= (car list1
) (car list2
))
279 ((< (car list1
) (car list2
))
280 (setcdr prev
(cdr list1
))
281 (setq list1
(cdr list1
)))
283 (setq list2
(cdr list2
)))))
288 (defun gnus-sorted-union (list1 list2
)
289 "Return union of LIST1 and LIST2.
290 LIST1 and LIST2 have to be sorted over <."
292 (while (and list1 list2
)
293 (cond ((= (car list1
) (car list2
))
294 (setq out
(cons (car list1
) out
)
297 ((< (car list1
) (car list2
))
298 (setq out
(cons (car list1
) out
)
301 (setq out
(cons (car list2
) out
)
302 list2
(cdr list2
)))))
304 (setq out
(cons (car list1
) out
)
307 (setq out
(cons (car list2
) out
)
312 (defun gnus-sorted-nunion (list1 list2
)
313 "Return union of LIST1 and LIST2 by modifying cdr pointers of LIST1.
314 LIST1 and LIST2 have to be sorted over <."
315 (let* ((top (cons nil list1
))
317 (while (and list1 list2
)
318 (cond ((= (car list1
) (car list2
))
322 ((< (car list1
) (car list2
))
326 (setcdr prev
(list (car list2
)))
327 (setq prev
(cdr prev
)
329 (setcdr prev list1
))))
331 (setcdr prev
(list (car list2
)))
332 (setq prev
(cdr prev
)
336 (defun gnus-compress-sequence (numbers &optional always-list
)
337 "Convert sorted list of numbers to a list of ranges or a single range.
338 If ALWAYS-LIST is non-nil, this function will always release a list of
340 (let* ((first (car numbers
))
345 (if (not (listp (cdr numbers
)))
348 (cond ((= last
(car numbers
)) nil
) ;Omit duplicated number
349 ((= (1+ last
) (car numbers
)) ;Still in sequence
350 (setq last
(car numbers
)))
351 (t ;End of one sequence
353 (cons (if (= first last
) first
356 (setq first
(car numbers
))
357 (setq last
(car numbers
))))
358 (setq numbers
(cdr numbers
)))
359 (if (and (not always-list
) (null result
))
360 (if (= first last
) (list first
) (cons first last
))
361 (nreverse (cons (if (= first last
) first
(cons first last
))
364 (defalias 'gnus-uncompress-sequence
'gnus-uncompress-range
)
365 (defun gnus-uncompress-range (ranges)
366 "Expand a list of ranges into a list of numbers.
367 RANGES is either a single range on the form `(num . num)' or a list of
369 (let (first last result
)
373 ((not (listp (cdr ranges
)))
374 (setq first
(car ranges
))
375 (setq last
(cdr ranges
))
376 (while (<= first last
)
377 (setq result
(cons first result
))
378 (setq first
(1+ first
)))
382 (if (atom (car ranges
))
383 (when (numberp (car ranges
))
384 (setq result
(cons (car ranges
) result
)))
385 (setq first
(caar ranges
))
386 (setq last
(cdar ranges
))
387 (while (<= first last
)
388 (setq result
(cons first result
))
389 (setq first
(1+ first
))))
390 (setq ranges
(cdr ranges
)))
391 (nreverse result
)))))
393 (defun gnus-add-to-range (ranges list
)
394 "Return a list of ranges that has all articles from both RANGES and LIST.
395 Note: LIST has to be sorted over `<'."
397 (gnus-compress-sequence list t
)
398 (setq list
(copy-sequence list
))
399 (unless (listp (cdr ranges
))
400 (setq ranges
(list ranges
)))
402 ilist lowest highest temp
)
403 (while (and ranges list
)
405 (setq lowest
(or (and (atom (car ranges
)) (car ranges
))
407 (while (and list
(cdr list
) (< (cadr list
) lowest
))
408 (setq list
(cdr list
)))
409 (when (< (car ilist
) lowest
)
411 (setq list
(cdr list
))
413 (setq out
(nconc (gnus-compress-sequence ilist t
) out
)))
414 (setq highest
(or (and (atom (car ranges
)) (car ranges
))
416 (while (and list
(<= (car list
) highest
))
417 (setq list
(cdr list
)))
418 (setq ranges
(cdr ranges
)))
420 (setq out
(nconc (gnus-compress-sequence list t
) out
)))
421 (setq out
(sort out
(lambda (r1 r2
)
422 (< (or (and (atom r1
) r1
) (car r1
))
423 (or (and (atom r2
) r2
) (car r2
))))))
426 (if (atom (car ranges
))
428 (if (atom (cadr ranges
))
429 (when (= (1+ (car ranges
)) (cadr ranges
))
430 (setcar ranges
(cons (car ranges
)
432 (setcdr ranges
(cddr ranges
)))
433 (when (= (1+ (car ranges
)) (caadr ranges
))
434 (setcar (cadr ranges
) (car ranges
))
435 (setcar ranges
(cadr ranges
))
436 (setcdr ranges
(cddr ranges
)))))
438 (if (atom (cadr ranges
))
439 (when (= (1+ (cdar ranges
)) (cadr ranges
))
440 (setcdr (car ranges
) (cadr ranges
))
441 (setcdr ranges
(cddr ranges
)))
442 (when (= (1+ (cdar ranges
)) (caadr ranges
))
443 (setcdr (car ranges
) (cdadr ranges
))
444 (setcdr ranges
(cddr ranges
))))))
445 (setq ranges
(cdr ranges
)))
448 (defun gnus-remove-from-range (range1 range2
)
449 "Return a range that has all articles from RANGE2 removed from RANGE1.
450 The returned range is always a list. RANGE2 can also be a unsorted
451 list of articles. RANGE1 is modified by side effects, RANGE2 is not
453 (if (or (null range1
) (null range2
))
455 (let (out r1 r2 r1_min r1_max r2_min r2_max
456 (range2 (gnus-copy-sequence range2
)))
457 (setq range1
(if (listp (cdr range1
)) range1
(list range1
))
458 range2
(sort (if (listp (cdr range2
)) range2
(list range2
))
460 (< (if (consp e1
) (car e1
) e1
)
461 (if (consp e2
) (car e2
) e2
))))
464 r1_min
(if (consp r1
) (car r1
) r1
)
465 r1_max
(if (consp r1
) (cdr r1
) r1
)
466 r2_min
(if (consp r2
) (car r2
) r2
)
467 r2_max
(if (consp r2
) (cdr r2
) r2
))
468 (while (and range1 range2
)
469 (cond ((< r2_max r1_min
) ; r2 < r1
471 (setq r2
(car range2
)
472 r2_min
(if (consp r2
) (car r2
) r2
)
473 r2_max
(if (consp r2
) (cdr r2
) r2
)))
474 ((and (<= r2_min r1_min
) (<= r1_max r2_max
)) ; r2 overlap r1
476 (setq r1
(car range1
)
477 r1_min
(if (consp r1
) (car r1
) r1
)
478 r1_max
(if (consp r1
) (cdr r1
) r1
)))
479 ((and (<= r2_min r1_min
) (<= r2_max r1_max
)) ; r2 overlap min r1
481 (setq r1_min
(1+ r2_max
)
483 r2_min
(if (consp r2
) (car r2
) r2
)
484 r2_max
(if (consp r2
) (cdr r2
) r2
)))
485 ((and (<= r1_min r2_min
) (<= r2_max r1_max
)) ; r2 contained in r1
486 (if (eq r1_min
(1- r2_min
))
488 (push (cons r1_min
(1- r2_min
)) out
))
490 (if (< r2_max r1_max
) ; finished with r1?
491 (setq r1_min
(1+ r2_max
))
493 (setq r1
(car range1
)
494 r1_min
(if (consp r1
) (car r1
) r1
)
495 r1_max
(if (consp r1
) (cdr r1
) r1
)))
496 (setq r2
(car range2
)
497 r2_min
(if (consp r2
) (car r2
) r2
)
498 r2_max
(if (consp r2
) (cdr r2
) r2
)))
499 ((and (<= r2_min r1_max
) (<= r1_max r2_max
)) ; r2 overlap max r1
500 (if (eq r1_min
(1- r2_min
))
502 (push (cons r1_min
(1- r2_min
)) out
))
504 (setq r1
(car range1
)
505 r1_min
(if (consp r1
) (car r1
) r1
)
506 r1_max
(if (consp r1
) (cdr r1
) r1
)))
507 ((< r1_max r2_min
) ; r2 > r1
509 (if (eq r1_min r1_max
)
511 (push (cons r1_min r1_max
) out
))
512 (setq r1
(car range1
)
513 r1_min
(if (consp r1
) (car r1
) r1
)
514 r1_max
(if (consp r1
) (cdr r1
) r1
)))))
516 (if (eq r1_min r1_max
)
518 (push (cons r1_min r1_max
) out
))
521 (push (pop range1
) out
))
524 (defun gnus-member-of-range (number ranges
)
525 (if (not (listp (cdr ranges
)))
526 (and (>= number
(car ranges
))
527 (<= number
(cdr ranges
)))
530 (if (numberp (car ranges
))
531 (>= number
(car ranges
))
532 (>= number
(caar ranges
)))
534 (when (if (numberp (car ranges
))
535 (= number
(car ranges
))
536 (and (>= number
(caar ranges
))
537 (<= number
(cdar ranges
))))
539 (setq ranges
(cdr ranges
)))
542 (defun gnus-list-range-intersection (list ranges
)
543 "Return a list of numbers in LIST that are members of RANGES.
544 LIST is a sorted list."
545 (setq ranges
(gnus-range-normalize ranges
))
547 (while (setq number
(pop list
))
549 (if (numberp (car ranges
))
550 (< (car ranges
) number
)
551 (< (cdar ranges
) number
)))
552 (setq ranges
(cdr ranges
)))
554 (if (numberp (car ranges
))
555 (= (car ranges
) number
)
556 ;; (caar ranges) <= number <= (cdar ranges)
557 (>= number
(caar ranges
))))
558 (push number result
)))
561 (defalias 'gnus-inverse-list-range-intersection
'gnus-list-range-difference
)
563 (defun gnus-list-range-difference (list ranges
)
564 "Return a list of numbers in LIST that are not members of RANGES.
565 LIST is a sorted list."
566 (setq ranges
(gnus-range-normalize ranges
))
568 (while (setq number
(pop list
))
570 (if (numberp (car ranges
))
571 (< (car ranges
) number
)
572 (< (cdar ranges
) number
)))
573 (setq ranges
(cdr ranges
)))
574 (when (or (not ranges
)
575 (if (numberp (car ranges
))
576 (not (= (car ranges
) number
))
577 ;; not ((caar ranges) <= number <= (cdar ranges))
578 (< number
(caar ranges
))))
579 (push number result
)))
582 (defun gnus-range-length (range)
583 "Return the length RANGE would have if uncompressed."
587 ((not (listp (cdr range
)))
588 (- (cdr range
) (car range
) -
1))
591 (dolist (x range sum
)
593 (+ sum
(if (consp x
) (- (cdr x
) (car x
) -
1) 1))))))))
595 (defun gnus-sublist-p (list sublist
)
596 "Test whether all elements in SUBLIST are members of LIST."
599 (unless (memq (pop sublist
) list
)
604 (defun gnus-range-add (range1 range2
)
605 "Add RANGE2 to RANGE1 (nondestructively)."
606 (unless (listp (cdr range1
))
607 (setq range1
(list range1
)))
608 (unless (listp (cdr range2
))
609 (setq range2
(list range2
)))
610 (let ((item1 (pop range1
))
613 (while (or item1 item2
)
618 ((and (numberp item1
) (numberp item2
)) (< item1 item2
))
619 ((numberp item1
) (< item1
(car item2
)))
620 ((numberp item2
) (< (car item1
) item2
))
621 (t (< (car item1
) (car item2
)))))
624 (let ((tmp1 item
) (tmp2 (if selector item1 item2
)))
628 ((and (numberp tmp1
) (numberp tmp2
))
630 ((eq tmp1 tmp2
) tmp1
)
631 ((eq (1+ tmp1
) tmp2
) (cons tmp1 tmp2
))
632 ((eq (1+ tmp2
) tmp1
) (cons tmp2 tmp1
))
636 ((and (>= tmp1
(car tmp2
)) (<= tmp1
(cdr tmp2
))) tmp2
)
637 ((eq (1+ tmp1
) (car tmp2
)) (cons tmp1
(cdr tmp2
)))
638 ((eq (1- tmp1
) (cdr tmp2
)) (cons (car tmp2
) tmp1
))
642 ((and (>= tmp2
(car tmp1
)) (<= tmp2
(cdr tmp1
))) tmp1
)
643 ((eq (1+ tmp2
) (car tmp1
)) (cons tmp2
(cdr tmp1
)))
644 ((eq (1- tmp2
) (cdr tmp1
)) (cons (car tmp1
) tmp2
))
646 ((< (1+ (cdr tmp1
)) (car tmp2
)) nil
)
647 ((< (1+ (cdr tmp2
)) (car tmp1
)) nil
)
648 (t (cons (min (car tmp1
) (car tmp2
))
649 (max (cdr tmp1
) (cdr tmp2
))))))
651 (if item
(push item range
))
652 (if selector item1 item2
))))
654 (setq item1
(pop range1
))
655 (setq item2
(pop range2
))))
656 (if item
(push item range
))
660 (defun gnus-add-to-sorted-list (list num
)
661 "Add NUM into sorted LIST by side effect."
662 (let* ((top (cons nil list
))
664 (while (and list
(< (car list
) num
))
667 (unless (eq (car list
) num
)
668 (setcdr prev
(cons num list
)))
671 (defun gnus-range-map (func range
)
672 "Apply FUNC to each value contained by RANGE."
673 (setq range
(gnus-range-normalize range
))
675 (let ((span (pop range
)))
678 (let ((first (car span
))
680 (while (<= first last
)
682 (setq first
(1+ first
))))))))
684 (provide 'gnus-range
)
686 ;;; gnus-range.el ends here