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, 2007, 2008, 2009 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 3 of the License, or
14 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
28 (eval-when-compile (require 'cl
))
30 ;;; List and range functions
32 (defsubst gnus-range-normalize
(range)
34 If RANGE is a single range, return (RANGE). Otherwise, return RANGE."
35 (if (listp (cdr-safe range
)) range
(list range
)))
37 (defun gnus-last-element (list)
38 "Return last element of LIST."
40 (setq list
(cdr list
)))
43 (defun gnus-copy-sequence (list)
44 "Do a complete, total copy of a list."
47 (if (consp (car list
))
48 (push (gnus-copy-sequence (pop list
)) out
)
49 (push (pop list
) out
)))
51 (nconc (nreverse out
) list
)
54 (defun gnus-set-difference (list1 list2
)
55 "Return a list of elements of LIST1 that do not appear in LIST2."
56 (let ((list1 (copy-sequence list1
)))
58 (setq list1
(delq (car list2
) list1
))
59 (setq list2
(cdr list2
)))
62 (defun gnus-range-difference (range1 range2
)
63 "Return the range of elements in RANGE1 that do not appear in RANGE2.
64 Both ranges must be in ascending order."
65 (setq range1
(gnus-range-normalize range1
))
66 (setq range2
(gnus-range-normalize range2
))
67 (let* ((new-range (cons nil
(copy-sequence range1
)))
73 (min1 (if (numberp r1
) r1
(car r1
)))
74 (max1 (if (numberp r1
) r1
(cdr r1
)))
75 (min2 (if (numberp r2
) r2
(car r2
)))
76 (max2 (if (numberp r2
) r2
(cdr r2
))))
79 ;; Invalid range: may result from overlap condition (below)
80 ;; remove Invalid range
84 ;; Inefficient representation: may result from overlap condition (below)
85 (setcar (cdr r
) min1
))
87 ;; All done with range2
90 ;; No overlap: range1 preceeds range2
93 ;; No overlap: range2 preceeds range1
95 ((and (<= min2 min1
) (<= max1 max2
))
96 ;; Complete overlap: range1 removed
99 (setcdr r
(nconc (list (cons min1
(1- min2
)) (cons (1+ max2
) max1
)) (cddr r
)))))))
105 (defun gnus-sorted-difference (list1 list2
)
106 "Return a list of elements of LIST1 that do not appear in LIST2.
107 Both lists have to be sorted over <.
108 The tail of LIST1 is not copied."
110 (while (and list1 list2
)
111 (cond ((= (car list1
) (car list2
))
112 (setq list1
(cdr list1
)
114 ((< (car list1
) (car list2
))
115 (setq out
(cons (car list1
) out
))
116 (setq list1
(cdr list1
)))
118 (setq list2
(cdr list2
)))))
119 (nconc (nreverse out
) list1
)))
122 (defun gnus-sorted-ndifference (list1 list2
)
123 "Return a list of elements of LIST1 that do not appear in LIST2.
124 Both lists have to be sorted over <.
126 (let* ((top (cons nil list1
))
128 (while (and list1 list2
)
129 (cond ((= (car list1
) (car list2
))
130 (setcdr prev
(cdr list1
))
131 (setq list1
(cdr list1
)
133 ((< (car list1
) (car list2
))
137 (setq list2
(cdr list2
)))))
141 (defun gnus-sorted-complement (list1 list2
)
142 "Return a list of elements that are in LIST1 or LIST2 but not both.
143 Both lists have to be sorted over <."
145 (if (or (null list1
) (null list2
))
147 (while (and list1 list2
)
148 (cond ((= (car list1
) (car list2
))
149 (setq list1
(cdr list1
)
151 ((< (car list1
) (car list2
))
152 (setq out
(cons (car list1
) out
))
153 (setq list1
(cdr list1
)))
155 (setq out
(cons (car list2
) out
))
156 (setq list2
(cdr list2
)))))
157 (nconc (nreverse out
) (or list1 list2
)))))
160 (defun gnus-intersection (list1 list2
)
163 (when (memq (car list2
) list1
)
164 (setq result
(cons (car list2
) result
)))
165 (setq list2
(cdr list2
)))
169 (defun gnus-sorted-intersection (list1 list2
)
170 "Return intersection of LIST1 and LIST2.
171 LIST1 and LIST2 have to be sorted over <."
173 (while (and list1 list2
)
174 (cond ((= (car list1
) (car list2
))
175 (setq out
(cons (car list1
) out
)
178 ((< (car list1
) (car list2
))
179 (setq list1
(cdr list1
)))
181 (setq list2
(cdr list2
)))))
185 (defun gnus-sorted-range-intersection (range1 range2
)
186 "Return intersection of RANGE1 and RANGE2.
187 RANGE1 and RANGE2 have to be sorted over <."
190 (max1 (if (numberp min1
)
191 (if (numberp (cdr range1
))
193 (setq range1 nil
)) min1
)
195 (setq min1
(car min1
)))))
197 (max2 (if (numberp min2
)
198 (if (numberp (cdr range2
))
200 (setq range2 nil
)) min2
)
202 (setq min2
(car min2
))))))
203 (setq range1
(cdr range1
)
205 (while (and min1 min2
)
206 (cond ((< max1 min2
) ; range1 preceeds range2
207 (setq range1
(cdr range1
)
209 ((< max2 min1
) ; range2 preceeds range1
210 (setq range2
(cdr range2
)
212 (t ; some sort of overlap is occurring
213 (let ((min (max min1 min2
))
214 (max (min max1 max2
)))
215 (setq out
(if (= min max
)
217 (cons (cons min max
) out
))))
218 (if (< max1 max2
) ; range1 ends before range2
219 (setq min1 nil
) ; incr range1
220 (setq min2 nil
)))) ; incr range2
222 (setq min1
(car range1
)
223 max1
(if (numberp min1
) min1
(prog1 (cdr min1
) (setq min1
(car min1
))))
224 range1
(cdr range1
)))
226 (setq min2
(car range2
)
227 max2
(if (numberp min2
) min2
(prog1 (cdr min2
) (setq min2
(car min2
))))
228 range2
(cdr range2
))))
237 (defalias 'gnus-set-sorted-intersection
'gnus-sorted-nintersection
)
240 (defun gnus-sorted-nintersection (list1 list2
)
241 "Return intersection of LIST1 and LIST2 by modifying cdr pointers of LIST1.
242 LIST1 and LIST2 have to be sorted over <."
243 (let* ((top (cons nil list1
))
245 (while (and list1 list2
)
246 (cond ((= (car list1
) (car list2
))
250 ((< (car list1
) (car list2
))
251 (setcdr prev
(cdr list1
))
252 (setq list1
(cdr list1
)))
254 (setq list2
(cdr list2
)))))
259 (defun gnus-sorted-union (list1 list2
)
260 "Return union of LIST1 and LIST2.
261 LIST1 and LIST2 have to be sorted over <."
263 (while (and list1 list2
)
264 (cond ((= (car list1
) (car list2
))
265 (setq out
(cons (car list1
) out
)
268 ((< (car list1
) (car list2
))
269 (setq out
(cons (car list1
) out
)
272 (setq out
(cons (car list2
) out
)
273 list2
(cdr list2
)))))
275 (setq out
(cons (car list1
) out
)
278 (setq out
(cons (car list2
) out
)
283 (defun gnus-sorted-nunion (list1 list2
)
284 "Return union of LIST1 and LIST2 by modifying cdr pointers of LIST1.
285 LIST1 and LIST2 have to be sorted over <."
286 (let* ((top (cons nil list1
))
288 (while (and list1 list2
)
289 (cond ((= (car list1
) (car list2
))
293 ((< (car list1
) (car list2
))
297 (setcdr prev
(list (car list2
)))
298 (setq prev
(cdr prev
)
300 (setcdr prev list1
))))
302 (setcdr prev
(list (car list2
)))
303 (setq prev
(cdr prev
)
307 (defun gnus-compress-sequence (numbers &optional always-list
)
308 "Convert sorted list of numbers to a list of ranges or a single range.
309 If ALWAYS-LIST is non-nil, this function will always release a list of
311 (let* ((first (car numbers
))
316 (if (not (listp (cdr numbers
)))
319 (cond ((= last
(car numbers
)) nil
) ;Omit duplicated number
320 ((= (1+ last
) (car numbers
)) ;Still in sequence
321 (setq last
(car numbers
)))
322 (t ;End of one sequence
324 (cons (if (= first last
) first
327 (setq first
(car numbers
))
328 (setq last
(car numbers
))))
329 (setq numbers
(cdr numbers
)))
330 (if (and (not always-list
) (null result
))
331 (if (= first last
) (list first
) (cons first last
))
332 (nreverse (cons (if (= first last
) first
(cons first last
))
335 (defalias 'gnus-uncompress-sequence
'gnus-uncompress-range
)
336 (defun gnus-uncompress-range (ranges)
337 "Expand a list of ranges into a list of numbers.
338 RANGES is either a single range on the form `(num . num)' or a list of
340 (let (first last result
)
344 ((not (listp (cdr ranges
)))
345 (setq first
(car ranges
))
346 (setq last
(cdr ranges
))
347 (while (<= first last
)
348 (setq result
(cons first result
))
349 (setq first
(1+ first
)))
353 (if (atom (car ranges
))
354 (when (numberp (car ranges
))
355 (setq result
(cons (car ranges
) result
)))
356 (setq first
(caar ranges
))
357 (setq last
(cdar ranges
))
358 (while (<= first last
)
359 (setq result
(cons first result
))
360 (setq first
(1+ first
))))
361 (setq ranges
(cdr ranges
)))
362 (nreverse result
)))))
364 (defun gnus-add-to-range (ranges list
)
365 "Return a list of ranges that has all articles from both RANGES and LIST.
366 Note: LIST has to be sorted over `<'."
368 (gnus-compress-sequence list t
)
369 (setq list
(copy-sequence list
))
370 (unless (listp (cdr ranges
))
371 (setq ranges
(list ranges
)))
373 ilist lowest highest temp
)
374 (while (and ranges list
)
376 (setq lowest
(or (and (atom (car ranges
)) (car ranges
))
378 (while (and list
(cdr list
) (< (cadr list
) lowest
))
379 (setq list
(cdr list
)))
380 (when (< (car ilist
) lowest
)
382 (setq list
(cdr list
))
384 (setq out
(nconc (gnus-compress-sequence ilist t
) out
)))
385 (setq highest
(or (and (atom (car ranges
)) (car ranges
))
387 (while (and list
(<= (car list
) highest
))
388 (setq list
(cdr list
)))
389 (setq ranges
(cdr ranges
)))
391 (setq out
(nconc (gnus-compress-sequence list t
) out
)))
392 (setq out
(sort out
(lambda (r1 r2
)
393 (< (or (and (atom r1
) r1
) (car r1
))
394 (or (and (atom r2
) r2
) (car r2
))))))
397 (if (atom (car ranges
))
399 (if (atom (cadr ranges
))
400 (when (= (1+ (car ranges
)) (cadr ranges
))
401 (setcar ranges
(cons (car ranges
)
403 (setcdr ranges
(cddr ranges
)))
404 (when (= (1+ (car ranges
)) (caadr ranges
))
405 (setcar (cadr ranges
) (car ranges
))
406 (setcar ranges
(cadr ranges
))
407 (setcdr ranges
(cddr ranges
)))))
409 (if (atom (cadr ranges
))
410 (when (= (1+ (cdar ranges
)) (cadr ranges
))
411 (setcdr (car ranges
) (cadr ranges
))
412 (setcdr ranges
(cddr ranges
)))
413 (when (= (1+ (cdar ranges
)) (caadr ranges
))
414 (setcdr (car ranges
) (cdadr ranges
))
415 (setcdr ranges
(cddr ranges
))))))
416 (setq ranges
(cdr ranges
)))
419 (defun gnus-remove-from-range (range1 range2
)
420 "Return a range that has all articles from RANGE2 removed from RANGE1.
421 The returned range is always a list. RANGE2 can also be a unsorted
422 list of articles. RANGE1 is modified by side effects, RANGE2 is not
424 (if (or (null range1
) (null range2
))
426 (let (out r1 r2 r1_min r1_max r2_min r2_max
427 (range2 (gnus-copy-sequence range2
)))
428 (setq range1
(if (listp (cdr range1
)) range1
(list range1
))
429 range2
(sort (if (listp (cdr range2
)) range2
(list range2
))
431 (< (if (consp e1
) (car e1
) e1
)
432 (if (consp e2
) (car e2
) e2
))))
435 r1_min
(if (consp r1
) (car r1
) r1
)
436 r1_max
(if (consp r1
) (cdr r1
) r1
)
437 r2_min
(if (consp r2
) (car r2
) r2
)
438 r2_max
(if (consp r2
) (cdr r2
) r2
))
439 (while (and range1 range2
)
440 (cond ((< r2_max r1_min
) ; r2 < r1
442 (setq r2
(car range2
)
443 r2_min
(if (consp r2
) (car r2
) r2
)
444 r2_max
(if (consp r2
) (cdr r2
) r2
)))
445 ((and (<= r2_min r1_min
) (<= r1_max r2_max
)) ; r2 overlap r1
447 (setq r1
(car range1
)
448 r1_min
(if (consp r1
) (car r1
) r1
)
449 r1_max
(if (consp r1
) (cdr r1
) r1
)))
450 ((and (<= r2_min r1_min
) (<= r2_max r1_max
)) ; r2 overlap min r1
452 (setq r1_min
(1+ r2_max
)
454 r2_min
(if (consp r2
) (car r2
) r2
)
455 r2_max
(if (consp r2
) (cdr r2
) r2
)))
456 ((and (<= r1_min r2_min
) (<= r2_max r1_max
)) ; r2 contained in r1
457 (if (eq r1_min
(1- r2_min
))
459 (push (cons r1_min
(1- r2_min
)) out
))
461 (if (< r2_max r1_max
) ; finished with r1?
462 (setq r1_min
(1+ r2_max
))
464 (setq r1
(car range1
)
465 r1_min
(if (consp r1
) (car r1
) r1
)
466 r1_max
(if (consp r1
) (cdr r1
) r1
)))
467 (setq r2
(car range2
)
468 r2_min
(if (consp r2
) (car r2
) r2
)
469 r2_max
(if (consp r2
) (cdr r2
) r2
)))
470 ((and (<= r2_min r1_max
) (<= r1_max r2_max
)) ; r2 overlap max r1
471 (if (eq r1_min
(1- r2_min
))
473 (push (cons r1_min
(1- r2_min
)) out
))
475 (setq r1
(car range1
)
476 r1_min
(if (consp r1
) (car r1
) r1
)
477 r1_max
(if (consp r1
) (cdr r1
) r1
)))
478 ((< r1_max r2_min
) ; r2 > r1
480 (if (eq r1_min r1_max
)
482 (push (cons r1_min r1_max
) out
))
483 (setq r1
(car range1
)
484 r1_min
(if (consp r1
) (car r1
) r1
)
485 r1_max
(if (consp r1
) (cdr r1
) r1
)))))
487 (if (eq r1_min r1_max
)
489 (push (cons r1_min r1_max
) out
))
492 (push (pop range1
) out
))
495 (defun gnus-member-of-range (number ranges
)
496 (if (not (listp (cdr ranges
)))
497 (and (>= number
(car ranges
))
498 (<= number
(cdr ranges
)))
501 (if (numberp (car ranges
))
502 (>= number
(car ranges
))
503 (>= number
(caar ranges
)))
505 (when (if (numberp (car ranges
))
506 (= number
(car ranges
))
507 (and (>= number
(caar ranges
))
508 (<= number
(cdar ranges
))))
510 (setq ranges
(cdr ranges
)))
513 (defun gnus-list-range-intersection (list ranges
)
514 "Return a list of numbers in LIST that are members of RANGES.
515 LIST is a sorted list."
516 (setq ranges
(gnus-range-normalize ranges
))
518 (while (setq number
(pop list
))
520 (if (numberp (car ranges
))
521 (< (car ranges
) number
)
522 (< (cdar ranges
) number
)))
523 (setq ranges
(cdr ranges
)))
525 (if (numberp (car ranges
))
526 (= (car ranges
) number
)
527 ;; (caar ranges) <= number <= (cdar ranges)
528 (>= number
(caar ranges
))))
529 (push number result
)))
532 (defalias 'gnus-inverse-list-range-intersection
'gnus-list-range-difference
)
534 (defun gnus-list-range-difference (list ranges
)
535 "Return a list of numbers in LIST that are not members of RANGES.
536 LIST is a sorted list."
537 (setq ranges
(gnus-range-normalize ranges
))
539 (while (setq number
(pop list
))
541 (if (numberp (car ranges
))
542 (< (car ranges
) number
)
543 (< (cdar ranges
) number
)))
544 (setq ranges
(cdr ranges
)))
545 (when (or (not ranges
)
546 (if (numberp (car ranges
))
547 (not (= (car ranges
) number
))
548 ;; not ((caar ranges) <= number <= (cdar ranges))
549 (< number
(caar ranges
))))
550 (push number result
)))
553 (defun gnus-range-length (range)
554 "Return the length RANGE would have if uncompressed."
558 ((not (listp (cdr range
)))
559 (- (cdr range
) (car range
) -
1))
562 (dolist (x range sum
)
564 (+ sum
(if (consp x
) (- (cdr x
) (car x
) -
1) 1))))))))
566 (defun gnus-sublist-p (list sublist
)
567 "Test whether all elements in SUBLIST are members of LIST."
570 (unless (memq (pop sublist
) list
)
575 (defun gnus-range-add (range1 range2
)
576 "Add RANGE2 to RANGE1 (nondestructively)."
577 (unless (listp (cdr range1
))
578 (setq range1
(list range1
)))
579 (unless (listp (cdr range2
))
580 (setq range2
(list range2
)))
581 (let ((item1 (pop range1
))
584 (while (or item1 item2
)
589 ((and (numberp item1
) (numberp item2
)) (< item1 item2
))
590 ((numberp item1
) (< item1
(car item2
)))
591 ((numberp item2
) (< (car item1
) item2
))
592 (t (< (car item1
) (car item2
)))))
595 (let ((tmp1 item
) (tmp2 (if selector item1 item2
)))
599 ((and (numberp tmp1
) (numberp tmp2
))
601 ((eq tmp1 tmp2
) tmp1
)
602 ((eq (1+ tmp1
) tmp2
) (cons tmp1 tmp2
))
603 ((eq (1+ tmp2
) tmp1
) (cons tmp2 tmp1
))
607 ((and (>= tmp1
(car tmp2
)) (<= tmp1
(cdr tmp2
))) tmp2
)
608 ((eq (1+ tmp1
) (car tmp2
)) (cons tmp1
(cdr tmp2
)))
609 ((eq (1- tmp1
) (cdr tmp2
)) (cons (car tmp2
) tmp1
))
613 ((and (>= tmp2
(car tmp1
)) (<= tmp2
(cdr tmp1
))) tmp1
)
614 ((eq (1+ tmp2
) (car tmp1
)) (cons tmp2
(cdr tmp1
)))
615 ((eq (1- tmp2
) (cdr tmp1
)) (cons (car tmp1
) tmp2
))
617 ((< (1+ (cdr tmp1
)) (car tmp2
)) nil
)
618 ((< (1+ (cdr tmp2
)) (car tmp1
)) nil
)
619 (t (cons (min (car tmp1
) (car tmp2
))
620 (max (cdr tmp1
) (cdr tmp2
))))))
622 (if item
(push item range
))
623 (if selector item1 item2
))))
625 (setq item1
(pop range1
))
626 (setq item2
(pop range2
))))
627 (if item
(push item range
))
631 (defun gnus-add-to-sorted-list (list num
)
632 "Add NUM into sorted LIST by side effect."
633 (let* ((top (cons nil list
))
635 (while (and list
(< (car list
) num
))
638 (unless (eq (car list
) num
)
639 (setcdr prev
(cons num list
)))
642 (defun gnus-range-map (func range
)
643 "Apply FUNC to each value contained by RANGE."
644 (setq range
(gnus-range-normalize range
))
646 (let ((span (pop range
)))
649 (let ((first (car span
))
651 (while (<= first last
)
653 (setq first
(1+ first
))))))))
655 (provide 'gnus-range
)
657 ;; arch-tag: 4780bdd8-5a15-4aff-be28-18727895b6ad
658 ;;; gnus-range.el ends here