From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,703c4f68db81387d X-Google-Thread: 109fba,703c4f68db81387d X-Google-Thread: 115aec,703c4f68db81387d X-Google-Thread: f43e6,703c4f68db81387d X-Google-Attributes: gid103376,gid109fba,gid115aec,gidf43e6,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news4.google.com!news.glorb.com!blackbush.cw.net!cw.net!newsfeed01.sul.t-online.de!newsfeed00.sul.t-online.de!t-online.de!tsicnews.teliasonera.com!news.otenet.gr!news.grnet.gr!newsfd02.forthnet.gr!not-for-mail From: Ioannis Vranos Newsgroups: comp.lang.ada,comp.lang.c++,comp.realtime,comp.software-eng Subject: Re: Teaching new tricks to an old dog (C++ -->Ada) Date: Tue, 22 Mar 2005 22:03:40 +0200 Organization: FORTHnet S.A., Atthidon 4, GR-17671 Kalithea, Greece, Tel: +30 2109559000, Fax: +30 2109559333, url: http://www.forthnet.gr Message-ID: <1111521825.653841@athnrd02> References: <4229bad9$0$1019$afc38c87@news.optusnet.com.au> <1110032222.447846.167060@g14g2000cwa.googlegroups.com> <871xau9nlh.fsf@insalien.org> <3SjWd.103128$Vf.3969241@news000.worldonline.dk> <87r7iu85lf.fsf@insalien.org> <1110052142.832650@athnrd02> <1110284070.410136.205090@o13g2000cwo.googlegroups.com> <395uqaF5rhu2mU1@individual.net> <1110329098.642196@athnrd02> <1110361741.551255@athnrd02> <422edaec$0$26554$9b4e6d93@newsread4.arcor-online.net> <1111464133.508323@athnrd02> <423fe9df$0$11476$9b4e6d93@newsread2.arcor-online.net> NNTP-Posting-Host: athnrd02.forthnet.gr Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: athprx02.forthnet.gr 1111521825 25705 193.92.150.73 (22 Mar 2005 20:03:45 GMT) X-Complaints-To: abuse@forthnet.gr NNTP-Posting-Date: Tue, 22 Mar 2005 20:03:45 +0000 (UTC) User-Agent: Mozilla Thunderbird 1.0 (Windows/20041206) X-Accept-Language: en-us, en In-Reply-To: <423fe9df$0$11476$9b4e6d93@newsread2.arcor-online.net> Cache-Post-Path: newsfd02!unknown@ppp14-adsl-66.ath.forthnet.gr Xref: g2news1.google.com comp.lang.ada:9752 comp.lang.c++:46722 comp.realtime:1586 comp.software-eng:5188 Date: 2005-03-22T22:03:40+02:00 List-Id: Georg Bauhaus wrote: > Hm. Searching a *map* of entries at numeric keys is different > from scanning an array of values and counting occurences. What > are you trying to do here? Just using the appropriate available container. :-) > The std::vector is missing an instantiation argument which adds > the guarantee that no index value is outside the range > -800_000..12_000_000; vector provides method at() that performs range checking. Also if you want a vector that has signed integer subscripts or even floating point, you can easily write one. However this does not "feel" C++ whose built in arrays always store elements from index 0 and upwards. > std::map is a different beast entirely, with > unknown size. Consider Vector ( -47..600); > > (How do you make a subrange of double, which is missing from > your example.) Do you mean like this? #include #include int main() { using namespace std; map id; id[-400.1]= 7.1; id[-2500.6]= -1; id[10.43]= 9; id[-300.65]= 7.1; unsigned counter= 0; for(map::const_iterator p= id.begin(); p!=id.end(); ++p) { if(p->second== 7.1) { cout<<"7.1 was found at index "<first<<"\n"; ++counter; } } cout<<"\n7.1 was found "<temp 7.1 was found at index -400.1 7.1 was found at index -300.65 7.1 was found 2 times in total. C:\c> You can also check the entire range if you want. Here the previous style along with your full-range (expensive) style: #include #include int main() { using namespace std; map id; id[-400]= 7.1; id[-2500]= -1; id[10]= 9; id[-300]= 7.1; unsigned counter= 0; for(map::const_iterator p= id.begin(); p!=id.end(); ++p) { if(p->second== 7.1) { cout<<"7.1 was found at index "<first<<"\n"; ++counter; } } cout<<"\n7.1 was found "<first; i<(--id.end())->first; ++i) if you want this kind of abstraction. But better abstraction is the iterator approach and the *best* the count family which (iterator and count family) work with *all* containers. These are bullet-proof code approaches. > Imagine an array shared between a number of threads. The program's > task is to count the number of occurences of a particular value > in the array. Examples: > 1) A shop has 10 unique doors (use an enum). We can use whatever I like, perhaps strings. What is wrong with "Door 1" etc? :-) > For each door 4 states > can be measured: open/closed, van/no van at the door. OK, this sounds easy. What do you think? Since you want an array: #include #include #include #include class Door { bool open, van; public: Door(){ open= van= true; } bool IsOpen() const { return open; } bool IsClosed() const { return !open; } bool IsVan() const { return van; } bool IsNoVan() const { return !van; } Door &SetOpen() { open= true; return *this; } Door &SetClosed() { open= false; return *this; } Door &SetVan() { van= true; return *this; } Door &SetNoVan() { van= false; return *this; } }; class CheckOpen { bool open, van; public: CheckOpen(bool isopen, bool isvan):open(isopen), van(isvan) {} bool operator() (const Door &arg) { return open== arg.IsOpen() && van== arg.IsVan(); } }; int main() { using namespace std; vector doors(10); doors[4].SetOpen().SetNoVan(); unsigned counter= count_if(doors.begin(), doors.end(), CheckOpen(true, false)); cout<<"Counted "<temp Counted 1 occurrences. C:\c> > 2) A 5-player team, each team is identified by number drawn from a fixed > set of team numbers. An array (an array, not some other data > structure) measures the number of players from each team present in > a room. Count the number of odd-team players in a room. This is easy too. I do not get your point. This can be done with arrays as also with other containers. Why should we be restricted to one type of container? > I hope these example illustrate some points. They are not meant to > trigger a discussion as to whether an array is the best data > structure for everything. (Note that it might be necessary to read > values from the array/Vector using random access in O(1), and to > store and replace values in O(1), another reason to use an array.) OK. >> In C++ you can create whatever you want. Even a container with >> abstract conceptual features (you are limited only by your imagination). > > > You can do that using assembly language or SmallTalk, whatever. > I think this was not the point, but I should have Richard Riehle's > message speak for itself. I am not talking about assembly. It is not difficult to write a container getting a signed integer as a subscript and have range checking. > For example, look here, and reconsider the programming task as > originally stated: > >>> Yes, I know you can do this in C++, but >>> from my >>> perspective, it is not nearly as easy, expressive, or readable. I think it is probably more and at least the same. That said, I like Ada somewhat. :-) -- Ioannis Vranos http://www23.brinkster.com/noicys