[REBOL] Re: bitset actions?
From: brett:codeconscious at: 10-Oct-2003 16:36
Hi Rebolinth,
I am not sure about what you are trying to achieve, but I think you may have
the wrong idea about bitsets. Here are some examples to help you.
Bitsets are not series:
>> series? make bitset! 8
== false
However they do have a length - the number of bits (always a multiple of 8):
>> length? make bitset! 8
== 8
>> length? make bitset! 16
== 16
The Charset function just creates bitsets from the character you specify:
>> source charset
charset: func [
"Makes a bitset of chars for the parse function."
chars [string! block!]
][
make bitset! chars
]
The number of bits in a bitset created by the Charset function is
(currently) 256 - one bit to represent each possible character:
>> length? charset ""
== 256
With bitsets you are *only* turning some bits on and some bits off by a
position number. Characters are translated to a position.
> Is there a way to 'pick on bitsets?
You use Find to test if a bit corresponding to your value is set:
>> find charset "ab" "a"
== true
>> find charset "ab" "b"
== true
>> find charset "ab" "c"
== none
You can use the ascii value:
>> find charset "*" 42
== true
You can set bits corresponding to your value with Insert:
>> find insert charset "ab" "d" "d"
== true
To enumerate all the characters represented in a bitset created using
charset use a loop (notice also that the bitset does not record duplicates):
bitset: charset "aaaaybcx"
chars: copy {}
for i 0 (subtract length? bitset 1) 1 [
if find bitset i [append chars to-char i]
]
?? chars
When using Parse bitsets can be used for matching against a set of
characters:
my-chars: charset "xyz"
is simpler and probably more efficient than
my-chars: [#"x" | #"y" | #"z"]
You can use bitsets with Find too:
>> find "123x5" charset "xyz"
== "x5"
Clear, Empty?, Union, Interset, Exclude, Difference, Negate and Complement
can be used on bitsets
Example using just a small bitset.
>> find insert make bitset! 8 6 6
== true
Outside the range:
>> insert make bitset! 8 9
** Script Error: Invalid argument: 9
** Near: insert make bitset! 8 9
>> find make bitset! 8 9
** Script Error: Invalid argument: 9
** Near: find make bitset! 8 9
Cheers,
Brett.