chickadee » iset

iset

Description

Integer sets.

Author

Alex Shinn

Requirements

None

Documentation

Bit-vectors

Bit-vectors provide an abstract interface to bitwise operations typically done with integers. They may in fact be implemented as integers on implementations with bignums, or they may be implemented by other means such as vectors which may be more efficient. These vectors are meant to be used as flags and sets, not integer values, and thus are operations are ones-complement (there are no negative bit-vectors).

Creating bit-vectors
(make-bit-vector size) procedure

A 'zero' bit-vector, with a hint that we wish to use SIZE bits.

(make-bit-vector size #t) procedure

Same as above but initialize the all bit elements to #t (= the integer 2^SIZE-1)

(integer->bit-vector n) procedure

Create a bit-vector with bits initialized to the corresponds bits in fixnum N

(bit-vector-copy bv) procedure

Make a copy of the bit-vector BV

Bit-vector predicates
(bit-vector? obj) procedure

#t iff OBJ is a valid bit-vector, which is not necessarily a disjoint type

(bit-vector-empty? bv) procedure

#t iff BV has no bits set (the bit-vector equivalent of the ZERO? procedure)

(bit-vector-full? bv to) procedure

#t iff BV has all bits lower than TO set (the low end is 2^i-1)

The individual bits in the vector are accessed and set as boolean values:

Accessing bit-vector elements
(bit-vector-ref bv i) procedure

#t if the I-th bit in BV is set, else #f

(bit-vector-set bv i x) procedure

Return a new copy of BV with the I-th bit set to boolean x (off iff X is #f)

(bit-vector-set! bv i x) procedure

In-place update version of the above, is allowed but not required to mutate BV

Bitwise operations on bit-vectors

The following procedures are direct analogues of the corresponding SRFI-33 bitwise operations:

(bit-vector-length bv) procedure

integer-length: the index of the largest bit set in BV

(bit-vector-count bv) procedure

bit-count: the number of set bits in BV

(bit-vector-shift bv n) procedure

arithmetic-shift

(bit-vector-and bv ...) procedure

bitwise-and

(bit-vector-ior bv ...) procedure

bitwise-ior

(bit-vector-xor bv ...) procedure

bitwise-xor

(bit-vector-eqv bv ...) procedure

bitwise-eqv

(bit-vector-nand bv ...) procedure

bitwise-nand

(bit-vector-nor bv ...) procedure

bitwise-nor

The following in-place update equivalents are also available, which are allowed, but not required, to mutate their first argument only:

(bit-vector-shift! bv n) procedure
(bit-vector-and! bv ...) procedure
(bit-vector-ior! bv ...) procedure
(bit-vector-xor! bv ...) procedure
(bit-vector-eqv! bv ...) procedure
(bit-vector-nand! bv ...) procedure
(bit-vector-nor! bv ...) procedure

Integer sets

An integer set is a set of exact integers optimized for minimal space usage and fast membership lookup. General set operations are provided based on the character set operations found in SRFI-14.

Creating isets
(make-iset) procedure

An empty integer set.

(make-iset n) procedure

A set of the single integer N.

(make-iset n m) procedure

A set of the range of all integers from N-M inclusive.

SRFI-14 analogues

The following procedures are provided as direct analogs of the SRFI-14 procedures, accepting and returning isets and integers in place of char-sets and characters:

(iset-copy is) procedure

A new copy of IS

(iset n ...) procedure

An iset containing the elements N...

(list->iset ls [base-is]) procedure

An iset containing all the integers in list LS, union BASE-IS if provided

(list->iset! ls base-is) procedure

Same as above, allowed but not required to modify base-is

(iset-size is) procedure

Return the # of elements in IS

(iset-contains? is n) procedure

Test N for membership in IS

(iset->list is) procedure

Returns a list of all integers in IS

(iset-any pred is) procedure

Apply PRED to every element of IS, returning the first value returned by PRED that is not #f, if any (order unspecified)

(iset-every pred is) procedure

Returns #t if every element of IS satisfies the predicate PRED (order unspecified)

(iset? obj) procedure

#t iff obj is an integer set.

(iset= is ...) procedure

#t iff all arguments are equivalent integer sets.

(iset<= is ...) procedure

#t iff the arguments are monotonically increasing sets.

(iset>= is ...) procedure

#t iff the arguments are monotonically decreasing sets.

(iset-fold kons knil is) procedure

char-set-fold

(iset-unfold f p g [base-is]) procedure

char-set-unfold

(iset-unfold! f p g base-is) procedure

char-set-unfold!

(iset-for-each proc is) procedure

char-set-for-each

(iset-map proc is) procedure

char-set-map

(iset-filter pred is [bas-is]) procedure

char-set-filter

(iset-filter! pred is base-is) procedure

char-set-filter!

(iset-cursor iset) procedure
(iset-ref iset cursor) procedure
(iset-cursor-next iset cursor) procedure
(end-of-iset? iset) procedure

Cursor-based traversal of isets.

(iset-adjoin is n ...) procedure

char-set-adjoin

(iset-delete is n ...) procedure

char-set-delete

(iset-adjoin! is n ...) procedure

char-set-adjoin!

(iset-delete! is n ...) procedure

char-set-delete!

(iset-union is1 ...) procedure

char-set-union

(iset-intersection is1 ...) procedure

char-set-intersection

(iset-difference is1 is2 ...) procedure

char-set-difference

(iset-xor is1 ...) procedure

char-set-xor

(iset-diff+intersection is1 is2 ...) procedure

char-set-diff+intersection

(iset-union! is1 ...) procedure

char-set-union!

(iset-intersection! is1 ...) procedure

char-set-intersection!

(iset-difference! is1 is2 ...) procedure

char-set-difference!

(iset-xor! is1 ...) procedure

char-set-xor!

(iset-diff+intersection! is1 is2 ...) procedure

char-set-diff+intersection!

Changelog

License

 Copyright (c) 2004-2006, Alex Shinn
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:
 1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
 2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.
 3. The name of the author may not be used to endorse or promote products
    derived from this software without specific prior written permission.
 
 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Contents »