Next: , Previous: , Up: srfi comparators constructors   [Index]


2.38.8.6 Bytevector comparators

Function: make-bytevector-comparator element-comparator

Build and return a comparator object behaving like bytevector-comparator but using element-comparator rather than default-comparator.

#!vicare
(import (vicare) (srfi :114))

(define-constant E
  (make-comparator (lambda (element)
                     (or (= 1 element)
                         (zero? element)))
                   fx=?
                   (lambda (a b)
                     (cond ((fx=? a b)   0)
                           ((fx<? a b)  -1)
                           (else        +1)))
                   fixnum-hash))

(define-constant C
  (make-bytevector-comparator E))

;; type test
(comparator-test-type C '#vu8())        ⇒ #t
(comparator-test-type C '#vu8(1 0))     ⇒ #t
(comparator-test-type C '#vu8(1 2))     ⇒ #f
(comparator-test-type C "ciao")         ⇒ #f

;; type check
(comparator-check-type C '#vu8(1 0))    ⇒ #t
(try
    (comparator-check-type C (void))
  (catch E
    ((&comparator-type-error)
     #t)
    (else E)))
⇒ #t

;; comparison
(comparator-compare C '#vu8(1 0) '#vu8(1 0))   ⇒ 0
(comparator-compare C '#vu8(1 0) '#vu8(1 1))   ⇒ -1
(comparator-compare C '#vu8(1 1) '#vu8(1 0))   ⇒ +1

(comparator-compare C '#vu8()    '#vu8())      ⇒ 0
(comparator-compare C '#vu8()    '#vu8(1 0))   ⇒ -1
(comparator-compare C '#vu8(1 0) '#vu8())      ⇒ +1

;; hash
(non-negative-exact-integer?
  (comparator-hash C '#vu8()))    ⇒ #t
(non-negative-exact-integer?
  (comparator-hash C '#vu8(1 0))) ⇒ #t