|
Boost : |
From: joel de guzman (isis-tech_at_[hidden])
Date: 2001-06-07 09:27:52
Hello,
The following code "stateless sets" would benefit very much from
the 'auto' type declarator or the typeof.
Static Sets (stateless sets).
Static sets are parameterized sets that can conceptually hold
up to 256 boolean keys. Yet, like magic does not hold any data
at all, *nil*! *zilch*! The data is encoded completely in its type.
The original version of Spirit had this data type which IMO is
quite cool. It was omitted in the initial release of Spirit because
1) A declaration without a typeof or auto facility is cumbersome.
2) A version for 16/32 bits is still in the drawing board.
Nevertheless, the TYPEOF discussion prompted me to
share this stuff...
http://www.mydestiny.net/~isis-tech/Spirit_StaticSet.h
Comments welcome...
Static Sets are constructed from basic primitives:
Key<v> key; Declares a set with a single element v
Range<start, end> range; Declares a set with elements from start to end
Both keys and ranges are in fact static sets and the
usual set operations apply to these:
1) Set negation: ~a Set of all keys not in a
2) Set union: a | b Set of keys in a or b
3) Set intersection: a & b Set of keys in a and b
4) Set difference: a - b Set of keys in a but not b
5) Set xor: a ^ b Set of keys in a or b but not both
Static sets may be tested for set membership through
its IsMember member function:
a_set.IsMember('x'); // Tests if 'x' is a member of a_set
(a | b).IsMember('x'); // Tests if 'x' is a member of sets a | b
Examples:
Range<'a','z'> atoz; // Set of all lower case characters
Key<'x'> x; // Set with single element 'x'
(atoz - x); // Yields a set of all lower case characters
except 'x'
Expressions can be arbitrarily complex. Yet like Spirit's
expressions that necessitated Rule<>s, holding a set in a
declaration is cumbersome. The 'auto' or typeof facility
would make this practical for use in Spirit.
Cheers,
Joel de Guzman
PS> Even without typeof or auto, this can be a cool replacement
to the standard C library's ctype functions. No global data,
no bitvectors. Example:
int isxdigit(char ch) { return (Range<'0','9'>() |
Range<'a','z'>()).IsMember(ch); }
// yes, I know it's not guaranteed that 'a'~'z' is contiguous, but you get
the picture...
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk