Skip to content

Instantly share code, notes, and snippets.

@scymtym
Last active May 2, 2020 21:19
Show Gist options
  • Save scymtym/1f2de19e73bdaa25dbe280bc9d838e72 to your computer and use it in GitHub Desktop.
Save scymtym/1f2de19e73bdaa25dbe280bc9d838e72 to your computer and use it in GitHub Desktop.
contiguous-numeric-set-type
(defun contiguous-numeric-set-type (xset)
(let ((members (xset-members xset)))
(cond ((null members)
nil)
((null (cdr members))
(ctype-of (car members)))
;; Is MEMBERS a contiguous integer range?
((loop for x in members
always (integerp x)
minimizing x into min
maximizing x into max
finally (return (when (= (- max min)
(1- (length members)))
(make-numeric-type :class 'integer
:low min :high max)))))
;; It's useful to know when something is not zero
((xset-member-p 0 xset)
(make-numeric-type :class 'integer :low 0 :high 0)))))
(defun contiguous-numeric-set-type (xset)
(cond ((xset-empty-p xset)
nil)
;; Is MEMBERS a contiguous integer range?
((block nil
(let ((count 0)
(min nil)
(max nil))
(declare (type fixnum count))
(map-xset (lambda (value)
(unless (integerp value)
(return))
(incf count)
(when (or (null min) (< value min))
(setf min value))
(when (or (null max) (> value max))
(setf max value)))
xset)
(when (= (- max min) (1- count))
(make-numeric-type :class 'integer
:low min :high max)))))
;; It's useful to know when something is not zero
((xset-member-p 0 xset)
(make-numeric-type :class 'integer :low 0 :high 0))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment