Boost logo

Boost :

Subject: Re: [boost] RFC: Sequence Properties and Boost Algorithm/Functional
From: Marshall Clow (mclow.lists_at_[hidden])
Date: 2010-01-22 18:29:00


On Jan 22, 2010, at 3:24 PM, Marshall Clow wrote:

>
> On Jan 22, 2010, at 2:02 PM, Grant Erickson wrote:
>
>> For a recent project, I needed to inquire about a sequence of objects and
>> make a determination as to whether the sequences were:
>>
>> * Increasing
>> * Strictly Increasing
>> * Decreasing
>> * Strictly Decreasing
>>
>> Looking through STL, Boost documentation, Boost sources and headers and the
>> Boost mailing lists I was able to find neither an existing algorithm or
>> stateful unary predicate functor for accomplishing this for a pair of input
>> iterators. However, I was a bit surprised considering that this seems like a
>> sequence property query that would tend to come up fairly often and serve a
>> general utility. Did I just fail to form the proper search/query or am I
>> overestimating the general utility?
>
> I've got this floating around from a while back (as well as the other three - increasing, decreasing, and strictly_decreasing ).
> Maybe these should go in the algorithms library:
>
> template<typename T>
> bool is_strictly_increasing ( T* begin, T* end ) {
> // Empty sequences are strictly increasing
> if ( begin == end )
> return true;
>
> T* iter = begin;
> T val = *iter++;
>
> while ( iter != end ) {
> if ( ! ( *iter > val ))
> return false;
> val = *iter++;
> }
> return true;
> }

Funny that.

Three minutes after posting my code, I noticed that I worked all the comparisons to use ">" (and the code *does* work),
rather than using "<" (or std::less), which is what I meant to do when I wrote this code.

Nothing like showing your work to a large audience to find problems.

-- Marshall


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk