Thursday, May 14, 2015

Digest for comp.lang.c++@googlegroups.com - 11 updates in 2 topics

Nikki Locke <nikki@trumphurst.com>: May 14 10:21PM

Available C++ Libraries FAQ
 
URL: http://www.trumphurst.com/cpplibs/
 
This is a searchable list of libraries and utilities (both free
and commercial) available to C++ programmers.
 
If you know of a library which is not in the list, why not fill
in the form at http://www.trumphurst.com/cpplibs/cppsub.php
 
Maintainer: Nikki Locke - if you wish to contact me, please use the form on the website.
Paavo Helde <myfirstname@osa.pri.ee>: May 14 12:33AM -0500

"Jason C. McDonald" <"i n d e l i b l e b l u e p e n "@ g m a i l . c
 
> bool isBalanced(string str)
> {
> int b;
 
Still not initialized...
 
"Jason C. McDonald" <"i n d e l i b l e b l u e p e n "@ g m a i l . c o m . i n v a l i d>: May 13 11:36PM -0700

On 05/13/2015 10:33 PM, Paavo Helde wrote:
>> {
>> int b;
 
> Still not initialized...
 
Ah, I can't believe I missed that. Should be `int b = 0;`. Thanks for
the catch.
 
 
--
Jason C. McDonald (CodeMouse92)
[CEO, Lead Dev @ MousePaw Games]
Juha Nieminen <nospam@thanks.invalid>: May 14 11:38AM

> }
 
> return depth == 0;
> }
 
If you allow your inner C hacker go wild, you can make it much shorter:
 
template <typename Iter>
bool balanced_parentheses(Iter begin, Iter end)
{
int depth = 0;
for(; begin != end; ++begin)
if((depth += (*begin == '(') - (*begin == ')')) < 0)
return false;
return depth == 0;
}
 
--- news://freenews.netfront.net/ - complaints: news@netfront.net ---
Victor Bazarov <v.bazarov@comcast.invalid>: May 14 08:53AM -0400

On 5/14/2015 7:38 AM, Juha Nieminen wrote:
> return false;
> return depth == 0;
> }
 
I was thinking more in the line of making the "open" and "close" symbols
arguments of the function (or even the template) instead:
 
template<char open_c, char close_c, typename Iter>
bool balanced_(Iter begin, Iter end) ...
 
and make balanced_parentheses, balanced_quotes typedef'ed
 
template<class Iter> using balanced_parentheses
= balanced_<'(',')', Iter>;
 
:-)
 
V
--
I do not respond to top-posted replies, please don't ask
legalize+jeeves@mail.xmission.com (Richard): May 14 04:48PM

[Please do not mail me a copy of your followup]
 
"Jason C. McDonald" <"i n d e l i b l e b l u e p e n "@ g m a i l . c o m . i n v a l i d> spake the secret code
 
>(Incidentally, I'm neck-deep in writing a lexer for an interpreted
>language on top of C++.)
 
Do checkout Boost.Spirit, it's really nice for writing lexers and
parsers.
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>
legalize+jeeves@mail.xmission.com (Richard): May 14 04:50PM

[Please do not mail me a copy of your followup]
 
Juha Nieminen <nospam@thanks.invalid> spake the secret code
 
>If you allow your inner C hacker go wild, you can make it much shorter:
 
I fired that guy around 1993 (Mr. "C hacker"). The goal of writing
code that is easy to understand isn't to minimize the number of tokens
present in the code. Otherwise everything would evolve into entries in
the obfuscated C contest. OH WAIT. That's why we fired Mr. C Hacker
in the first place.
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>
James Moe <jimoeDESPAM@sohnen-moe.com>: May 14 11:15AM -0700

On 05/11/2015 11:30 AM, Paul wrote:
> bool isBracket(char c) {
> return c=='(' || c == ')' ;
> }
 
Since the test is indiscriminate about opening and closing parentheses,
your function will approve:
 
" )oops(."
 
 
--
James Moe
jmm-list at sohnen-moe dot com
asetofsymbols@gmail.com: May 14 11:40AM -0700

Juha wrote:"
template <typename Iter>
bool balanced_parentheses(Iter begin, Iter end)
{
int depth = 0;
for(; begin != end; ++begin)
if((depth += (*begin == '(') - (*begin == ')')) < 0)
return false;
return depth == 0;
}"
 
i32 pairedPar(u8* b, u8* fin )
{u8 *p;
i32 s;
if(b==0||fin==0||b>fin)
return -1;
for(p=b, s=0; p<fin; ++p)
{ if(*p=='(') ++s;
else if(*p==')') --s;
if(s<0) return 0;
if(s>0xFFFFFF)
return -1;
}
if(s>0) return 0;
return 1;
}
"Öö Tiib" <ootiib@hot.ee>: May 14 11:42AM -0700

On Thursday, 14 May 2015 19:50:13 UTC+3, Richard wrote:
> present in the code. Otherwise everything would evolve into entries in
> the obfuscated C contest. OH WAIT. That's why we fired Mr. C Hacker
> in the first place.
 
The needless complexity in Juha's code is that assignment inside
of if that looks shorter but possibly actually even adds useless
branches in generated binary.

Both of codes (yours and Juha's) are mildly hackish in sense that
both reuse the "begin" iterator of range to iterate over range.
Modern compilers do such optimizations.
 
Predicate functions are easier to read when the name is the fact
that it returns (for example "parentheses_are_balanced").
It is like method "empty" of std containers has confusing name
because it is possible to interpret it as verb. Lot better
name: "is_empty".
"Jason C. McDonald" <"i n d e l i b l e b l u e p e n "@ g m a i l . c o m . i n v a l i d>: May 14 12:33PM -0700

On 05/14/2015 09:48 AM, Richard wrote:
>> language on top of C++.)
 
> Do checkout Boost.Spirit, it's really nice for writing lexers and
> parsers.
 
Hi Richard,
 
Thanks for the tip. I'll look into it, though I'll let you know that I'm
trying to avoid external libraries where possible. We have an unusually
ambitious backward comparability goal, so I generally avoid external
libraries for that reason.
 
--
Jason C. McDonald (CodeMouse92)
[CEO, Lead Dev @ MousePaw Games]
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com.

No comments: