CMPS 144L Spring 2022
Lab #11 (April 21/22): Integer Range Set

Task

In this lab, you are to complete as many of the STUBbed methods as you can in the IntRangeSet class. Each instance of this class represents a subset of natural numbers in the interval [0,N), for some N.

For the purpose of testing your work, you can use the IntRangeSetTester application.

Concept

Assuming that you have a universal set of N elements (for some relatively small value of N) and a convenient way to translate between the elements of that universe and the natural numbers in the interval [0,N), a good way to represent a subset of the universe is by using what traditionally has been called a bit vector, meaning a sequence of 0's and 1's. Specifically, the sequence would be of length N, where, for each k, the k-th bit would indicate whether or not the k-th element of the universe is a member of the set.

To keep things simple, here we will be assuming that the universal set is literally the set of integers in the interval [0,N) (for some chosen N>0). An instance of the IntRangeSet class represents a subset of that universe (where the value of N is chosen by the client who creates the object). It does so by using the modern version of a bit vector, which is an array of type boolean[]. As an example, suppose that N = 10 and that the set being represented is { 2, 3, 5, 8 }. Then the "bit vector" (embodied in the class's member instance variable) would be as follows, where 't' stands for true and 'f' for false.

  0   1   2   3   4   5   6   7   8   9
+---+---+---+---+---+---+---+---+---+---+
| f | f | t | t | f | t | f | f | t | f |
+---+---+---+---+---+---+---+---+---+---+

Notice that the array elements having value true are precisely those at locations corresponding to members of the set (namely, 2, 3, 5, and 8).

Given this representation scheme, algorithms for basic set operations (e.g., insert/remove element, isMemberOf?, cardinality, union, intersection) should be fairly obvious.

Consider this to be your introduction to using a data structure (here, an array of boolean values) to represent a mathematical abstraction (here, a set). Note that the data structure is "encapsulated" within the Java class, so that a client program need not "know" —nor may it exploit— the details of that representation. This is an idea that you will see over and over again in this course and the ones to follow.