diff --git a/doc/Jamfile.v2 b/doc/Jamfile.v2 new file mode 100644 index 0000000..8b8d189 --- /dev/null +++ b/doc/Jamfile.v2 @@ -0,0 +1,46 @@ +# Boost.Algorithm +# +# Copyright (c) 2010-2012 Marshall Clow +# +# Distributed under the Boost Software License, Version 1.0. +# (See accompanying file LICENSE_1_0.txt or copy at +# http://www.boost.org/LICENSE_1_0.txt) + + +# Quickbook +# ----------------------------------------------------------------------------- + +import os ; + +using quickbook ; +using doxygen ; +using boostbook ; + +local BOOST_ROOT = [ os.environ BOOST_ROOT ] ; + +doxygen autodoc + : + [ glob ../../../boost/algorithm/*.hpp ../../../boost/algorithm/searching/*.hpp ] + : + "PREDEFINED=\"BOOST_ALGORITHM_DOXYGEN=1\"" + WARNINGS=YES # Default NO, but useful to see warnings, especially in a logfile. + ; + + +xml algorithm : algorithm.qbk ; + +# path-constant boost-images : $(BOOST_ROOT)/doc/src/images ; + +boostbook standalone + : + algorithm + : + autodoc + boost.root=$(BOOST_ROOT) + "boost.doxygen.reftitle=Boost.Algorithms C++ Reference" + chapter.autolabel=0 + chunk.section.depth=8 + toc.section.depth=2 + toc.max.depth=2 + generate.section.toc.level=1 + ; diff --git a/doc/algorithm.qbk b/doc/algorithm.qbk new file mode 100644 index 0000000..f5e4d0d --- /dev/null +++ b/doc/algorithm.qbk @@ -0,0 +1,68 @@ +[library The Boost Algorithm Library + [quickbook 1.5] + [id algorithm] + [dirname algorithm] + [purpose Library of useful algorithms] + [category algorithms] + [authors [Clow, Marshall]] + [copyright 2010-2012 Marshall Clow] + [source-mode c++] + [license + Distributed under the Boost Software License, Version 1.0. + (See accompanying file LICENSE_1_0.txt or copy at + [@http://www.boost.org/LICENSE_1_0.txt]) + ] +] + +[section Description and Rationale] + +Boost.Algorithm is a collection of general purpose algorithms. While Boost contains many libraries of data structures, there is no single library for general purpose algorithms. Even though the algorithms are generally useful, many tend to be thought of as "too small" for Boost. + +An implementation of Boyer-Moore searching, for example, might take a developer a week or so to implement, including test cases and documentation. However, scheduling a review to include that code into Boost might take several months, and run into resistance because "it is too small". Nevertheless, a library of tested, reviewed, documented algorithms can make the developer's life much easier, and that is the purpose of this library. + +[heading Future plans] + +I will be soliciting submissions from other developers, as well as looking through the literature for existing algorithms to include. The Adobe Source Library, for example, contains many useful algorithms that already have documentation and test cases. Knuth's _The Art of Computer Programming_ is chock-full of algorithm descriptions, too. + +My goal is to run regular algorithm reviews, similar to the Boost library review process, but with smaller chunks of code. + +[heading Dependencies] + +Boost.Algorithm uses Boost.Range, Boost.Assert, Boost.Array, Boost.TypeTraits, and Boost.StaticAssert. + + +[heading Acknowledgements] + +Thanks to all the people who have reviewed this library and made suggestions for improvements. Steven Watanabe and Sean Parent, in particular, have provided a great deal of help. + +[endsect] + +[/ include toc.qbk] + + +[section:Searching Searching Algorithms] +[include boyer_moore.qbk] +[include boyer_moore_horspool.qbk] +[include knuth_morris_pratt.qbk] +[endsect] + +[section:CXX11 C++11 Algorithms] +[include all_of.qbk] +[include any_of.qbk] +[include none_of.qbk] +[include one_of.qbk] +[include ordered-hpp.qbk] +[include is_partitioned.qbk] +[include partition_point.qbk] +[endsect] + +[section:Misc Other Algorithms] +[include clamp-hpp.qbk] +[include hex.qbk] +[endsect] + + + +[xinclude autodoc.xml] + + diff --git a/doc/all_of.qbk b/doc/all_of.qbk new file mode 100644 index 0000000..91b7b36 --- /dev/null +++ b/doc/all_of.qbk @@ -0,0 +1,89 @@ +[/ File all_of.qbk] + +[section:all_of all_of] + +[/license +Copyright (c) 2010-2012 Marshall Clow + +Distributed under the Boost Software License, Version 1.0. +(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +] + +The header file 'boost/algorithm/cxx11/all_of.hpp' contains four variants of a single algorithm, `all_of`. The algorithm tests all the elements of a sequence and returns true if they all share a property. + +The routine `all_of` takes a sequence and a predicate. It will return true if the predicate returns true when applied to every element in the sequence. + +The routine `all_of_equal` takes a sequence and a value. It will return true if every element in the sequence compares equal to the passed in value. + +Both routines come in two forms; the first one takes two iterators to define the range. The second form takes a single range parameter, and uses Boost.Range to traverse it. + + +[heading interface] + +The function `all_of` returns true if the predicate returns true for every item in the sequence. There are two versions; one takes two iterators, and the other takes a range. + +`` +namespace boost { namespace algorithm { +template + bool all_of ( InputIterator first, InputIterator last, Predicate p ); +template + bool all_of ( const Range &r, Predicate p ); +}} +`` + +The function `all_of_equal` is similar to `all_of`, but instead of taking a predicate to test the elements of the sequence, it takes a value to compare against. + +`` +namespace boost { namespace algorithm { +template + bool all_of_equal ( InputIterator first, InputIterator last, V const &val ); +template + bool all_of_equal ( const Range &r, V const &val ); +}} +`` + +[heading Examples] + +Given the container `c` containing `{ 0, 1, 2, 3, 14, 15 }`, then +`` +bool isOdd ( int i ) { return i % 2 == 1; } +bool lessThan10 ( int i ) { return i < 10; } + +using boost::algorithm; +all_of ( c, isOdd ) --> false +all_of ( c.begin (), c.end (), lessThan10 ) --> false +all_of ( c.begin (), c.begin () + 3, lessThan10 ) --> true +all_of ( c.end (), c.end (), isOdd ) --> true // empty range +all_of_equal ( c, 3 ) --> false +all_of_equal ( c.begin () + 3, c.begin () + 4, 3 ) --> true +all_of_equal ( c.begin (), c.begin (), 99 ) --> true // empty range +`` + +[heading Iterator Requirements] + +`all_of` and `all_of_equal` work on all iterators except output iterators. + +[heading Complexity] + +All of the variants of `all_of` and `all_of_equal` run in ['O(N)] (linear) time; that is, they compare against each element in the list once. If any of the comparisons fail, the algorithm will terminate immediately, without examining the remaining members of the sequence. + +[heading Exception Safety] + +All of the variants of `all_of` and `all_of_equal` take their parameters by value or const reference, and do not depend upon any global state. Therefore, all the routines in this file provide the strong exception guarantee. + +[heading Notes] + +* The routine `all_of` is part of the C++11 standard. When compiled using a C++11 implementation, the implementation from the standard library will be used. + +* `all_of` and `all_of_equal` both return true for empty ranges, no matter what is passed to test against. When there are no items in the sequence to test, they all satisfy the condition to be tested against. + +* The second parameter to `all_of_value` is a template parameter, rather than deduced from the first parameter (`std::iterator_traits::value_type`) because that allows more flexibility for callers, and takes advantage of built-in comparisons for the type that is pointed to by the iterator. The function is defined to return true if, for all elements in the sequence, the expression `*iter == val` evaluates to true (where `iter` is an iterator to each element in the sequence) + +[endsect] + +[/ File all_of.qbk +Copyright 2011 Marshall Clow +Distributed under the Boost Software License, Version 1.0. +(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt). +] + diff --git a/doc/any_of.qbk b/doc/any_of.qbk new file mode 100644 index 0000000..61a6603 --- /dev/null +++ b/doc/any_of.qbk @@ -0,0 +1,89 @@ +[/ File any_of.qbk] + +[section:any_of any_of] + +[/license +Copyright (c) 2010-2012 Marshall Clow + +Distributed under the Boost Software License, Version 1.0. +(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +] + +The header file 'boost/algorithm/cxx11/any_of.hpp' contains four variants of a single algorithm, `any_of`. The algorithm tests the elements of a sequence and returns true if any of the elements has a particular property. + +The routine `any_of` takes a sequence and a predicate. It will return true if the predicate returns true for any element in the sequence. + +The routine `any_of_equal` takes a sequence and a value. It will return true if any element in the sequence compares equal to the passed in value. + +Both routines come in two forms; the first one takes two iterators to define the range. The second form takes a single range parameter, and uses Boost.Range to traverse it. + + +[heading interface] + +The function `any_of` returns true if the predicate returns true any item in the sequence. There are two versions; one takes two iterators, and the other takes a range. + +`` +namespace boost { namespace algorithm { +template + bool any_of ( InputIterator first, InputIterator last, Predicate p ); +template + bool any_of ( const Range &r, Predicate p ); +}} +`` + +The function `any_of_equal` is similar to `any_of`, but instead of taking a predicate to test the elements of the sequence, it takes a value to compare against. + +`` +namespace boost { namespace algorithm { +template + bool any_of_equal ( InputIterator first, InputIterator last, V const &val ); +template + bool any_of_equal ( const Range &r, V const &val ); +}} +`` + +[heading Examples] + +Given the container `c` containing `{ 0, 1, 2, 3, 14, 15 }`, then +`` +bool isOdd ( int i ) { return i % 2 == 1; } +bool lessThan10 ( int i ) { return i < 10; } + +using boost::algorithm; +any_of ( c, isOdd ) --> true +any_of ( c.begin (), c.end (), lessThan10 ) --> true +any_of ( c.begin () + 4, c.end (), lessThan10 ) --> false +any_of ( c.end (), c.end (), isOdd ) --> false // empty range +any_of_equal ( c, 3 ) --> true +any_of_equal ( c.begin (), c.begin () + 3, 3 ) --> false +any_of_equal ( c.begin (), c.begin (), 99 ) --> false // empty range +`` + +[heading Iterator Requirements] + +`any_of` and `any_of_equal` work on all iterators except output iterators. + +[heading Complexity] + +All of the variants of `any_of` and `any_of_equal` run in ['O(N)] (linear) time; that is, they compare against each element in the list once. If any of the comparisons succeed, the algorithm will terminate immediately, without examining the remaining members of the sequence. + +[heading Exception Safety] + +All of the variants of `any_of` and `any_of_equal` take their parameters by value or const reference, and do not depend upon any global state. Therefore, all the routines in this file provide the strong exception guarantee. + +[heading Notes] + +* The routine `any_of` is part of the C++11 standard. When compiled using a C++11 implementation, the implementation from the standard library will be used. + +* `any_of` and `any_of_equal` both return false for empty ranges, no matter what is passed to test against. + +* The second parameter to `any_of_value` is a template parameter, rather than deduced from the first parameter (`std::iterator_traits::value_type`) because that allows more flexibility for callers, and takes advantage of built-in comparisons for the type that is pointed to by the iterator. The function is defined to return true if, for any element in the sequence, the expression `*iter == val` evaluates to true (where `iter` is an iterator to each element in the sequence) + +[endsect] + +[/ File any_of.qbk +Copyright 2011 Marshall Clow +Distributed under the Boost Software License, Version 1.0. +(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt). +] + diff --git a/doc/boyer_moore.qbk b/doc/boyer_moore.qbk new file mode 100644 index 0000000..16be44f --- /dev/null +++ b/doc/boyer_moore.qbk @@ -0,0 +1,93 @@ +[/ QuickBook Document version 1.5 ] + +[section:BoyerMoore Boyer-Moore Search] + +[/license + +Copyright (c) 2010-2012 Marshall Clow + +Distributed under the Boost Software License, Version 1.0. +(See accompanying file LICENSE_1_0.txt or copy at +http://www.boost.org/LICENSE_1_0.txt) +] + + +[heading Overview] + +The header file 'boyer_moore.hpp' contains an an implementation of the Boyer-Moore algorithm for searching sequences of values. + +The Boyer–Moore string search algorithm is a particularly efficient string searching algorithm, and it has been the standard benchmark for the practical string search literature. The Boyer-Moore algorithm was invented by Bob Boyer and J. Strother Moore, and published in the October 1977 issue of the Communications of the ACM , and a copy of that article is available at [@http://www.cs.utexas.edu/~moore/publications/fstrpos.pdf]. + +The Boyer-Moore algorithm uses two precomputed tables to give better performance than a naive search. These tables depend on the pattern being searched for, and give the Boyer-Moore algorithm larger a memory footprint and startup costs than a simpler algorithm, but these costs are recovered quickly during the searching process, especially if the pattern is longer than a few elements. + +However, the Boyer-Moore algorithm cannot be used with comparison predicates like `std::search`. + +Nomenclature: I refer to the sequence being searched for as the "pattern", and the sequence being searched in as the "corpus". + +For flexibility, the Boyer-Moore algorithm has has two interfaces; an object-based interface and a procedural one. The object-based interface builds the tables in the constructor, and uses operator () to perform the search. The procedural interface builds the table and does the search all in one step. If you are going to be searching for the same pattern in multiple corpora, then you should use the object interface, and only build the tables once. + +Here is the object interface: +`` +template +class boyer_moore { +public: + boyer_moore ( patIter first, patIter last ); + ~boyer_moore (); + + template + corpusIter operator () ( corpusIter corpus_first, corpusIter corpus_last ); + }; +`` + +and here is the corresponding procedural interface: + +`` +template +corpusIter boyer_moore_search ( + corpusIter corpus_first, corpusIter corpus_last, + patIter pat_first, patIter pat_last ); +`` + +Each of the functions is passed two pairs of iterators. The first two define the corpus and the second two define the pattern. Note that the two pairs need not be of the same type, but they do need to "point" at the same type. In other words, `patIter::value_type` and `curpusIter::value_type` need to be the same type. + +The return value of the function is an iterator pointing to the start of the pattern in the corpus. If the pattern is not found, it returns the end of the corpus (`corpus_last`). + +[heading Performance] + +The execution time of the Boyer-Moore algorithm, while still linear in the size of the string being searched, can have a significantly lower constant factor than many other search algorithms: it doesn't need to check every character of the string to be searched, but rather skips over some of them. Generally the algorithm gets faster as the pattern being searched for becomes longer. Its efficiency derives from the fact that with each unsuccessful attempt to find a match between the search string and the text it is searching, it uses the information gained from that attempt to rule out as many positions of the text as possible where the string cannot match. + +[heading Memory Use] + +The algorithm allocates two internal tables. The first one is proportional to the length of the pattern; the second one has one entry for each member of the "alphabet" in the pattern. For (8-bit) character types, this table contains 256 entries. + +[heading Complexity] + +The worst-case performance to find a pattern in the corpus is ['O(N)] (linear) time; that is, proportional to the length of the corpus being searched. In general, the search is sub-linear; not every entry in the corpus need be checked. + +[heading Exception Safety] + +Both the object-oriented and procedural versions of the Boyer-Moore algorithm take their parameters by value and do not use any information other than what is passed in. Therefore, both interfaces provide the strong exception guarantee. + +[heading Notes] + +* When using the object-based interface, the pattern must remain unchanged for during the searches; i.e, from the time the object is constructed until the final call to operator () returns. + +* The Boyer-Moore algorithm requires random-access iterators for both the pattern and the corpus. + +[heading Customization points] + +The Boyer-Moore object takes a traits template parameter which enables the caller to customize how one of the precomputed tables is stored. This table, called the skip table, contains (logically) one entry for every possible value that the pattern can contain. When searching 8-bit character data, this table contains 256 elements. The traits class defines the table to be used. + +The default traits class uses a `boost::array` for small 'alphabets' and a `tr1::unordered_map` for larger ones. The array-based skip table gives excellent performance, but could be prohibitively large when the "alphabet" of elements to be searched grows. The unordered_map based version only grows as the number of unique elements in the pattern, but makes many more heap allocations, and gives slower lookup performance. + +To use a different skip table, you should define your own skip table object and your own traits class, and use them to instantiate the Boyer-Moore object. The interface to these objects is described TBD. + + +[endsect] + +[/ File boyer_moore.qbk +Copyright 2011 Marshall Clow +Distributed under the Boost Software License, Version 1.0. +(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt). +] + diff --git a/doc/boyer_moore_horspool.qbk b/doc/boyer_moore_horspool.qbk new file mode 100644 index 0000000..765cba9 --- /dev/null +++ b/doc/boyer_moore_horspool.qbk @@ -0,0 +1,27 @@ +[/ QuickBook Document version 1.5 ] + +[section:BoyerMooreHorspool Boyer-Moore-Horspool Search] + +[/license + +Copyright (c) 2010-2012 Marshall Clow + +Distributed under the Boost Software License, Version 1.0. +(See accompanying file LICENSE_1_0.txt or copy at +http://www.boost.org/LICENSE_1_0.txt) +] + + +[heading Overview] + +The header file 'boyer_moore_horspool.hpp' contains an an implementation of the Boyer-Moore-Horspool algorithm for searching sequences of values. + + +[endsect] + +[/ File boyer_moore_horspool.qbk +Copyright 2011 Marshall Clow +Distributed under the Boost Software License, Version 1.0. +(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt). +] + diff --git a/doc/clamp-hpp.qbk b/doc/clamp-hpp.qbk new file mode 100644 index 0000000..9c80a8e --- /dev/null +++ b/doc/clamp-hpp.qbk @@ -0,0 +1,73 @@ +[/ QuickBook Document version 1.5 ] +[section:clamp clamp] + +[/license + +Copyright (c) 2010-2012 Marshall Clow + +Distributed under the Boost Software License, Version 1.0. +(See accompanying file LICENSE_1_0.txt or copy at +http://www.boost.org/LICENSE_1_0.txt) + +] + + +The header file clamp.hpp contains two functions for "clamping" a value between a pair of boundary values. + +[heading clamp] + +The function `clamp (v, lo, hi)` returns: + +* lo if v < lo +* hi if hi < v +* otherwise, v + +Note: using `clamp` with floating point numbers may give unexpected results if one of the values is `NaN`. + +There is also a version that allows the caller to specify a comparison predicate to use instead of `operator <`. + +`` +template +V clamp ( V val, V lo, V hi ); + +template +V clamp ( V val, V lo, V hi, Pred p ); +`` + +The following code: `` + int foo = 23; + foo = clamp ( foo, 1, 10 ); +`` +will leave `foo` with a value of 10 + +Complexity: + `clamp` will make either one or two calls to the comparison predicate before returning one of the three parameters. + +[heading clamp_range] +There are also four range-based versions of clamp, that apply clamping to a series of values. You could write them yourself with std::transform and bind, like this: `std::transform ( first, last, out, bind ( clamp ( _1, lo, hi )))`, but they are provided here for your convenience. + +`` +template +OutputIterator clamp_range ( InputIterator first, InputIterator last, OutputIterator out, + typename std::iterator_traits::value_type lo, + typename std::iterator_traits::value_type hi ); + +template +OutputIterator clamp_range ( const Range &r, OutputIterator out, + typename std::iterator_traits::type>::value_type lo, + typename std::iterator_traits::type>::value_type hi ); + +template +OutputIterator clamp_range ( InputIterator first, InputIterator last, OutputIterator out, + typename std::iterator_traits::value_type lo, + typename std::iterator_traits::value_type hi, Pred p ); + +template +OutputIterator clamp_range ( const Range &r, OutputIterator out, + typename std::iterator_traits::type>::value_type lo, + typename std::iterator_traits::type>::value_type hi, + Pred p ); +`` + + +[endsect] diff --git a/doc/hex.qbk b/doc/hex.qbk new file mode 100644 index 0000000..8b6e7ae --- /dev/null +++ b/doc/hex.qbk @@ -0,0 +1,109 @@ +[/ File hex.qbk] + +[section:hex hex] + +[/license +Copyright (c) 2011-2012 Marshall Clow + +Distributed under the Boost Software License, Version 1.0. +(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +] + +The header file `'boost/algorithm/hex.hpp'` contains three variants each of two algorithms, `hex` and `unhex`. They are inverse algorithms; that is, one undoes the effort of the other. `hex` takes a sequence of values, and turns them into hexadecimal characters. `unhex` takes a sequence of hexadecimal characters, and outputs a sequence of values. + +`hex` and `unhex` come from MySQL, where they are used in database queries and stored procedures. + +[heading interface] + +The function `hex` takes a sequence of values and writes hexadecimal characters. There are three different interfaces, differing only in how the input sequence is specified. + +The first one takes an iterator pair. The second one takes a pointer to the start of a zero-terminated sequence, such as a c string, and the third takes a range as defined by the Boost.Range library. + +`` +template +OutputIterator hex ( InputIterator first, InputIterator last, OutputIterator out ); + +template +OutputIterator hex ( const T *ptr, OutputIterator out ); + +template +OutputIterator hex ( const Range &r, OutputIterator out ); +`` + +`hex` writes only values in the range '0'..'9' and 'A'..'F', but is not limited to character output. The output iterator could refer to a wstring, or a vector of integers, or any other integral type. + +The function `unhex` takes the output of `hex` and turns it back into a sequence of values. + +The input parameters for the different variations of `unhex` are the same as `hex`. + +`` +template +OutputIterator unhex ( InputIterator first, InputIterator last, OutputIterator out ); + +template +OutputIterator unhex ( const T *ptr, OutputIterator out ); + +template +OutputIterator unhex ( const Range &r, OutputIterator out ); +`` + +[heading Error Handling] +The header 'hex.hpp' defines three exception classes: +`` +struct hex_decode_error: virtual boost::exception, virtual std::exception {}; +struct not_enough_input : public hex_decode_error; +struct non_hex_input : public hex_decode_error; +`` + +If the input to `unhex` does not contain an "even number" of hex digits, then an exception of type `boost::algorithm::not_enough_input` is thrown. + +If the input to `unhex` contains any non-hexadecimal characters, then an exception of type `boost::algorithm::non_hex_input` is thrown. + +If you want to catch all the decoding errors, you can catch exceptions of type `boost::algorithm::hex_decode_error`. + +[heading Examples] + +Assuming that `out` is an iterator that accepts `char` values, and `wout` accepts `wchar_t` values (and that sizeof ( wchar_t ) == 2) + +`` +hex ( "abcde", out ) --> "616263646566" +hex ( "32", out ) --> "3332" +hex ( "abcde", wout ) --> "006100620063006400650066" +hex ( "32", wout ) --> "00330032" + +unhex ( "616263646566", out ) --> "abcde" +unhex ( "3332", out ) --> "32" +unhex ( "616263646566", wout ) --> "\6162\6364\6566" ( i.e, a 3 character string ) +unhex ( "3332", wout ) --> "\3233" ( U+3332, SQUARE HUARADDO ) + +unhex ( "3", out ) --> Error - not enough input +unhex ( "32", wout ) --> Error - not enough input + +unhex ( "ACEG", out ) --> Error - non-hex input + +`` + +[heading Iterator Requirements] + +`hex` and `unhex` work on all iterator types. + +[heading Complexity] + +All of the variants of `hex` and `unhex` run in ['O(N)] (linear) time; that is, that is, they process each element in the input sequence once. + +[heading Exception Safety] + +All of the variants of `hex` and `unhex` take their parameters by value or const reference, and do not depend upon any global state. Therefore, all the routines in this file provide the strong exception guarantee. However, when working on input iterators, if an exception is thrown, the input iterators will not be reset to their original values (i.e, the characters read from the iterator cannot be un-read) + +[heading Notes] + +* `hex` and `unhex` both do nothing when passed empty ranges. + +[endsect] + +[/ File hex.qbk +Copyright 2011 Marshall Clow +Distributed under the Boost Software License, Version 1.0. +(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt). +] + diff --git a/doc/is_partitioned.qbk b/doc/is_partitioned.qbk new file mode 100644 index 0000000..3c7075b --- /dev/null +++ b/doc/is_partitioned.qbk @@ -0,0 +1,69 @@ +[/ File is_partitioned.qbk] + +[section:is_partitioned is_partitioned ] + +[/license +Copyright (c) 2010-2012 Marshall Clow + +Distributed under the Boost Software License, Version 1.0. +(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +] + +The header file 'is_partitioned.hpp' contains two variants of a single algorithm, `is_partitioned`. The algorithm tests to see if a sequence is partitioned according to a predicate; in other words, all the items in the sequence that satisfy the predicate are at the beginning of the sequence. + +The routine `is_partitioned` takes a sequence and a predicate. It returns true if the sequence is partitioned according to the predicate. + +`is_partitioned` come in two forms; the first one takes two iterators to define the range. The second form takes a single range parameter, and uses Boost.Range to traverse it. + + +[heading interface] + +The function `is_partitioned` returns true the items in the sequence are separated according to their ability to satisfy the predicate. There are two versions; one takes two iterators, and the other takes a range. + +`` +template + bool is_partitioned ( InputIterator first, InputIterator last, Predicate p ); +template + bool is_partitioned ( const Range &r, Predicate p ); +`` + +[heading Examples] + +Given the container `c` containing `{ 0, 1, 2, 3, 14, 15 }`, then +`` +bool isOdd ( int i ) { return i % 2 == 1; } +bool lessThan10 ( int i ) { return i < 10; } + +is_partitioned ( c, isOdd ) --> false +is_partitioned ( c, lessThan10 ) --> true +is_partitioned ( c.begin (), c.end (), lessThan10 ) --> true +is_partitioned ( c.begin (), c.begin () + 3, lessThan10 ) --> true +is_partitioned ( c.end (), c.end (), isOdd ) --> true // empty range +`` + +[heading Iterator Requirements] + +`is_partitioned` works on all iterators except output iterators. + +[heading Complexity] + +Both of the variants of `is_partitioned` run in ['O(N)] (linear) time; that is, they compare against each element in the list once. If the sequence is found to be not partitioned at any point, the routine will terminate immediately, without examining the rest of the elements. + +[heading Exception Safety] + +Both of the variants of `is_partitioned` take their parameters by value or const reference, and do not depend upon any global state. Therefore, all the routines in this file provide the strong exception guarantee. + +[heading Notes] + +* The iterator-based version of the routine `is_partitioned` is part of the C++11 standard. When compiled using a C++11 implementation, the implementation from the standard library will be used. + +* `is_partitioned` returns true for empty ranges, no matter what predicate is passed to test against. + +[endsect] + +[/ File is_partitioned.qbk +Copyright 2011 Marshall Clow +Distributed under the Boost Software License, Version 1.0. +(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt). +] + diff --git a/doc/knuth_morris_pratt.qbk b/doc/knuth_morris_pratt.qbk new file mode 100644 index 0000000..dfe717e --- /dev/null +++ b/doc/knuth_morris_pratt.qbk @@ -0,0 +1,27 @@ +[/ QuickBook Document version 1.5 ] + +[section:KnuthMorrisPratt Knuth-Morris-Pratt Search] + +[/license + +Copyright (c) 2010-2012 Marshall Clow + +Distributed under the Boost Software License, Version 1.0. +(See accompanying file LICENSE_1_0.txt or copy at +http://www.boost.org/LICENSE_1_0.txt) +] + + +[heading Overview] + +The header file 'knuth_morris_pratt.hpp' contains an an implementation of the Knuth-Morris-Pratt algorithm for searching sequences of values. + + +[endsect] + +[/ File knuth_morris_pratt.qbk +Copyright 2011 Marshall Clow +Distributed under the Boost Software License, Version 1.0. +(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt). +] + diff --git a/doc/none_of.qbk b/doc/none_of.qbk new file mode 100644 index 0000000..3cda5f9 --- /dev/null +++ b/doc/none_of.qbk @@ -0,0 +1,90 @@ +[/ File none_of.qbk] + +[section:none_of none_of] + +[/license +Copyright (c) 2010-2012 Marshall Clow + +Distributed under the Boost Software License, Version 1.0. +(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +] + +The header file 'boost/algorithm/cxx11/none_of.hpp' contains four variants of a single algorithm, `none_of`. The algorithm tests all the elements of a sequence and returns true if they none of them share a property. + +The routine `none_of` takes a sequence and a predicate. It will return true if the predicate returns false when applied to every element in the sequence. + +The routine `none_of_equal` takes a sequence and a value. It will return true if none of the elements in the sequence compare equal to the passed in value. + +Both routines come in two forms; the first one takes two iterators to define the range. The second form takes a single range parameter, and uses Boost.Range to traverse it. + + +[heading interface] + +The function `none_of` returns true if the predicate returns false for every item in the sequence. There are two versions; one takes two iterators, and the other takes a range. + +`` +namespace boost { namespace algorithm { +template + bool none_of ( InputIterator first, InputIterator last, Predicate p ); +template + bool none_of ( const Range &r, Predicate p ); +}} +`` + +The function `none_of_equal` is similar to `none_of`, but instead of taking a predicate to test the elements of the sequence, it takes a value to compare against. + +`` +namespace boost { namespace algorithm { +template + bool none_of_equal ( InputIterator first, InputIterator last, V const &val ); +template + bool none_of_equal ( const Range &r, V const &val ); +}} +`` + +[heading Examples] + +Given the container `c` containing `{ 0, 1, 2, 3, 14, 15 }`, then +`` +bool isOdd ( int i ) { return i % 2 == 1; } +bool lessThan10 ( int i ) { return i < 10; } + +using boost::algorithm; + +none_of ( c, isOdd ) --> false +none_of ( c.begin (), c.end (), lessThan10 ) --> false +none_of ( c.begin () + 4, c.end (), lessThan10 ) --> true +none_of ( c.end (), c.end (), isOdd ) --> true // empty range +none_of_equal ( c, 3 ) --> false +none_of_equal ( c.begin (), c.begin () + 3, 3 ) --> true +none_of_equal ( c.begin (), c.begin (), 99 ) --> true // empty range +`` + +[heading Iterator Requirements] + +`none_of` and `none_of_equal` work on all iterators except output iterators. + +[heading Complexity] + +All of the variants of `none_of` and `none_of_equal` run in ['O(N)] (linear) time; that is, they compare against each element in the list once. If any of the comparisons succeed, the algorithm will terminate immediately, without examining the remaining members of the sequence. + +[heading Exception Safety] + +All of the variants of `none_of` and `none_of_equal` take their parameters by value or const reference, and do not depend upon any global state. Therefore, all the routines in this file provide the strong exception guarantee. + +[heading Notes] + +* The routine `none_of` is part of the C++11 standard. When compiled using a C++11 implementation, the implementation from the standard library will be used. + +* `none_of` and `none_of_equal` both return true for empty ranges, no matter what is passed to test against. + +* The second parameter to `none_of_value` is a template parameter, rather than deduced from the first parameter (`std::iterator_traits::value_type`) because that allows more flexibility for callers, and takes advantage of built-in comparisons for the type that is pointed to by the iterator. The function is defined to return true if, for all elements in the sequence, the expression `*iter == val` evaluates to false (where `iter` is an iterator to each element in the sequence) + +[endsect] + +[/ File none_of.qbk +Copyright 2011 Marshall Clow +Distributed under the Boost Software License, Version 1.0. +(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt). +] + diff --git a/doc/one_of.qbk b/doc/one_of.qbk new file mode 100644 index 0000000..4170407 --- /dev/null +++ b/doc/one_of.qbk @@ -0,0 +1,87 @@ +[/ File one_of.qbk] + +[section:one_of one_of] + +[/license +Copyright (c) 2010-2012 Marshall Clow + +Distributed under the Boost Software License, Version 1.0. +(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +] + +The header file 'boost/algorithm/cxx11/one_of.hpp' contains four variants of a single algorithm, `one_of`. The algorithm tests the elements of a sequence and returns true if exactly one of the elements in the sequence has a particular property. + +The routine `one_of` takes a sequence and a predicate. It will return true if the predicate returns true for one element in the sequence. + +The routine `one_of_equal` takes a sequence and a value. It will return true if one element in the sequence compares equal to the passed in value. + +Both routines come in two forms; the first one takes two iterators to define the range. The second form takes a single range parameter, and uses Boost.Range to traverse it. + + +[heading interface] + +The function `one_of` returns true if the predicate returns true for one item in the sequence. There are two versions; one takes two iterators, and the other takes a range. + +`` +namespace boost { namespace algorithm { +template + bool one_of ( InputIterator first, InputIterator last, Predicate p ); +template + bool one_of ( const Range &r, Predicate p ); +}} +`` + +The function `one_of_equal` is similar to `one_of`, but instead of taking a predicate to test the elements of the sequence, it takes a value to compare against. + +`` +namespace boost { namespace algorithm { +template + bool one_of_equal ( InputIterator first, InputIterator last, V const &val ); +template + bool one_of_equal ( const Range &r, V const &val ); +}} +`` + +[heading Examples] + +Given the container `c` containing `{ 0, 1, 2, 3, 14, 15 }`, then +`` +bool isOdd ( int i ) { return i % 2 == 1; } +bool lessThan10 ( int i ) { return i < 10; } + +using boost::algorithm; +one_of ( c, isOdd ) --> false +one_of ( c.begin (), c.end (), lessThan10 ) --> false +one_of ( c.begin () + 3, c.end (), lessThan10 ) --> true +one_of ( c.end (), c.end (), isOdd ) --> false // empty range +one_of_equal ( c, 3 ) --> true +one_of_equal ( c.begin (), c.begin () + 3, 3 ) --> false +one_of_equal ( c.begin (), c.begin (), 99 ) --> false // empty range +`` + +[heading Iterator Requirements] + +`one_of` and `one_of_equal` work on all iterators except output iterators. + +[heading Complexity] + +All of the variants of `one_of` and `one_of_equal` run in ['O(N)] (linear) time; that is, they compare against each element in the list once. If more than one of the elements in the sequence satisfy the condition, then algorithm will return false immediately, without examining the remaining members of the sequence. + +[heading Exception Safety] + +All of the variants of `one_of` and `one_of_equal` take their parameters by value or const reference, and do not depend upon any global state. Therefore, all the routines in this file provide the strong exception guarantee. + +[heading Notes] + +* `one_of` and `one_of_equal` both return false for empty ranges, no matter what is passed to test against. + +* The second parameter to `one_of_value` is a template parameter, rather than deduced from the first parameter (`std::iterator_traits::value_type`) because that allows more flexibility for callers, and takes advantage of built-in comparisons for the type that is pointed to by the iterator. The function is defined to return true if, for one element in the sequence, the expression `*iter == val` evaluates to true (where `iter` is an iterator to each element in the sequence) + +[endsect] + +[/ File one_of.qbk +Copyright 2011 Marshall Clow +Distributed under the Boost Software License, Version 1.0. +(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt). +] + diff --git a/doc/ordered-hpp.qbk b/doc/ordered-hpp.qbk new file mode 100644 index 0000000..a7632ac --- /dev/null +++ b/doc/ordered-hpp.qbk @@ -0,0 +1,128 @@ +[/ QuickBook Document version 1.5 ] +[section:is_sorted is_sorted ] + +[/license + +Copyright (c) 2010-2012 Marshall Clow + +Distributed under the Boost Software License, Version 1.0. +(See accompanying file LICENSE_1_0.txt or copy at +http://www.boost.org/LICENSE_1_0.txt) + +] + + +The header file `` contains functions for determining if a sequence is ordered. + +[heading is_sorted] +The function `is_sorted(sequence)` determines whether or not a sequence is completely sorted according so some criteria. If no comparison predicate is specified, then std::less_equal is used (i.e, the test is to see if the sequence is non-decreasing) + +`` +namespace boost { namespace algorithm { + template + bool is_sorted ( Iterator first, Iterator last, Pred p ); + + template + bool is_sorted ( Iterator first, Iterator last ); + + + template + bool is_sorted ( const Range &r, Pred p ); + + template + bool is_sorted ( const Range &r ); +}} +`` + +Iterator requirements: The `is_sorted` functions will work on all kinds of iterators (except output iterators). + +[heading is_sorted_until] + +The function `is_sorted_until(sequence, predicate)` compares each sequential pair of elements in the sequence, checking if they satisfy the predicate. it returns the first element of the sequence that does not satisfy the predicate with its' predecessor. In short, it returns the element in the sequence that is "out of order". If all adjacent pairs satisfy the predicate, then it will return one past the last element of the sequence. + +`` +namespace boost { namespace algorithm { + template + FI is_sorted_until ( ForwardIterator first, ForwardIterator last, Pred p ); + + template + ForwardIterator is_sorted_until ( ForwardIterator first, ForwardIterator last ); + + + template + typename boost::range_iterator::type is_sorted_until ( const Range &r, Pred p ); + + template + typename boost::range_iterator::type is_sorted_until ( const Range &r ); +}} +`` + +Iterator requirements: The `is_sorted_until` functions will work on forward iterators or better. Since they have to return a place in the input sequence, input iterators will not suffice. + +Complexity: + `is_sorted_until` will make at most ['N-1] calls to the predicate (given a sequence of length ['N]). + +Examples: + +Given the sequence `{ 1, 2, 3, 4, 5, 3 }`, `is_sorted_until ( beg, end, std::less())` would return an iterator pointing at the second `3`. + +Given the sequence `{ 1, 2, 3, 4, 5, 9 }`, `is_sorted_until ( beg, end, std::less())` would return `end`. + + +There are also a set of "wrapper functions" for is_ordered which make it easy to see if an entire sequence is ordered. These functions return a boolean indicating success or failure rather than an iterator to where the out of order items were found. + +To test if a sequence is increasing (each element at least as large as the preceeding one): +`` +namespace boost { namespace algorithm { + template + bool is_increasing ( Iterator first, Iterator last ); + + template + bool is_increasing ( const R &range ); +}} +`` + +To test if a sequence is decreasing (each element no larger than the preceeding one): + +`` +namespace boost { namespace algorithm { + template + bool is_decreasing ( Iterator first, Iterator last ); + + template + bool is_decreasing ( const R &range ); +}} +`` + +To test if a sequence is strictly increasing (each element larger than the preceeding one): +`` +namespace boost { namespace algorithm { + template + bool is_strictly_increasing ( Iterator first, Iterator last ); + + template + bool is_strictly_increasing ( const R &range ); +}} +`` + +To test if a sequence is strictly decreasing (each element smaller than the preceeding one): +`` +namespace boost { namespace algorithm { + template + bool is_strictly_decreasing ( Iterator first, Iterator last ); + + template + bool is_strictly_decreasing ( const R &range ); +}} +`` + +Complexity: + Each of these calls is just a thin wrapper over `is_sorted`, so they have the same complexity as `is_sorted`. + +[heading Notes] + +* The routines `is_sorted` and `is_sorted_until` are part of the C++11 standard. When compiled using a C++11 implementation, the implementation from the standard library will be used. + +* `is_sorted` and `is_sorted_until` both return true for empty ranges and ranges of length one. + +[endsect] diff --git a/doc/partition_point.qbk b/doc/partition_point.qbk new file mode 100644 index 0000000..8d1f76c --- /dev/null +++ b/doc/partition_point.qbk @@ -0,0 +1,68 @@ +[/ File partition_point.qbk] + +[section:partition_point partition_point ] + +[/license +Copyright (c) 2010-2012 Marshall Clow + +Distributed under the Boost Software License, Version 1.0. +(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +] + +The header file 'partition_point.hpp' contains two variants of a single algorithm, `partition_point`. Given a partitioned sequence and a predicate, the algorithm finds the partition point; i.e, the first element in the sequence that does not satisfy the predicate. + +The routine `partition_point` takes a partitioned sequence and a predicate. It returns an iterator which 'points to' the first element in the sequence that does not satisfy the predicate. If all the items in the sequence satisfy the predicate, then it returns one past the final element in the sequence. + +`partition_point` come in two forms; the first one takes two iterators to define the range. The second form takes a single range parameter, and uses Boost.Range to traverse it. + + +[heading interface] + +There are two versions; one takes two iterators, and the other takes a range. + +`` +template + ForwardIterator partition_point ( ForwardIterator first, ForwardIterator last, Predicate p ); +template + boost::range_iterator partition_point ( const Range &r, Predicate p ); +`` + +[heading Examples] + +Given the container `c` containing `{ 0, 1, 2, 3, 14, 15 }`, then +`` +bool lessThan10 ( int i ) { return i < 10; } +bool isOdd ( int i ) { return i % 2 == 1; } + +partition_point ( c, lessThan10 ) --> c.begin () + 4 (pointing at 14) +partition_point ( c.begin (), c.end (), lessThan10 ) --> c.begin () + 4 (pointing at 14) +partition_point ( c.begin (), c.begin () + 3, lessThan10 ) -> c.begin () + 3 (end) +partition_point ( c.end (), c.end (), isOdd ) --> c.end () // empty range +`` + +[heading Iterator Requirements] + +`partition_point` requires forward iterators or better; it will not work on input iterators or output iterators. + +[heading Complexity] + +Both of the variants of `partition_point` run in ['O( log (N))] (logarithmic) time; that is, the predicate will be will be applied approximately ['log(N)] times. To do this, however, the algorithm needs to know the size of the sequence. For forward and bidirectional iterators, calculating the size of the sequence is an ['O(N)] operation. + +[heading Exception Safety] + +Both of the variants of `partition_point` take their parameters by value or const reference, and do not depend upon any global state. Therefore, all the routines in this file provide the strong exception guarantee. + +[heading Notes] + +* The iterator-based version of the routine `partition_point` is part of the C++11 standard. When compiled using a C++11 implementation, the implementation from the standard library will be used. + +* For empty ranges, the partition point is the end of the range. + +[endsect] + +[/ File partition_point.qbk +Copyright 2011 Marshall Clow +Distributed under the Boost Software License, Version 1.0. +(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt). +] + diff --git a/include/boost/algorithm/cxx11/all_of.hpp b/include/boost/algorithm/cxx11/all_of.hpp new file mode 100644 index 0000000..4d1690f --- /dev/null +++ b/include/boost/algorithm/cxx11/all_of.hpp @@ -0,0 +1,90 @@ +/* + Copyright (c) Marshall Clow 2008-2012. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +*/ + +/// \file all_of.hpp +/// \brief Test ranges to see if all elements match a value or predicate. +/// \author Marshall Clow + +#ifndef BOOST_ALGORITHM_ALL_OF_HPP +#define BOOST_ALGORITHM_ALL_OF_HPP + +#include +#include + +namespace boost { namespace algorithm { + +#if __cplusplus >= 201103L +// Use the C++11 versions of all_of if it is available +using std::all_of; // Section 25.2.1 +#else +/// \fn all_of ( InputIterator first, InputIterator last, Predicate p ) +/// \return true if all elements in [first, last) satisfy the predicate 'p' +/// \note returns true on an empty range +/// +/// \param first The start of the input sequence +/// \param last One past the end of the input sequence +/// \param p A predicate for testing the elements of the sequence +/// +/// \note This function is part of the C++2011 standard library. +/// We will use the standard one if it is available, +/// otherwise we have our own implementation. +template +bool all_of ( InputIterator first, InputIterator last, Predicate p ) +{ + for ( ; first != last; ++first ) + if ( !p(*first)) + return false; + return true; +} +#endif + +/// \fn all_of ( const Range &r, Predicate p ) +/// \return true if all elements in the range satisfy the predicate 'p' +/// \note returns true on an empty range +/// +/// \param r The input range +/// \param p A predicate for testing the elements of the range +/// +template +bool all_of ( const Range &r, Predicate p ) +{ + return boost::algorithm::all_of ( boost::begin (r), boost::end (r), p ); +} + +/// \fn all_of_equal ( InputIterator first, InputIterator last, const T &val ) +/// \return true if all elements in [first, last) are equal to 'val' +/// \note returns true on an empty range +/// +/// \param first The start of the input sequence +/// \param last One past the end of the input sequence +/// \param val A value to compare against +/// +template +bool all_of_equal ( InputIterator first, InputIterator last, const T &val ) +{ + for ( ; first != last; ++first ) + if ( val != *first ) + return false; + return true; +} + +/// \fn all_of_equal ( const Range &r, const T &val ) +/// \return true if all elements in the range are equal to 'val' +/// \note returns true on an empty range +/// +/// \param r The input range +/// \param val A value to compare against +/// +template +bool all_of_equal ( const Range &r, const T &val ) +{ + return boost::algorithm::all_of_equal ( boost::begin (r), boost::end (r), val ); +} + +}} // namespace boost and algorithm + +#endif // BOOST_ALGORITHM_ALL_OF_HPP diff --git a/include/boost/algorithm/cxx11/any_of.hpp b/include/boost/algorithm/cxx11/any_of.hpp new file mode 100644 index 0000000..4389a13 --- /dev/null +++ b/include/boost/algorithm/cxx11/any_of.hpp @@ -0,0 +1,89 @@ +/* + Copyright (c) Marshall Clow 2008-2012. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + + For more information, see http://www.boost.org +*/ + +/// \file +/// \brief Test ranges to see if any elements match a value or predicate. +/// \author Marshall Clow + +#ifndef BOOST_ALGORITHM_ANY_OF_HPP +#define BOOST_ALGORITHM_ANY_OF_HPP + +#include +#include + +namespace boost { namespace algorithm { + +// Use the C++11 versions of any_of if it is available +#if __cplusplus >= 201103L +using std::any_of; // Section 25.2.2 +#else +/// \fn any_of ( InputIterator first, InputIterator last, Predicate p ) +/// \return true if any of the elements in [first, last) satisfy the predicate +/// \note returns false on an empty range +/// +/// \param first The start of the input sequence +/// \param last One past the end of the input sequence +/// \param p A predicate for testing the elements of the sequence +/// +template +bool any_of ( InputIterator first, InputIterator last, Predicate p ) +{ + for ( ; first != last; ++first ) + if ( p(*first)) + return true; + return false; +} +#endif + +/// \fn any_of ( const Range &r, Predicate p ) +/// \return true if any elements in the range satisfy the predicate 'p' +/// \note returns false on an empty range +/// +/// \param r The input range +/// \param p A predicate for testing the elements of the range +/// +template +bool any_of ( const Range &r, Predicate p ) +{ + return boost::algorithm::any_of (boost::begin (r), boost::end (r), p); +} + +/// \fn any_of_equal ( InputIterator first, InputIterator last, const V &val ) +/// \return true if any of the elements in [first, last) are equal to 'val' +/// \note returns false on an empty range +/// +/// \param first The start of the input sequence +/// \param last One past the end of the input sequence +/// \param val A value to compare against +/// +template +bool any_of_equal ( InputIterator first, InputIterator last, const V &val ) +{ + for ( ; first != last; ++first ) + if ( val == *first ) + return true; + return false; +} + +/// \fn any_of_equal ( const Range &r, const V &val ) +/// \return true if any of the elements in the range are equal to 'val' +/// \note returns false on an empty range +/// +/// \param r The input range +/// \param val A value to compare against +/// +template +bool any_of_equal ( const Range &r, const V &val ) +{ + return boost::algorithm::any_of_equal (boost::begin (r), boost::end (r), val); +} + +}} // namespace boost and algorithm + +#endif // BOOST_ALGORITHM_ANY_OF_HPP diff --git a/include/boost/algorithm/cxx11/copy_if.hpp b/include/boost/algorithm/cxx11/copy_if.hpp new file mode 100644 index 0000000..84048e7 --- /dev/null +++ b/include/boost/algorithm/cxx11/copy_if.hpp @@ -0,0 +1,133 @@ +/* + Copyright (c) Marshall Clow 2008-2012. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +*/ + +/// \file copy_if.hpp +/// \brief Copy a subset of a sequence to a new sequence +/// \author Marshall Clow + +#ifndef BOOST_ALGORITHM_COPY_IF_HPP +#define BOOST_ALGORITHM_COPY_IF_HPP + +#include // for std::copy_if, if available +#include +#include + +namespace boost { namespace algorithm { + +#if __cplusplus >= 201103L +// Use the C++11 versions of copy_if if it is available +using std::copy_if; // Section 25.3.1 +#else +/// \fn copy_if ( InputIterator first, InputIterator last, OutputIterator result, Predicate p ) +/// \brief Copies all the elements from the input range that satisfy the +/// predicate to the output range. +/// \return The updated output iterator +/// +/// \param first The start of the input sequence +/// \param last One past the end of the input sequence +/// \param result An output iterator to write the results into +/// \param p A predicate for testing the elements of the range +/// \note This function is part of the C++2011 standard library. +/// We will use the standard one if it is available, +/// otherwise we have our own implementation. +template +OutputIterator copy_if ( InputIterator first, InputIterator last, OutputIterator result, Predicate p ) +{ + for ( ; first != last; ++first ) + if (p(*first)) + *result++ = first; + return result; +} +#endif + +/// \fn copy_if ( const Range &r, OutputIterator result, Predicate p ) +/// \brief Copies all the elements from the input range that satisfy the +/// predicate to the output range. +/// \return The updated output iterator +/// +/// \param r The input range +/// \param result An output iterator to write the results into +/// \param p A predicate for testing the elements of the range +/// +template +OutputIterator copy_if ( const Range &r, OutputIterator result, Predicate p ) +{ + return boost::algorithm::copy_if (boost::begin (r), boost::end(r), result, p); +} + + +/// \fn copy_while ( InputIterator first, InputIterator last, OutputIterator result, Predicate p ) +/// \brief Copies all the elements at the start of the input range that +/// satisfy the predicate to the output range. +/// \return The updated output iterator +/// +/// \param first The start of the input sequence +/// \param last One past the end of the input sequence +/// \param result An output iterator to write the results into +/// \param p A predicate for testing the elements of the range +/// +template +OutputIterator copy_while ( InputIterator first, InputIterator last, + OutputIterator result, Predicate p ) +{ + for ( ; first != last && p(*first); ++first ) + *result++ = first; + return result; +} + +/// \fn copy_while ( const Range &r, OutputIterator result, Predicate p ) +/// \brief Copies all the elements at the start of the input range that +/// satisfy the predicate to the output range. +/// \return The updated output iterator +/// +/// \param r The input range +/// \param result An output iterator to write the results into +/// \param p A predicate for testing the elements of the range +/// +template +OutputIterator copy_while ( const Range &r, OutputIterator result, Predicate p ) +{ + return boost::algorithm::copy_while (boost::begin (r), boost::end(r), result, p); +} + + +/// \fn copy_until ( InputIterator first, InputIterator last, OutputIterator result, Predicate p ) +/// \brief Copies all the elements at the start of the input range that do not +/// satisfy the predicate to the output range. +/// \return The updated output iterator +/// +/// \param first The start of the input sequence +/// \param last One past the end of the input sequence +/// \param result An output iterator to write the results into +/// \param p A predicate for testing the elements of the range +/// +template +OutputIterator copy_until ( InputIterator first, InputIterator last, OutputIterator result, Predicate p ) +{ + for ( ; first != last && !p(*first); ++first ) + *result++ = first; + return result; +} + +/// \fn copy_until ( const Range &r, OutputIterator result, Predicate p ) +/// \brief Copies all the elements at the start of the input range that do not +/// satisfy the predicate to the output range. +/// \return The updated output iterator +/// +/// \param r The input range +/// \param result An output iterator to write the results into +/// \param p A predicate for testing the elements of the range +/// +template +OutputIterator copy_until ( const Range &r, OutputIterator result, Predicate p ) +{ + return boost::algorithm::copy_until (boost::begin (r), boost::end(r), result, p); +} + +}} // namespace boost and algorithm + +#endif // BOOST_ALGORITHM_COPY_IF_HPP diff --git a/include/boost/algorithm/cxx11/copy_n.hpp b/include/boost/algorithm/cxx11/copy_n.hpp new file mode 100644 index 0000000..0ea53bd --- /dev/null +++ b/include/boost/algorithm/cxx11/copy_n.hpp @@ -0,0 +1,44 @@ +/* + Copyright (c) Marshall Clow 2011-2012. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +*/ + +/// \file copy_n.hpp +/// \brief Copy n items from one sequence to another +/// \author Marshall Clow + +#ifndef BOOST_ALGORITHM_COPY_N_HPP +#define BOOST_ALGORITHM_COPY_N_HPP + +#include // for std::copy_n, if available + +namespace boost { namespace algorithm { + +#if __cplusplus >= 201103L +// Use the C++11 versions of copy_n if it is available +using std::copy_n; // Section 25.3.1 +#else +/// \fn copy_n ( InputIterator first, Size n, OutputIterator result ) +/// \brief Copies exactly n (n > 0) elements from the range starting at first to +/// the range starting at result. +/// \return The updated output iterator +/// +/// \param first The start of the input sequence +/// \param n The number of elements to copy +/// \param result An output iterator to write the results into +/// \note This function is part of the C++2011 standard library. +/// We will use the standard one if it is available, +/// otherwise we have our own implementation. +template +OutputIterator copy_n ( InputIterator first, Size n, OutputIterator result ) +{ + for ( ; n > 0; --n, ++first, ++result ) + *result = *first; + return result; +} +#endif +}} // namespace boost and algorithm + +#endif // BOOST_ALGORITHM_COPY_IF_HPP diff --git a/include/boost/algorithm/cxx11/find_if_not.hpp b/include/boost/algorithm/cxx11/find_if_not.hpp new file mode 100644 index 0000000..4beed00 --- /dev/null +++ b/include/boost/algorithm/cxx11/find_if_not.hpp @@ -0,0 +1,60 @@ +/* + Copyright (c) Marshall Clow 2011-2012. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +*/ + +/// \file find_if_not.hpp +/// \brief Find the first element in a sequence that does not satisfy a predicate. +/// \author Marshall Clow + +#ifndef BOOST_ALGORITHM_FIND_IF_NOT_HPP +#define BOOST_ALGORITHM_FIND_IF_NOT_HPP + +#include // for std::find_if_not, if it exists + +#include +#include + +namespace boost { namespace algorithm { + +#if __cplusplus >= 201103L +// Use the C++11 versions of find_if_not if it is available +using std::find_if_not; // Section 25.2.5 +#else +/// \fn find_if_not(InputIterator first, InputIterator last, Predicate p) +/// \brief Finds the first element in the sequence that does not satisfy the predicate. +/// \return The iterator pointing to the desired element. +/// +/// \param first The start of the input sequence +/// \param last One past the end of the input sequence +/// \param p A predicate for testing the elements of the range +/// \note This function is part of the C++2011 standard library. +/// We will use the standard one if it is available, +/// otherwise we have our own implementation. +template +InputIterator find_if_not ( InputIterator first, InputIterator last, Predicate p ) +{ + for ( ; first != last; ++first ) + if ( !p(*first)) + break; + return first; +} +#endif + +/// \fn find_if_not ( const Range &r, Predicate p ) +/// \brief Finds the first element in the sequence that does not satisfy the predicate. +/// \return The iterator pointing to the desired element. +/// +/// \param r The input range +/// \param p A predicate for testing the elements of the range +/// +template +typename boost::range_iterator::type find_if_not ( const Range &r, Predicate p ) +{ + return boost::algorithm::find_if_not (boost::begin (r), boost::end(r), p); +} + +}} +#endif // BOOST_ALGORITHM_FIND_IF_NOT_HPP diff --git a/include/boost/algorithm/cxx11/iota.hpp b/include/boost/algorithm/cxx11/iota.hpp new file mode 100644 index 0000000..cda7195 --- /dev/null +++ b/include/boost/algorithm/cxx11/iota.hpp @@ -0,0 +1,74 @@ +/* + Copyright (c) Marshall Clow 2008-2012. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +*/ + +/// \file iota.hpp +/// \brief Generate an increasing series +/// \author Marshall Clow + +#ifndef BOOST_ALGORITHM_IOTA_HPP +#define BOOST_ALGORITHM_IOTA_HPP + +#include + +#include +#include + +namespace boost { namespace algorithm { + +#if __cplusplus >= 201103L +// Use the C++11 versions of iota if it is available +using std::iota; // Section 26.7.6 +#else +/// \fn iota ( ForwardIterator first, ForwardIterator last, T value ) +/// \brief Generates an increasing sequence of values, and stores them in [first, last) +/// +/// \param first The start of the input sequence +/// \param last One past the end of the input sequence +/// \param value The initial value of the sequence to be generated +/// \note This function is part of the C++2011 standard library. +/// We will use the standard one if it is available, +/// otherwise we have our own implementation. +template +void iota ( ForwardIterator first, ForwardIterator last, T value ) +{ + for ( ; first != last; ++first, ++value ) + *first = value; +} +#endif + +/// \fn iota ( Range &r, T value ) +/// \brief Generates an increasing sequence of values, and stores them in the input Range. +/// +/// \param r The input range +/// \param value The initial value of the sequence to be generated +/// +template +void iota ( Range &r, T value ) +{ + boost::algorithm::iota (boost::begin(r), boost::end(r), value); +} + + +/// \fn iota_n ( OutputIterator out, T value, std::size_t n ) +/// \brief Generates an increasing sequence of values, and stores them in the input Range. +/// +/// \param out An output iterator to write the results into +/// \param value The initial value of the sequence to be generated +/// \param n The number of items to write +/// +template +OutputIterator iota_n ( OutputIterator out, T value, std::size_t n ) +{ + while ( n-- > 0 ) + *out++ = value++; + + return out; +} + +}} + +#endif // BOOST_ALGORITHM_IOTA_HPP \ No newline at end of file diff --git a/include/boost/algorithm/cxx11/is_partitioned.hpp b/include/boost/algorithm/cxx11/is_partitioned.hpp new file mode 100644 index 0000000..98d7266 --- /dev/null +++ b/include/boost/algorithm/cxx11/is_partitioned.hpp @@ -0,0 +1,65 @@ +/* + Copyright (c) Marshall Clow 2011-2012. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +*/ + +/// \file is_partitioned.hpp +/// \brief Tell if a sequence is partitioned +/// \author Marshall Clow + +#ifndef BOOST_ALGORITHM_IS_PARTITIONED_HPP +#define BOOST_ALGORITHM_IS_PARTITIONED_HPP + +#include // for std::is_partitioned, if available + +#include +#include + +namespace boost { namespace algorithm { + +#if __cplusplus >= 201103L +// Use the C++11 versions of iota if it is available +using std::is_partitioned; // Section 25.3.13 +#else +/// \fn is_partitioned ( InputIterator first, InputIterator last, UnaryPredicate p ) +/// \brief Tests to see if a sequence is partititioned according to a predicate +/// +/// \param first The start of the input sequence +/// \param last One past the end of the input sequence +/// \param p The predicicate to test the values with +/// \note This function is part of the C++2011 standard library. +/// We will use the standard one if it is available, +/// otherwise we have our own implementation. +template +bool is_partitioned ( InputIterator first, InputIterator last, UnaryPredicate p ) +{ +// Run through the part that satisfy the predicate + for ( ; first != last; ++first ) + if ( !p (*first)) + break; +// Now the part that does not satisfy the predicate + for ( ; first != last; ++first ) + if ( p (*first)) + return false; + return true; +} +#endif + +/// \fn is_partitioned ( const Range &r, UnaryPredicate p ) +/// \brief Generates an increasing sequence of values, and stores them in the input Range. +/// +/// \param r The input range +/// \param p The predicicate to test the values with +/// +template +bool is_partitioned ( const Range &r, UnaryPredicate p ) +{ + return boost::algorithm::is_partitioned (boost::begin(r), boost::end(r), p); +} + + +}} + +#endif // BOOST_ALGORITHM_IS_PARTITIONED_HPP \ No newline at end of file diff --git a/include/boost/algorithm/cxx11/is_permutation.hpp b/include/boost/algorithm/cxx11/is_permutation.hpp new file mode 100644 index 0000000..f03e0e5 --- /dev/null +++ b/include/boost/algorithm/cxx11/is_permutation.hpp @@ -0,0 +1,139 @@ +/* + Copyright (c) Marshall Clow 2011-2012. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +*/ + +/// \file is_permutation.hpp +/// \brief Is a sequence a permutation of another sequence +/// \author Marshall Clow + +#ifndef BOOST_ALGORITHM_IS_PERMUTATION_HPP +#define BOOST_ALGORITHM_IS_PERMUTATION_HPP + +#include // for std::less, tie, mismatch and is_permutation (if available) +#include // for std::make_pair +#include // for std::equal_to +#include + +#include +#include +#include +#include +#include // for tie + +namespace boost { namespace algorithm { + +#if __cplusplus >= 201103L +// Use the C++11 versions of is_permutation if it is available +using std::is_permutation; // Section 25.2.12 +#else +/// \cond DOXYGEN_HIDE +namespace detail { + template + struct value_predicate { + value_predicate ( Predicate p, Iterator it ) : p_ ( p ), it_ ( it ) {} + + template + bool operator () ( const T1 &t1 ) const { return p_ ( *it_, t1 ); } + private: + Predicate &p_; + Iterator it_; + }; +} +/// \endcond + + +/// \fn is_permutation ( ForwardIterator1 first, ForwardIterator1 last, ForwardIterator2 first2, BinaryPredicate p ) +/// \brief Tests to see if a the sequence [first,last) is a permutation of the sequence starting at first2 +/// +/// \param first The start of the input sequence +/// \param last One past the end of the input sequence +/// \param first2 The start of the second sequence +/// \param p The predicate to compare elements with +/// +/// \note This function is part of the C++2011 standard library. +/// We will use the standard one if it is available, +/// otherwise we have our own implementation. +template< class ForwardIterator1, class ForwardIterator2, class BinaryPredicate > +bool is_permutation ( ForwardIterator1 first1, ForwardIterator1 last1, + ForwardIterator2 first2, BinaryPredicate p ) +{ +// Skip the common prefix (if any) +// std::tie (first1, first2) = std::mismatch (first1, last1, first2, p); + std::pair eq = std::mismatch (first1, last1, first2, p); + first1 = eq.first; + first2 = eq.second; + if ( first1 != last1 ) { + // Create last2 + ForwardIterator2 last2 = first2; + std::advance ( last2, std::distance (first1, last1)); + + // for each unique value in the sequence [first1,last1), count how many times + // it occurs, and make sure it occurs the same number of times in [first2, last2) + for ( ForwardIterator1 iter = first1; iter != last1; ++iter ) { + detail::value_predicate pred ( p, iter ); + + /* For each value we haven't seen yet... */ + if ( std::find_if ( first1, iter, pred ) == iter ) { + std::size_t dest_count = std::count_if ( first2, last2, pred ); + if ( dest_count == 0 || dest_count != (std::size_t) std::count_if ( iter, last1, pred )) + return false; + } + } + } + + return true; +} + +/// \fn is_permutation ( ForwardIterator1 first, ForwardIterator1 last, ForwardIterator2 first2 ) +/// \brief Tests to see if a the sequence [first,last) is a permutation of the sequence starting at first2 +/// +/// \param first The start of the input sequence +/// \param last One past the end of the input sequence +/// \param first2 The start of the second sequence +/// \note This function is part of the C++2011 standard library. +/// We will use the standard one if it is available, +/// otherwise we have our own implementation. +template< class ForwardIterator1, class ForwardIterator2 > +bool is_permutation ( ForwardIterator1 first, ForwardIterator1 last, ForwardIterator2 first2 ) +{ +// How should I deal with the idea that ForwardIterator1::value_type +// and ForwardIterator2::value_type could be different? Define my own comparison predicate? + return boost::algorithm::is_permutation ( first, last, first2, + std::equal_to::value_type> ()); +} + +#endif + +/// \fn is_permutation ( const Range &r, ForwardIterator first2 ) +/// \brief Tests to see if a the sequence [first,last) is a permutation of the sequence starting at first2 +/// +/// \param r The input range +/// \param first2 The start of the second sequence +template +bool is_permutation ( const Range &r, ForwardIterator first2 ) +{ + return boost::algorithm::is_permutation (boost::begin (r), boost::end (r), first2 ); +} + +/// \fn is_permutation ( const Range &r, ForwardIterator first2, BinaryPredicate pred ) +/// \brief Tests to see if a the sequence [first,last) is a permutation of the sequence starting at first2 +/// +/// \param r The input range +/// \param first2 The start of the second sequence +/// \param pred The predicate to compare elements with +/// +// Disable this template when the first two parameters are the same type +// That way the non-range version will be chosen. +template +typename boost::disable_if_c::value, bool>::type +is_permutation ( const Range &r, ForwardIterator first2, BinaryPredicate pred ) +{ + return boost::algorithm::is_permutation (boost::begin (r), boost::end (r), first2, pred ); +} + +}} + +#endif // BOOST_ALGORITHM_IS_PERMUTATION_HPP \ No newline at end of file diff --git a/include/boost/algorithm/cxx11/is_sorted.hpp b/include/boost/algorithm/cxx11/is_sorted.hpp new file mode 100644 index 0000000..ef2ec41 --- /dev/null +++ b/include/boost/algorithm/cxx11/is_sorted.hpp @@ -0,0 +1,291 @@ +// Copyright (c) 2010 Nuovation System Designs, LLC +// Grant Erickson +// +// Reworked somewhat by Marshall Clow; August 2010 +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/ for latest version. +// + +#ifndef BOOST_ALGORITHM_ORDERED_HPP +#define BOOST_ALGORITHM_ORDERED_HPP + +#include +#include +#include + +#include +#include + +#include +#include +#include + +namespace boost { namespace algorithm { + +#if __cplusplus >= 201103L +// Use the C++11 versions of iota if it is available +using std::is_sorted_until; // Section 25.4.1.5 +using std::is_sorted; // Section 25.4.1.5 +#else +/// \fn is_sorted_until ( ForwardIterator first, ForwardIterator last, Pred p ) +/// \return the point in the sequence [first, last) where the elements are unordered +/// (according to the comparison predicate 'p'). +/// +/// \param first The start of the sequence to be tested. +/// \param last One past the end of the sequence +/// \param p A binary predicate that returns true if two elements are ordered. +/// + template + ForwardIterator is_sorted_until ( ForwardIterator first, ForwardIterator last, Pred p ) + { + if ( first == last ) return last; // the empty sequence is ordered + ForwardIterator next = first; + while ( ++next != last ) + { + if ( !p ( *first, *next )) + return next; + first = next; + } + return last; + } + +/// \fn is_sorted_until ( ForwardIterator first, ForwardIterator last ) +/// \return the point in the sequence [first, last) where the elements are unordered +/// +/// \param first The start of the sequence to be tested. +/// \param last One past the end of the sequence +/// + template + ForwardIterator is_sorted_until ( ForwardIterator first, ForwardIterator last ) + { + typedef typename std::iterator_traits::value_type value_type; + return boost::algorithm::is_sorted_until ( first, last, std::less_equal()); + } + + +/// \fn is_sorted ( ForwardIterator first, ForwardIterator last, Pred p ) +/// \return whether or not the entire sequence is sorted +/// +/// \param first The start of the sequence to be tested. +/// \param last One past the end of the sequence +/// \param p A binary predicate that returns true if two elements are ordered. +/// + template + bool is_sorted ( ForwardIterator first, ForwardIterator last, Pred p ) + { + return boost::algorithm::is_sorted_until (first, last, p) == last; + } + +/// \fn is_sorted ( ForwardIterator first, ForwardIterator last ) +/// \return whether or not the entire sequence is sorted +/// +/// \param first The start of the sequence to be tested. +/// \param last One past the end of the sequence +/// + template + bool is_sorted ( ForwardIterator first, ForwardIterator last ) + { + return boost::algorithm::is_sorted_until (first, last) == last; + } +#endif + +/// +/// -- Range based versions of the C++11 functions +/// + +/// \fn is_sorted_until ( const R &range, Pred p ) +/// \return the point in the range R where the elements are unordered +/// (according to the comparison predicate 'p'). +/// +/// \param range The range to be tested. +/// \param p A binary predicate that returns true if two elements are ordered. +/// + template + typename boost::lazy_disable_if_c< + boost::is_same::value, + typename boost::range_iterator + >::type is_sorted_until ( const R &range, Pred p ) + { + return boost::algorithm::is_sorted_until ( boost::begin ( range ), boost::end ( range ), p ); + } + + +/// \fn is_sorted_until ( const R &range ) +/// \return the point in the range R where the elements are unordered +/// +/// \param range The range to be tested. +/// + template + typename boost::range_iterator::type is_sorted_until ( const R &range ) + { + return boost::algorithm::is_sorted_until ( boost::begin ( range ), boost::end ( range )); + } + +namespace detail { + typedef struct { typedef bool type; } bool_; +}; + +/// \fn is_sorted ( const R &range, Pred p ) +/// \return whether or not the entire range R is sorted +/// (according to the comparison predicate 'p'). +/// +/// \param range The range to be tested. +/// \param p A binary predicate that returns true if two elements are ordered. +/// + template + typename boost::lazy_disable_if_c< boost::is_same::value, boost::mpl::identity >::type + is_sorted ( const R &range, Pred p ) + { + return boost::algorithm::is_sorted ( boost::begin ( range ), boost::end ( range ), p ); + } + + +/// \fn is_sorted ( const R &range ) +/// \return whether or not the entire range R is sorted +/// +/// \param range The range to be tested. +/// + template + bool is_sorted ( const R &range ) + { + return boost::algorithm::is_sorted ( boost::begin ( range ), boost::end ( range )); + } + + +/// +/// -- Range based versions of the C++11 functions +/// + +/// \fn is_increasing ( ForwardIterator first, ForwardIterator last ) +/// \return true if the entire sequence is increasing; i.e, each item is greater than or +/// equal to the previous one. +/// +/// \param first The start of the sequence to be tested. +/// \param last One past the end of the sequence +/// +/// \note This function will return true for sequences that contain items that compare +/// equal. If that is not what you intended, you should use is_strictly_increasing instead. + template + bool is_increasing ( ForwardIterator first, ForwardIterator last ) + { + typedef typename std::iterator_traits::value_type value_type; + return boost::algorithm::is_sorted (first, last, std::less_equal()); + } + + +/// \fn is_increasing ( const R &range ) +/// \return true if the entire sequence is increasing; i.e, each item is greater than or +/// equal to the previous one. +/// +/// \param range The range to be tested. +/// +/// \note This function will return true for sequences that contain items that compare +/// equal. If that is not what you intended, you should use is_strictly_increasing instead. + template + bool is_increasing ( const R &range ) + { + return is_increasing ( boost::begin ( range ), boost::end ( range )); + } + + + +/// \fn is_decreasing ( ForwardIterator first, ForwardIterator last ) +/// \return true if the entire sequence is decreasing; i.e, each item is less than +/// or equal to the previous one. +/// +/// \param first The start of the sequence to be tested. +/// \param last One past the end of the sequence +/// +/// \note This function will return true for sequences that contain items that compare +/// equal. If that is not what you intended, you should use is_strictly_decreasing instead. + template + bool is_decreasing ( ForwardIterator first, ForwardIterator last ) + { + typedef typename std::iterator_traits::value_type value_type; + return boost::algorithm::is_sorted (first, last, std::greater_equal()); + } + +/// \fn is_decreasing ( const R &range ) +/// \return true if the entire sequence is decreasing; i.e, each item is less than +/// or equal to the previous one. +/// +/// \param range The range to be tested. +/// +/// \note This function will return true for sequences that contain items that compare +/// equal. If that is not what you intended, you should use is_strictly_decreasing instead. + template + bool is_decreasing ( const R &range ) + { + return is_decreasing ( boost::begin ( range ), boost::end ( range )); + } + + + +/// \fn is_strictly_increasing ( ForwardIterator first, ForwardIterator last ) +/// \return true if the entire sequence is strictly increasing; i.e, each item is greater +/// than the previous one +/// +/// \param first The start of the sequence to be tested. +/// \param last One past the end of the sequence +/// +/// \note This function will return false for sequences that contain items that compare +/// equal. If that is not what you intended, you should use is_increasing instead. + template + bool is_strictly_increasing ( ForwardIterator first, ForwardIterator last ) + { + typedef typename std::iterator_traits::value_type value_type; + return boost::algorithm::is_sorted (first, last, std::less()); + } + +/// \fn is_strictly_increasing ( const R &range ) +/// \return true if the entire sequence is strictly increasing; i.e, each item is greater +/// than the previous one +/// +/// \param range The range to be tested. +/// +/// \note This function will return false for sequences that contain items that compare +/// equal. If that is not what you intended, you should use is_increasing instead. + template + bool is_strictly_increasing ( const R &range ) + { + return is_strictly_increasing ( boost::begin ( range ), boost::end ( range )); + } + + +/// \fn is_strictly_decreasing ( ForwardIterator first, ForwardIterator last ) +/// \return true if the entire sequence is strictly decreasing; i.e, each item is less than +/// the previous one +/// +/// \param first The start of the sequence to be tested. +/// \param last One past the end of the sequence +/// +/// \note This function will return false for sequences that contain items that compare +/// equal. If that is not what you intended, you should use is_decreasing instead. + template + bool is_strictly_decreasing ( ForwardIterator first, ForwardIterator last ) + { + typedef typename std::iterator_traits::value_type value_type; + return boost::algorithm::is_sorted (first, last, std::greater()); + } + +/// \fn is_strictly_decreasing ( const R &range ) +/// \return true if the entire sequence is strictly decreasing; i.e, each item is less than +/// the previous one +/// +/// \param range The range to be tested. +/// +/// \note This function will return false for sequences that contain items that compare +/// equal. If that is not what you intended, you should use is_decreasing instead. + template + bool is_strictly_decreasing ( const R &range ) + { + return is_strictly_decreasing ( boost::begin ( range ), boost::end ( range )); + } + +}} // namespace boost + +#endif // BOOST_ALGORITHM_ORDERED_HPP diff --git a/include/boost/algorithm/cxx11/none_of.hpp b/include/boost/algorithm/cxx11/none_of.hpp new file mode 100644 index 0000000..db670c6 --- /dev/null +++ b/include/boost/algorithm/cxx11/none_of.hpp @@ -0,0 +1,87 @@ +/* + Copyright (c) Marshall Clow 2008-2012. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +*/ + +/// \file none_of.hpp +/// \brief Test ranges to see if no elements match a value or predicate. +/// \author Marshall Clow + +#ifndef BOOST_ALGORITHM_NONE_OF_HPP +#define BOOST_ALGORITHM_NONE_OF_HPP + +#include +#include + +namespace boost { namespace algorithm { + +// Use the C++11 versions of the none_of if it is available +#if __cplusplus >= 201103L +using std::none_of; // Section 25.2.3 +#else +/// \fn none_of ( InputIterator first, InputIterator last, Predicate p ) +/// \return true if none of the elements in [first, last) satisfy the predicate 'p' +/// \note returns true on an empty range +/// +/// \param first The start of the input sequence +/// \param last One past the end of the input sequence +/// \param p A predicate for testing the elements of the sequence +/// +template +bool none_of ( InputIterator first, InputIterator last, Predicate p ) +{ +for ( ; first != last; ++first ) + if ( p(*first)) + return false; + return true; +} +#endif + +/// \fn none_of ( const Range &r, Predicate p ) +/// \return true if none of the elements in the range satisfy the predicate 'p' +/// \note returns true on an empty range +/// +/// \param r The input range +/// \param p A predicate for testing the elements of the range +/// +template +bool none_of ( const Range &r, Predicate p ) +{ + return boost::algorithm::none_of (boost::begin (r), boost::end (r), p ); +} + +/// \fn none_of_equal ( InputIterator first, InputIterator last, const V &val ) +/// \return true if none of the elements in [first, last) are equal to 'val' +/// \note returns true on an empty range +/// +/// \param first The start of the input sequence +/// \param last One past the end of the input sequence +/// \param val A value to compare against +/// +template +bool none_of_equal ( InputIterator first, InputIterator last, const V &val ) +{ + for ( ; first != last; ++first ) + if ( val == *first ) + return false; + return true; +} + +/// \fn none_of_equal ( const Range &r, const V &val ) +/// \return true if none of the elements in the range are equal to 'val' +/// \note returns true on an empty range +/// +/// \param r The input range +/// \param val A value to compare against +/// +template +bool none_of_equal ( const Range &r, const V & val ) +{ + return boost::algorithm::none_of_equal (boost::begin (r), boost::end (r), val); +} + +}} // namespace boost and algorithm + +#endif // BOOST_ALGORITHM_NONE_OF_HPP diff --git a/include/boost/algorithm/cxx11/one_of.hpp b/include/boost/algorithm/cxx11/one_of.hpp new file mode 100644 index 0000000..b6e8c77 --- /dev/null +++ b/include/boost/algorithm/cxx11/one_of.hpp @@ -0,0 +1,82 @@ +/* + Copyright (c) Marshall Clow 2008-2012. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +*/ + +/// \file one_of.hpp +/// \brief Test ranges to see if only one element matches a value or predicate. +/// \author Marshall Clow + +#ifndef BOOST_ALGORITHM_ONE_OF_HPP +#define BOOST_ALGORITHM_ONE_OF_HPP + +#include // for std::find and std::find_if +#include + +#include +#include + +namespace boost { namespace algorithm { + +/// \fn one_of ( InputIterator first, InputIterator last, Predicate p ) +/// \return true if the predicate 'p' is true for exactly one item in [first, last). +/// +/// \param first The start of the input sequence +/// \param last One past the end of the input sequence +/// \param p A predicate for testing the elements of the sequence +/// +template +bool one_of ( InputIterator first, InputIterator last, Predicate p ) +{ + InputIterator i = std::find_if (first, last, p); + if (i == last) + return false; // Didn't occur at all + return boost::algorithm::none_of (++i, last, p); +} + +/// \fn one_of ( const Range &r, Predicate p ) +/// \return true if the predicate 'p' is true for exactly one item in the range. +/// +/// \param r The input range +/// \param p A predicate for testing the elements of the range +/// +template +bool one_of ( const Range &r, Predicate p ) +{ + return boost::algorithm::one_of ( boost::begin (r), boost::end (r), p ); +} + + +/// \fn one_of_equal ( InputIterator first, InputIterator last, const V &val ) +/// \return true if the value 'val' exists only once in [first, last). +/// +/// \param first The start of the input sequence +/// \param last One past the end of the input sequence +/// \param val A value to compare against +/// +template +bool one_of_equal ( InputIterator first, InputIterator last, const V &val ) +{ + InputIterator i = std::find (first, last, val); // find first occurrence of 'val' + if (i == last) + return false; // Didn't occur at all + return boost::algorithm::none_of_equal (++i, last, val); +} + +/// \fn one_of_equal ( const Range &r, const V &val ) +/// \return true if the value 'val' exists only once in the range. +/// +/// \param r The input range +/// \param val A value to compare against +/// +template +bool one_of_equal ( const Range &r, const V &val ) +{ + return boost::algorithm::one_of_equal ( boost::begin (r), boost::end (r), val ); +} + +}} // namespace boost and algorithm + +#endif // BOOST_ALGORITHM_ALL_HPP diff --git a/include/boost/algorithm/cxx11/partition_copy.hpp b/include/boost/algorithm/cxx11/partition_copy.hpp new file mode 100644 index 0000000..17db978 --- /dev/null +++ b/include/boost/algorithm/cxx11/partition_copy.hpp @@ -0,0 +1,77 @@ +/* + Copyright (c) Marshall Clow 2011-2012. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +*/ + +/// \file partition_copy.hpp +/// \brief Copy a subset of a sequence to a new sequence +/// \author Marshall Clow + +#ifndef BOOST_ALGORITHM_PARTITION_COPY_HPP +#define BOOST_ALGORITHM_PARTITION_COPY_HPP + +#include // for make_pair + +#include +#include + +namespace boost { namespace algorithm { + +#if __cplusplus >= 201103L +// Use the C++11 versions of partition_copy if it is available +using std::partition_copy; // Section 25.3.13 +#else +/// \fn partition_copy ( InputIterator first, InputIterator last, +/// OutputIterator1 out_true, OutputIterator2 out_false, UnaryPredicate p ) +/// \brief Copies the elements that satisfy the predicate p from the range [first, last) +/// to the range beginning at d_first_true, and +/// copies the elements that do not satisfy p to the range beginning at d_first_false. +/// +/// +/// \param first The start of the input sequence +/// \param last One past the end of the input sequence +/// \param out_true An output iterator to write the elements that satisfy the predicate into +/// \param out_false An output iterator to write the elements that do not satisfy the predicate into +/// \param p A predicate for dividing the elements of the input sequence. +/// +/// \note This function is part of the C++2011 standard library. +/// We will use the standard one if it is available, +/// otherwise we have our own implementation. +template +std::pair +partition_copy ( InputIterator first, InputIterator last, + OutputIterator1 out_true, OutputIterator2 out_false, UnaryPredicate p ) +{ + for ( ; first != last; ++first ) + if ( p (*first)) + *out_true++ = *first; + else + *out_false++ = *first; + return std::pair ( out_true, out_false ); +} +#endif + +/// \fn partition_copy ( const Range &r, +/// OutputIterator1 out_true, OutputIterator2 out_false, UnaryPredicate p ) +/// +/// \param r The input range +/// \param out_true An output iterator to write the elements that satisfy the predicate into +/// \param out_false An output iterator to write the elements that do not satisfy the predicate into +/// \param p A predicate for dividing the elements of the input sequence. +/// +template +std::pair +partition_copy ( const Range &r, OutputIterator1 out_true, OutputIterator2 out_false, + UnaryPredicate p ) +{ + return boost::algorithm::partition_copy + (boost::begin(r), boost::end(r), out_true, out_false, p ); +} + +}} // namespace boost and algorithm + +#endif // BOOST_ALGORITHM_PARTITION_COPY_HPP diff --git a/include/boost/algorithm/cxx11/partition_point.hpp b/include/boost/algorithm/cxx11/partition_point.hpp new file mode 100644 index 0000000..f41fb30 --- /dev/null +++ b/include/boost/algorithm/cxx11/partition_point.hpp @@ -0,0 +1,72 @@ +/* + Copyright (c) Marshall Clow 2011-2012. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +*/ + +/// \file partition_point.hpp +/// \brief Find the partition point in a sequence +/// \author Marshall Clow + +#ifndef BOOST_ALGORITHM_PARTITION_POINT_HPP +#define BOOST_ALGORITHM_PARTITION_POINT_HPP + +#include // for std::partition_point, if available + +#include +#include + +namespace boost { namespace algorithm { + +#if __cplusplus >= 201103L +// Use the C++11 versions of iota if it is available +using std::partition_point; // Section 25.3.13 +#else +/// \fn partition_point ( ForwardIterator first, ForwardIterator last, Predicate p ) +/// \brief Given a partitioned range, returns the partition point, i.e, the first element +/// that does not satisfy p +/// +/// \param first The start of the input sequence +/// \param last One past the end of the input sequence +/// \param p The predicate to test the values with +/// \note This function is part of the C++2011 standard library. +/// We will use the standard one if it is available, +/// otherwise we have our own implementation. +template +ForwardIterator partition_point ( ForwardIterator first, ForwardIterator last, Predicate p ) +{ + std::size_t dist = std::distance ( first, last ); + while ( first != last ) { + std::size_t d2 = dist / 2; + ForwardIterator ret_val = first; + std::advance (ret_val, d2); + if (p (*ret_val)) { + first = ++ret_val; + dist -= d2 + 1; + } + else { + last = ret_val; + dist = d2; + } + } + return first; +} +#endif + +/// \fn partition_point ( Range &r, Predicate p ) +/// \brief Given a partitioned range, returns the partition point +/// +/// \param r The input range +/// \param p The predicate to test the values with +/// +template +typename boost::range_iterator partition_point ( Range &r, Predicate p ) +{ + return boost::algorithm::partition_point (boost::begin(r), boost::end(r), p); +} + + +}} + +#endif // BOOST_ALGORITHM_PARTITION_POINT_HPP \ No newline at end of file diff --git a/include/boost/algorithm/hex.hpp b/include/boost/algorithm/hex.hpp new file mode 100644 index 0000000..c598dcb --- /dev/null +++ b/include/boost/algorithm/hex.hpp @@ -0,0 +1,279 @@ +/* + Copyright (c) Marshall Clow 2011-2012. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + + Thanks to Nevin for his comments/help. +*/ + +/* + General problem - turn a sequence of integral types into a sequence of hexadecimal characters. + - and back. + +TO DO: + 1. these should really only work on integral types. (see the >> and << operations) + -- this is done, I think. + 2. The 'value_type_or_char' struct is really a hack. + -- but it's a better hack now that it works with back_insert_iterators +*/ + +/// \file hex.hpp +/// \brief Convert sequence of integral types into a sequence of hexadecimal +/// characters and back. Based on the MySQL functions HEX and UNHEX +/// \author Marshall Clow + +#ifndef BOOST_ALGORITHM_HEXHPP +#define BOOST_ALGORITHM_HEXHPP + +#include // for std::iterator_traits +#include + +#include +#include +#include + +#include +#include + + +namespace boost { namespace algorithm { + +/*! + \struct hex_decode_error + \brief Base exception class for all hex decoding errors + + \struct non_hex_input + \brief Thrown when a non-hex value (0-9, A-F) encountered when decoding. + Contains the offending character + + \struct not_enough_input + \brief Thrown when the input sequence unexpectedly ends + +*/ +struct hex_decode_error: virtual boost::exception, virtual std::exception {}; +struct not_enough_input : public hex_decode_error {}; +struct non_hex_input : public hex_decode_error { + non_hex_input ( char ch ) : bad_char ( ch ) {} + char bad_char; +private: + non_hex_input (); // don't allow creation w/o a char + }; + +namespace detail { +/// \cond DOXYGEN_HIDE + + template + OutputIterator encode_one ( T val, OutputIterator out ) { + const std::size_t num_hex_digits = 2 * sizeof ( T ); + char res [ num_hex_digits ]; + char *p = res + num_hex_digits; + for ( std::size_t i = 0; i < num_hex_digits; ++i, val >>= 4 ) + *--p = "0123456789ABCDEF" [ val & 0x0F ]; + return std::copy ( res, res + num_hex_digits, out ); + } + + unsigned hex_char_to_int ( char c ) { + if ( c >= '0' && c <= '9' ) return c - '0'; + if ( c >= 'A' && c <= 'F' ) return c - 'A' + 10; + if ( c >= 'a' && c <= 'f' ) return c - 'a' + 10; + BOOST_THROW_EXCEPTION (non_hex_input (c)); + return 0; // keep dumb compilers happy + } + + +// My own iterator_traits class. +// It is here so that I can "reach inside" some kinds of output iterators +// and get the type to write. + template + struct hex_iterator_traits { + typedef typename std::iterator_traits::value_type value_type; + }; + + template + struct hex_iterator_traits< std::back_insert_iterator > { + typedef typename Container::value_type value_type; + }; + + template + struct hex_iterator_traits< std::front_insert_iterator > { + typedef typename Container::value_type value_type; + }; + + template + struct hex_iterator_traits< std::insert_iterator > { + typedef typename Container::value_type value_type; + }; + +// ostream_iterators have three template parameters. +// The first one is the output type, the second one is the character type of +// the underlying stream, the third is the character traits. +// We only care about the first one. + template + struct hex_iterator_traits< std::ostream_iterator > { + typedef T value_type; + }; + +// Output Iterators have a value type of 'void'. Kinda sucks. +// We special case some output iterators, but we can't enumerate them all. +// If we can't figure it out, we assume that you want to output chars. +// If you don't, pass in an iterator with a real value_type. + template struct value_type_or_char { typedef T value_type; }; + template <> struct value_type_or_char { typedef char value_type; }; + +// All in one step + template + struct iterator_value_type { +// typedef typename value_type_or_char::value_type>::value_type value_type; + typedef typename hex_iterator_traits::value_type value_type; + }; + +// What can we assume here about the inputs? +// is std::iterator_traits::value_type always 'char' ? +// Could it be wchar_t, say? Does it matter? +// We are assuming ASCII for the values - but what about the storage? + template + typename boost::enable_if::value_type>, OutputIterator>::type + decode_one ( InputIterator &first, InputIterator last, OutputIterator out ) { + typedef typename iterator_value_type::value_type T; + T res (0); + + // Need to make sure that we get can read that many chars here. + for ( std::size_t i = 0; i < 2 * sizeof ( T ); ++i, ++first ) { + if ( first == last ) + BOOST_THROW_EXCEPTION (not_enough_input ()); + res = ( 16 * res ) + hex_char_to_int (static_cast (*first)); + } + + *out = res; + return ++out; + } +/// \endcond + } + + +/// \fn hex ( InputIterator first, InputIterator last, OutputIterator out ) +/// \brief Converts a sequence of integral types into a hexadecimal sequence of characters. +/// +/// \param first The start of the input sequence +/// \param last One past the end of the input sequence +/// \param out An output iterator to the results into +/// \return The updated output iterator +/// \note Based on the MySQL function of the same name +template +typename boost::enable_if::value_type>, OutputIterator>::type +hex ( InputIterator first, InputIterator last, OutputIterator out ) { + for ( ; first != last; ++first ) + out = detail::encode_one ( *first, out ); + return out; + } + + +/// \fn hex ( const T *ptr, OutputIterator out ) +/// \brief Converts a sequence of integral types into a hexadecimal sequence of characters. +/// +/// \param ptr A pointer to a 0-terminated sequence of data. +/// \param out An output iterator to the results into +/// \return The updated output iterator +/// \note Based on the MySQL function of the same name +template +typename boost::enable_if, OutputIterator>::type +hex ( const T *ptr, OutputIterator out ) { + while ( *ptr ) + out = detail::encode_one ( *ptr++, out ); + return out; + } + +/// \fn hex ( const Range &r, OutputIterator out ) +/// \brief Converts a sequence of integral types into a hexadecimal sequence of characters. +/// +/// \param r The input range +/// \param out An output iterator to the results into +/// \return The updated output iterator +/// \note Based on the MySQL function of the same name +template +typename boost::enable_if::value_type>, OutputIterator>::type +hex ( const Range &r, OutputIterator out ) { + return hex (boost::begin(r), boost::end(r), out); +} + + +/// \fn unhex ( InputIterator first, InputIterator last, OutputIterator out ) +/// \brief Converts a sequence of hexadecimal characters into a sequence of integers. +/// +/// \param first The start of the input sequence +/// \param last One past the end of the input sequence +/// \param out An output iterator to the results into +/// \return The updated output iterator +/// \note Based on the MySQL function of the same name +template +OutputIterator unhex ( InputIterator first, InputIterator last, OutputIterator out ) { + while ( first != last ) + out = detail::decode_one ( first, last, out ); + return out; + } + + +/// \fn unhex ( const T *ptr, OutputIterator out ) +/// \brief Converts a sequence of hexadecimal characters into a sequence of integers. +/// +/// \param ptr A pointer to a null-terminated input sequence. +/// \param out An output iterator to the results into +/// \return The updated output iterator +/// \note Based on the MySQL function of the same name +template +OutputIterator unhex ( const T *ptr, OutputIterator out ) { + typedef typename detail::iterator_value_type::value_type OutputType; +// If we run into the terminator while decoding, we will throw a +// malformed input exception. It would be nicer to throw a 'Not enough input' +// exception - but how much extra work would that require? +// I just make up an "end iterator" which we will never get to - +// two Ts per byte of the output type. + while ( *ptr ) + out = detail::decode_one ( ptr, ptr + 2 * sizeof(OutputType), out ); + return out; + } + + +/// \fn OutputIterator unhex ( const Range &r, OutputIterator out ) +/// \brief Converts a sequence of hexadecimal characters into a sequence of integers. +/// +/// \param r The input range +/// \param out An output iterator to the results into +/// \return The updated output iterator +/// \note Based on the MySQL function of the same name +template +OutputIterator unhex ( const Range &r, OutputIterator out ) { + return unhex (boost::begin(r), boost::end(r), out); + } + + +/// \fn String hex ( const String &input ) +/// \brief Converts a sequence of integral types into a hexadecimal sequence of characters. +/// +/// \param input A container to be converted +/// \return A container with the encoded text +template +String hex ( const String &input ) { + String output; + output.reserve (input.size () * (2 * sizeof (typename String::value_type))); + (void) hex (input, std::back_inserter (output)); + return output; + } + +/// \fn String unhex ( const String &input ) +/// \brief Converts a sequence of hexadecimal characters into a sequence of characters. +/// +/// \param input A container to be converted +/// \return A container with the decoded text +template +String unhex ( const String &input ) { + String output; + output.reserve (input.size () / (2 * sizeof (typename String::value_type))); + (void) unhex (input, std::back_inserter (output)); + return output; + } + +}} + +#endif // BOOST_ALGORITHM_HEXHPP diff --git a/include/boost/algorithm/string/find.hpp b/include/boost/algorithm/string/find.hpp index 304646d..cc99ca1 100644 --- a/include/boost/algorithm/string/find.hpp +++ b/include/boost/algorithm/string/find.hpp @@ -228,13 +228,13 @@ namespace boost { //! Find head algorithm /*! Get the head of the input. Head is a prefix of the string of the - given size. If the input is shorter then required, whole input if considered + given size. If the input is shorter then required, whole input is considered to be the head. \param Input An input string \param N Length of the head For N>=0, at most N characters are extracted. - For N<0, size(Input)-|N| characters are extracted. + For N<0, at most size(Input)-|N| characters are extracted. \return An \c iterator_range delimiting the match. Returned iterator is either \c Range1T::iterator or @@ -258,13 +258,13 @@ namespace boost { //! Find tail algorithm /*! Get the tail of the input. Tail is a suffix of the string of the - given size. If the input is shorter then required, whole input if considered + given size. If the input is shorter then required, whole input is considered to be the tail. \param Input An input string \param N Length of the tail. For N>=0, at most N characters are extracted. - For N<0, size(Input)-|N| characters are extracted. + For N<0, at most size(Input)-|N| characters are extracted. \return An \c iterator_range delimiting the match. Returned iterator is either \c RangeT::iterator or diff --git a/string/doc/usage.xml b/string/doc/usage.xml index a30d366..291a525 100644 --- a/string/doc/usage.xml +++ b/string/doc/usage.xml @@ -163,7 +163,7 @@ Trimming - When parsing the input from a user, strings usually have unwanted leading or trailing + When parsing the input from a user, strings often have unwanted leading or trailing characters. To get rid of them, we need trim functions: diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 5949110..7e9292d 100755 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -22,6 +22,28 @@ import testing ; # Clamp tests [ run clamp_test.cpp : : : : clamp_test ] + +# Cxx11 tests + [ run all_of_test.cpp : : : : all_of_test ] + [ run any_of_test.cpp : : : : any_of_test ] + [ run none_of_test.cpp : : : : none_of_test ] + [ run one_of_test.cpp : : : : one_of_test ] + + [ run ordered_test.cpp : : : : ordered_test ] + [ run find_if_not_test1.cpp : : : : find_if_not_test1 ] + [ run copy_n_test1.cpp : : : : copy_n_test1 ] + [ run iota_test1.cpp : : : : iota_test1 ] + + [ run is_permutation_test1.cpp : : : : is_permutation_test1 ] + [ run partition_point_test1.cpp : : : : partition_point_test1 ] + [ run is_partitioned_test1.cpp : : : : is_partitioned_test1 ] + [ run partition_copy_test1.cpp : : : : partition_copy_test1 ] + +# Hex tests + [ run hex_test1.cpp : : : : hex_test1 ] + [ run hex_test2.cpp : : : : hex_test2 ] + [ run hex_test3.cpp : : : : hex_test3 ] + [ compile-fail hex_fail1.cpp ] ; } diff --git a/test/all_of_test.cpp b/test/all_of_test.cpp new file mode 100644 index 0000000..bf35da1 --- /dev/null +++ b/test/all_of_test.cpp @@ -0,0 +1,86 @@ +/* + Copyright (c) Marshall Clow 2010-2012. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + + For more information, see http://www.boost.org +*/ + +#include +#include +#include + +#include +#include +#include + +template +struct is_ : public std::unary_function { + is_ ( T v ) : val_ ( v ) {} + ~is_ () {} + bool operator () ( T comp ) const { return val_ == comp; } +private: + is_ (); // need a value + + T val_; + }; + +namespace ba = boost::algorithm; + +void test_all () +{ +// Note: The literal values here are tested against directly, careful if you change them: + int some_numbers[] = { 1, 1, 1, 18, 10 }; + std::vector vi(some_numbers, some_numbers + 5); + std::list li(vi.begin(), vi.end ()); + + int some_letters[] = { 'a', 'q', 'n', 'y', 'n' }; + std::vector vc(some_letters, some_letters + 5); + + + BOOST_CHECK (!ba::all_of_equal ( vi, 1 )); + BOOST_CHECK (!ba::all_of ( vi, is_ ( 1 ))); + BOOST_CHECK (!ba::all_of_equal ( vi.begin(), vi.end(), 1 )); + BOOST_CHECK (!ba::all_of ( vi.begin(), vi.end(), is_ ( 1 ))); + + BOOST_CHECK (!ba::all_of_equal ( vi, 0 )); + BOOST_CHECK (!ba::all_of ( vi, is_ ( 0 ))); + BOOST_CHECK (!ba::all_of_equal ( vi.begin(), vi.end(), 0 )); + BOOST_CHECK (!ba::all_of ( vi.begin(), vi.end(), is_ ( 0 ))); + + BOOST_CHECK ( ba::all_of_equal ( vi.end(), vi.end(), 0 )); + BOOST_CHECK ( ba::all_of ( vi.end(), vi.end(), is_ ( 0 ))); + + BOOST_CHECK ( ba::all_of_equal ( vi.begin(), vi.begin () + 3, 1 )); + BOOST_CHECK ( ba::all_of ( vi.begin(), vi.begin () + 3, is_ ( 1 ))); + + BOOST_CHECK ( ba::all_of_equal ( vc.begin() + 1, vc.begin() + 2, 'q' )); + BOOST_CHECK ( ba::all_of ( vc.begin() + 1, vc.begin() + 2, is_ ( 'q' ))); + + BOOST_CHECK (!ba::all_of_equal ( vc, '!' )); + BOOST_CHECK (!ba::all_of ( vc, is_ ( '!' ))); + + BOOST_CHECK ( ba::all_of_equal ( vi.begin(), vi.begin(), 1 )); + BOOST_CHECK ( ba::all_of_equal ( vc.begin(), vc.begin(), 'a' )); + BOOST_CHECK ( ba::all_of ( vi.begin(), vi.begin(), is_ ( 1 ))); + BOOST_CHECK ( ba::all_of ( vc.begin(), vc.begin(), is_ ( 'a' ))); + + BOOST_CHECK (!ba::all_of_equal ( li, 1 )); + BOOST_CHECK (!ba::all_of ( li, is_ ( 1 ))); + BOOST_CHECK (!ba::all_of_equal ( li.begin(), li.end(), 1 )); + BOOST_CHECK (!ba::all_of ( li.begin(), li.end(), is_ ( 1 ))); + + std::list::iterator l_iter = li.begin (); + l_iter++; l_iter++; l_iter++; + BOOST_CHECK ( ba::all_of_equal ( li.begin(), l_iter, 1 )); + BOOST_CHECK ( ba::all_of ( li.begin(), l_iter, is_ ( 1 ))); + +} + + +int test_main( int , char* [] ) +{ + test_all (); + return 0; +} diff --git a/test/any_of_test.cpp b/test/any_of_test.cpp new file mode 100644 index 0000000..57f3fb2 --- /dev/null +++ b/test/any_of_test.cpp @@ -0,0 +1,105 @@ +/* + Copyright (c) Marshall Clow 2010-2012. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + + For more information, see http://www.boost.org +*/ + +#include +#include +#include + +#include +#include +#include + +template +struct is_ : public std::unary_function { + is_ ( T v ) : val_ ( v ) {} + ~is_ () {} + bool operator () ( T comp ) const { return val_ == comp; } +private: + is_ (); // need a value + + T val_; + }; + +namespace ba = boost::algorithm; + +void test_any () +{ +// Note: The literal values here are tested against directly, careful if you change them: + int some_numbers[] = { 1, 5, 0, 18, 10 }; + std::vector vi(some_numbers, some_numbers + 5); + std::list li(vi.begin(), vi.end ()); + + int some_letters[] = { 'a', 'q', 'n', 'y', 'n' }; + std::vector vc(some_letters, some_letters + 5); + + BOOST_CHECK ( ba::any_of_equal ( vi, 1 )); + BOOST_CHECK ( ba::any_of ( vi, is_ ( 1 ))); + BOOST_CHECK ( ba::any_of_equal ( vi.begin(), vi.end(), 1 )); + BOOST_CHECK ( ba::any_of ( vi.begin(), vi.end(), is_ ( 1 ))); + + BOOST_CHECK (!ba::any_of_equal ( vi, 9 )); + BOOST_CHECK (!ba::any_of ( vi, is_ ( 9 ))); + BOOST_CHECK (!ba::any_of_equal ( vi.begin(), vi.end(), 9 )); + BOOST_CHECK (!ba::any_of ( vi.begin(), vi.end(), is_ ( 9 ))); + + BOOST_CHECK ( ba::any_of_equal ( vi, 10 )); + BOOST_CHECK ( ba::any_of ( vi, is_ ( 10 ))); + BOOST_CHECK (!ba::any_of_equal ( vi, 4 )); + BOOST_CHECK (!ba::any_of ( vi, is_ ( 4 ))); + + BOOST_CHECK (!ba::any_of_equal ( vi.end(), vi.end(), 0 )); + BOOST_CHECK (!ba::any_of ( vi.end(), vi.end(), is_ ( 0 ))); + +// 5 is not in { 0, 18, 10 }, but 10 is + BOOST_CHECK ( ba::any_of_equal ( vi.begin() + 2, vi.end(), 10 )); + BOOST_CHECK ( ba::any_of ( vi.begin() + 2, vi.end(), is_ ( 10 ))); + + BOOST_CHECK (!ba::any_of_equal ( vi.begin() + 2, vi.end(), 5 )); + BOOST_CHECK (!ba::any_of ( vi.begin() + 2, vi.end(), is_ ( 5 ))); + +// 18 is not in { 1, 5, 0 }, but 5 is + BOOST_CHECK ( ba::any_of_equal ( vi.begin(), vi.begin() + 3, 5 )); + BOOST_CHECK ( ba::any_of ( vi.begin(), vi.begin() + 3, is_ ( 5 ))); + + BOOST_CHECK (!ba::any_of_equal ( vi.begin(), vi.begin() + 3, 18 )); + BOOST_CHECK (!ba::any_of ( vi.begin(), vi.begin() + 3, is_ ( 18 ))); + + BOOST_CHECK ( ba::any_of_equal ( vc, 'q' )); + BOOST_CHECK ( ba::any_of ( vc, is_ ( 'q' ))); + + BOOST_CHECK (!ba::any_of_equal ( vc, '!' )); + BOOST_CHECK (!ba::any_of ( vc, is_ ( '!' ))); + + BOOST_CHECK ( ba::any_of_equal ( vc, 'n' )); + BOOST_CHECK ( ba::any_of ( vc, is_ ( 'n' ))); + + BOOST_CHECK (!ba::any_of_equal ( vi.begin(), vi.begin(), 1 )); + BOOST_CHECK (!ba::any_of_equal ( vc.begin(), vc.begin(), 'a' )); + BOOST_CHECK (!ba::any_of ( vi.begin(), vi.begin(), is_ ( 1 ))); + BOOST_CHECK (!ba::any_of ( vc.begin(), vc.begin(), is_ ( 'a' ))); + + BOOST_CHECK ( ba::any_of_equal ( li, 1 )); + BOOST_CHECK ( ba::any_of ( li, is_ ( 1 ))); + BOOST_CHECK ( ba::any_of_equal ( li.begin(), li.end(), 1 )); + BOOST_CHECK ( ba::any_of ( li.begin(), li.end(), is_ ( 1 ))); + + std::list::iterator l_iter = li.begin (); + l_iter++; l_iter++; l_iter++; + BOOST_CHECK ( ba::any_of_equal ( li.begin(), l_iter, 5 )); + BOOST_CHECK ( ba::any_of ( li.begin(), l_iter, is_ ( 5 ))); + BOOST_CHECK (!ba::any_of_equal ( li.begin(), l_iter, 18 )); + BOOST_CHECK (!ba::any_of ( li.begin(), l_iter, is_ ( 18 ))); +} + + +int test_main( int , char* [] ) +{ + test_any (); + return 0; +} diff --git a/test/copy_n_test1.cpp b/test/copy_n_test1.cpp new file mode 100644 index 0000000..addf9a2 --- /dev/null +++ b/test/copy_n_test1.cpp @@ -0,0 +1,85 @@ +/* + Copyright (c) Marshall Clow 2011-2012. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + + For more information, see http://www.boost.org +*/ + +#include +#include +#include + +#include +#include +#include +#include + +namespace ba = boost::algorithm; +// namespace ba = boost; + +template +void test_sequence ( Container const &c ) { + + typedef typename Container::value_type value_type; + std::vector v; + +// Copy zero elements + v.clear (); + ba::copy_n ( c.begin (), 0, back_inserter ( v )); + BOOST_CHECK ( v.size () == 0 ); + ba::copy_n ( c.begin (), 0U, back_inserter ( v )); + BOOST_CHECK ( v.size () == 0 ); + + if ( c.size () > 0 ) { + // Just one element + v.clear (); + ba::copy_n ( c.begin (), 1, back_inserter ( v )); + BOOST_CHECK ( v.size () == 1 ); + BOOST_CHECK ( v[0] == *c.begin ()); + + v.clear (); + ba::copy_n ( c.begin (), 1U, back_inserter ( v )); + BOOST_CHECK ( v.size () == 1 ); + BOOST_CHECK ( v[0] == *c.begin ()); + + // Half the elements + v.clear (); + ba::copy_n ( c.begin (), c.size () / 2, back_inserter ( v )); + BOOST_CHECK ( v.size () == c.size () / 2); + BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ())); + + // Half the elements + 1 + v.clear (); + ba::copy_n ( c.begin (), c.size () / 2 + 1, back_inserter ( v )); + BOOST_CHECK ( v.size () == c.size () / 2 + 1 ); + BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ())); + + // All the elements + v.clear (); + ba::copy_n ( c.begin (), c.size (), back_inserter ( v )); + BOOST_CHECK ( v.size () == c.size ()); + BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ())); + } + } + + +void test_sequence1 () { + std::vector v; + for ( int i = 5; i < 15; ++i ) + v.push_back ( i ); + test_sequence ( v ); + + std::list l; + for ( int i = 25; i > 15; --i ) + l.push_back ( i ); + test_sequence ( l ); + } + + +int test_main( int , char* [] ) +{ + test_sequence1 (); + return 0; +} diff --git a/test/find_if_not_test1.cpp b/test/find_if_not_test1.cpp new file mode 100644 index 0000000..821cce9 --- /dev/null +++ b/test/find_if_not_test1.cpp @@ -0,0 +1,90 @@ +/* + Copyright (c) Marshall Clow 2011-2012. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + + For more information, see http://www.boost.org +*/ + +#include + +#include +#include +#include + +#include +#include +#include + +namespace ba = boost::algorithm; +// namespace ba = boost; + +template +typename Container::iterator offset_to_iter ( Container &v, int offset ) { + typename Container::iterator retval; + + if ( offset >= 0 ) { + retval = v.begin (); + std::advance ( retval, offset ); + } + else { + retval = v.end (); + std::advance ( retval, offset + 1 ); + } + return retval; + } + +template +void test_sequence ( Container &v, Predicate comp, int expected ) { + typename Container::iterator res, exp; + + res = ba::find_if_not ( v.begin (), v.end (), comp ); + exp = offset_to_iter ( v, expected ); + std::cout << "Expected(1): " << std::distance ( v.begin (), exp ) + << ", got: " << std::distance ( v.begin (), res ) << std::endl; + BOOST_CHECK ( exp == res ); + } + +template +struct less_than { +public: + less_than ( T foo ) : val ( foo ) {} + less_than ( const less_than &rhs ) : val ( rhs.val ) {} + + bool operator () ( const T &v ) const { return v < val; } +private: + less_than (); + less_than operator = ( const less_than &rhs ); + T val; + }; + + +void test_sequence1 () { + std::vector v; + + v.clear (); + for ( int i = 5; i < 15; ++i ) + v.push_back ( i ); + test_sequence ( v, less_than(3), 0 ); // no elements + test_sequence ( v, less_than(6), 1 ); // only the first element + test_sequence ( v, less_than(10), 5 ); + test_sequence ( v, less_than(99), -1 ); // all elements satisfy + +// With bidirectional iterators. + std::list l; + for ( int i = 5; i < 15; ++i ) + l.push_back ( i ); + test_sequence ( l, less_than(3), 0 ); // no elements + test_sequence ( l, less_than(6), 1 ); // only the first element + test_sequence ( l, less_than(10), 5 ); + test_sequence ( l, less_than(99), -1 ); // all elements satisfy + + } + + +int test_main( int , char* [] ) +{ + test_sequence1 (); + return 0; +} diff --git a/test/hex_fail1.cpp b/test/hex_fail1.cpp new file mode 100644 index 0000000..7192245 --- /dev/null +++ b/test/hex_fail1.cpp @@ -0,0 +1,25 @@ +/* + Copyright (c) Marshall Clow 2011-2012. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + + For more information, see http://www.boost.org +*/ + +#include +#include +#include + +#include +#include +#include + +// should not compile: vector is not an integral type +int test_main( int , char* [] ) +{ + std::vector v; + std::string out; + boost::algorithm::unhex ( out, std::back_inserter(v)); + return 0; +} diff --git a/test/hex_test1.cpp b/test/hex_test1.cpp new file mode 100644 index 0000000..891afe4 --- /dev/null +++ b/test/hex_test1.cpp @@ -0,0 +1,154 @@ +/* + Copyright (c) Marshall Clow 2011-2012. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + + For more information, see http://www.boost.org +*/ + +#include +#include +#include + +#include +#include + + +template +void test_to_hex ( const typename String::value_type ** tests ) { + for ( const typename String::value_type **p = tests; *p; p++ ) { + String arg, argh, one, two, three, four; + arg.assign ( *p ); + boost::algorithm::hex ( *p, std::back_inserter ( one )); + boost::algorithm::hex ( arg, std::back_inserter ( two )); + boost::algorithm::hex ( arg.begin (), arg.end (), std::back_inserter ( three )); + four = boost::algorithm::hex ( arg ); + BOOST_CHECK ( one == two ); + BOOST_CHECK ( one == three ); + BOOST_CHECK ( one == four ); + argh = one; + one.clear (); two.clear (); three.clear (); four.clear (); + boost::algorithm::unhex ( argh.c_str (), std::back_inserter ( one )); + boost::algorithm::unhex ( argh, std::back_inserter ( two )); + boost::algorithm::unhex ( argh.begin (), argh.end (), std::back_inserter ( three )); + four = boost::algorithm::unhex ( argh ); + BOOST_CHECK ( one == two ); + BOOST_CHECK ( one == three ); + BOOST_CHECK ( one == four ); + BOOST_CHECK ( one == arg ); + } + } + + +template +void test_from_hex_success ( const typename String::value_type ** tests ) { + for ( const typename String::value_type **p = tests; *p; p++ ) { + String arg, argh, one, two, three, four; + arg.assign ( *p ); + boost::algorithm::unhex ( *p, std::back_inserter ( one )); + boost::algorithm::unhex ( arg, std::back_inserter ( two )); + boost::algorithm::unhex ( arg.begin (), arg.end (), std::back_inserter ( three )); + four = boost::algorithm::unhex ( arg ); + BOOST_CHECK ( one == two ); + BOOST_CHECK ( one == three ); + BOOST_CHECK ( one == four ); + argh = one; + one.clear (); two.clear (); three.clear (); four.clear (); + boost::algorithm::hex ( argh.c_str (), std::back_inserter ( one )); + boost::algorithm::hex ( argh, std::back_inserter ( two )); + boost::algorithm::hex ( argh.begin (), argh.end (), std::back_inserter ( three )); + four = boost::algorithm::hex ( argh ); + BOOST_CHECK ( one == two ); + BOOST_CHECK ( one == three ); + BOOST_CHECK ( one == four ); + BOOST_CHECK ( one == arg ); + } + } + +template +void test_from_hex_failure ( const typename String::value_type ** tests ) { + int num_catches; + for ( const typename String::value_type **p = tests; *p; p++ ) { + String arg, one; + arg.assign ( *p ); + num_catches = 0; + + try { boost::algorithm::unhex ( *p, std::back_inserter ( one )); } + catch ( const boost::algorithm::hex_decode_error & /*ex*/ ) { num_catches++; } + try { boost::algorithm::unhex ( arg, std::back_inserter ( one )); } + catch ( const boost::algorithm::hex_decode_error & /*ex*/ ) { num_catches++; } + try { boost::algorithm::unhex ( arg.begin (), arg.end (), std::back_inserter ( one )); } + catch ( const boost::algorithm::hex_decode_error & /*ex*/ ) { num_catches++; } + BOOST_CHECK ( num_catches == 3 ); + } + } + + + +const char *tohex [] = { + "", + "a", + "\001", + "12", + "asdfadsfsad", + "01234567890ABCDEF", + NULL // End of the list + }; + + +const wchar_t *tohex_w [] = { + L"", + L"a", + L"\001", + L"12", + L"asdfadsfsad", + L"01234567890ABCDEF", + NULL // End of the list + }; + + +const char *fromhex [] = { + "20", + "2122234556FF", + NULL // End of the list + }; + + +const wchar_t *fromhex_w [] = { + L"00101020", + L"2122234556FF3456", + NULL // End of the list + }; + + +const char *fromhex_fail [] = { + "2", + "H", + "234", + "21222G4556FF", + NULL // End of the list + }; + + +const wchar_t *fromhex_fail_w [] = { + L"2", + L"12", + L"H", + L"234", + L"21222G4556FF", + NULL // End of the list + }; + + +int test_main( int , char* [] ) +{ + test_to_hex ( tohex ); + test_from_hex_success ( fromhex ); + test_from_hex_failure ( fromhex_fail ); + + test_to_hex ( tohex_w ); + test_from_hex_success ( fromhex_w ); + test_from_hex_failure ( fromhex_fail_w ); + return 0; +} diff --git a/test/hex_test2.cpp b/test/hex_test2.cpp new file mode 100644 index 0000000..7092154 --- /dev/null +++ b/test/hex_test2.cpp @@ -0,0 +1,137 @@ +/* + Copyright (c) Marshall Clow 2011-2012. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + + For more information, see http://www.boost.org + +Test non-string cases; vector and list +*/ + +#include +#include +#include + +#include +#include +#include +#include + + +const char *tohex [] = { + "", + "a", + "\001", + "12", + "asdfadsfsad", + "01234567890ABCDEF", + NULL // End of the list + }; + +void test_to_hex () { + for ( const char **p = tohex; *p; p++ ) { + std::deque arg, argh; + std::list one, two, three; + arg.assign ( *p, *p + strlen (*p)); + boost::algorithm::hex ( *p, std::back_inserter ( one )); + boost::algorithm::hex ( arg, std::back_inserter ( two )); + boost::algorithm::hex ( arg.begin (), arg.end (), std::back_inserter ( three )); + BOOST_CHECK ( std::equal ( one.begin (), one.end (), two.begin ())); + BOOST_CHECK ( std::equal ( two.begin (), two.end (), three.begin ())); + + std::copy ( one.begin (), one.end (), std::back_inserter ( argh )); + one.clear (); two.clear (); three.clear (); + +// boost::algorithm::unhex ( argh.c_str (), std::back_inserter ( one )); + boost::algorithm::unhex ( argh, std::back_inserter ( two )); + boost::algorithm::unhex ( argh.begin (), argh.end (), std::back_inserter ( three )); +// BOOST_CHECK ( std::equal ( one.begin (), one.end (), two.begin ())); + BOOST_CHECK ( std::equal ( two.begin (), two.end (), three.begin ())); + BOOST_CHECK ( std::equal ( two.begin (), two.end (), arg.begin ())); + } + +// Again, with a front_inserter + for ( const char **p = tohex; *p; p++ ) { + std::deque arg, argh; + std::list one, two, three; + arg.assign ( *p, *p + strlen (*p)); + boost::algorithm::hex ( *p, std::front_inserter ( one )); + boost::algorithm::hex ( arg, std::front_inserter ( two )); + boost::algorithm::hex ( arg.begin (), arg.end (), std::front_inserter ( three )); + BOOST_CHECK ( std::equal ( one.begin (), one.end (), two.begin ())); + BOOST_CHECK ( std::equal ( two.begin (), two.end (), three.begin ())); + + // Copy, reversing + std::copy ( one.begin (), one.end (), std::front_inserter ( argh )); + one.clear (); two.clear (); three.clear (); + +// boost::algorithm::unhex ( argh.c_str (), std::front_inserter ( one )); + boost::algorithm::unhex ( argh, std::front_inserter ( two )); + boost::algorithm::unhex ( argh.begin (), argh.end (), std::front_inserter ( three )); +// BOOST_CHECK ( std::equal ( one.begin (), one.end (), two.begin ())); + BOOST_CHECK ( std::equal ( two.begin (), two.end (), three.begin ())); + BOOST_CHECK ( std::equal ( two.begin (), two.end (), arg.rbegin ())); // reverse + } + } + +const char *fromhex [] = { + "20", + "2122234556FF", + NULL // End of the list + }; + + +void test_from_hex_success () { + for ( const char **p = fromhex; *p; p++ ) { + std::deque arg, argh; + std::list one, two, three; + arg.assign ( *p, *p + strlen (*p)); + boost::algorithm::unhex ( *p, std::back_inserter ( one )); + boost::algorithm::unhex ( arg, std::back_inserter ( two )); + boost::algorithm::unhex ( arg.begin (), arg.end (), std::back_inserter ( three )); + BOOST_CHECK ( std::equal ( one.begin (), one.end (), two.begin ())); + BOOST_CHECK ( std::equal ( two.begin (), two.end (), three.begin ())); + + std::copy ( one.begin (), one.end (), std::back_inserter ( argh )); + one.clear (); two.clear (); three.clear (); + +// boost::algorithm::hex ( argh.c_str (), std::back_inserter ( one )); + boost::algorithm::hex ( argh, std::back_inserter ( two )); + boost::algorithm::hex ( argh.begin (), argh.end (), std::back_inserter ( three )); +// BOOST_CHECK ( std::equal ( one.begin (), one.end (), two.begin ())); + BOOST_CHECK ( std::equal ( two.begin (), two.end (), three.begin ())); + BOOST_CHECK ( std::equal ( two.begin (), two.end (), arg.begin ())); + } + +// Again, with a front_inserter + for ( const char **p = fromhex; *p; p++ ) { + std::deque arg, argh; + std::list one, two, three; + arg.assign ( *p, *p + strlen (*p)); + boost::algorithm::unhex ( *p, std::front_inserter ( one )); + boost::algorithm::unhex ( arg, std::front_inserter ( two )); + boost::algorithm::unhex ( arg.begin (), arg.end (), std::front_inserter ( three )); + BOOST_CHECK ( std::equal ( one.begin (), one.end (), two.begin ())); + BOOST_CHECK ( std::equal ( two.begin (), two.end (), three.begin ())); + + // Copy, reversing + std::copy ( one.begin (), one.end (), std::front_inserter ( argh )); + one.clear (); two.clear (); three.clear (); + +// boost::algorithm::hex ( argh.c_str (), std::front_inserter ( one )); + boost::algorithm::hex ( argh, std::front_inserter ( two )); + boost::algorithm::hex ( argh.begin (), argh.end (), std::front_inserter ( three )); +// BOOST_CHECK ( std::equal ( one.begin (), one.end (), two.begin ())); + BOOST_CHECK ( std::equal ( two.begin (), two.end (), three.begin ())); + BOOST_CHECK ( std::equal ( two.begin (), two.end (), arg.rbegin ())); // reversed + } + } + + +int test_main( int , char* [] ) +{ + test_to_hex (); + test_from_hex_success (); + return 0; +} diff --git a/test/hex_test3.cpp b/test/hex_test3.cpp new file mode 100644 index 0000000..cb91d95 --- /dev/null +++ b/test/hex_test3.cpp @@ -0,0 +1,123 @@ +/* + Copyright (c) Marshall Clow 2011-2012. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + + For more information, see http://www.boost.org + +Try ostream_iterators +*/ + +#include +#include +#include + +#include +#include +#include +#include + + +template +void test_to_hex ( const char_type ** tests ) { + typedef std::basic_string String; + typedef std::basic_ostringstream Stream; + typedef std::ostream_iterator Iter; + + for ( const char_type **p = tests; *p; p++ ) { + String arg, argh; + Stream one, two, three; + arg.assign ( *p ); + boost::algorithm::hex ( *p, Iter ( one )); + boost::algorithm::hex ( arg, Iter ( two )); + boost::algorithm::hex ( arg.begin (), arg.end (), Iter ( three )); + boost::algorithm::hex ( arg ); + BOOST_CHECK ( one.str () == two.str ()); + BOOST_CHECK ( one.str () == three.str ()); + argh = one.str (); + one.str (String()); two.str (String()); three.str (String()); + boost::algorithm::unhex ( argh.c_str (), Iter ( one )); + boost::algorithm::unhex ( argh, Iter ( two )); + boost::algorithm::unhex ( argh.begin (), argh.end (), Iter ( three )); + BOOST_CHECK ( one.str () == two.str ()); + BOOST_CHECK ( one.str () == three.str ()); + BOOST_CHECK ( one.str () == arg ); + } + } + + +template +void test_from_hex_success ( const char_type ** tests ) { + typedef std::basic_string String; + typedef std::basic_ostringstream Stream; + typedef std::ostream_iterator Iter; + + for ( const char_type **p = tests; *p; p++ ) { + String arg, argh; + Stream one, two, three; + arg.assign ( *p ); + boost::algorithm::unhex ( *p, Iter ( one )); + boost::algorithm::unhex ( arg, Iter ( two )); + boost::algorithm::unhex ( arg.begin (), arg.end (), Iter ( three )); + + BOOST_CHECK ( one.str () == two.str ()); + BOOST_CHECK ( one.str () == three.str ()); + + argh = one.str (); + one.str (String()); two.str (String()); three.str (String()); + + boost::algorithm::hex ( argh.c_str (), Iter ( one )); + boost::algorithm::hex ( argh, Iter ( two )); + boost::algorithm::hex ( argh.begin (), argh.end (), Iter ( three )); + + BOOST_CHECK ( one.str () == two.str ()); + BOOST_CHECK ( one.str () == three.str ()); + BOOST_CHECK ( one.str () == arg ); + } + + } + +const char *tohex [] = { + "", + "a", + "\001", + "12", + "asdfadsfsad", + "01234567890ABCDEF", + NULL // End of the list + }; + +const wchar_t *tohex_w [] = { + L"", + L"a", + L"\001", + L"12", + L"asdfadsfsad", + L"01234567890ABCDEF", + NULL // End of the list + }; + + +const char *fromhex [] = { + "20", + "2122234556FF", + NULL // End of the list + }; + +const wchar_t *fromhex_w [] = { + L"11223320", + L"21222345010256FF", + NULL // End of the list + }; + + + +int test_main( int , char* [] ) +{ + test_to_hex ( tohex ); + test_to_hex ( tohex_w ); + test_from_hex_success ( fromhex ); + test_from_hex_success ( fromhex_w ); + return 0; +} diff --git a/test/iota_test1.cpp b/test/iota_test1.cpp new file mode 100644 index 0000000..c567d1b --- /dev/null +++ b/test/iota_test1.cpp @@ -0,0 +1,79 @@ +/* + Copyright (c) Marshall Clow 2011-2012. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + + For more information, see http://www.boost.org +*/ + +#include +#include +#include + +#include +#include +#include +#include + +// Test to make sure a sequence is "correctly formed"; i.e, ascending by one +template +bool test_iota_results ( Iterator first, Iterator last, T initial_value ) { + if ( first == last ) return true; + if ( initial_value != *first ) return false; + Iterator prev = first; + while ( ++first != last ) { + if (( *first - *prev ) != 1 ) + return false; + prev = first; + } + return true; + } + +template +bool test_iota_results ( const Range &r, T initial_value ) { + return test_iota_results (boost::begin (r), boost::end (r), initial_value ); +} + + +void test_ints () { + std::vector v; + std::list l; + + v.clear (); v.reserve ( 10 ); + boost::algorithm::iota ( v.begin (), v.end (), 23 ); + BOOST_CHECK ( test_iota_results ( v.begin (), v.end (), 23 )); + + v.clear (); v.reserve ( 19 ); + boost::algorithm::iota ( v, 18 ); + BOOST_CHECK ( test_iota_results ( v, 18 )); + + v.clear (); + boost::algorithm::iota_n ( std::back_inserter(v), 99, 20 ); + BOOST_CHECK ( test_iota_results ( v, 99 )); + +/* + l.clear (); l.reserve ( 5 ); + boost::algorithm::iota ( l.begin (), l.end (), 123 ); + BOOST_CHECK ( test_iota_results ( l.begin (), l.end (), 123 )); + + l.clear (); l.reserve ( 9 ); + boost::algorithm::iota ( l.begin (), l.end (), 87 ); + BOOST_CHECK ( test_iota_results ( l.begin (), l.end (), 87 )); +*/ + + l.clear (); + boost::algorithm::iota_n ( std::back_inserter(l), 99, 20 ); + BOOST_CHECK ( test_iota_results ( l, 99 )); + + l.clear (); + boost::algorithm::iota_n ( std::front_inserter(l), 123, 20 ); + BOOST_CHECK ( test_iota_results ( l.rbegin (), l.rend (), 123 )); + } + + +int test_main( int , char* [] ) +{ + test_ints (); + return 0; +} diff --git a/test/is_partitioned_test1.cpp b/test/is_partitioned_test1.cpp new file mode 100644 index 0000000..435ca5a --- /dev/null +++ b/test/is_partitioned_test1.cpp @@ -0,0 +1,63 @@ +/* + Copyright (c) Marshall Clow 2011-2012. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + + For more information, see http://www.boost.org +*/ + +#include + +#include +#include +#include + +#include +#include +#include + +namespace ba = boost::algorithm; +// namespace ba = boost; + +template +struct less_than { +public: + less_than ( T foo ) : val ( foo ) {} + less_than ( const less_than &rhs ) : val ( rhs.val ) {} + + bool operator () ( const T &v ) const { return v < val; } +private: + less_than (); + less_than operator = ( const less_than &rhs ); + T val; + }; + + +void test_sequence1 () { + std::vector v; + + v.clear (); + for ( int i = 5; i < 15; ++i ) + v.push_back ( i ); + BOOST_CHECK ( ba::is_partitioned ( v, less_than(3))); // no elements + BOOST_CHECK ( ba::is_partitioned ( v, less_than(6))); // only the first element + BOOST_CHECK ( ba::is_partitioned ( v, less_than(10))); // in the middle somewhere + BOOST_CHECK ( ba::is_partitioned ( v, less_than(99))); // all elements satisfy + +// With bidirectional iterators. + std::list l; + for ( int i = 5; i < 15; ++i ) + l.push_back ( i ); + BOOST_CHECK ( ba::is_partitioned ( l.begin (), l.end (), less_than(3))); // no elements + BOOST_CHECK ( ba::is_partitioned ( l.begin (), l.end (), less_than(6))); // only the first element + BOOST_CHECK ( ba::is_partitioned ( l.begin (), l.end (), less_than(10))); // in the middle somewhere + BOOST_CHECK ( ba::is_partitioned ( l.begin (), l.end (), less_than(99))); // all elements satisfy + } + + +int test_main( int , char* [] ) +{ + test_sequence1 (); + return 0; +} diff --git a/test/is_permutation_test1.cpp b/test/is_permutation_test1.cpp new file mode 100644 index 0000000..f9c3b1b --- /dev/null +++ b/test/is_permutation_test1.cpp @@ -0,0 +1,49 @@ +/* + Copyright (c) Marshall Clow 2011-2012. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + + For more information, see http://www.boost.org +*/ + +#include + +#include +#include +#include + +#include +#include +#include + +namespace ba = boost::algorithm; +// namespace ba = boost; + +void test_sequence1 () { + std::vector v, v1; + + v.clear (); + for ( std::size_t i = 5; i < 15; ++i ) + v.push_back ( i ); + v1 = v; + BOOST_CHECK ( ba::is_permutation ( v.begin (), v.end (), v.begin ())); // better be a permutation of itself! + BOOST_CHECK ( ba::is_permutation ( v.begin (), v.end (), v1.begin ())); + +// With bidirectional iterators. + std::list l; + std::copy ( v.begin (), v.end (), std::back_inserter ( l )); + BOOST_CHECK ( ba::is_permutation ( l.begin (), l.end (), l.begin ())); // better be a permutation of itself! + BOOST_CHECK ( ba::is_permutation ( l.begin (), l.end (), v1.begin ())); + for ( std::size_t i = 0; i < l.size (); ++i ) { + l.push_back ( *l.begin ()); l.pop_front (); // rotation + BOOST_CHECK ( ba::is_permutation ( l.begin (), l.end (), v1.begin ())); + } + } + + +int test_main( int , char* [] ) +{ + test_sequence1 (); + return 0; +} diff --git a/test/none_of_test.cpp b/test/none_of_test.cpp new file mode 100644 index 0000000..ab7ded3 --- /dev/null +++ b/test/none_of_test.cpp @@ -0,0 +1,96 @@ +/* + Copyright (c) Marshall Clow 2010-2012. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + + For more information, see http://www.boost.org +*/ + +#include +#include +#include + +#include +#include +#include + +template +struct is_ : public std::unary_function { + is_ ( T v ) : val_ ( v ) {} + ~is_ () {} + bool operator () ( T comp ) const { return val_ == comp; } +private: + is_ (); // need a value + + T val_; + }; + +namespace ba = boost::algorithm; + +void test_none() +{ +// Note: The literal values here are tested against directly, careful if you change them: + int some_numbers[] = { 1, 5, 0, 18, 1 }; + std::vector vi(some_numbers, some_numbers + 5); + std::list li(vi.begin(), vi.end ()); + + int some_letters[] = { 'a', 'q', 'n', 'y', 'n' }; + std::vector vc(some_letters, some_letters + 5); + + BOOST_CHECK ( ba::none_of_equal ( vi, 100 )); + BOOST_CHECK ( ba::none_of ( vi, is_ ( 100 ))); + BOOST_CHECK ( ba::none_of_equal ( vi.begin(), vi.end(), 100 )); + BOOST_CHECK ( ba::none_of ( vi.begin(), vi.end(), is_ ( 100 ))); + + BOOST_CHECK (!ba::none_of_equal ( vi, 1 )); + BOOST_CHECK (!ba::none_of ( vi, is_ ( 1 ))); + BOOST_CHECK (!ba::none_of_equal ( vi.begin(), vi.end(), 1 )); + BOOST_CHECK (!ba::none_of ( vi.begin(), vi.end(), is_ ( 1 ))); + + BOOST_CHECK ( ba::none_of_equal ( vi.end(), vi.end(), 0 )); + BOOST_CHECK ( ba::none_of ( vi.end(), vi.end(), is_ ( 0 ))); + +// 5 is not in { 0, 18, 1 }, but 1 is + BOOST_CHECK ( ba::none_of_equal ( vi.begin() + 2, vi.end(), 5 )); + BOOST_CHECK ( ba::none_of ( vi.begin() + 2, vi.end(), is_ ( 5 ))); + BOOST_CHECK (!ba::none_of_equal ( vi.begin() + 2, vi.end(), 1 )); + BOOST_CHECK (!ba::none_of ( vi.begin() + 2, vi.end(), is_ ( 1 ))); + +// 18 is not in { 1, 5, 0 }, but 5 is + BOOST_CHECK ( ba::none_of_equal ( vi.begin(), vi.begin() + 3, 18 )); + BOOST_CHECK ( ba::none_of ( vi.begin(), vi.begin() + 3, is_ ( 18 ))); + BOOST_CHECK (!ba::none_of_equal ( vi.begin(), vi.begin() + 3, 5 )); + BOOST_CHECK (!ba::none_of ( vi.begin(), vi.begin() + 3, is_ ( 5 ))); + + BOOST_CHECK ( ba::none_of_equal ( vc, 'z' )); + BOOST_CHECK ( ba::none_of ( vc, is_ ( 'z' ))); + + BOOST_CHECK (!ba::none_of_equal ( vc, 'a' )); + BOOST_CHECK (!ba::none_of ( vc, is_ ( 'a' ))); + + BOOST_CHECK (!ba::none_of_equal ( vc, 'n' )); + BOOST_CHECK (!ba::none_of ( vc, is_ ( 'n' ))); + + BOOST_CHECK ( ba::none_of_equal ( vi.begin(), vi.begin(), 1 )); + BOOST_CHECK ( ba::none_of_equal ( vc.begin(), vc.begin(), 'a' )); + BOOST_CHECK ( ba::none_of ( vi.begin(), vi.begin(), is_ ( 1 ))); + BOOST_CHECK ( ba::none_of ( vc.begin(), vc.begin(), is_ ( 'a' ))); + + BOOST_CHECK ( ba::none_of_equal ( li, 100 )); + BOOST_CHECK ( ba::none_of ( li, is_ ( 100 ))); + BOOST_CHECK ( ba::none_of_equal ( li.begin(), li.end(), 100 )); + BOOST_CHECK ( ba::none_of ( li.begin(), li.end(), is_ ( 100 ))); + + std::list::iterator l_iter = li.begin (); + l_iter++; l_iter++; l_iter++; + BOOST_CHECK ( ba::none_of_equal ( li.begin(), l_iter, 18 )); + BOOST_CHECK ( ba::none_of ( li.begin(), l_iter, is_ ( 18 ))); + BOOST_CHECK (!ba::none_of ( li.begin(), l_iter, is_ ( 5 ))); +} + +int test_main( int , char* [] ) +{ + test_none(); + return 0; +} diff --git a/test/one_of_test.cpp b/test/one_of_test.cpp new file mode 100644 index 0000000..0b3f5e0 --- /dev/null +++ b/test/one_of_test.cpp @@ -0,0 +1,101 @@ +/* + Copyright (c) Marshall Clow 2008-2012. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + + For more information, see http://www.boost.org +*/ + +#include +#include +#include + +#include +#include +#include + +template +struct is_ : public std::unary_function { + is_ ( T v ) : val_ ( v ) {} + ~is_ () {} + bool operator () ( T comp ) const { return val_ == comp; } +private: + is_ (); // need a value + + T val_; + }; + +namespace ba = boost::algorithm; + +void test_one () +{ +// Note: The literal values here are tested against directly, careful if you change them: + int some_numbers[] = { 1, 1, 2, 3, 5 }; + std::vector vi(some_numbers, some_numbers + 5); + std::list li(vi.begin(), vi.end ()); + + int some_letters[] = { 'a', 'q', 'n', 'y', 'n' }; + std::vector vc(some_letters, some_letters + 5); + + BOOST_CHECK (!ba::one_of_equal ( vi, 1 )); + BOOST_CHECK (!ba::one_of ( vi, is_ ( 1 ))); + BOOST_CHECK (!ba::one_of_equal ( vi.begin(), vi.end(), 1 )); + BOOST_CHECK (!ba::one_of ( vi.begin(), vi.end(), is_ ( 1 ))); + + BOOST_CHECK (!ba::one_of_equal ( vi, 0 )); + BOOST_CHECK (!ba::one_of ( vi, is_ ( 0 ))); + BOOST_CHECK (!ba::one_of_equal ( vi.begin(), vi.end(), 0 )); + BOOST_CHECK (!ba::one_of ( vi.begin(), vi.end(), is_ ( 0 ))); + + BOOST_CHECK ( ba::one_of_equal ( vi, 2 )); + BOOST_CHECK ( ba::one_of ( vi, is_ ( 2 ))); + BOOST_CHECK ( ba::one_of_equal ( vi.begin(), vi.end(), 2 )); + BOOST_CHECK ( ba::one_of ( vi.begin(), vi.end(), is_ ( 2 ))); + +// Check for a match at the end + BOOST_CHECK ( ba::one_of_equal ( vi, 5 )); + BOOST_CHECK ( ba::one_of ( vi, is_ ( 5 ))); + BOOST_CHECK ( ba::one_of_equal ( vi.begin(), vi.end(), 5 )); + BOOST_CHECK ( ba::one_of ( vi.begin(), vi.end(), is_ ( 5 ))); + + BOOST_CHECK ( ba::one_of_equal ( vi.begin() + 1, vi.end(), 1 )); + BOOST_CHECK ( ba::one_of ( vi.begin() + 1, vi.end(), is_ ( 1 ))); + + BOOST_CHECK ( ba::one_of_equal ( vc.begin() + 1, vc.begin() + 2, 'q' )); + BOOST_CHECK ( ba::one_of ( vc.begin() + 1, vc.begin() + 2, is_ ( 'q' ))); + + BOOST_CHECK (!ba::one_of_equal ( vc, '!' )); + BOOST_CHECK (!ba::one_of ( vc, is_ ( '!' ))); + + BOOST_CHECK (!ba::one_of_equal ( vc, 'n' )); + BOOST_CHECK (!ba::one_of ( vc, is_ ( 'n' ))); + +// Empty range check + BOOST_CHECK (!ba::one_of_equal ( vi.begin(), vi.begin(), 1 )); + BOOST_CHECK (!ba::one_of_equal ( vc.begin(), vc.begin(), 'a' )); + BOOST_CHECK (!ba::one_of ( vi.begin(), vi.begin(), is_ ( 1 ))); + BOOST_CHECK (!ba::one_of ( vc.begin(), vc.begin(), is_ ( 'a' ))); + + BOOST_CHECK (!ba::one_of_equal ( li, 1 )); + BOOST_CHECK (!ba::one_of ( li, is_ ( 1 ))); + BOOST_CHECK (!ba::one_of_equal ( li.begin(), li.end(), 1 )); + BOOST_CHECK (!ba::one_of ( li.begin(), li.end(), is_ ( 1 ))); + + std::list::iterator l_iter = li.begin (); + l_iter++; l_iter++; l_iter++; + BOOST_CHECK (!ba::one_of_equal ( li.begin(), l_iter, 1 )); + BOOST_CHECK (!ba::one_of ( li.begin(), l_iter, is_ ( 1 ))); + BOOST_CHECK ( ba::one_of_equal ( li.begin(), l_iter, 2 )); + BOOST_CHECK ( ba::one_of ( li.begin(), l_iter, is_ ( 2 ))); + BOOST_CHECK (!ba::one_of_equal ( li.begin(), l_iter, 3 )); + BOOST_CHECK (!ba::one_of ( li.begin(), l_iter, is_ ( 3 ))); + +} + + +int test_main( int , char* [] ) +{ + test_one (); + return 0; +} diff --git a/test/ordered_test.cpp b/test/ordered_test.cpp new file mode 100644 index 0000000..3278955 --- /dev/null +++ b/test/ordered_test.cpp @@ -0,0 +1,154 @@ +// Copyright (c) 2010 Nuovation System Designs, LLC +// Grant Erickson +// +// Reworked by Marshall Clow; August 2010 +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/ for latest version. + +#include +#include + +#include +#include + +using namespace boost; + +/* Preprocessor Defines */ + +#define elementsof(v) (sizeof (v) / sizeof (v[0])) +#define a_begin(v) (&v[0]) +#define a_end(v) (v + elementsof (v)) +#define a_range(v) v +#define b_e(v) a_begin(v),a_end(v) + +namespace ba = boost::algorithm; + +static void +test_ordered(void) +{ + const int strictlyIncreasingValues[] = { 1, 2, 3, 4, 5 }; + const int randomValues[] = { 3, 6, 1, 2, 7 }; + const int constantValues[] = { 1, 2, 2, 2, 5 }; + int nonConstantArray[] = { 1, 2, 2, 2, 5 }; + const int inOrderUntilTheEnd [] = { 0, 1, 2, 3, 4, 5, 6, 7, 6 }; + +// Begin/end checks + BOOST_CHECK ( ba::is_sorted (b_e(strictlyIncreasingValues))); + BOOST_CHECK ( !ba::is_sorted (b_e(randomValues))); + BOOST_CHECK ( ba::is_sorted (b_e(strictlyIncreasingValues), std::less())); + BOOST_CHECK ( !ba::is_sorted (b_e(strictlyIncreasingValues), std::greater())); + +// Range checks + BOOST_CHECK ( ba::is_sorted (a_range(strictlyIncreasingValues))); + BOOST_CHECK ( !ba::is_sorted (a_range(randomValues))); + BOOST_CHECK ( ba::is_sorted (a_range(strictlyIncreasingValues), std::less())); + BOOST_CHECK ( !ba::is_sorted (a_range(strictlyIncreasingValues), std::greater())); + + BOOST_CHECK ( ba::is_sorted_until ( b_e(strictlyIncreasingValues)) == a_end(strictlyIncreasingValues)); + BOOST_CHECK ( ba::is_sorted_until ( b_e(strictlyIncreasingValues), std::less()) == a_end(strictlyIncreasingValues)); + BOOST_CHECK ( ba::is_sorted_until ( a_range(strictlyIncreasingValues)) == boost::end(strictlyIncreasingValues)); + BOOST_CHECK ( ba::is_sorted_until ( a_range(strictlyIncreasingValues), std::less()) == boost::end(strictlyIncreasingValues)); + +// Check for const and non-const arrays + BOOST_CHECK ( ba::is_sorted_until ( b_e(constantValues), std::less()) != a_end(constantValues)); + BOOST_CHECK ( ba::is_sorted_until ( a_range(constantValues), std::less()) != boost::end(constantValues)); + BOOST_CHECK ( ba::is_sorted_until ( b_e(nonConstantArray), std::less()) != a_end(nonConstantArray)); + BOOST_CHECK ( ba::is_sorted_until ( a_range(nonConstantArray), std::less()) != boost::end(nonConstantArray)); + + BOOST_CHECK ( ba::is_sorted_until ( b_e(randomValues), std::less()) == &randomValues[2] ); + BOOST_CHECK ( ba::is_sorted_until ( b_e(randomValues)) == &randomValues[2] ); + BOOST_CHECK ( ba::is_sorted_until ( a_range(randomValues), std::less()) == &randomValues[2] ); + BOOST_CHECK ( ba::is_sorted_until ( a_range(randomValues)) == &randomValues[2] ); + + BOOST_CHECK ( ba::is_sorted_until ( a_range(inOrderUntilTheEnd), std::less()) == &inOrderUntilTheEnd[8] ); + BOOST_CHECK ( ba::is_sorted_until ( a_range(inOrderUntilTheEnd)) == &inOrderUntilTheEnd[8] ); + +// For zero and one element collections, the comparison predicate should never be called + BOOST_CHECK ( ba::is_sorted_until ( a_begin(randomValues), a_begin(randomValues), std::equal_to()) == a_begin(randomValues)); + BOOST_CHECK ( ba::is_sorted_until ( a_begin(randomValues), a_begin(randomValues)) == a_begin(randomValues)); + BOOST_CHECK ( ba::is_sorted_until ( a_begin(randomValues), a_begin(randomValues) + 1, std::equal_to()) == a_begin(randomValues) + 1); + BOOST_CHECK ( ba::is_sorted_until ( a_begin(randomValues), a_begin(randomValues) + 1 ) == a_begin(randomValues) + 1); +} + + +static void +test_increasing_decreasing(void) +{ + const int strictlyIncreasingValues[] = { 1, 2, 3, 4, 5 }; + const int strictlyDecreasingValues[] = { 9, 8, 7, 6, 5 }; + const int increasingValues[] = { 1, 2, 2, 2, 5 }; + const int decreasingValues[] = { 9, 7, 7, 7, 5 }; + const int randomValues[] = { 3, 6, 1, 2, 7 }; + const int constantValues[] = { 7, 7, 7, 7, 7 }; + + // Test a strictly increasing sequence + BOOST_CHECK ( ba::is_strictly_increasing (b_e(strictlyIncreasingValues))); + BOOST_CHECK ( ba::is_increasing (b_e(strictlyIncreasingValues))); + BOOST_CHECK ( !ba::is_strictly_decreasing (b_e(strictlyIncreasingValues))); + BOOST_CHECK ( !ba::is_decreasing (b_e(strictlyIncreasingValues))); + + BOOST_CHECK ( ba::is_strictly_increasing (a_range(strictlyIncreasingValues))); + BOOST_CHECK ( ba::is_increasing (a_range(strictlyIncreasingValues))); + BOOST_CHECK ( !ba::is_strictly_decreasing (a_range(strictlyIncreasingValues))); + BOOST_CHECK ( !ba::is_decreasing (a_range(strictlyIncreasingValues))); + + // Test a strictly decreasing sequence + BOOST_CHECK ( !ba::is_strictly_increasing (b_e(strictlyDecreasingValues))); + BOOST_CHECK ( !ba::is_increasing (b_e(strictlyDecreasingValues))); + BOOST_CHECK ( ba::is_strictly_decreasing (b_e(strictlyDecreasingValues))); + BOOST_CHECK ( ba::is_decreasing (b_e(strictlyDecreasingValues))); + + // Test an increasing sequence + BOOST_CHECK ( !ba::is_strictly_increasing (b_e(increasingValues))); + BOOST_CHECK ( ba::is_increasing (b_e(increasingValues))); + BOOST_CHECK ( !ba::is_strictly_decreasing (b_e(increasingValues))); + BOOST_CHECK ( !ba::is_decreasing (b_e(increasingValues))); + + // Test a decreasing sequence + BOOST_CHECK ( !ba::is_strictly_increasing (b_e(decreasingValues))); + BOOST_CHECK ( !ba::is_increasing (b_e(decreasingValues))); + BOOST_CHECK ( !ba::is_strictly_decreasing (b_e(decreasingValues))); + BOOST_CHECK ( ba::is_decreasing (b_e(decreasingValues))); + + // Test a random sequence + BOOST_CHECK ( !ba::is_strictly_increasing (b_e(randomValues))); + BOOST_CHECK ( !ba::is_increasing (b_e(randomValues))); + BOOST_CHECK ( !ba::is_strictly_decreasing (b_e(randomValues))); + BOOST_CHECK ( !ba::is_decreasing (b_e(randomValues))); + + // Test a constant sequence + BOOST_CHECK ( !ba::is_strictly_increasing (b_e(constantValues))); + BOOST_CHECK ( ba::is_increasing (b_e(constantValues))); + BOOST_CHECK ( !ba::is_strictly_decreasing (b_e(constantValues))); + BOOST_CHECK ( ba::is_decreasing (b_e(constantValues))); + + // Test an empty sequence + BOOST_CHECK ( ba::is_strictly_increasing (strictlyIncreasingValues, strictlyIncreasingValues)); + BOOST_CHECK ( ba::is_increasing (strictlyIncreasingValues, strictlyIncreasingValues)); + BOOST_CHECK ( ba::is_strictly_decreasing (strictlyIncreasingValues, strictlyIncreasingValues)); + BOOST_CHECK ( ba::is_decreasing (strictlyIncreasingValues, strictlyIncreasingValues)); + + // Test a one-element sequence + BOOST_CHECK ( ba::is_strictly_increasing (strictlyIncreasingValues, strictlyIncreasingValues+1)); + BOOST_CHECK ( ba::is_increasing (strictlyIncreasingValues, strictlyIncreasingValues+1)); + BOOST_CHECK ( ba::is_strictly_decreasing (strictlyIncreasingValues, strictlyIncreasingValues+1)); + BOOST_CHECK ( ba::is_decreasing (strictlyIncreasingValues, strictlyIncreasingValues+1)); + + // Test a two-element sequence + BOOST_CHECK ( ba::is_strictly_increasing (strictlyIncreasingValues, strictlyIncreasingValues+2)); + BOOST_CHECK ( ba::is_increasing (strictlyIncreasingValues, strictlyIncreasingValues+2)); + BOOST_CHECK ( !ba::is_strictly_decreasing (strictlyIncreasingValues, strictlyIncreasingValues+2)); + BOOST_CHECK ( !ba::is_decreasing (strictlyIncreasingValues, strictlyIncreasingValues+2)); + +} + +int test_main( int, char * [] ) +{ + test_ordered (); + test_increasing_decreasing (); + return 0; +} diff --git a/test/partition_copy_test1.cpp b/test/partition_copy_test1.cpp new file mode 100644 index 0000000..f3d8d2a --- /dev/null +++ b/test/partition_copy_test1.cpp @@ -0,0 +1,87 @@ +/* + Copyright (c) Marshall Clow 2011-2012. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + + For more information, see http://www.boost.org +*/ + +#include + +#include +#include +#include + +#include +#include +#include +#include +#include + +namespace ba = boost::algorithm; +// namespace ba = boost; + +template +void test_sequence ( const Container &c, Predicate comp ) { + std::vector v1, v2; + + v1.clear (); v2.clear (); + ba::partition_copy ( c.begin (), c.end (), + std::back_inserter (v1), std::back_inserter (v2), comp ); +// std::cout << "Sizes(1): " << c.size () << " -> { " << v1.size () << ", " << v2.size () << " }" << std::endl; + BOOST_CHECK ( v1.size () + v2.size () == c.size ()); + BOOST_CHECK ( ba::all_of ( v1.begin (), v1.end (), comp )); + BOOST_CHECK ( ba::none_of ( v2.begin (), v2.end (), comp )); + + v1.clear (); v2.clear (); + ba::partition_copy ( c, std::back_inserter (v1), std::back_inserter ( v2 ), comp ); +// std::cout << "Sizes(2): " << c.size () << " -> { " << v1.size () << ", " << v2.size () << " }" << std::endl; + BOOST_CHECK ( v1.size () + v2.size () == c.size ()); + BOOST_CHECK ( ba::all_of ( v1, comp )); + BOOST_CHECK ( ba::none_of ( v2, comp )); + } + +template +struct less_than { +public: + less_than ( T foo ) : val ( foo ) {} + less_than ( const less_than &rhs ) : val ( rhs.val ) {} + + bool operator () ( const T &v ) const { return v < val; } +private: + less_than (); + less_than operator = ( const less_than &rhs ); + T val; + }; + +bool is_even ( int v ) { return v % 2 == 0; } + +void test_sequence1 () { + std::vector v; + + v.clear (); + for ( int i = 5; i < 15; ++i ) + v.push_back ( i ); + test_sequence ( v, less_than(3)); // no elements + test_sequence ( v, less_than(6)); // only the first element + test_sequence ( v, less_than(10)); + test_sequence ( v, less_than(99)); // all elements satisfy + +// With bidirectional iterators. + std::list l; + for ( int i = 5; i < 16; ++i ) + l.push_back ( i ); + test_sequence ( l, less_than(3)); // no elements + test_sequence ( l, less_than(6)); // only the first element + test_sequence ( l, less_than(10)); + test_sequence ( l, less_than(99)); // all elements satisfy + + } + + +int test_main( int , char* [] ) +{ + test_sequence1 (); + return 0; +} diff --git a/test/partition_point_test1.cpp b/test/partition_point_test1.cpp new file mode 100644 index 0000000..ff4763e --- /dev/null +++ b/test/partition_point_test1.cpp @@ -0,0 +1,98 @@ +/* + Copyright (c) Marshall Clow 2011-2012. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + + For more information, see http://www.boost.org +*/ + +#include + +#include +#include +#include + +#include +#include +#include + +namespace ba = boost::algorithm; +// namespace ba = boost; + +template +typename Container::iterator offset_to_iter ( Container &v, int offset ) { + typename Container::iterator retval; + + if ( offset >= 0 ) { + retval = v.begin (); + std::advance ( retval, offset ); + } + else { + retval = v.end (); + std::advance ( retval, offset + 1 ); + } + return retval; + } + +template +void test_sequence ( Container &v, Predicate comp, int expected ) { + typename Container::iterator res, exp; + + res = ba::partition_point ( v.begin (), v.end (), comp ); + exp = offset_to_iter ( v, expected ); + std::cout << "Expected(1): " << std::distance ( v.begin (), exp ) + << ", got: " << std::distance ( v.begin (), res ) << std::endl; + BOOST_CHECK ( exp == res ); + +// Duplicate the last element; this checks for any even/odd problems + v.push_back ( * v.rbegin ()); + res = ba::partition_point ( v.begin (), v.end (), comp ); + exp = offset_to_iter ( v, expected ); + std::cout << "Expected(2): " << std::distance ( v.begin (), exp ) + << ", got: " << std::distance ( v.begin (), res ) << std::endl; + BOOST_CHECK ( exp == res ); + } + +template +struct less_than { +public: + less_than ( T foo ) : val ( foo ) {} + less_than ( const less_than &rhs ) : val ( rhs.val ) {} + + bool operator () ( const T &v ) const { return v < val; } +private: + less_than (); + less_than operator = ( const less_than &rhs ); + T val; + }; + + +void test_sequence1 () { + std::vector v; + + v.clear (); + for ( int i = 5; i < 15; ++i ) + v.push_back ( i ); + test_sequence ( v, less_than(3), 0 ); // no elements + test_sequence ( v, less_than(6), 1 ); // only the first element + test_sequence ( v, less_than(10), 5 ); + test_sequence ( v, less_than(99), -1 ); // all elements satisfy + +// With bidirectional iterators. + std::list l; + for ( int i = 5; i < 15; ++i ) + l.push_back ( i ); + test_sequence ( l, less_than(3), 0 ); // no elements + test_sequence ( l, less_than(6), 1 ); // only the first element + test_sequence ( l, less_than(10), 5 ); + test_sequence ( l, less_than(99), -1 ); // all elements satisfy + + } + + +int test_main( int , char* [] ) +{ + test_sequence1 (); + return 0; +}