Re: Pascal-like set class

From: Rich Herrick (rich.delete.me_at_richherrick.com.delete.me.too)
Date: 09/06/04


Date: Mon, 06 Sep 2004 05:17:42 GMT


"David Hilsee" <davidhilseenews@yahoo.com> wrote in message
news:2OSdnYhAfKCBRqbcRVn-jQ@comcast.com...
> "Rich Herrick" <rich.delete.me@richherrick.com.delete.me.too> wrote in
> message news:zBM_c.60065$Kt5.31893@twister.nyroc.rr.com...
> <snip>
> I'll buy the other operators, but I think that <<, >>, !, nad operator
void*
> are, at the very least, questionable. Also, the == and != for set
> membership seem odd, because I pronounce the code "If element e is equal
to
> set s" when it really means "if set s contains element e".
I agree. To be honest, I had forget about the == and != operators, and in
my code I use the contains() member. I think I will remove them on future
releases. But I still waffle back and forth on << and >>. I can see where
they are extraneous, but I tend to like and use them a lot. The ! and void*
operator I will probably remove also.

>
> A few more questions:
>
> Why does the traits class provide the array element type? Why would
anyone
> want to customize the element?
Because, the reason I wrote it was because I wanted low-level control of the
layout and the size. If I have a set with n items and n <= CHAR_BIT, I may
want to fit them all into am unsigned char instead of an unsigned int. I do
a lot of embedded software, dealing with drivers and memory-mapped registers
and such. I'm use to have that kind of control, and tend to write
interfaces with that in mind. I also deal with resources that are more
constrained than an app. writer on a PC might. So, I guess the simple
answer to your question is they wouldn't, except when the array is only one
element in size, then I may want to use the smallest int type I can.

>
> Why even bother with an array? Why does std::bitset not provide what you
> need? It took me a few minutes to wrap my head around the "traits" code,
> and then I realized that it was basically implementing std::bitset. I
think
> that's what it's doing, anyway.
Depends on what you are asking. If you are asking why pascal_set instead of
bitset, then the answer is pascal_set is more typesafe then bitset and I
want to mimic the Pascal set type as closely as I could, although I suppose
I could have just wrapped bitset inside pascal_set. If you are asking why I
didn't just wrap bitset inside my class instead of using an array, it comes
down to control. I wanted control of the size and control of the bit-order.
A lot of what I do interfaces with other languages (I write a lot of C, C++
and Ada--both '83 and '95), and memory-mapped registers. I have to know the
layout. pascal_set makes it nice when dealing with set of bit flags in a
memory-mapped register or an Ada bit array. Though its implementation
specific, Ada compilers tend to use the MSB as bit 0 on a big-endian machine
and the LSB as bit 0 on a little-endian machine. I wanted to be able to set
my bit order to match. Plus, I want to see if I could use templates to
create compile-set constant sets, which I may not be able to do with bitset.
This is how the setof template evolved.

My reasons probably aren't good enough for someone looking for a set class
to choose mine over bitset or even set. But that's OK, I wrote it for me,
and found it useful and a joy to use and thought to share it with anyone you
may be interested. I am currently using it recreationally in writing some
Interactive Fiction.

And while writing this, I just thought of a big bug in the whole "traits"
thing I must now fix....

Regards.

--
Rich Herrick


Relevant Pages

  • Re: Are there any databases that can store multidiimensional boolean arrays?
    ... This person wants to store a four dimensional boolean array, ... I'd never noticed the BitSet class before in several years of coding Java ...
    (comp.lang.java.databases)
  • Re: algorithm help
    ... i added a larger range 1000..5000 to the test set which should come ... closer to ara's problem at hand. ... Interestingly this brings the bitset approach back in play, ... Your approach has a clear advantage if ranges are fairly large compared to the overall number of elements in the array. ...
    (comp.lang.ruby)
  • Using <bitset>
    ... Is there a way to use <bitset> on an external array of bits (rather than ... bitset's built in _Array)? ...
    (microsoft.public.vc.stl)
  • Re: sets and vectors
    ... >> Pascal provides sets natively from memory. ... >> internally as a bit array, with one bit for each potential item in the ... > isn't that a bitset? ... that can go into the set are identifiers. ...
    (comp.programming)