Note: This is taken from the Chicken Wiki, where a more recent version could be available.

iset

Integer sets.

Alex Shinn

None

iset.egg

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

<procedure>(make-bit-vector size)</procedure>

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

<procedure>(make-bit-vector size #t)</procedure>

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

<procedure>(integer→bit-vector n)</procedure>

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

<procedure>(bit-vector-copy bv)</procedure>

Make a copy of the bit-vector BV

Bit-vector predicates

<procedure>(bit-vector? obj)</procedure>

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

<procedure>(bit-vector-empty? bv)</procedure>

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

<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

<procedure>(bit-vector-ref bv i)</procedure>

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

<procedure>(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)

<procedure>(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:

<procedure>(bit-vector-length bv)</procedure>

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

<procedure>(bit-vector-count bv)</procedure>

bit-count: the number of set bits in BV

<procedure>(bit-vector-shift bv n)</procedure>

arithmetic-shift

<procedure>(bit-vector-and bv ...)</procedure>

bitwise-and

<procedure>(bit-vector-ior bv ...)</procedure>

bitwise-ior

<procedure>(bit-vector-xor bv ...)</procedure>

bitwise-xor

<procedure>(bit-vector-eqv bv ...)</procedure>

bitwise-eqv

<procedure>(bit-vector-nand bv ...)</procedure>

bitwise-nand

<procedure>(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:

<procedure>(bit-vector-shift! bv n)</procedure> <procedure>(bit-vector-and! bv ...)</procedure> <procedure>(bit-vector-ior! bv ...)</procedure> <procedure>(bit-vector-xor! bv ...)</procedure> <procedure>(bit-vector-eqv! bv ...)</procedure> <procedure>(bit-vector-nand! bv ...)</procedure> <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

<procedure>(make-iset)</procedure>

An empty integer set.

<procedure>(make-iset n)</procedure>

A set of the single integer N.

<procedure>(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:

<procedure>(iset-copy is)</procedure>

A new copy of IS

<procedure>(iset n ...)</procedure>

An iset containing the elements N...

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

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

<procedure>(list→iset! ls base-is)</procedure>

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

<procedure>(iset-size is)</procedure>

Return the # of elements in IS

<procedure>(iset-contains? is n)</procedure>

Test N for membership in IS

<procedure>(iset→list is)</procedure>

Returns a list of all integers in IS

<procedure>(iset-any pred is)</procedure>

Apply PRED to every element of IS, returning the first element it finds (order unspecified)

<procedure>(iset-every pred is)</procedure>

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

<procedure>(iset? obj)</procedure>

#t iff obj is an integer set.

<procedure>(iset= is ...)</procedure>

#t iff all arguments are equivalent integer sets.

<procedure>(iset⇐ is ...)</procedure>

#t iff the arguments are monotonically increasing sets.

<procedure>(iset>= is ...)</procedure>

#t iff the arguments are monotonically decreasing sets.

<procedure>(iset-fold kons knil is)</procedure>

char-set-fold

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

char-set-unfold

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

char-set-unfold!

<procedure>(iset-for-each proc is)</procedure>

char-set-for-each

<procedure>(iset-map proc is)</procedure>

char-set-map

<procedure>(iset-filter pred is [bas-is])</procedure>

char-set-filter

<procedure>(iset-filter! pred is base-is)</procedure>

char-set-filter!

<procedure>(iset-cursor iset)</procedure> <procedure>(iset-ref iset cursor)</procedure> <procedure>(iset-cursor-next iset cursor)</procedure> <procedure>(end-of-iset? iset)</procedure>

Cursor-based traversal of isets.

<procedure>(iset-delete is n ...)</procedure>

char-set-delete

<procedure>(iset-delete! is n ...)</procedure>

char-set-delete!

<procedure>(iset-union is1 ...)</procedure>

char-set-union

<procedure>(iset-intersection is1 ...)</procedure>

char-set-intersection

<procedure>(iset-difference is1 is2 ...)</procedure>

char-set-difference

<procedure>(iset-xor is1 ...)</procedure>

char-set-xor

<procedure>(iset-diff+intersection is1 is2 ...)</procedure>

char-set-diff+intersection

<procedure>(iset-union! is1 ...)</procedure>

char-set-union!

<procedure>(iset-intersection! is1 ...)</procedure>

char-set-intersection!

<procedure>(iset-difference! is1 is2 ...)</procedure>

char-set-difference!

<procedure>(iset-xor! is1 ...)</procedure>

char-set-xor!

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

char-set-diff+intersection!

Changelog

• 1.4 Workaround in bit-vector-copy for zero-length vectors.
• 1.3 Fixed bit-vector-copy export but no define [by Kon Lovett]
• 1.2 Added iset-optimize and iset-balance, fixed some bugs
• 1.0 Initial release

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.