Pascal式的集合类 [英] Pascal-like set class

查看:49
本文介绍了Pascal式的集合类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Pascal-like的最新版本set class可在此处获得:

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

来自旧YAHOO BOOST论坛的人可能会在几个月内记住它

回来了。我已经增强了代码,并且deboostifized它对于那些不熟悉它的人来说,它是一个模板类,它实现了类似于Pascal语言中的set类型的set

类。比如

Pascal,任何整数(序数)类型都可以与集合一起使用,但我主要是

将它写成用于枚举。设置元素存储在

级别,所以它是紧凑的。甚至还有一个成员模板用于将

设置对象定义为编译时常量。我写的是因为没有一个C ++

容器(即set和bitset)符合我的需要。


旧版本的一个重大改变是增加了一个特征模板

参数用于获得对类的行为的更好控制,例如位排序和元素范围(默认为[0,

元素数量 - 1]。


文档示例:


声明:

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;


编译时常量:

const color_set additive_primaries =

color_set :: setof< RED,GREEN,BLUE> :: value;


const color_set subtractive_primaries =

color_set :: setof< RED,YELLOW,BLUE> :: value;


我对代码,界面的评论非常感兴趣(我可能已经在操作员身上花了很多钱!)或者我对模板的使用(我仍然感到朦胧)

何时使用typename和template)。


我的目标是使接口尽可能像Pascal集类型一样

可行,同时使代码符合标准(无论是否b / b特定编译器符合)。


代码当前编译器with:

GCC 3.4.0(MinGW)

Visual C ++ Toolkit 2003

Rich Herrick
里************ @ richherrick.com.delet e.me.too

解决方案

" Rich Herrick" < RI ************ @ richherrick.com.delete.me.too>写在

消息新闻:hX ******************* @ twister.nyroc.rr.com ...

< snip>

我对代码,界面的评论非常感兴趣(我可能
已经超出了运营商!),或者我对模板的使用(
何时使用typename和template,我仍然感到朦胧。



< snip>


我认为你确实对运营商过火了。对于

这类的大多数运营商而言,这种运营商并不坚持其传统意义。有些是近距离通话,

但运营商bool的定义,!,<,< =,>,> =,<<,>>,/和*看看

对我很奇怪。


-

David Hilsee


< blockquote>

" David Hilsee" <哒************* @ yahoo.com>在留言中写道

news:8 - ******************** @ comcast.com ...

Rich Herrick < RI ************ @ richherrick.com.delete.me.too>在
消息新闻中写道:hX ******************* @ twister.nyroc.rr.com ...
< snip>

我对代码,界面的评论非常感兴趣(我可能有

对操作员过分了!),或者我对模板的使用(我仍然朦胧关于何时使用typename和template的


< snip>

我认为你确实对运营商过火了。这类课程的大部分操作者都不遵守传统意义。有些是近距离



调用,但运算符bool的定义,!,<,< =,>,> =,<<,>>, /,*
对我来说很奇怪。

-
David Hilsee




谢谢回复。所有关系都匹配Pascal如何定义它们,

并且需要进行子集/超集比较。 /和*运算符

也是Pascal如何定义它们分别用于对称差异和

交集。真的没有操作员bool,它真的是

操作符void *,这将允许在没有令人讨厌的一面的if

语句中测试非空集 - 你会得到操作员bool的影响。

虽然我会承认操作员void *和操作员!并不是真的需要,

因为有一个成员会测试一个空集(你也可以测试
测试等于NULL_SET)。我知道有人会质疑的是>>

和<<,因为我几乎把它们拿了几次。但我喜欢他们看的方式

用于向集合添加元素。有一个+运算符同样的东西,但我认为S + e1 + e2看起来很混乱,特别是如果e1

和e2是整数:S + 1 + 2.我认为S<< 1<< 2在代码中看起来更可读

。此外,前者创建了一个新的集合而后者没有创建一个新的集合,并且

S + = 1 + 2当然不会起作用。虽然还有许多其他的方法可以将元素添加到集合中,例如:S * = set_type(e1,e2),这将是
union S与新集合,结果是在S.


谢谢,


Rich Herrick


< blockquote>" Rich Herrick" < RI ************ @ richherrick.com.delete.me.too>写在

消息新闻:zB ******************* @ twister.nyroc.rr.com ...

< snip>

感谢您的回复。所有关系都匹配Pascal如何定义
它们,并且需要进行子集/超集比较。 /和*运算符也是Pascal如何定义它们分别用于对称差异和交叉。真的没有运算符bool,它的
真的是操作符void *,这将允许在if
语句中测试非空集,而不会出现操作符bool所带来的令人讨厌的副作用。
虽然我会承认操作员void *和操作员!并不是真的需要
,因为有一个成员会测试一个空集(你也可以测试等于NULL_SET)。我知道有人会质疑的是

和<<,因为我几乎把它们拿了几次。但我只是喜欢他们看的方式。用于向集合添加元素。有一个+运算符可以做同样的事情,但我认为S + e1 + e2看起来很混乱,特别是如果



e1和e2是整数:S + 1 + 2.我认为S<< 1<< 2在代码中看起来更具可读性。此外,前者创建一个新的集合而后者不会,
和S + = 1 + 2当然不会起作用。虽然还有许多其他方法可以将元素添加到集合中,例如:S * = set_type(e1,e2),它将使用新集合来结合S,其结果在S中。




我会买其他的运营商,但我认为<<,>>,!,nad operator void *

至少是值得怀疑的。此外,==和!= for set

成员资格似乎很奇怪,因为我发出代码如果元素e等于

set s当它真的意味着如果set s包含元素e。


还有一些问题:


为什么traits类提供了数组元素类型?为什么有人会想要自定义元素?


为什么甚至打算使用数组呢?为什么std :: bitset不能提供你需要的
?我用了几分钟的时间将头围绕在特征上。代码,

然后我意识到它基本上是在实现std :: bitset。无论如何,我认为

就是它正在做的事情。


-

David Hilsee

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


解决方案

"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



"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


"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


这篇关于Pascal式的集合类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆