473,396 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Pascal-like set class

The latest version of my "Pascal-like" set class is available here:

http://www.richherrick.com/software/herrick-1.0.zip

Those from the old YAHOO BOOST forum might remember it from several months
back. I have enhanced the code, and "deboostifized" it.

For those unfamiliar with it, it is a template class that implements set
classes that are similar to the set type in the Pascal language. Like
Pascal, any integer (ordinal) type may be used with the set, but I mainly
wrote it for use with enumerations. Set elements are stored at the bit
level, so it is every compact. There is even a member template for defining
set objects as compile-time constants. I wrote it because none of the C++
containers (i.e., set and bitset) suited my needs.

A big change from older versions is the addition of a traits template
parameter for use gaining finer control over the class' behavior for such
things as bit ordering and element range (which defaults to [0,
number-of-elements - 1]).

Examples from documentation:

declaration:
enum color_type
{NON_COLOR = -1, RED, ORANGE, YELLOW, GREEN, BLUE, INDIGO, VIOLET,
NUM_COLORS};

typedef herrick::pascal_set<color_type, NUM_COLORS> color_set;

compile-time constants:
const color_set additive_primaries =
color_set::setof<RED, GREEN, BLUE>::value;

const color_set subtractive_primaries =
color_set::setof<RED, YELLOW, BLUE>::value;

I'd be very interested in comments about the code, the interface (I may have
went overboard on operators!), or my use of templates (I still get hazy on
when to use "typename" and "template").

My goal is to have the interface as much as like the Pascal set type as
feasible, while having the code conform to the lated standard (regardless
particular compiler conformity).

The code currenty compilers with:
GCC 3.4.0 (MinGW)
Visual C++ Toolkit 2003
Rich Herrick
ri************@richherrick.com.delete.me.too


Jul 22 '05 #1
9 2740
"Rich Herrick" <ri************@richherrick.com.delete.me.too> wrote in
message news:hX*******************@twister.nyroc.rr.com...
<snip>
I'd be very interested in comments about the code, the interface (I may have went overboard on operators!), or my use of templates (I still get hazy on
when to use "typename" and "template").

<snip>

I think you did go overboard on the operators. Most of the operators for
that class do not stick to their traditional meaning. Some are close calls,
but the definitions of operator bool, !, <, <=, >, >=, <<, >>, /, and * look
odd to me.

--
David Hilsee
Jul 22 '05 #2

"David Hilsee" <da*************@yahoo.com> wrote in message
news:8-********************@comcast.com...
"Rich Herrick" <ri************@richherrick.com.delete.me.too> wrote in
message news:hX*******************@twister.nyroc.rr.com...
<snip>
I'd be very interested in comments about the code, the interface (I may have
went overboard on operators!), or my use of templates (I still get hazy on when to use "typename" and "template").

<snip>

I think you did go overboard on the operators. Most of the operators for
that class do not stick to their traditional meaning. Some are close

calls, but the definitions of operator bool, !, <, <=, >, >=, <<, >>, /, and * look odd to me.

--
David Hilsee


Thanks for replying. All the relational ones match how Pascal defines them,
and are needed to do subset/superset comparisions. The / and * operators
are also how Pascal defines them for doing symmetric difference and
intersection, respectively. There really isn't an operator bool, its really
operator void*, which will allow testing for a non-empty set in an if
statement without the nasty side-effects you will get with operator bool.
Though I will admit that operator void* and operator ! aren't really needed,
as there is a member that will test for an empty set (and you could also
test for equals to NULL_SET). The ones I knew someone would question are >>
and <<, as I almost took them out several times. But I just like the way
they "look" for adding elements to a set. There is a + operator that does
the same thing, but I think "S + e1 + e2" looks confusing, especially if e1
and e2 are integers: S + 1 + 2. I think S << 1 << 2 appears more readable
in code. Besides, the former creates a new set and the later does not, and
S += 1 + 2 will, of course, not work. Though again there are many other
ways to add elements to the set such as: S *= set_type(e1, e2), which will
union S with the new set, the result of which is in S.

Thanks,

Rich Herrick
Jul 22 '05 #3
"Rich Herrick" <ri************@richherrick.com.delete.me.too> wrote in
message news:zB*******************@twister.nyroc.rr.com...
<snip>
Thanks for replying. All the relational ones match how Pascal defines them, and are needed to do subset/superset comparisions. The / and * operators
are also how Pascal defines them for doing symmetric difference and
intersection, respectively. There really isn't an operator bool, its really operator void*, which will allow testing for a non-empty set in an if
statement without the nasty side-effects you will get with operator bool.
Though I will admit that operator void* and operator ! aren't really needed, as there is a member that will test for an empty set (and you could also
test for equals to NULL_SET). The ones I knew someone would question are
and <<, as I almost took them out several times. But I just like the way
they "look" for adding elements to a set. There is a + operator that does
the same thing, but I think "S + e1 + e2" looks confusing, especially if

e1 and e2 are integers: S + 1 + 2. I think S << 1 << 2 appears more readable
in code. Besides, the former creates a new set and the later does not, and S += 1 + 2 will, of course, not work. Though again there are many other
ways to add elements to the set such as: S *= set_type(e1, e2), which will
union S with the new set, the result of which is in S.


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".

A few more questions:

Why does the traits class provide the array element type? Why would anyone
want to customize the element?

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.

--
David Hilsee
Jul 22 '05 #4

"David Hilsee" <da*************@yahoo.com> wrote in message
news:2O********************@comcast.com...
"Rich Herrick" <ri************@richherrick.com.delete.me.too> wrote in
message news:zB*******************@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
Jul 22 '05 #5
By the way David, what is your opinion of the constructors provided? Too
much?

Thanks

--
Rich Herrick
Jul 22 '05 #6
"Rich Herrick" <ri************@richherrick.com.delete.me.too> wrote in
message news:Wp****************@twister.nyroc.rr.com...
<snip>
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.


I understand that, but I'm surprised that there would be cases where you
would want to use an unsigned char, and other cases where you would want to
use an unsigned int. In the latter case, wouldn't you just use multiple
unsigned chars? I've never done embedded programming, so I might be
overlooking something. Is there an alignment issue?

--
David Hilsee
Jul 22 '05 #7
"Rich Herrick" <ri************@richherrick.com.delete.me.too> wrote in
message news:xN***************@twister.nyroc.rr.com...
By the way David, what is your opinion of the constructors provided? Too
much?

<snip>

I don't see any problem with them. Many might be unnecessary, though,
thanks to the ones that take arrays. I can see how they look a tad nicer
than the equivalent code that uses arrays, though.

--
David Hilsee
Jul 22 '05 #8

"David Hilsee" <da*************@yahoo.com> wrote in message
news:Eq********************@comcast.com...
I understand that, but I'm surprised that there would be cases where you
would want to use an unsigned char, and other cases where you would want to use an unsigned int. In the latter case, wouldn't you just use multiple
unsigned chars? I've never done embedded programming, so I might be
overlooking something. Is there an alignment issue?

--
David Hilsee

Well, one reason would be a memory-mapped register accessed across a VME
bus. If a 16-bit register that is mapped as a D16 access, but you attempt
to access it as a D32 or 2 D8, you will get a data access exception. Same
with an 8-bit register. So, if I make the underlining type match (which
again is implementation specific), I can access it with my class.

If I had a large set, then sure, I would use int, because the set operations
are done on a whole int at a time, not bit. So, on a platform where int is
32 bits, and I had a set with 64 elements, it would do a union in two
operations, not 64. But, on the other hand, if I may be more concerned with
size. If, assuming char is 8 bits, and I need my set to fit into 24 bits
(maybe its in a struct talking to an Ada record, and Ada had a
representation specification that maps it into 24 bits), I can achieve this
by setting the unit type to unsigned char. I would be trading efficiency to
make it fit, but that would probably not be an issue.

--
Rich Herrick
Jul 22 '05 #9

"David Hilsee" <da*************@yahoo.com> wrote in message
news:bO********************@comcast.com...
"Rich Herrick" <ri************@richherrick.com.delete.me.too> wrote in
message news:xN***************@twister.nyroc.rr.com...
By the way David, what is your opinion of the constructors provided? Too much?

<snip>

I don't see any problem with them. Many might be unnecessary, though,
thanks to the ones that take arrays. I can see how they look a tad nicer
than the equivalent code that uses arrays, though.

--
David Hilsee

I wrote the multitude of constructors that take different numbers of
elements for type-safety (over var. length args.) and efficiently. I was
wondering if maybe the array ones were too much (especially since there is
two of them).

Thanks for your insights.

--
Rich Herrick
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Chris Gordon-Smith | last post by:
I am tying to call a Pascal function from C++, and vice versa. Does anyone know how to do this, or where detailed information on this topic can be found? For the C++ to Pascal call I have...
14
by: digital | last post by:
hello anyone... pls explain me , how different between function and procedure for C/C++ and Pascal. Thankx......
28
by: Skybuck Flying | last post by:
Hi, I think I understand now a bit better what the difference is between a c compiler and a pascal compiler. For example: When compiling source code with a pascal compiler. The pascal...
90
by: Jhon smith | last post by:
Hi all,Just wondering are there any problems with learning c from older books,as I have picked up some from 1988,1994,1997,1998. By using books of this age(Im on a tight budget)am I going to...
14
by: Peter Williams | last post by:
Hi All, I am attempting to reverse-obsfuscate an IOCCC entry, and my ultimate goal is to convert the code into Delphi (OO Pascal). I don't understand what the question-mark (?) operator, and...
17
by: David Scemama | last post by:
Hi, I'm writing a program using VB.NET that needs to communicate with a DOS Pascal program than cannot be modified. The communication channel is through some file databases, and I have a huge...
3
by: riku | last post by:
Does any1 know a site where i can download Turbo Pascal for free and is not a trial version. If you do, can u please tell me?
15
by: jacob navia | last post by:
Programming languages come and go. Still is amazing that in this survey from http://www.devsource.com/article2/0,1895,2016936,00.asp the C language comes second, right after Java. Java # What...
0
by: dhruba.bandopadhyay | last post by:
Am using Borland C++ 4.5 for the old dos.h APIs. It appears that newer versions of compilers stop support for the oldskool DOS routines. Am trying to convert/port an oldskool Pascal program that...
7
by: SMALLp | last post by:
Hy! I desperately need help! I need to make application that would accept Pascal code and check if it returns good results. My idea is (from a beginner point of view) to make application in...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.