Goto Chapter: Top 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 Bib Ind
 [Top of Book]  [Contents]   [Previous Chapter]   [Next Chapter] 

22 Boolean Lists
 22.1 IsBlist (Filter)
 22.2 Boolean Lists Representing Subsets
 22.3 Set Operations via Boolean Lists
 22.4 Function that Modify Boolean Lists
 22.5 More about Boolean Lists

22 Boolean Lists

This chapter describes boolean lists. A boolean list is a list that has no holes and contains only the boolean values true and false (see Chapter 20). In function names we call boolean lists blists for brevity.

22.1 IsBlist (Filter)

22.1-1 IsBlist
‣ IsBlist( obj )( category )

A boolean list (blist) is a list that has no holes and contains only true and false. Boolean lists can be represented in an efficient compact form, see 22.5 for details.

gap> IsBlist( [ true, true, false, false ] );
true
gap> IsBlist( [] );
true
gap> IsBlist( [false,,true] );  # has holes
false
gap> IsBlist( [1,1,0,0] );      # contains not only boolean values
false
gap> IsBlist( 17 );             # is not even a list
false

Boolean lists are lists and all operations for lists are therefore applicable to boolean lists.

Boolean lists can be used in various ways, but maybe the most important application is their use for the description of subsets of finite sets. Suppose \(set\) is a finite set, represented as a list. Then a subset \(sub\) of \(set\) is represented by a boolean list \(blist\) of the same length as \(set\) such that \(blist[i]\) is true if \(set[i]\) is in \(sub\), and false otherwise.

22.2 Boolean Lists Representing Subsets

22.2-1 BlistList
‣ BlistList( list, sub )( function )

returns a new boolean list that describes the list sub as a sublist of the dense list list. That is BlistList returns a boolean list \(blist\) of the same length as list such that \(blist[i]\) is true if list\([i]\) is in sub and false otherwise.

list need not be a proper set (see 21.19), even though in this case BlistList is most efficient. In particular list may contain duplicates. sub need not be a proper sublist of list, i.e., sub may contain elements that are not in list. Those elements of course have no influence on the result of BlistList.

gap> BlistList( [1..10], [2,3,5,7] );
[ false, true, true, false, true, false, true, false, false, false ]
gap> BlistList( [1,2,3,4,5,2,8,6,4,10], [4,8,9,16] );
[ false, false, false, true, false, false, true, false, true, false ]

See also UniteBlistList (22.4-2).

22.2-2 ListBlist
‣ ListBlist( list, blist )( function )

returns the sublist \(sub\) of the list list, which must have no holes, represented by the boolean list blist, which must have the same length as list.

\(sub\) contains the element list\([i]\) if blist\([i]\) is true and does not contain the element if blist\([i]\) is false. The order of the elements in \(sub\) is the same as the order of the corresponding elements in list.

gap> ListBlist([1..8],[false,true,true,true,true,false,true,true]);
[ 2, 3, 4, 5, 7, 8 ]
gap> ListBlist( [1,2,3,4,5,2,8,6,4,10],
> [false,false,false,true,false,false,true,false,true,false] );
[ 4, 8, 4 ]

22.2-3 SizeBlist
‣ SizeBlist( blist )( function )

returns the number of entries of the boolean list blist that are true. This is the size of the subset represented by the boolean list blist.

gap> SizeBlist( [ false, true, false, true, false ] );
2

22.2-4 IsSubsetBlist
‣ IsSubsetBlist( blist1, blist2 )( function )

returns true if the boolean list blist2 is a subset of the boolean list blist1, which must have equal length, and false otherwise. blist2 is a subset of blist1 if blist1\([i] =\) blist1\([i]\) or blist2\([i]\) for all \(i\).

gap> blist1 := [ true, true, false, false ];;
gap> blist2 := [ true, false, true, false ];;
gap> IsSubsetBlist( blist1, blist2 );
false
gap> blist2 := [ true, false, false, false ];;
gap> IsSubsetBlist( blist1, blist2 );
true

22.3 Set Operations via Boolean Lists

22.3-1 UnionBlist
‣ UnionBlist( blist1, blist2[, ...] )( function )
‣ UnionBlist( list )( function )

In the first form UnionBlist returns the union of the boolean lists blist1, blist2, etc., which must have equal length. The union is a new boolean list that contains at position \(i\) the value blist1\([i]\) or blist2\([i]\) or \(\ldots\).

The second form takes the union of all blists (which as for the first form must have equal length) in the list list.

22.3-2 IntersectionBlist
‣ IntersectionBlist( blist1, blist2[, ...] )( function )
‣ IntersectionBlist( list )( function )

In the first form IntersectionBlist returns the intersection of the boolean lists blist1, blist2, etc., which must have equal length. The intersection is a new blist that contains at position \(i\) the value blist1\([i]\) and blist2\([i]\) and \(\ldots\).

In the second form list must be a list of boolean lists blist1, blist2, etc., which must have equal length, and IntersectionBlist returns the intersection of those boolean lists.

22.3-3 DifferenceBlist
‣ DifferenceBlist( blist1, blist2 )( function )

returns the asymmetric set difference of the two boolean lists blist1 and blist2, which must have equal length. The asymmetric set difference is a new boolean list that contains at position \(i\) the value blist1\([i]\) and not blist2\([i]\).

gap> blist1 := [ true, true, false, false ];;
gap> blist2 := [ true, false, true, false ];;
gap> UnionBlist( blist1, blist2 );
[ true, true, true, false ]
gap> IntersectionBlist( blist1, blist2 );
[ true, false, false, false ]
gap> DifferenceBlist( blist1, blist2 );
[ false, true, false, false ]

22.4 Function that Modify Boolean Lists

22.4-1 UniteBlist
‣ UniteBlist( blist1, blist2 )( function )

UniteBlist unites the boolean list blist1 with the boolean list blist2, which must have the same length. This is equivalent to assigning blist1\([i] :=\) blist1\([i]\) or blist2\([i]\) for all \(i\).

UniteBlist returns nothing, it is only called to change blist1.

gap> blist1 := [ true, true, false, false ];;
gap> blist2 := [ true, false, true, false ];;
gap> UniteBlist( blist1, blist2 );
gap> blist1;
[ true, true, true, false ]

The function UnionBlist (22.3-1) is the nondestructive counterpart to UniteBlist.

22.4-2 UniteBlistList
‣ UniteBlistList( list, blist, sub )( function )

works like UniteBlist(blist,BlistList(list,sub)). As no intermediate blist is created, the performance is better than the separate function calls.

22.4-3 IntersectBlist
‣ IntersectBlist( blist1, blist2 )( function )

intersects the boolean list blist1 with the boolean list blist2, which must have the same length. This is equivalent to assigning blist1\([i]:=\) blist1\([i]\) and blist2\([i]\) for all \(i\).

IntersectBlist returns nothing, it is only called to change blist1.

gap> blist1 := [ true, true, false, false ];;
gap> blist2 := [ true, false, true, false ];;
gap> IntersectBlist( blist1, blist2 );
gap> blist1;
[ true, false, false, false ]

The function IntersectionBlist (22.3-2) is the nondestructive counterpart to IntersectBlist.

22.4-4 SubtractBlist
‣ SubtractBlist( blist1, blist2 )( function )

subtracts the boolean list blist2 from the boolean list blist1, which must have equal length. This is equivalent to assigning blist1\([i]:=\) blist1\([i]\) and not blist2\([i]\) for all \(i\).

SubtractBlist returns nothing, it is only called to change blist1.

gap> blist1 := [ true, true, false, false ];;
gap> blist2 := [ true, false, true, false ];;
gap> SubtractBlist( blist1, blist2 );
gap> blist1;
[ false, true, false, false ]

The function DifferenceBlist (22.3-3) is the nondestructive counterpart to SubtractBlist.

22.4-5 FlipBlist
‣ FlipBlist( blist )( function )

Changes every entry in blist that equals true to false and vice versa. If blist1 and blist2 are boolean lists with equal length and every value in blist2 is true, then FlipBlist( blist1 ) is equivalent to SubtractBlist( blist2, blist1 ); blist1 := blist2; but FlipBlist is faster, and simpler to type.

FlipBlist returns nothing, it is only called to change blist in-place.

gap> blist1 := [ true, true, true, true ];;
gap> blist2 := [ true, false, true, false ];;
gap> SubtractBlist( blist1, blist2 );
gap> blist1;
[ false, true, false, true ]
gap> FlipBlist( blist2 );
gap> blist2;
[ false, true, false, true ]

22.4-6 SetAllBlist
‣ SetAllBlist( blist )( function )

Changes every entry in blist to true. If blist1 and blist2 are boolean lists with equal length and every value in blist2 is true, then SetAllBlist( blist1 ) is equivalent to UniteBlist( blist1, blist2 ); but is faster, and simpler to type.

SetAllBlist returns nothing, it is only called to change blist in-place.

gap> blist1 := [ true, true, true, true ];;
gap> blist2 := [ true, false, true, false ];;
gap> SetAllBlist( blist1 );
gap> blist1;
[ true, true, true, true ]
gap> SetAllBlist(blist2);
gap> blist2;
[ true, true, true, true ]

22.4-7 ClearAllBlist
‣ ClearAllBlist( blist )( function )

Changes every entry in blist to false. If blist1 and blist2 are boolean lists with equal length and every value in blist2 is false, then ClearAllBlist( blist1 ) is equivalent to IntersectBlist( blist1, blist2 ); but is faster, and simpler to type.

ClearAllBlist returns nothing, it is only called to change blist in-place.

gap> blist1 := [ true, true, true, true ];;
gap> blist2 := [ true, false, true, false ];;
gap> ClearAllBlist( blist1 );
gap> blist1;
[ false, false, false, false ]
gap> ClearAllBlist(blist2);
gap> blist2;
[ false, false, false, false ]

22.5 More about Boolean Lists

We defined a boolean list as a list that has no holes and contains only true and false. There is a special internal representation for boolean lists that needs only 1 bit for each entry. This bit is set if the entry is true and reset if the entry is false. This representation is of course much more compact than the ordinary representation of lists, which needs 32 or 64 bits per entry.

Not every boolean list is represented in this compact representation. It would be too much work to test every time a list is changed, whether this list has become a boolean list. This section tells you under which circumstances a boolean list is represented in the compact representation, so you can write your functions in such a way that you make best use of the compact representation.

If a dense list containing only true and false is read, it is stored in the compact representation. Furthermore, the results of BlistList (22.2-1), UnionBlist (22.3-1), IntersectionBlist (22.3-2) and DifferenceBlist (22.3-3) are known to be boolean lists by construction, and thus are represented in the compact representation upon creation.

If an argument of IsSubsetBlist (22.2-4), ListBlist (22.2-2), UnionBlist (22.3-1), IntersectionBlist (22.3-2), DifferenceBlist (22.3-3), UniteBlist (22.4-1), IntersectBlist (22.4-3) and SubtractBlist (22.4-4) is a list represented in the ordinary representation, it is tested to see if it is in fact a boolean list. If it is not, an error is signalled. If it is, the representation of the list is changed to the compact representation.

If you change a boolean list that is represented in the compact representation by assignment (see 21.4) or Add (21.4-2) in such a way that the list remains a boolean list it will remain represented in the compact representation. Note that changing a list that is not represented in the compact representation, whether it is a boolean list or not, in such a way that the resulting list becomes a boolean list, will never change the representation of the list.

22.5-1 IsBlistRep
‣ IsBlistRep( obj )( representation )
‣ ConvertToBlistRep( blist )( function )

Returns: true or false

The first function is a filter that returns true if the object obj is a boolean list in compact representation and false otherwise, see 22.5.

The second function converts the object blist to a boolean list in compact representation and returns true if this is possible. Otherwise blist is unchanged and false is returned.

gap> l := [true, false, true];
[ true, false, true ]
gap> IsBlistRep(l);
true
gap> l := [true, false, 1];
[ true, false, 1 ]
gap> l[3] := false;
false
gap> IsBlistRep(l);
false
gap> ConvertToBlistRep(l);
true
 [Top of Book]  [Contents]   [Previous Chapter]   [Next Chapter] 
Goto Chapter: Top 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 Bib Ind

generated by GAPDoc2HTML