diff options
Diffstat (limited to 'src/boost/libs/dynamic_bitset')
23 files changed, 6054 insertions, 0 deletions
diff --git a/src/boost/libs/dynamic_bitset/CMakeLists.txt b/src/boost/libs/dynamic_bitset/CMakeLists.txt new file mode 100644 index 00000000..86d4a667 --- /dev/null +++ b/src/boost/libs/dynamic_bitset/CMakeLists.txt @@ -0,0 +1,25 @@ +# Copyright 2019 Sam Day +# 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 +# +# NOTE: CMake support for Boost.dynamic_bitset is currently experimental at best +# and the interface is likely to change in the future + +cmake_minimum_required(VERSION 3.5) +project(BoostDynamicBitset LANGUAGES CXX) + +add_library(boost_dynamic_bitset INTERFACE) +add_library(Boost::dynamic_bitset ALIAS boost_dynamic_bitset) + +target_include_directories(boost_dynamic_bitset INTERFACE include) + +target_link_libraries(boost_dynamic_bitset + INTERFACE + Boost::assert + Boost::config + Boost::core + Boost::integer + Boost::move + Boost::static_assert + Boost::throw_exception + ) diff --git a/src/boost/libs/dynamic_bitset/Jamfile b/src/boost/libs/dynamic_bitset/Jamfile new file mode 100644 index 00000000..8f6db438 --- /dev/null +++ b/src/boost/libs/dynamic_bitset/Jamfile @@ -0,0 +1,11 @@ +# Boost.DynamicBitset Library Jamfile +# +# Copyright (c) 2018 James E. King III +# +# Use, modification, and distribution are subject to 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) + +# please order by name to ease maintenance +build-project example ; +build-project test ; diff --git a/src/boost/libs/dynamic_bitset/LICENSE b/src/boost/libs/dynamic_bitset/LICENSE new file mode 100644 index 00000000..36b7cd93 --- /dev/null +++ b/src/boost/libs/dynamic_bitset/LICENSE @@ -0,0 +1,23 @@ +Boost Software License - Version 1.0 - August 17th, 2003 + +Permission is hereby granted, free of charge, to any person or organization +obtaining a copy of the software and accompanying documentation covered by +this license (the "Software") to use, reproduce, display, distribute, +execute, and transmit the Software, and to prepare derivative works of the +Software, and to permit third-parties to whom the Software is furnished to +do so, all subject to the following: + +The copyright notices in the Software and this entire statement, including +the above license grant, this restriction and the following disclaimer, +must be included in all copies of the Software, in whole or in part, and +all derivative works of the Software, unless such copies or derivative +works are solely in the form of machine-executable object code generated by +a source language processor. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/src/boost/libs/dynamic_bitset/README.md b/src/boost/libs/dynamic_bitset/README.md new file mode 100644 index 00000000..20affc9c --- /dev/null +++ b/src/boost/libs/dynamic_bitset/README.md @@ -0,0 +1,34 @@ +DynamicBitset, part of collection of the [Boost C++ Libraries](http://github.com/boostorg), is similar to std::bitset however the size is specified at run-time instead of at compile-time. + +### License + +Distributed under the [Boost Software License, Version 1.0](http://www.boost.org/LICENSE_1_0.txt). + +### Properties + +* C++03 +* Header-only + +### Build Status + +Branch | Travis | Appveyor | Coverity Scan | codecov.io | Deps | Docs | Tests | +:-------------: | ------ | -------- | ------------- | ---------- | ---- | ---- | ----- | +[`master`](https://github.com/boostorg/dynamic_bitset/tree/master) | [![Build Status](https://travis-ci.org/boostorg/dynamic_bitset.svg?branch=master)](https://travis-ci.org/boostorg/dynamic_bitset) | [![Build status](https://ci.appveyor.com/api/projects/status/keyn57y5d3sl1gw5/branch/master?svg=true)](https://ci.appveyor.com/project/jeking3/dynamic_bitset-jv17p/branch/master) | [![Coverity Scan Build Status](https://scan.coverity.com/projects/16167/badge.svg)](https://scan.coverity.com/projects/boostorg-dynamic_bitset) | [![codecov](https://codecov.io/gh/boostorg/dynamic_bitset/branch/master/graph/badge.svg)](https://codecov.io/gh/boostorg/dynamic_bitset/branch/master)| [![Deps](https://img.shields.io/badge/deps-master-brightgreen.svg)](https://pdimov.github.io/boostdep-report/master/dynamic_bitset.html) | [![Documentation](https://img.shields.io/badge/docs-master-brightgreen.svg)](http://www.boost.org/doc/libs/master/doc/html/dynamic_bitset.html) | [![Enter the Matrix](https://img.shields.io/badge/matrix-master-brightgreen.svg)](http://www.boost.org/development/tests/master/developer/dynamic_bitset.html) +[`develop`](https://github.com/boostorg/dynamic_bitset/tree/develop) | [![Build Status](https://travis-ci.org/boostorg/dynamic_bitset.svg?branch=develop)](https://travis-ci.org/boostorg/dynamic_bitset) | [![Build status](https://ci.appveyor.com/api/projects/status/keyn57y5d3sl1gw5/branch/develop?svg=true)](https://ci.appveyor.com/project/jeking3/dynamic_bitset-jv17p/branch/develop) | [![Coverity Scan Build Status](https://scan.coverity.com/projects/16167/badge.svg)](https://scan.coverity.com/projects/boostorg-dynamic_bitset) | [![codecov](https://codecov.io/gh/boostorg/dynamic_bitset/branch/develop/graph/badge.svg)](https://codecov.io/gh/boostorg/dynamic_bitset/branch/develop) | [![Deps](https://img.shields.io/badge/deps-develop-brightgreen.svg)](https://pdimov.github.io/boostdep-report/develop/dynamic_bitset.html) | [![Documentation](https://img.shields.io/badge/docs-develop-brightgreen.svg)](http://www.boost.org/doc/libs/develop/doc/html/dynamic_bitset.html) | [![Enter the Matrix](https://img.shields.io/badge/matrix-develop-brightgreen.svg)](http://www.boost.org/development/tests/develop/developer/dynamic_bitset.html) + +### Directories + +| Name | Purpose | +| ----------- | ------------------------------ | +| `example` | examples | +| `doc` | documentation | +| `include` | headers | +| `test` | unit tests | + +### More information + +* [Ask questions](http://stackoverflow.com/questions/ask?tags=c%2B%2B,boost,boost-dynamic_bitset) +* [Report bugs](https://github.com/boostorg/dynamic_bitset/issues): Be sure to mention Boost version, platform and compiler you're using. A small compilable code sample to reproduce the problem is always good as well. +* Submit your patches as pull requests against **develop** branch. Note that by submitting patches you agree to license your modifications under the [Boost Software License, Version 1.0](http://www.boost.org/LICENSE_1_0.txt). +* Discussions about the library are held on the [Boost developers mailing list](http://www.boost.org/community/groups.html#main). Be sure to read the [discussion policy](http://www.boost.org/community/policy.html) before posting and add the `[dynamic_bitset]` tag at the beginning of the subject line. + diff --git a/src/boost/libs/dynamic_bitset/dynamic_bitset.html b/src/boost/libs/dynamic_bitset/dynamic_bitset.html new file mode 100644 index 00000000..b7dbd799 --- /dev/null +++ b/src/boost/libs/dynamic_bitset/dynamic_bitset.html @@ -0,0 +1,1799 @@ +<?xml version="1.0" encoding="ascii"?> +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" + "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" > + +<!-- + Copyright (c) 2001 Jeremy Siek + Copyright (c) 2003-2004, 2008 Gennaro Prota + Copyright (c) 2014 Ahmed Charles + Copyright (c) 2014 Riccardo Marcangelo + Copyright (c) 2018 Evgeny Shulgin + + 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) +--> + +<!-- + Copyright (c) 1996-1999 + Silicon Graphics Computer Systems, Inc. + + Permission to use, copy, modify, distribute and sell this software + and its documentation for any purpose is hereby granted without fee, + provided that the above copyright notice appears in all copies and + that both that copyright notice and this permission notice appear + in supporting documentation. Silicon Graphics makes no + representations about the suitability of this software for any + purpose. It is provided "as is" without express or implied warranty. + + Copyright (c) 1994 + Hewlett-Packard Company + + Permission to use, copy, modify, distribute and sell this software + and its documentation for any purpose is hereby granted without fee, + provided that the above copyright notice appears in all copies and + that both that copyright notice and this permission notice appear + in supporting documentation. Hewlett-Packard Company makes no + representations about the suitability of this software for any + purpose. It is provided "as is" without express or implied warranty. + + --> +<head> +<title>dynamic_bitset<Block, Allocator></title> +<link rel="stylesheet" type="text/css" href="../../rst.css" /> +</head> + +<body> +<div id="body"> +<div id="body-inner"> +<div id="content"> +<div class="section" id="docs"> +<div class="section-0"> +<div class="section-body"> + +<div id="boost-logo"><img src="../../boost.png" alt="Boost C++ Libraries" /></div> +<h1>dynamic_bitset<Block, Allocator></h1> +<h2>Contents</h2> + +<dl class="index"> +<dt><a href="#description">Description</a></dt> +<dt><a href="#synopsis">Synopsis</a></dt> +<dt><a href="#definitions">Definitions</a></dt> +<dt><a href="#examples">Examples</a></dt> +<dt><a href="#rationale">Rationale</a></dt> +<dt><a href="#header-files">Header Files</a></dt> +<dt><a href="#template-parameters">Template Parameters</a></dt> +<dt><a href="#concepts-modeled">Concepts modeled</a></dt> + +<dt><a href="#type-requirements">Type requirements</a></dt> +<dt><a href="#public-base-classes">Public base classes</a></dt> +<dt><a href="#nested-type-names">Nested type names</a></dt> +<dt><a href="#public-data-members">Public data members</a></dt> +<dt><a href="#constructors">Constructors</a></dt> +<dt><a href="#destructor">Destructor</a></dt> +<dt><a href="#member-functions">Member functions</a></dt> +<dt><a href="#non-member-functions">Non-member functions</a></dt> +<dt><a href="#exception-guarantees">Exception guarantees</a></dt> + +<dt><a href="#changes-from-previous-ver"><b>Changes from previous version(s)</b></a></dt> +<dt><a href="#see-also">See also</a></dt> +<dt><a href="#acknowledgements">Acknowledgements</a></dt> +</dl> +<h3><a id="description">Description</a></h3> +<p>The <tt>dynamic_bitset</tt> class represents a set of bits. It +provides accesses to the value of individual bits via an +<tt>operator[]</tt> and provides all of the bitwise operators +that one can apply to builtin integers, such as +<tt>operator&</tt> and <tt>operator<<</tt>. The number +of bits in the set is specified at runtime via a parameter to the +constructor of the <tt>dynamic_bitset</tt>.</p> + +<p>The <tt>dynamic_bitset</tt> class is nearly identical to the +<a href= +"https://boost.org/sgi/stl/bitset.html"><tt>std::bitset</tt></a> +class. The difference is that the size of the +<tt>dynamic_bitset</tt> (the number of bits) is specified at +run-time during the construction of a <tt>dynamic_bitset</tt> +object, whereas the size of a <tt>std::bitset</tt> is specified +at compile-time through an integer template parameter.</p> + +<p>The main problem that <tt>dynamic_bitset</tt> is designed to +solve is that of representing a subset of a finite set. Each bit +represents whether an element of the finite set is in the subset +or not. As such the bitwise operations of +<tt>dynamic_bitset</tt>, such as <tt>operator&</tt> and +<tt>operator|</tt>, correspond to set operations, such as +intersection and union.</p> +<h3><a id ="synopsis">Synopsis</a></h3> +<pre> +namespace boost { + +template <typename Block, typename Allocator> +class dynamic_bitset +{ +public: + typedef Block <a href="#block_type">block_type</a>; + typedef Allocator <a href="#allocator_type">allocator_type</a>; + typedef <i>implementation-defined</i> <a href="#size_type">size_type</a>; + + static const int <a href= + "#bits_per_block">bits_per_block</a> = <i>implementation-defined</i>; + static const size_type <a href= + "#npos">npos</a> = <i>implementation-defined</i>; + + class <a href="#reference">reference</a> + { + void operator&(); // not defined + + public: + // An automatically generated copy constructor. + + reference& operator=(bool value); + reference& operator=(const reference& rhs); + + reference& operator|=(bool value); + reference& operator&=(bool value); + reference& operator^=(bool value); + reference& operator-=(bool value); + + bool operator~() const; + operator bool() const; + reference& flip(); + }; + + typedef bool <a href="#const_reference">const_reference</a>; + + <a href= +"#cons0">dynamic_bitset</a>(); + + explicit <a href= +"#cons1">dynamic_bitset</a>(const Allocator& alloc); + + explicit <a href= +"#cons2">dynamic_bitset</a>(size_type num_bits, unsigned long value = 0, + const Allocator& alloc = Allocator()); + + template <typename CharT, typename Traits, typename Alloc> + explicit <a href= +"#cons3">dynamic_bitset</a>(const std::basic_string<CharT, Traits, Alloc>& s, + typename std::basic_string<CharT, Traits, Alloc>::size_type pos = 0, + typename std::basic_string<CharT, Traits, Alloc>::size_type n = std::basic_string<CharT, Traits, Alloc>::npos, + const Allocator& alloc = Allocator()); + + template <typename BlockInputIterator> + <a href= +"#cons4">dynamic_bitset</a>(BlockInputIterator first, BlockInputIterator last, + const Allocator& alloc = Allocator()); + + <a href= +"#cons5">dynamic_bitset</a>(const dynamic_bitset& b); + + <a href= +"#move-cons">dynamic_bitset</a>(dynamic_bitset&& b); + + void <a href="#swap">swap</a>(dynamic_bitset& b); + + dynamic_bitset& <a href= +"#assign">operator=</a>(const dynamic_bitset& b); + + dynamic_bitset& <a href= +"#move-assign">operator=</a>(dynamic_bitset&& b); + + allocator_type <a href="#get_allocator">get_allocator()</a> const; + + void <a href= +"#resize">resize</a>(size_type num_bits, bool value = false); + void <a href="#clear">clear</a>(); + void <a href="#pop_back">pop_back</a>(); + void <a href="#push_back">push_back</a>(bool bit); + void <a href="#append1">append</a>(Block block); + + template <typename BlockInputIterator> + void <a href="#append2">append</a>(BlockInputIterator first, BlockInputIterator last); + + dynamic_bitset& <a href="#op-and-assign">operator&=</a>(const dynamic_bitset& b); + dynamic_bitset& <a href="#op-or-assign">operator|=</a>(const dynamic_bitset& b); + dynamic_bitset& <a href="#op-xor-assign">operator^=</a>(const dynamic_bitset& b); + dynamic_bitset& <a href="#op-sub-assign">operator-=</a>(const dynamic_bitset& b); + dynamic_bitset& <a href="#op-sl-assign">operator<<=</a>(size_type n); + dynamic_bitset& <a href="#op-sr-assign">operator>>=</a>(size_type n); + dynamic_bitset <a href="#op-sl">operator<<</a>(size_type n) const; + dynamic_bitset <a href="#op-sr">operator>></a>(size_type n) const; + + dynamic_bitset& <a href="#set3">set</a>(size_type n, size_type len, bool val); + dynamic_bitset& <a href="#set2">set</a>(size_type n, bool val = true); + dynamic_bitset& <a href="#set1">set</a>(); + dynamic_bitset& <a href="#reset3">reset</a>(size_type n, size_type len); + dynamic_bitset& <a href="#reset2">reset</a>(size_type n); + dynamic_bitset& <a href="#reset1">reset</a>(); + dynamic_bitset& <a href="#flip3">flip</a>(size_type n, size_type len); + dynamic_bitset& <a href="#flip2">flip</a>(size_type n); + dynamic_bitset& <a href="#flip1">flip</a>(); + bool <a href="#test">test</a>(size_type n) const; + bool <a href="#test">test_set</a>(size_type n, bool val = true); + bool <a href="#all">all</a>() const; + bool <a href="#any">any</a>() const; + bool <a href="#none">none</a>() const; + dynamic_bitset <a href="#op-not">operator~</a>() const; + size_type <a href="#count">count</a>() const noexcept; + + reference <a href="#bracket">operator[]</a>(size_type pos); + bool <a href="#const-bracket">operator[]</a>(size_type pos) const; + + unsigned long <a href="#to_ulong">to_ulong</a>() const; + + size_type <a href="#size">size</a>() const noexcept; + size_type <a href="#num_blocks">num_blocks</a>() const noexcept; + size_type <a href="#max_size">max_size</a>() const noexcept; + bool <a href="#empty">empty</a>() const noexcept; + size_type <a href="#capacity">capacity</a>() const noexcept; + void <a href="#reserve">reserve</a>(size_type num_bits); + void <a href="#shrink_to_fit">shrink_to_fit</a>(); + + bool <a href="#is_subset_of">is_subset_of</a>(const dynamic_bitset& a) const; + bool <a href="#is_proper_subset_of">is_proper_subset_of</a>(const dynamic_bitset& a) const; + bool <a href="#intersects">intersects</a>(const dynamic_bitset& a) const; + + size_type <a href="#find_first">find_first</a>() const; + size_type <a href="#find_next">find_next</a>(size_type pos) const; + +}; + + +template <typename B, typename A> +bool <a href= +"#op-equal">operator==</a>(const dynamic_bitset<B, A>& a, const dynamic_bitset<B, A>& b); + +template <typename Block, typename Allocator> +bool <a href= +"#op-not-equal">operator!=</a>(const dynamic_bitset<Block, Allocator>& a, const dynamic_bitset<Block, Allocator>& b); + +template <typename B, typename A> +bool <a href= +"#op-less">operator<</a>(const dynamic_bitset<B, A>& a, const dynamic_bitset<B, A>& b); + +template <typename Block, typename Allocator> +bool <a href= +"#op-less-equal">operator<=</a>(const dynamic_bitset<Block, Allocator>& a, const dynamic_bitset<Block, Allocator>& b); + +template <typename Block, typename Allocator> +bool <a href= +"#op-greater">operator></a>(const dynamic_bitset<Block, Allocator>& a, const dynamic_bitset<Block, Allocator>& b); + +template <typename Block, typename Allocator> +bool <a href= +"#op-greater-equal">operator>=</a>(const dynamic_bitset<Block, Allocator>& a, const dynamic_bitset<Block, Allocator>& b); + +template <typename Block, typename Allocator> +dynamic_bitset<Block, Allocator> +<a href= +"#op-and">operator&</a>(const dynamic_bitset<Block, Allocator>& b1, const dynamic_bitset<Block, Allocator>& b2); + +template <typename Block, typename Allocator> +dynamic_bitset<Block, Allocator> +<a href= +"#op-or">operator|</a>(const dynamic_bitset<Block, Allocator>& b1, const dynamic_bitset<Block, Allocator>& b2); + +template <typename Block, typename Allocator> +dynamic_bitset<Block, Allocator> +<a href= +"#op-xor">operator^</a>(const dynamic_bitset<Block, Allocator>& b1, const dynamic_bitset<Block, Allocator>& b2); + +template <typename Block, typename Allocator> +dynamic_bitset<Block, Allocator> +<a href= +"#op-sub">operator-</a>(const dynamic_bitset<Block, Allocator>& b1, const dynamic_bitset<Block, Allocator>& b2); + +template <typename Block, typename Allocator, typename CharT, typename Alloc> +void <a href= +"#to_string">to_string</a>(const dynamic_bitset<Block, Allocator>& b, + std::basic_string<CharT, Alloc>& s); + +template <typename Block, typename Allocator, typename BlockOutputIterator> +void <a href= +"#to_block_range">to_block_range</a>(const dynamic_bitset<Block, Allocator>& b, + BlockOutputIterator result); + +template <typename CharT, typename Traits, typename Block, typename Allocator> +std::basic_ostream<CharT, Traits>& +<a href= +"#op-out">operator<<</a>(std::basic_ostream<CharT, Traits>& os, const dynamic_bitset<Block, Allocator>& b); + +template <typename CharT, typename Traits, typename Block, typename Allocator> +std::basic_istream<CharT, Traits>& +<a href= +"#op-in">operator>></a>(std::basic_istream<CharT, Traits>& is, dynamic_bitset<Block, Allocator>& b); + +} // namespace boost +</pre> + +<h3><a id="definitions">Definitions</a></h3> + +<p>Each bit represents either the Boolean value true or false (1 +or 0). To <i>set</i> a bit is to assign it 1. To <i>clear</i> or +<i>reset</i> a bit is to assign it 0. To <i>flip</i> a bit is to +change the value to 1 if it was 0 and to 0 if it was 1. Each bit +has a non-negative <i>position</i>. A bitset <tt>x</tt> contains +<tt>x.size()</tt> bits, with each bit assigned a unique position +in the range <tt>[0,x.size())</tt>. The bit at position 0 is +called the <i>least significant bit</i> and the bit at position +<tt>size() - 1</tt> is the <i>most significant bit</i>. When +converting an instance of <tt>dynamic_bitset</tt> to or from an +unsigned long <tt>n</tt>, the bit at position <tt>i</tt> of the +bitset has the same value as <tt>(n >> i) & 1</tt>.</p> + +<h3><a id="examples">Examples</a></h3> + +<p> +<a href="./example/example1.cpp">Example 1</a> (setting +and reading some bits) +</p> +<p> +<a href="./example/example2.cpp">Example 2</a> (creating +some bitsets from integers) +</p> + +<p> +<a href="./example/example3.cpp">Example 3</a> (performing +input/output and some bitwise operations). +</p> + + +<h3><a id="rationale">Rationale</a></h3> +<p> +<tt>dynamic_bitset</tt> is not a <a href= +"https://boost.org/sgi/stl/Container.html">Container</a> and +does not provide iterators for the following reason: +</p> + +<ul> +<li>A container with a proxy <tt>reference</tt> type can not +fulfill the container requirements as specified in the C++ +standard (unless one resorts to strange iterator semantics). +<tt>std::vector<bool></tt> has a proxy <tt>reference</tt> +type and does not fulfill the container requirements and as a +result has caused many problems. One common problem is when +people try to use iterators from <tt>std::vector<bool></tt> +with a Standard algorithm such as <tt>std::search</tt>. The +<tt>std::search</tt> requirements say that the iterator must be a +<a href= +"https://boost.org/sgi/stl/ForwardIterator.html">Forward +Iterator</a>, but the <tt>std::vector<bool>::iterator</tt> +does not meet this requirement because of the proxy reference. +Depending on the implementation, they may or not be a compile +error or even a run-time error due to this misuse. For further +discussion of the problem see <i>Effective STL</i> by Scott +Meyers). So <tt>dynamic_bitset</tt> tries to avoid these problems +by not pretending to be a container.</li> +</ul> + +<p>Some people prefer the name "toggle" to "flip". The name +"flip" was chosen because that is the name used in <a href= +"https://boost.org/sgi/stl/bitset.html"><tt>std::bitset</tt></a>. +In fact, most of the function names for <tt>dynamic_bitset</tt> +were chosen for this reason.</p> + +<p><tt>dynamic_bitset</tt> does not throw exceptions when a +precondition is violated (as is done in <tt>std::bitset</tt>). +Instead <tt>assert</tt> is used. See the guidelines for <a href= +"http://www.boost.org/community/error_handling.html">Error and Exception Handling</a> +for the explanation.</p> + +<h3><a id="header-files">Header Files</a></h3> + +<p>The class <tt>dynamic_bitset</tt> is defined in the header <a +href= +"../../boost/dynamic_bitset.hpp">boost/dynamic_bitset.hpp</a>. +Also, there is a forward declaration for <tt>dynamic_bitset</tt> +in the header <a href= +"../../boost/dynamic_bitset_fwd.hpp">boost/dynamic_bitset_fwd.hpp</a>.</p> + +<h3><a id="template-parameters">Template parameters</a></h3> + +<table summary= +"Describes the meaning of the template parameters and lists their corresponding default arguments"> +<tr> +<th>Parameter</th> +<th>Description</th> +<th>Default</th> +</tr> +<tr> +<td><tt>Block</tt></td> +<td>The integer type in which the bits are stored.</td> +<td><tt>unsigned long</tt></td> +</tr> +<tr> + +<td><tt>Allocator</tt></td> +<td>The allocator type used for all internal memory management.</td> +<td><tt>std::allocator<Block></tt></td> +</tr> +</table> +<h3><a id="concepts-modeled">Concepts Modeled</a></h3> +<a href= +"https://boost.org/sgi/stl/Assignable.html">Assignable</a>, <a +href= +"https://boost.org/sgi/stl/DefaultConstructible.html">Default +Constructible</a>, <a href= +"https://boost.org/sgi/stl/EqualityComparable.html">Equality +Comparable</a>, <a href= +"https://boost.org/sgi/stl/LessThanComparable.html">LessThan +Comparable</a>. + +<h3><a id="type-requirements">Type requirements</a></h3> + +<tt>Block</tt> is an unsigned integer type. <tt>Allocator</tt> +satisfies the Standard requirements for an allocator. + +<h3><a id="public-base-classes">Public base classes</a></h3> + +None. +<h3><a id="nested-type-names">Nested type names</a></h3> +<hr /> + +<pre> +<a id="reference">dynamic_bitset::reference</a> +</pre> + +<p>A proxy class that acts as a reference to a single bit. It +contains an assignment operator, a conversion to <tt>bool</tt>, +an <tt>operator~</tt>, and a member function <tt>flip</tt>. It +exists only as a helper class for <tt>dynamic_bitset</tt>'s +<tt>operator[]</tt>. The following table describes the valid +operations on the <tt>reference</tt> type. Assume that <tt>b</tt> +is an instance of <tt>dynamic_bitset</tt>, <tt>i, j</tt> are of +<tt>size_type</tt> and in the range <tt>[0,b.size())</tt>. Also, +note that when we write <tt>b[i]</tt> we mean an object of type +<tt>reference</tt> that was initialized from <tt>b[i]</tt>. The +variable <tt>x</tt> is a <tt>bool</tt>.</p> + +<table border="1" summary= +"Semantics of several expressions involving usage of dynamic_bitset::reference"> +<tr> +<th>Expression</th> +<th>Semantics</th> +</tr> +<tr> +<td><tt>x = b[i]</tt></td> +<td>Assign the ith bit of <tt>b</tt> to <tt>x</tt>.</td> +</tr> +<tr> +<td><tt>(bool)b[i]</tt></td> +<td>Return the ith bit of <tt>b</tt>.</td> +</tr> +<tr> +<td><tt>b[i] = x</tt></td> +<td>Set the ith bit of <tt>b</tt> to the value of <tt>x</tt> and +return <tt>b[i]</tt>.</td> +</tr> +<tr> +<td><tt>b[i] |= x</tt></td> +<td>Or the ith bit of <tt>b</tt> with the value of <tt>x</tt> and +return <tt>b[i]</tt>.</td> +</tr> +<tr> +<td><tt>b[i] &= x</tt></td> +<td>And the ith bit of <tt>b</tt> with the value of <tt>x</tt> +and return <tt>b[i]</tt>.</td> +</tr> + +<tr> +<td><tt>b[i] ^= x</tt></td> +<td>Exclusive-Or the ith bit of <tt>b</tt> with the value of +<tt>x</tt> and return <tt>b[i]</tt>.</td> +</tr> + +<tr> +<td><tt>b[i] -= x</tt></td> +<td>If <tt>x==true</tt>, clear the ith bit of <tt>b</tt>. Returns +<tt>b[i]</tt>.</td> +</tr> + +<tr> +<td><tt>b[i] = b[j]</tt></td> +<td>Set the ith bit of <tt>b</tt> to the value of the jth bit of +<tt>b</tt> and return <tt>b[i]</tt>.</td> +</tr> + +<tr> +<td><tt>b[i] |= b[j]</tt></td> +<td>Or the ith bit of <tt>b</tt> with the jth bit of <tt>b</tt> +and return <tt>b[i]</tt>.</td> +</tr> + +<tr> +<td><tt>b[i] &= b[j]</tt></td> +<td>And the ith bit of <tt>b</tt> with the jth bit of <tt>b</tt> and return <tt>b[i]</tt>.</td> +</tr> +<tr> +<td><tt>b[i] ^= b[j]</tt></td> +<td>Exclusive-Or the ith bit of <tt>b</tt> with the jth bit of <tt>b</tt> and return <tt>b[i]</tt>.</td> + +</tr> +<tr> +<td><tt>b[i] -= b[j]</tt></td> +<td>If the jth bit of <tt>b</tt> is set, clear the ith bit of <tt>b</tt>. Returns <tt>b[i]</tt>.</td> +</tr> +<tr> +<td><tt>x = ~b[i]</tt></td> + +<td>Assign the opposite of the ith bit of <tt>b</tt> to <tt>x</tt>.</td> +</tr> +<tr> +<td><tt>(bool)~b[i]</tt></td> +<td>Return the opposite of the ith bit of <tt>b</tt>.</td> +</tr> +<tr> + +<td><tt>b[i].flip()</tt></td> +<td>Flip the ith bit of <tt>b</tt> and return <tt>b[i]</tt>.</td> +</tr> +</table> +<hr /> +<pre> +<a id="const_reference">dynamic_bitset::const_reference</a> +</pre> +The type <tt>bool</tt>. + +<pre> +<a id="size_type">dynamic_bitset::size_type</a> +</pre> +The unsigned integer type for representing the size of the bit set. + +<pre> +<a id="block_type">dynamic_bitset::block_type</a> +</pre> +The same type as <tt>Block</tt>. + +<pre> +<a id="allocator_type">dynamic_bitset::allocator_type;</a> +</pre> +The same type as <tt>Allocator</tt>. + + +<hr /> +<h3><a id="public-data-members">Public data members</a></h3> + +<pre> +<a id="bits_per_block">dynamic_bitset::bits_per_block</a> +</pre> +The number of bits the type <tt>Block</tt> uses to represent values, +excluding any padding bits. Numerically equal +to <tt>std::numeric_limits<Block>::digits</tt>. + +<pre> +<a id="npos">dynamic_bitset::npos</a> +</pre> +The maximum value of <tt>size_type</tt>. + +<hr /> +<h3><a id="constructors">Constructors</a></h3> + +<hr /> +<pre> +<a id= +"cons0">dynamic_bitset</a>() +</pre> + +<b>Effects:</b> Constructs a bitset of size zero. The allocator +for this bitset is a default-constructed object of type +<tt>Allocator</tt>.<br /> + <b>Postconditions:</b> <tt>this->size() == 0</tt>.<br /> + <b>Throws:</b> Nothing unless the default constructor of +<tt>Allocator</tt> throws an exception.<br /> + (Required by <a href= +"https://boost.org/sgi/stl/DefaultConstructible.html">Default +Constructible</a>.) + +<hr /> +<pre> +<a id= +"cons1">dynamic_bitset</a>(const Allocator& alloc) +</pre> + +<b>Effects:</b> Constructs a bitset of size zero. A copy of the +<tt>alloc</tt> object will be used in subsequent bitset +operations such as <tt>resize</tt> to allocate memory.<br /> + <b>Postconditions:</b> <tt>this->size() == 0</tt>.<br /> + <b>Throws:</b> nothing. + +<hr /> +<pre> +<a id="cons2">dynamic_bitset</a>(size_type num_bits, + unsigned long value = 0, + const Allocator& alloc = Allocator()) +</pre> + +<b>Effects:</b> Constructs a bitset from an integer. The first +<tt>M</tt> bits are initialized to the corresponding bits in +<tt>value</tt> and all other bits, if any, to zero (where <tt>M = +min(num_bits, std::numeric_limits<unsigned long>::digits)</tt>). A copy of +the <tt>alloc</tt> object will be used in subsequent bitset +operations such as <tt>resize</tt> to allocate memory. Note that, e.g., the +following +<br /><br /> +<tt> +dynamic_bitset b<>( 16, 7 ); +</tt><br /><br /> +will match the <a href="#cons4">constructor from an iterator range</a> (not this +one), but the underlying implementation will still "do the right thing" and +construct a bitset of 16 bits, from the value 7. +<br /> +<b>Postconditions:</b> + +<ul> +<li><tt>this->size() == num_bits</tt></li> + +<li>For all <tt>i</tt> in the range <tt>[0,M)</tt>, +<tt>(*this)[i] == (value >> i) & 1</tt>.</li> + +<li>For all <tt>i</tt> in the range <tt>[M,num_bits)</tt>, +<tt>(*this)[i] == false</tt>.</li> +</ul> + +<b>Throws:</b> An allocation error if memory is exhausted +(<tt>std::bad_alloc</tt> if +<tt>Allocator=std::allocator</tt>).<br /> + + +<hr /> +<pre> +<a id="cons5">dynamic_bitset</a>(const dynamic_bitset& x) +</pre> + +<b>Effects:</b> Constructs a bitset that is a copy of the bitset +<tt>x</tt>. The allocator for this bitset is a copy of the +allocator in <tt>x</tt>. <br /> + <b>Postconditions:</b> For all <tt>i</tt> in the range +<tt>[0,x.size())</tt>, <tt>(*this)[i] == x[i]</tt>.<br /> + <b>Throws:</b> An allocation error if memory is exhausted +(<tt>std::bad_alloc</tt> if +<tt>Allocator=std::allocator</tt>).<br /> + (Required by <a href= +"https://boost.org/sgi/stl/Assignable.html">Assignable</a>.) + +<hr /> +<pre> +<a id="move-cons">dynamic_bitset</a>(dynamic_bitset&& x) +</pre> + +<b>Effects:</b> Constructs a bitset that is the same as the bitset +<tt>x</tt>, while using the resources from <tt>x</tt>. The allocator +for this bitset is moved from the allocator in <tt>x</tt>. <br /> + <b>Postconditions:</b> For all <tt>i</tt> in the range +<tt>[0,x.size())</tt>, <tt>(*this)[i] == x[i]</tt>.<br /> + <b>Throws:</b> An allocation error if memory is exhausted +(<tt>std::bad_alloc</tt> if +<tt>Allocator=std::allocator</tt>). + +<hr /> +<pre> +template <typename BlockInputIterator> +explicit +<a id= +"cons4">dynamic_bitset</a>(BlockInputIterator first, BlockInputIterator last, + const Allocator& alloc = Allocator()); +</pre> + +<b>Effects:</b> +<ul> +<li> +If this constructor is called with a type <tt>BlockInputIterator</tt> which +<i>is actually an integral type</i>, the library behaves as if the constructor +from <tt>unsigned long</tt> were called, with arguments +<tt>static_cast<size_type>(first), last and alloc</tt>, in that order. +<br /><br /> +Example: +<pre> +// b is constructed as if by calling the constructor +// +// dynamic_bitset(size_type num_bits, +// unsigned long value = 0, +// const Allocator& alloc = Allocator()) +// +// with arguments +// +// static_cast<dynamic_bitset<unsigned short>::size_type>(8), +// 7, +// Allocator() +// +dynamic_bitset<unsigned short> b(8, 7); +</pre><br /> +<i>Note:</i><br/> +At the time of writing (October 2008) this is aligned with the +proposed resolution for <a href= + "http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#438">library + issue 438</a>. That is a <em>post <tt>C++03</tt></em> +change, and is currently in the working paper for +<tt>C++0x</tt>. Informally speaking, the critical changes with +respect to <tt>C++03</tt> are the drop of a <tt>static_cast</tt> +on the second argument, and more leeway as to <em>when</em> the +templated constructor should have the same effect as the (size, +value) one: only when <tt>InputIterator</tt> is an integral +type, in <tt>C++03</tt>; when it is either an integral type or +any other type that the implementation might detect as +impossible to be an input iterator, with the proposed +resolution. For the purposes of <tt>dynamic_bitset</tt> we limit +ourselves to the first of these two changes.<br /><br /> +</li> +<li> +<i>Otherwise</i> (<i>i.e.</i> if the template argument is not an +integral type), constructs—under the condition in the +<tt>requires</tt> clause—a bitset based on a range of +blocks. Let <tt>*first</tt> be block number 0, <tt>*++first</tt> +block number 1, etc. Block number <tt>b</tt> is used to +initialize the bits of the dynamic_bitset in the position range +<tt>[b*bits_per_block, (b+1)*bits_per_block)</tt>. For each +block number <tt>b</tt> with value <tt>bval</tt>, the bit +<tt>(bval >> i) & 1</tt> corresponds to the bit at +position <tt>(b * bits_per_block + i)</tt> in the bitset (where +<tt>i</tt> goes through the range <tt>[0, bits_per_block)</tt>). +</li> +</ul> +<br /> +<b>Requires:</b> <tt>BlockInputIterator</tt> must be either an +integral type or a model of <a href= + "https://boost.org/sgi/stl/InputIterator.html">Input + Iterator</a> whose <tt>value_type</tt> is the same type as +<tt>Block</tt>.<br /> <b>Throws:</b> An allocation error if +memory is exhausted (<tt>std::bad_alloc</tt> if +<tt>Allocator=std::allocator</tt>).<br /> + +<hr /> +<pre> +template<typename Char, typename Traits, typename Alloc> +explicit +<a id="cons3">dynamic_bitset</a>(const <a href= +"https://boost.org/sgi/stl/basic_string.html">std::basic_string</a><Char,Traits,Alloc>& s, + typename std::basic_string<CharT, Traits, Alloc>::size_type pos = 0, + typename std::basic_string<CharT, Traits, Alloc>::size_type n = <a + href= +"https://boost.org/sgi/stl/basic_string.html">std::basic_string</a><Char,Traits,Alloc>::npos, + const Allocator& alloc = Allocator()) +</pre> + +<b>Precondition:</b> <tt>pos <= s.size()</tt> and the +characters used to initialize the bits must be <tt>0</tt> or +<tt>1</tt>.<br /> + <b>Effects:</b> Constructs a bitset from a string of 0's and +1's. The first <tt>M</tt> bits are initialized to the +corresponding characters in <tt>s</tt>, where <tt>M = +min(s.size() - pos, n)</tt>. Note that the <i>highest</i> +character position in <tt>s</tt>, not the lowest, corresponds to +the least significant bit. That is, character position <tt>pos + +M - 1 - i</tt> corresponds to bit <tt>i</tt>. So, for example, +<tt>dynamic_bitset(string("1101"))</tt> is the same as +<tt>dynamic_bitset(13ul)</tt>.<br /> + <b>Throws:</b> an allocation error if memory is exhausted +(<tt>std::bad_alloc</tt> if <tt>Allocator=std::allocator</tt>). + +<hr /> +<h3><a id="destructor">Destructor</a></h3> + +<hr /> +<pre> +~dynamic_bitset() +</pre> + +<b>Effects:</b> Releases the memory associated with this bitset +and destroys the bitset object itself.<br /> + <b>Throws:</b> nothing. + +<hr /> +<h3><a id="member-functions">Member Functions</a></h3> + +<hr /> +<pre> +void <a id="swap">swap</a>(dynamic_bitset& b); +</pre> + +<b>Effects:</b> The contents of this bitset and bitset <tt>b</tt> +are exchanged.<br /> +<b>Postconditions:</b> This bitset is equal to the original +<tt>b</tt>, and <tt>b</tt> is equal to the previous version of +this bitset.<br /> +<b>Throws:</b> nothing. + +<hr /> +<pre> +dynamic_bitset& <a id= +"assign">operator=</a>(const dynamic_bitset& x) +</pre> + +<b>Effects:</b> This bitset becomes a copy of the bitset +<tt>x</tt>.<br /> + <b>Postconditions:</b> For all <tt>i</tt> in the range +<tt>[0,x.size())</tt>, <tt>(*this)[i] == x[i]</tt>.<br /> + <b>Returns:</b> <tt>*this</tt>.<br /> + <b>Throws:</b> nothing. <br /> +(Required by <a href= +"https://boost.org/sgi/stl/Assignable.html">Assignable</a>.) + +<hr /> +<pre> +dynamic_bitset& <a id= +"move-assign">operator=</a>(dynamic_bitset&& x) +</pre> + +<b>Effects:</b> This bitset becomes the same as the bitset +<tt>x</tt>, while using the resources from <tt>x</tt>.<br /> + <b>Postconditions:</b> For all <tt>i</tt> in the range +<tt>[0,x.size())</tt>, <tt>(*this)[i] == x[i]</tt>.<br /> + <b>Returns:</b> <tt>*this</tt>.<br /> + <b>Throws:</b> An allocation error if memory is exhausted +(<tt>std::bad_alloc</tt> if <tt>Allocator=std::allocator</tt>). + +<hr /> +<pre> +allocator_type <a id="get_allocator">get_allocator()</a> const; +</pre> + <b>Returns:</b> A copy of the allocator object used to construct <tt>*this</tt>. + +<hr /> +<pre> +void <a id= +"resize">resize</a>(size_type num_bits, bool value = false); +</pre> + +<b>Effects:</b> Changes the number of bits of the bitset to +<tt>num_bits</tt>. If <tt>num_bits > size()</tt> then the bits +in the range <tt>[0,size())</tt> remain the same, and the bits in +<tt>[size(),num_bits)</tt> are all set to <tt>value</tt>. If +<tt>num_bits < size()</tt> then the bits in the range +<tt>[0,num_bits)</tt> stay the same (and the remaining bits are +discarded).<br /> + <b>Postconditions:</b> <tt>this->size() == num_bits</tt>.<br /> + <b>Throws:</b> An allocation error if memory is exhausted +(<tt>std::bad_alloc</tt> if +<tt>Allocator=std::allocator</tt>).<br /> + + +<hr /> +<pre> +void <a id="clear">clear</a>() +</pre> + +<b>Effects:</b> The size of the bitset becomes zero.<br /> + <b>Throws:</b> nothing. + +<hr /> +<pre> +void <a id="pop_back">pop_back</a>(); +</pre> + +<b>Precondition:</b> <tt>!this->empty()</tt>.<br /> +<b>Effects:</b> Decreases the size of the bitset by one.<br /> + <b>Throws:</b> nothing. + +<hr /> +<pre> +void <a id="push_back">push_back</a>(bool value); +</pre> + +<b>Effects:</b> Increases the size of the bitset by one, and sets +the value of the new most-significant bit to <tt>value</tt>.<br /> + <b>Throws:</b> An allocation error if memory is exhausted +(<tt>std::bad_alloc</tt> if +<tt>Allocator=std::allocator</tt>).<br /> + + +<hr /> +<pre> +void <a id="append1">append</a>(Block value); +</pre> + +<b>Effects:</b> Appends the bits in <tt>value</tt> to the bitset +(appends to the most-significant end). This increases the size of +the bitset by <tt>bits_per_block</tt>. Let <tt>s</tt> be the old +size of the bitset, then for <tt>i</tt> in the range +<tt>[0,bits_per_block)</tt>, the bit at position <tt>(s + i)</tt> +is set to <tt>((value >> i) & 1)</tt>.<br /> + <b>Throws:</b> An allocation error if memory is exhausted +(<tt>std::bad_alloc</tt> if +<tt>Allocator=std::allocator</tt>).<br /> + + +<hr /> +<pre> +template <typename BlockInputIterator> +void <a id= +"append2">append</a>(BlockInputIterator first, BlockInputIterator last); +</pre> + +<b>Effects:</b> This function provides the same end result as the +following code, but is typically more efficient. + +<pre> +for (; first != last; ++first) + append(*first); +</pre> + +<b>Requires:</b> The <tt>BlockInputIterator</tt> type must be a +model of <a href= +"https://boost.org/sgi/stl/InputIterator.html">Input +Iterator</a> and the <tt>value_type</tt> must be the same type as +<tt>Block</tt>.<br /> + <b>Throws:</b> An allocation error if memory is exhausted +(<tt>std::bad_alloc</tt> if +<tt>Allocator=std::allocator</tt>).<br /> + + +<hr /> +<pre> +dynamic_bitset& <a id= +"op-and-assign">operator&=</a>(const dynamic_bitset& rhs) +</pre> + +<b>Requires:</b> <tt>this->size() == rhs.size()</tt>.<br /> + <b>Effects:</b> Bitwise-AND all the bits in <tt>rhs</tt> with +the bits in this bitset. This is equivalent to: + +<pre> +for (size_type i = 0; i != this->size(); ++i) + (*this)[i] = (*this)[i] & rhs[i]; +</pre> + +<b>Returns:</b> <tt>*this</tt>.<br /> + <b>Throws:</b> nothing. + +<hr /> +<pre> +dynamic_bitset& <a id= +"op-or-assign">operator|=</a>(const dynamic_bitset& rhs) +</pre> + +<b>Requires:</b> <tt>this->size() == rhs.size()</tt>.<br /> + <b>Effects:</b> Bitwise-OR's all the bits in <tt>rhs</tt> with +the bits in this bitset. This is equivalent to: + +<pre> +for (size_type i = 0; i != this->size(); ++i) + (*this)[i] = (*this)[i] | rhs[i]; +</pre> + +<b>Returns:</b> <tt>*this</tt>.<br /> + <b>Throws:</b> nothing. + +<hr /> +<pre> +dynamic_bitset& <a id= +"op-xor-assign">operator^=</a>(const dynamic_bitset& rhs) +</pre> + +<b>Requires:</b> <tt>this->size() == rhs.size()</tt>.<br /> + <b>Effects:</b> Bitwise-XOR's all the bits in <tt>rhs</tt> with +the bits in this bitset. This is equivalent to: + +<pre> +for (size_type i = 0; i != this->size(); ++i) + (*this)[i] = (*this)[i] ^ rhs[i]; +</pre> + +<b>Returns:</b> <tt>*this</tt>.<br /> + <b>Throws:</b> nothing. + +<hr /> +<pre> +dynamic_bitset& <a id= +"op-sub-assign">operator-=</a>(const dynamic_bitset& rhs) +</pre> + +<b>Requires:</b> <tt>this->size() == rhs.size()</tt>.<br /> + <b>Effects:</b> Computes the set difference of this bitset and +the <tt>rhs</tt> bitset. This is equivalent to: + +<pre> +for (size_type i = 0; i != this->size(); ++i) + (*this)[i] = (*this)[i] && !rhs[i]; +</pre> + +<b>Returns:</b> <tt>*this</tt>.<br /> + <b>Throws:</b> nothing. + +<hr /> +<pre> +dynamic_bitset& <a id= +"op-sl-assign">operator<<=</a>(size_type n) +</pre> + +<b>Effects:</b> Shifts the bits in this bitset to the left by +<tt>n</tt> bits. For each bit in the bitset, the bit at position +pos takes on the previous value of the bit at position <tt>pos - +n</tt>, or zero if no such bit exists.<br /> + <b>Returns:</b> <tt>*this</tt>.<br /> + <b>Throws:</b> nothing. + +<hr /> +<pre> +dynamic_bitset& <a id= +"op-sr-assign">operator>>=</a>(size_type n) +</pre> + +<b>Effects:</b> Shifts the bits in this bitset to the right by +<tt>n</tt> bits. For each bit in the bitset, the bit at position +<tt>pos</tt> takes on the previous value of bit <tt>pos + n</tt>, +or zero if no such bit exists.<br /> + <b>Returns:</b> <tt>*this</tt>.<br /> + <b>Throws:</b> nothing. + +<hr /> +<pre> +dynamic_bitset <a id= +"op-sl">operator<<</a>(size_type n) const +</pre> + +<b>Returns:</b> a copy of <tt>*this</tt> shifted to the left by +<tt>n</tt> bits. For each bit in the returned bitset, the bit at +position pos takes on the value of the bit at position <tt>pos - +n</tt> of this bitset, or zero if no such bit exists.<br /> + <b>Throws:</b> An allocation error if memory is exhausted +(<tt>std::bad_alloc</tt> if <tt>Allocator=std::allocator</tt>). + +<hr /> +<pre> +dynamic_bitset <a id= +"op-sr">operator>></a>(size_type n) const +</pre> + +<b>Returns:</b> a copy of <tt>*this</tt> shifted to the right by +<tt>n</tt> bits. For each bit in the returned bitset, the bit at +position pos takes on the value of the bit at position <tt>pos + +n</tt> of this bitset, or zero if no such bit exists.<br /> + <b>Throws:</b> An allocation error if memory is exhausted +(<tt>std::bad_alloc</tt> if <tt>Allocator=std::allocator</tt>). + +<hr /> +<pre> +dynamic_bitset& <a id="set1">set</a>() +</pre> + +<b>Effects:</b> Sets every bit in this bitset to 1.<br /> +<b>Returns:</b> <tt>*this</tt><br /> +<b>Throws:</b> nothing. + +<hr /> +<pre> +dynamic_bitset& <a id="flip1">flip</a>() +</pre> + +<b>Effects:</b> Flips the value of every bit in this bitset.<br /> +<b>Returns:</b> <tt>*this</tt><br /> +<b>Throws:</b> nothing. + +<hr /> +<pre> +dynamic_bitset <a id="op-not">operator~</a>() const +</pre> + +<b>Returns:</b> a copy of <tt>*this</tt> with all of its bits +flipped.<br /> +<b>Throws:</b> An allocation error if memory is exhausted +(<tt>std::bad_alloc</tt> if <tt>Allocator=std::allocator</tt>). + +<hr /> +<pre> +dynamic_bitset& <a id="reset1">reset</a>() +</pre> + +<b>Effects:</b> Clears every bit in this bitset.<br /> +<b>Returns:</b> <tt>*this</tt><br /> +<b>Throws:</b> nothing. + +<hr /> +<pre> +dynamic_bitset& <a id= +"set3">set</a>(size_type n, size_type len, bool val); +</pre> + +<b>Precondition:</b> <tt>n + len < this->size()</tt>.<br /> +<b>Effects:</b> Sets every bit indexed from <tt>n</tt> to +<tt>n + len - 1</tt> inclusively if <tt>val</tt> is <tt>true</tt>, and +clears them if <tt>val</tt> is <tt>false</tt>. <br /> +<b>Returns:</b> <tt>*this</tt> + +<hr /> +<pre> +dynamic_bitset& <a id= +"set2">set</a>(size_type n, bool val = true) +</pre> + +<b>Precondition:</b> <tt>n < this->size()</tt>.<br /> + <b>Effects:</b> Sets bit <tt>n</tt> if <tt>val</tt> is +<tt>true</tt>, and clears bit <tt>n</tt> if <tt>val</tt> is +<tt>false</tt>. <br /> + <b>Returns:</b> <tt>*this</tt> + +<hr /> +<pre> +dynamic_bitset& <a id= +"reset3">reset</a>(size_type n, size_type len); +</pre> + +<b>Precondition:</b> <tt>n + len < this->size()</tt>.<br /> +<b>Effects:</b> Clears every bit indexed from <tt>n</tt> to +<tt>n + len - 1</tt> inclusively.<br /> +<b>Returns:</b> <tt>*this</tt> + +<hr /> +<pre> +dynamic_bitset& <a id="reset2">reset</a>(size_type n) +</pre> + +<b>Precondition:</b> <tt>n < this->size()</tt>.<br /> +<b>Effects:</b> Clears bit <tt>n</tt>.<br /> +<b>Returns:</b> <tt>*this</tt> + +<hr /> +<pre> +dynamic_bitset& <a id="flip3">flip</a>(size_type n, size_type len) +</pre> + +<b>Precondition:</b> <tt>n + len < this->size()</tt>.<br /> +<b>Effects:</b> Flips every bit indexed from <tt>n</tt> to +<tt>n + len - 1</tt> inclusively.<br /> +<b>Returns:</b> <tt>*this</tt> + +<hr /> +<pre> +dynamic_bitset& <a id="flip2">flip</a>(size_type n) +</pre> + +<b>Precondition:</b> <tt>n < this->size()</tt>.<br /> +<b>Effects:</b> Flips bit <tt>n</tt>.<br /> +<b>Returns:</b> <tt>*this</tt> + +<hr /> +<pre> +size_type <a id="size">size</a>() const +</pre> + +<b>Returns:</b> the number of bits in this bitset.<br /> +<b>Throws:</b> nothing. + +<hr /> +<pre> +size_type <a id="num_blocks">num_blocks</a>() const +</pre> + +<b>Returns:</b> the number of blocks in this bitset.<br /> +<b>Throws:</b> nothing. + +<hr /> +<pre> +size_type <a id="max_size">max_size</a>() const; +</pre> + +<b>Returns:</b> the maximum size of a <tt>dynamic_bitset</tt> +object having the same type as <tt>*this</tt>. Note that if +any <tt>dynamic_bitset</tt> operation causes <tt>size()</tt> to +exceed <tt>max_size()</tt> then the <i>behavior is undefined</i>. +<br /><br />[The semantics of this function could change slightly +when lib issue 197 will be closed]<br /> + +<hr /> +<pre> +bool <a id="empty">empty</a>() const; +</pre> + +<b>Returns:</b> <tt>true</tt> if <tt>this->size() == 0</tt>, <tt>false</tt> +otherwise. <i>Note</i>: not to be confused with <tt>none()</tt>, that has +different semantics. + +<hr /> +<pre> +size_type <a id="capacity">capacity</a>() const; +</pre> + +<b>Returns:</b> The total number of elements that <tt>*this</tt> can hold without requiring +reallocation.<br /> +<b>Throws:</b> nothing. + +<hr /> +<pre> +void <a id="reserve">reserve</a>(size_type num_bits); +</pre> + +<b>Effects:</b> A directive that informs the bitset of a planned change in size, so that it can +manage the storage allocation accordingly. After reserve(), capacity() is greater or equal to the +argument of reserve() if reallocation happens; and equal to the previous value of capacity() otherwise. +Reallocation happens at this point if and only if the current capacity is less than the argument of +reserve(). <br /> +<i>Note:</i> It does not change the size() of the bitset.<br /> +<b>Postcondtitions:</b> <tt>this->capacity() >= num_bits</tt>.<br /> +<b>Throws:</b> An allocation error if memory is exhausted +(<tt>std::bad_alloc</tt> if <tt>Allocator=std::allocator</tt>). + +<hr /> +<pre> +void <a id="shrink_to_fit">shrink_to_fit</a>(); +</pre> + +<b>Effects:</b> shrink_to_fit() is a request to reduce memory use by removing unused capacity.<br /> +<i>Note:</i> It does not change the size() of the bitset.<br /> +<b>Throws:</b> An allocation error if memory is exhausted +(<tt>std::bad_alloc</tt> if <tt>Allocator=std::allocator</tt>). + +<hr /> +<pre> +size_type <a id="count">count</a>() const +</pre> + +<b>Returns:</b> the number of bits in this bitset that are +set.<br /> +<b>Throws:</b> nothing. + +<hr /> +<pre> +bool <a id="all">all</a>() const +</pre> + +<b>Returns:</b> <tt>true</tt> if all bits in this bitset are set or +if <tt>size() == 0</tt>, and otherwise returns <tt>false</tt>.<br /> +<b>Throws:</b> nothing. + +<hr /> +<pre> +bool <a id="any">any</a>() const +</pre> + +<b>Returns:</b> <tt>true</tt> if any bits in this bitset are set, +and otherwise returns <tt>false</tt>.<br /> +<b>Throws:</b> nothing. + +<hr /> +<pre> +bool <a id="none">none</a>() const +</pre> + +<b>Returns:</b> <tt>true</tt> if no bits are set, and otherwise +returns <tt>false</tt>.<br /> +<b>Throws:</b> nothing. + +<hr /> +<pre> +bool <a id="test">test</a>(size_type n) const +</pre> + +<b>Precondition:</b> <tt>n < this->size()</tt>.<br /> + <b>Returns:</b> <tt>true</tt> if bit <tt>n</tt> is set and +<tt>false</tt> is bit <tt>n</tt> is 0. + +<hr /> +<pre> +bool <a id="test">test_set</a>(size_type n, bool val = true) +</pre> + +<b>Precondition:</b> <tt>n < this->size()</tt>.<br /> + <b>Effects:</b> Sets bit <tt>n</tt> if <tt>val</tt> is +<tt>true</tt>, and clears bit <tt>n</tt> if <tt>val</tt> is +<tt>false</tt>. <br /> + <b>Returns:</b> <tt>true</tt> if the previous state of bit +<tt>n</tt> was set and <tt>false</tt> is bit <tt>n</tt> is 0. + +<hr /> +<pre> +reference <a id="bracket">operator[]</a>(size_type n) +</pre> + +<b>Precondition:</b> <tt>n < this->size()</tt>.<br /> + <b>Returns:</b> a <tt>reference</tt> to bit <tt>n</tt>. Note +that <tt>reference</tt> is a proxy class with an assignment +operator and a conversion to <tt>bool</tt>, which allows you to +use <tt>operator[]</tt> for assignment. That is, you can write +both <tt>x = b[n]</tt> and <tt>b[n] = x</tt>. However, in many +other respects the proxy is not the same as the true reference +type <tt>bool&</tt>. + +<hr /> +<pre> +bool <a id="const-bracket">operator[]</a>(size_type n) const +</pre> + +<b>Precondition:</b> <tt>n < this->size()</tt>.<br /> +<b>Returns:</b> The same as <tt>test(n)</tt>. + +<hr /> +<pre> +unsigned long <a id="to_ulong">to_ulong</a>() const +</pre> + +<b>Returns:</b> The numeric value corresponding to the bits in <tt>*this</tt>. +<br /> +<b>Throws:</b> <tt>std::overflow_error</tt> if that value is too large to +be represented in an <tt>unsigned long</tt>, i.e. if <tt>*this</tt> has +any non-zero bit at a position <tt>>= +std::numeric_limits<unsigned long>::digits</tt>. + +<hr /> +<pre> +bool <a id= +"is_subset_of">is_subset_of</a>(const dynamic_bitset& a) const +</pre> + +<b>Requires:</b> <tt>this->size() == a.size()</tt><br /> +<b>Returns:</b> true if this bitset is a subset of bitset +<tt>a</tt>. That is, it returns true if, for every bit that is +set in this bitset, the corresponding bit in bitset <tt>a</tt> is +also set. Otherwise this function returns false.<br /> +<b>Throws:</b> nothing. + +<hr /> +<pre> +bool <a id= +"is_proper_subset_of">is_proper_subset_of</a>(const dynamic_bitset& a) const +</pre> + +<b>Requires:</b> <tt>this->size() == a.size()</tt><br /> +<b>Returns:</b> true if this bitset is a proper subset of bitset +<tt>a</tt>. That is, it returns true if, for every bit that is +set in this bitset, the corresponding bit in bitset <tt>a</tt> is +also set and if <tt>this->count() < a.count()</tt>. +Otherwise this function returns false.<br /> +<b>Throws:</b> nothing. + +<hr /> +<pre> +bool <a id= +"intersects">intersects</a>(const dynamic_bitset& a) const +</pre> + +<b>Requires:</b> <tt>this->size() == a.size()</tt><br /> +<b>Returns:</b> true if this bitset and <tt>a</tt> intersect. +That is, it returns true if, there is a bit which is set in this +bitset, such that the corresponding bit in bitset <tt>a</tt> is +also set. Otherwise this function returns false.<br /> +<b>Throws:</b> nothing. + +<hr /> +<pre> +size_type <a id = "find_first">find_first</a>() const; +</pre> + +<b>Returns:</b> the lowest index <tt>i</tt> such as bit <tt>i</tt> +is set, or <tt>npos</tt> if <tt>*this</tt> has no on bits. + +<hr /> +<pre> +size_type <a id="find_next">find_next</a>(size_type pos) const; +</pre> + +<b>Returns:</b> the lowest index <tt>i</tt> greater than +<tt>pos</tt> such as bit <tt>i</tt> is set, or <tt>npos</tt> if +no such index exists. + +<hr /> +<pre> +bool <a id= +"op-equal">operator==</a>(const dynamic_bitset& rhs) const +</pre> + +<b>Returns:</b> <tt>true</tt> if <tt>this->size() == +rhs.size()</tt> and if for all <tt>i</tt> in the range +<tt>[0,rhs.size())</tt>, <tt>(*this)[i] == rhs[i]</tt>. Otherwise +returns <tt>false</tt>.<br /> + <b>Throws:</b> nothing.<br /> + (Required by <a href= +"https://boost.org/sgi/stl/EqualityComparable.html">Equality +Comparable</a>.) + +<hr /> +<pre> +bool <a id= +"op-not-equal">operator!=</a>(const dynamic_bitset& rhs) const +</pre> + +<b>Returns:</b> <tt>!((*this) == rhs)</tt><br /> +<b>Throws:</b> nothing.<br /> +(Required by <a href= +"https://boost.org/sgi/stl/EqualityComparable.html">Equality +Comparable</a>.) + +<hr /> +<pre> +bool <a id= +"op-less">operator<</a>(const dynamic_bitset& rhs) const +</pre> + +<b>Returns:</b> <tt>true</tt> if this bitset is lexicographically +less than <tt>rhs</tt>, and returns <tt>false</tt> otherwise. +(See the description of <a href= +"https://boost.org/sgi/stl/lexicographical_compare.html">lexicographical_compare</a> +for a definition of lexicographic ordering). <br /> +<b>Throws:</b> nothing.<br /> +(Required by <a href= +"https://boost.org/sgi/stl/LessThanComparable.html">Less Than +Comparable</a>.) + +<hr /> +<pre> +bool <a id= +"op-greater">operator></a>(const dynamic_bitset& rhs) const +</pre> + +<b>Returns:</b> <tt>!((*this) < rhs || (*this) == +rhs)</tt><br /> +<b>Throws:</b> nothing.<br /> +(Required by <a href= +"https://boost.org/sgi/stl/LessThanComparable.html">Less Than +Comparable</a>.) + +<hr /> +<pre> +bool <a id= +"op-less-equal">operator<=</a>(const dynamic_bitset& rhs) const +</pre> + +<b>Returns:</b> <tt>(*this) < rhs || (*this) == rhs</tt><br /> +<b>Throws:</b> nothing.<br /> +(Required by <a href= +"https://boost.org/sgi/stl/LessThanComparable.html">Less Than +Comparable</a>.) + +<hr /> +<pre> +bool <a id= +"op-greater-equal">operator>=</a>(const dynamic_bitset& rhs) const +</pre> + +<b>Returns:</b> <tt>(*this) > rhs || (*this) == rhs</tt><br /> +<b>Throws:</b> nothing.<br /> +(Required by <a href= +"https://boost.org/sgi/stl/LessThanComparable.html">Less Than +Comparable</a>.) + +<hr /> +<h3><a id="non-member-functions">Non-Member Functions</a></h3> + +<hr /> +<pre> +dynamic_bitset <a id= +"op-and">operator&</a>(const dynamic_bitset& a, const dynamic_bitset& b) +</pre> + +<b>Requires:</b> <tt>a.size() == b.size()</tt><br /> +<b>Returns:</b> A new bitset that is the bitwise-AND of the +bitsets <tt>a</tt> and <tt>b</tt>.<br /> +<b>Throws:</b> An allocation error if memory is exhausted +(<tt>std::bad_alloc</tt> if <tt>Allocator=std::allocator</tt>). + +<hr /> +<pre> +dynamic_bitset <a id= +"op-or">operator|</a>(const dynamic_bitset& a, const dynamic_bitset& b) +</pre> + +<b>Requires:</b> <tt>a.size() == b.size()</tt><br /> +<b>Returns:</b> A new bitset that is the bitwise-OR of the +bitsets <tt>a</tt> and <tt>b</tt>.<br /> +<b>Throws:</b> An allocation error if memory is exhausted +(<tt>std::bad_alloc</tt> if <tt>Allocator=std::allocator</tt>). + +<hr /> +<pre> +dynamic_bitset <a id= +"op-xor">operator^</a>(const dynamic_bitset& a, const dynamic_bitset& b) +</pre> + +<b>Requires:</b> <tt>a.size() == b.size()</tt><br /> +<b>Returns:</b> A new bitset that is the bitwise-XOR of the +bitsets <tt>a</tt> and <tt>b</tt>.<br /> +<b>Throws:</b> An allocation error if memory is exhausted +(<tt>std::bad_alloc</tt> if <tt>Allocator=std::allocator</tt>). + +<hr /> +<pre> +dynamic_bitset <a id= +"op-sub">operator-</a>(const dynamic_bitset& a, const dynamic_bitset& b) +</pre> + +<b>Requires:</b> <tt>a.size() == b.size()</tt><br /> +<b>Returns:</b> A new bitset that is the set difference of the +bitsets <tt>a</tt> and <tt>b</tt>.<br /> +<b>Throws:</b> An allocation error if memory is exhausted +(<tt>std::bad_alloc</tt> if <tt>Allocator=std::allocator</tt>). + +<hr /> +<pre> +template <typename CharT, typename Alloc> +void <a id= +"to_string">to_string</a>(const dynamic_bitset<Block, Allocator>& b, + <a href= +"https://boost.org/sgi/stl/basic_string.html">std::basic_string</a><Char,Traits,Alloc>& s) +</pre> + +<b>Effects:</b> Copies a representation of <tt>b</tt> into the +string <tt>s</tt>. A character in the string is <tt>'1'</tt> if +the corresponding bit is set, and <tt>'0'</tt> if it is not. +Character position <tt>i</tt> in the string corresponds to bit +position <tt>b.size() - 1 - i</tt>. <br /> + <b>Throws:</b> If memory is exhausted, the string will throw an +allocation error.<br /> + <b>Rationale:</b> This function is not a member function taking +zero arguments and returning a string for a couple reasons. +First, this version can be slighly more efficient because the +string is not copied (due to being passed by value). Second, as a +member function, to allow for flexibility with regards to the +template parameters of <tt>basic_string</tt>, the member function +would require explicit template parameters. Few C++ programmers +are familiar with explicit template parameters, and some C++ +compilers do not handle them properly. + +<hr /> +<pre> +template <typename Block, typename Alloc, typename BlockOutputIterator> +void <a id= +"to_block_range">to_block_range</a>(const dynamic_bitset<Block, Alloc>& b, BlockOutputIterator result) +</pre> + +<b>Effects:</b> Writes the bits of the bitset into the iterator +<tt>result</tt> a block at a time. The first block written +represents the bits in the position range +<tt>[0,bits_per_block)</tt> in the bitset, the second block +written the bits in the range +<tt>[bits_pre_block,2*bits_per_block)</tt>, and so on. For each +block <tt>bval</tt> written, the bit <tt>(bval >> i) & +1</tt> corresponds to the bit at position <tt>(b * bits_per_block ++ i)</tt> in the bitset.<br /> + <b>Requires:</b> The type <tt>BlockOutputIterator</tt> must be a +model of <a href= +"https://boost.org/sgi/stl/OutputIterator.html">Output +Iterator</a> and its <tt>value_type</tt> must be the same type as +<tt>Block</tt>. Further, the size of the output range must be +greater or equal <tt>b.num_blocks()</tt>. + +<hr /> +<pre> +template <typename BlockIterator, typename Block, typename Alloc> +void <a id= +"from_block_range">from_block_range</a>(BlockIterator first, + BlockIterator last, const dynamic_bitset<Block, Alloc>& b) +</pre> + +<b>Effects:</b> Reads blocks from the iterator range into the +bitset. <br /> + <b>Requires:</b> The type <tt>BlockIterator</tt> must be a model +of <a href="https://boost.org/sgi/stl/InputIterator.html">Input +Iterator</a> and its <tt>value_type</tt> must be the same type as +<tt>Block</tt>. The size of the iterator range must be less or +equal to <tt>b.num_blocks()</tt>. + +<hr /> +<pre> +template <typename Char, typename Traits, typename Block, typename Alloc> +basic_ostream<Char, Traits>& +<a id= +"op-out">operator<<</a>(basic_ostream<Char, Traits>& os, const dynamic_bitset<Block, Alloc>& b) +</pre> + +<b>Effects:</b> Inserts a textual representation of b into the stream +<tt>os</tt> (highest bit first). Informally, the output is the same as doing + +<pre> +std::basic_string<Char, Traits> s; +boost::to_string(x, s): +os << s; +</pre> + +except that the stream inserter takes into accout the locale imbued into +<tt>os</tt>, which <tt>boost::to_string()</tt> can't do. Here is a more +precise specification, given in terms of "as if" rule: first, for each +valid position i into the bitset <tt>b</tt> let's put: + + <tt>character_of(b[i)]) = b[i]? os.widen('1') : os.widen('0');</tt> + +Let also <tt>s</tt> be a <tt>std::basic_string<Char, Traits></tt> +object, having length <tt>b.size()</tt> and such as, for each <tt>i</tt> +in <tt>[0, b.size())</tt>, + + <tt>s[i] is character_of(b[i])</tt> + +Then, the output, the effects on <tt>os</tt> and the exception behavior +is the same as outputting the object <tt>s</tt> to <tt>os</tt> (same +width, same exception mask, same padding, same setstate() logic) +<br /> +<b>Returns:</b> os <br /> +<b>Throws:</b> <tt>std::ios_base::failure</tt> if there is a +problem writing to the stream. + +<hr /> +<pre> +template <typename Char, typename Traits, typename Block, typename Alloc> +std::basic_istream<Char,Traits>& +<a id= +"op-in">operator>></a>(std::basic_istream<Char,Traits>& is, dynamic_bitset<Block, Alloc>& b) +</pre> + +<b>Effects:</b> Extracts a <tt>dynamic_bitset</tt> from an input stream. +<br /><br /> + <i>Definitions:</i><br /><br /> + Let <i>Tr</i> be the traits_type of <i>is</i>. Then: + <ol> + <li> + A (non-eof) character <tt>c</tt> extracted from <tt>is</tt> + is a <i>bitset digit</i> if and only if either Tr::eq(c, is.widen('0')) or + Tr::eq(c, is.widen('1')) return true. + </li> + <li>If c is a bitset digit, it's <i>corresponding bit value</i> is 0 if + Tr::eq(c, is.widen('0')) is true, 1 otherwise. + </li> + </ol> + +The function begins by constructing a <tt>sentry</tt> object <tt>k</tt> as if <tt>k</tt> +were constructed by + + <tt>typename std::basic_istream<Char, Traits>::sentry k(is)</tt>. + +If <tt>bool(k)</tt> is true, it calls <tt>b.clear()</tt> +then attempts to extract characters from <tt>is</tt>. For each character c +that is a <i>bitset digit</i> the <i>corresponding bit value</i> is +appended to the less significant end of <tt>b</tt> (appending may throw). +If <tt>is.width()</tt> is greater than zero and smaller than <tt>b.max_size()</tt> +then the maximum number <tt>n</tt> of bits appended is <tt>is.width()</tt>; +otherwise <tt>n</tt> = <tt>b.max_size()</tt>. + +Unless the extractor is exited via an exception, characters are extracted (and +corresponding bits appended) until any of the following occurs:<br /> + +<ul> +<li> <tt>n</tt> bits are stored into the bitset;</li> +<li> end-of-file, or an error, occurs on the input sequence;</li> +<li> the next available input character isn't a bitset digit</li> +</ul> +<br /> If no exception caused the function to exit then <tt>is.width(0)</tt> is + called, regardless of how many characters were actually extracted. The + sentry object k is destroyed. +<br /> +<br />If the function extracts no characters[???], it calls is.setstate(std::ios::failbit), + which may throw <tt>std::ios_base::failure</tt>. + + +<br />------ + + +<br /> +<b>Throws:</b> An allocation error if memory is exhausted +(<tt>std::bad_alloc</tt> if <tt>Allocator=std::allocator</tt>). +A <tt>std::ios_base::failure</tt> if there is a problem reading +from the stream. + +<hr /> +<h3><a id="exception-guarantees">Exception guarantees</a></h3> + +All of <tt>dynamic_bitset</tt> functions offer at least the basic +exception guarantee. + +<hr /> +<h3><a id="changes-from-previous-ver">Changes from previous version(s)</a></h3> + +<h4><i>Changes in Boost 1.56.0</i></h4> +<ul> +<li>Support for C++11 move constructors.</li> +<li>Warning fixes on MSVC 2013.</li> +<li>Support for C++11 minimal allocators.</li> +<li>Add noexcept specifications.</li> +</ul> + +<h4><i>Changes in Boost 1.37.0</i></h4> +<ul> +<li>The constructor from a block range implements a "do the right thing" +behavior, a la standard sequences.</li> +</ul> + +<!-- Changes from Boost 1.31.0 --> +<h4><i>Changes from Boost 1.31.0</i></h4> +<ul> +<li> +The stream extractor has completely different semantics: as natural +for a dynamic structure, it now expands the bitset as needed during +extraction. The new behaviour mimics that of the <tt>basic_string</tt> +extractor but there are some differences the user should be aware of; +so, please, check the <a href="#op-in">documentation</a>. (One +difference concerns the case where <code>stream.width() > + bitset.max_size() > 0</code>. In that circumstance the +extractor of <tt>dynamic_bitset</tt> never attempts to extract more +than <tt>max_size()</tt> characters, whereas the extractor of +<tt>basic_string</tt> goes on and, on conforming implementations, +eventually throws a <tt>length_error</tt> exception. Note: That's what +the standard mandates -see especially <a + href="http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#83">library + issue 83</a>- but not all implementations conform) +<br /><br /> +The stream extractor is now also "exception-aware" in the sense that +it works correctly when setting exception masks on the stream. +<br /><br /> +</li> +<li> +Several member functions (<tt>empty()</tt>, <tt>find_first()</tt> +, <tt>find_next()</tt>, <tt>get_allocator()</tt>, <tt>intersects()</tt> +, <tt>max_size()</tt> <!--, <tt>reserve()</tt>, <tt>capacity()</tt> -->) +have been added. +</li> +<li> +The constructor from <tt>basic_string</tt> has a new parameter that was totally +forgotten before. +</li> + +</ul> +<i>Technicalities and minor changes</i> +<ul> +<li> +The class <tt>reference</tt> has been reimplemented so that +dynamic_bitset's references behave more like references to standard +container elements. In particular it is now guaranteed that they +cannot be invalidated from a standard library swap() function +applied to their corresponding <tt>dynamic_bitset</tt>s. +</li> +</ul> +<i>General improvements</i> +<ul> +<li> +Several optimizations to member and non-member functions and to the +nested class <tt>reference</tt>. +</li> +</ul> + +<hr /> +<h3><a id="see-also">See also</a></h3> + +<tt><a href= +"https://boost.org/sgi/stl/bitset.html">std::bitset</a></tt>, +<tt><a href= +"https://boost.org/sgi/stl/Vector.html">std::vector</a></tt>, + +<h3><a id="acknowledgements">Acknowledgements</a></h3> + +<p>We would like to thank the Boost community for putting in the +time to review and accept this library. This library is much +better than it ever would have been due to all the suggestions +from Boost members. We especially thank Matt Marcus for taking on +the task of review manager. Also, a special thanks goes to +James Kanze for his invaluable help with the internationalization +issues.</p> + +<table summary="Copyright"> <tr> <td>Copyright © 2001</td> +<td><a href="http://www.boost.org/people/jeremy_siek.htm">Jeremy +Siek</a>, Indiana University (<a +href="mailto:jsiek@osl.iu.edu">jsiek@osl.iu.edu</a>)<br /> <a +href="http://freshsources.com">Chuck Allison</a>, Senior Editor, +C/C++ Users Journal (<a +href="mailto:cda@freshsources.com">cda@freshsources.com</a>)<br +/></td> </tr> <tr> +<td>Copyright © 2003-2004, 2008</td> <td><a + href="http://gennaro-prota.50webs.com/">Gennaro Prota</a> + (name.surname yahoo.com)</td> +</tr> +<tr> +<td>Copyright © 2014</td> +<td>Ahmed Charles (<a href="mailto:acharles@outlook.com">acharles@outlook.com</a>)</td> +</tr> +<tr> +<td>Copyright © 2014</td> +<td>Glen Fernandes (<a href="mailto:glenjofe@gmail.com">glenjofe@gmail.com</a>)</td> +</tr> +<tr> +<td>Copyright © 2014</td> +<td>Riccardo Marcangelo (<a href="mailto:ricky.65@outlook.com">ricky.65@outlook.com</a>)</td> +</tr> +</table> +<br /> +<div class="legalnotice"> + Distributed under the Boost Software License, Version 1.0. + (See accompanying file <a href="../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> + or copy at <a class="ulink" href="http://www.boost.org/LICENSE_1_0.txt"> +http://www.boost.org/LICENSE_1_0.txt</a>) +</div> + + +</div> +</div> +</div> +</div> +</div> +</div> +</body> +<!-- LocalWords: dynamic bitset alt gif iostream hpp int bitsets const ul ulong --> +<!-- LocalWords: STL LessThan alloc num typename BlockInputIterator html pos --> +<!-- LocalWords: npos bool rhs OR's XOR's val CharT istream ostream os siek --> +<!-- LocalWords: htm namespace enum sizeof BlockOutputIterator fwd ith jth --> +</html> diff --git a/src/boost/libs/dynamic_bitset/example/Jamfile b/src/boost/libs/dynamic_bitset/example/Jamfile new file mode 100644 index 00000000..6b4b0f3e --- /dev/null +++ b/src/boost/libs/dynamic_bitset/example/Jamfile @@ -0,0 +1,26 @@ +# ----------------------------------------------------------- +# Copyright (c) 2002 Gennaro Prota +# +# 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) +# +# ----------------------------------------------------------- + +exe timing_tests + : timing_tests.cpp + ../../timer/build//boost_timer + ; + +exe example1 + : example1.cpp + ; + +exe example2 + : example2.cpp + ; + +exe example3 + : example3.cpp + ; + diff --git a/src/boost/libs/dynamic_bitset/example/example1.cpp b/src/boost/libs/dynamic_bitset/example/example1.cpp new file mode 100644 index 00000000..5a03602b --- /dev/null +++ b/src/boost/libs/dynamic_bitset/example/example1.cpp @@ -0,0 +1,35 @@ +// (C) Copyright Jeremy Siek 2001. +// 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) + + + +// An example of setting and reading some bits. Note that operator[] +// goes from the least-significant bit at 0 to the most significant +// bit at size()-1. The operator<< for dynamic_bitset prints the +// bitset from most-significant to least-significant, since that is +// the format most people are used to reading. +// +// The output is: +// +// 11001 +// 10011 +// --------------------------------------------------------------------- + +#include <iostream> +#include <boost/dynamic_bitset.hpp> + +int main() +{ + boost::dynamic_bitset<> x(5); // all 0's by default + x[0] = 1; + x[1] = 1; + x[4] = 1; + for (boost::dynamic_bitset<>::size_type i = 0; i < x.size(); ++i) + std::cout << x[i]; + std::cout << "\n"; + std::cout << x << "\n"; + + return 0; +} diff --git a/src/boost/libs/dynamic_bitset/example/example2.cpp b/src/boost/libs/dynamic_bitset/example/example2.cpp new file mode 100644 index 00000000..151aa337 --- /dev/null +++ b/src/boost/libs/dynamic_bitset/example/example2.cpp @@ -0,0 +1,32 @@ +// (C) Copyright Jeremy Siek 2001. +// 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) +// +// Sample output: +// +// bits(0) = 00 +// bits(1) = 01 +// bits(2) = 10 +// bits(3) = 11 + + +#include <iostream> +#include <boost/dynamic_bitset.hpp> + +int main() +{ + const boost::dynamic_bitset<> b0(2, 0ul); + std::cout << "bits(0) = " << b0 << std::endl; + + const boost::dynamic_bitset<> b1(2, 1ul); + std::cout << "bits(1) = " << b1 << std::endl; + + const boost::dynamic_bitset<> b2(2, 2ul); + std::cout << "bits(2) = " << b2 << std::endl; + + const boost::dynamic_bitset<> b3(2, 3ul); + std::cout << "bits(3) = " << b3 << std::endl; + + return 0; +} diff --git a/src/boost/libs/dynamic_bitset/example/example3.cpp b/src/boost/libs/dynamic_bitset/example/example3.cpp new file mode 100644 index 00000000..e78a2cd9 --- /dev/null +++ b/src/boost/libs/dynamic_bitset/example/example3.cpp @@ -0,0 +1,71 @@ +// Copyright (c) 2001 Jeremy Siek +// Copyright (c) 2008 Gennaro Prota +// 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) +// +// Sample run: +// +// mask = 101010101010 +// x.size() = 0 +// Enter a bitset in binary: x = 100100010 +// +// Input number: 100100010 +// x.size() is now: 9 +// As unsigned long: 290 +// Mask (possibly resized): 010101010 +// And with mask: 000100010 +// Or with mask: 110101010 +// Shifted left by 1: 001000100 +// Shifted right by 1: 010010001 + + + +#include "boost/dynamic_bitset.hpp" + +#include <ostream> +#include <iostream> + +int main() +{ + boost::dynamic_bitset<> mask(12, 2730ul); + std::cout << "mask = " << mask << std::endl; + + boost::dynamic_bitset<> x; + std::cout << "x.size() = " << x.size() << std::endl; + + std::cout << "Enter a bitset in binary: x = " << std::flush; + if (std::cin >> x) { + const std::size_t sz = x.size(); + std::cout << std::endl; + std::cout << "Input number: " << x << std::endl; + std::cout << "x.size() is now: " << sz << std::endl; + + bool fits_in_ulong = true; + unsigned long ul = 0; + try { + ul = x.to_ulong(); + } catch(std::overflow_error &) { + fits_in_ulong = false; + } + + std::cout << "As unsigned long: "; + if(fits_in_ulong) { + std::cout << ul; + } else { + std::cout << "(overflow exception)"; + } + + std::cout << std::endl; + + mask.resize(sz); + + std::cout << "Mask (possibly resized): " << mask << std::endl; + + std::cout << "And with mask: " << (x & mask) << std::endl; + std::cout << "Or with mask: " << (x | mask) << std::endl; + std::cout << "Shifted left by 1: " << (x << 1) << std::endl; + std::cout << "Shifted right by 1: " << (x >> 1) << std::endl; + } + return 0; +} diff --git a/src/boost/libs/dynamic_bitset/example/timing_tests.cpp b/src/boost/libs/dynamic_bitset/example/timing_tests.cpp new file mode 100644 index 00000000..0e96bf43 --- /dev/null +++ b/src/boost/libs/dynamic_bitset/example/timing_tests.cpp @@ -0,0 +1,142 @@ +// ----------------------------------------------------------- +// +// Copyright (c) 2003-2004 Gennaro Prota +// +// 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) +// +// ----------------------------------------------------------- + +// boost::dynamic_bitset timing tests +// +// NOTE: +// ~~~~~ +// This is a preliminary, incomplete version. +// +// If you are interested in having more benchmarks please make a +// request on the boost list, which could encourage me to continue +// this work. + +// Also, if you use boost::dynamic_bitset on a platform where +// CHAR_BIT >= 9 I suggest experimenting with the size of the count +// table in detail/dynamic_bitset.hpp and report any interesting +// discovery on the list as well. + +// You might also want to try both counting methods (by_bytes vs. +// by_blocks) to see if the one that is selected automatically is +// actually the fastest on your system. + +// +// +// -----------------------------------------------------------------------// + + +#include "boost/config.hpp" + +#if defined (__STL_CONFIG_H) && !defined (__STL_USE_NEW_IOSTREAMS) + // for pre 3.0 versions of libstdc++ +# define BOOST_OLD_IOSTREAMS +#endif +// ------------------------------------------------- // + +#include <typeinfo> +#include <iostream> +#if !defined(BOOST_OLD_IOSTREAMS) +# include <ostream> +#endif + + +#include "boost/cstdlib.hpp" +#include "boost/version.hpp" +#include "boost/timer/timer.hpp" +#include "boost/dynamic_bitset.hpp" + + +namespace { + + // the m_ prefixes, below, are mainly to avoid problems with g++: + // see http://gcc.gnu.org/ml/gcc-bugs/1999-03n/msg00884.html + // + class boost_version { + int m_major; + int m_minor; + int m_subminor; + + public: + boost_version(unsigned long v = BOOST_VERSION): + m_major(v / 100000), m_minor(v / 100 % 1000), m_subminor(v % 100) {} + + friend std::ostream & operator<<(std::ostream &, const boost_version &); + }; + + + // give up using basic_ostream, to avoid headaches with old libraries + std::ostream& operator<<(std::ostream& os, const boost_version & v) { + return os << v.m_major << '.' << v.m_minor << '.' << v.m_subminor; + } + +} + + +void prologue() +{ + std::cout << '\n'; + std::cout << "Compiler: " << BOOST_COMPILER << '\n'; + std::cout << "Std lib : " << BOOST_STDLIB << '\n'; + std::cout << "Boost v.: " << boost_version() << '\n'; + + std::cout << '\n'; +} + + + +template <typename T> +void timing_test(T* = 0) // dummy parameter to workaround VC6 +{ +#ifndef BOOST_NO_STRESS_TEST + const unsigned long num = 30 * 100000; +#else + const unsigned long num = 30 * 1000; +#endif + + + // This variable is printed at the end of the test, + // to prevent the optimizer from removing the call to + // count() in the loop below. + typename boost::dynamic_bitset<T>::size_type dummy = 0; + + std::cout << "\nTimings for dynamic_bitset<" << typeid(T).name() + << "> [" << num << " iterations]\n"; + std::cout << "--------------------------------------------------\n"; + + { + boost::timer::auto_cpu_timer time; + + const typename boost::dynamic_bitset<T>::size_type sz = 5000; + for (unsigned long i = 0; i < num; ++i) { + boost::dynamic_bitset<T> bs(sz, i); + dummy += bs.count(); + } + } + + std::cout << "(total count: " << dummy << ")\n\n"; +} + + + +int main() +{ + prologue(); + + timing_test<unsigned char>(); + timing_test<unsigned short>(); + timing_test<unsigned int>(); + timing_test<unsigned long>(); +# ifdef BOOST_HAS_LONG_LONG + timing_test< ::boost::ulong_long_type>(); +# endif + + return boost::exit_success; +} + diff --git a/src/boost/libs/dynamic_bitset/index.html b/src/boost/libs/dynamic_bitset/index.html new file mode 100644 index 00000000..d7a08c1e --- /dev/null +++ b/src/boost/libs/dynamic_bitset/index.html @@ -0,0 +1,15 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> +<html> +<head> +<meta http-equiv=refresh content="0; URL=dynamic_bitset.html"> +<title>Automatic redirection</title> +</head> +<body> +Automatic redirection failed, please go to +<a href="dynamic_bitset.html">dynamic_bitset.html</a>. <hr> +<p>© Copyright Beman Dawes, 2001</p> +<p>Distributed under the Boost Software License, Version 1.0. (See accompanying +file <a href="../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or copy +at <a href="http://www.boost.org/LICENSE_1_0.txt">www.boost.org/LICENSE_1_0.txt</a>)</p> +</body> +</html>
\ No newline at end of file diff --git a/src/boost/libs/dynamic_bitset/meta/libraries.json b/src/boost/libs/dynamic_bitset/meta/libraries.json new file mode 100644 index 00000000..2b872e2e --- /dev/null +++ b/src/boost/libs/dynamic_bitset/meta/libraries.json @@ -0,0 +1,16 @@ +{ + "key": "dynamic_bitset", + "name": "Dynamic Bitset", + "authors": [ + "Jeremy Siek", + "Chuck Allison" + ], + "description": "The dynamic_bitset class represents a set of bits. It provides accesses to the value of individual bits via an operator[] and provides all of the bitwise operators that one can apply to builtin integers, such as operator& and operator<<. The number of bits in the set is specified at runtime via a parameter to the constructor of the dynamic_bitset.", + "documentation": "dynamic_bitset.html", + "category": [ + "Containers" + ], + "maintainers": [ + "Jeremy Siek <jeremy.siek -at- gmail.com>" + ] +} diff --git a/src/boost/libs/dynamic_bitset/test/Jamfile.v2 b/src/boost/libs/dynamic_bitset/test/Jamfile.v2 new file mode 100644 index 00000000..91f91def --- /dev/null +++ b/src/boost/libs/dynamic_bitset/test/Jamfile.v2 @@ -0,0 +1,42 @@ +# +# Copyright Vladimir Prus 2004 +# +# 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) +# + +import os ; + +# import rules for testing conditional on config file variables +import ../../config/checks/config : requires ; + +test-suite dynamic_bitset : + + [ run dyn_bitset_unit_tests1.cpp : : : <library>/boost/system//boost_system ] + [ run dyn_bitset_unit_tests2.cpp : : : <library>/boost/system//boost_system ] + [ run dyn_bitset_unit_tests3.cpp : : : <library>/boost/system//boost_system ] + [ run dyn_bitset_unit_tests4.cpp : : : <library>/boost/filesystem//boost_filesystem + <library>/boost/system//boost_system ] + [ run test_ambiguous_set.cpp ] + [ run test_lowest_bit.cpp ] + + [ run test_boost_hash.cpp ] + [ run test_std_hash.cpp : : : [ requires cxx11_hdr_unordered_set ] ] + + [ compile-fail test_std_hash.cpp : [ requires cxx11_hdr_unordered_set ] + <define>BOOST_DYNAMIC_BITSET_NO_STD_HASH + : test_std_hash_disabled ] + ; + +# due to https://github.com/boostorg/serialization/issues/108 +if ! [ os.environ UBSAN_OPTIONS ] +{ + test-suite dynamic_bitset_serialization : + + [ run dyn_bitset_unit_tests5.cpp + : : : <define>_SCL_SECURE_NO_WARNINGS=1 + <library>/boost/serialization//boost_serialization + <library>/boost/system//boost_system ] + ; +} diff --git a/src/boost/libs/dynamic_bitset/test/bitset_test.hpp b/src/boost/libs/dynamic_bitset/test/bitset_test.hpp new file mode 100644 index 00000000..669aca5b --- /dev/null +++ b/src/boost/libs/dynamic_bitset/test/bitset_test.hpp @@ -0,0 +1,1454 @@ +// ----------------------------------------------------------- +// Copyright (c) 2001 Jeremy Siek +// Copyright (c) 2003-2006, 2008 Gennaro Prota +// Copyright (c) 2014 Ahmed Charles +// Copyright (c) 2014 Riccardo Marcangelo +// Copyright (c) 2018 Evgeny Shulgin +// +// 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) +// +// ----------------------------------------------------------- + +#ifndef BOOST_BITSET_TEST_HPP_GP_20040319 +#define BOOST_BITSET_TEST_HPP_GP_20040319 + +#include <boost/config.hpp> +#if !defined (BOOST_NO_STD_LOCALE) +# include <locale> +#endif + +#include <vector> +#include <fstream> // used for operator<< +#include <string> // for (basic_string and) getline() +#include <algorithm> // for std::min +#include <assert.h> // <cassert> is sometimes macro-guarded :-( + +#include <boost/limits.hpp> +#include <boost/dynamic_bitset/dynamic_bitset.hpp> +#include <boost/core/lightweight_test.hpp> +#include <boost/filesystem.hpp> + +template <typename Block> +inline bool nth_bit(Block num, std::size_t n) +{ +#ifndef NDEBUG +#ifdef __BORLANDC__ + // Borland deduces Block as a const qualified type, + // and thus finds numeric_limits<Block> to be zero :( + // (though not directly relevant here, see also + // lib issue 559) + int block_width = sizeof(Block) * CHAR_BIT; +#else + int block_width = std::numeric_limits<Block>::digits; +#endif + assert(n < (std::size_t) block_width); +#endif + + return (num >> n) & 1; +} + +// A long, 'irregular', string useful for various tests +std::string get_long_string() +{ + const char * const p = + // 6 5 4 3 2 1 + // 3210987654321098765432109876543210987654321098765432109876543210 + "1110011100011110000011110000011111110000000000000110101110000000" + "1010101000011100011101010111110000101011111000001111100011100011" + "0000000110000001000000111100000111100010101111111000000011100011" + "1111111111111111111111111111111111111111111111111111111111111100" + "1000001100000001111111111111110000000011111000111100001010100000" + "101000111100011010101110011011000000010"; + + return std::string(p); +} + +class scoped_temp_file +{ +public: + scoped_temp_file() + : m_path(boost::filesystem::unique_path()) + { + } + + ~scoped_temp_file() + { + boost::filesystem::remove(m_path); + } + + const boost::filesystem::path& path() const + { + return m_path; + } + +private: + boost::filesystem::path m_path; +}; + +#if defined BOOST_OLD_IOSTREAMS || defined BOOST_NO_STD_LOCALE +template <typename Stream> +bool is_one_or_zero(const Stream & /*s*/, char c) +{ + return c == '1' || c == '0'; +} +template <typename Stream> +bool is_white_space(const Stream & /*s*/, char c) +{ + return std::isspace(c); +} +#else +template <typename Stream, typename Ch> +bool is_one_or_zero(const Stream& s, Ch c) +{ + typedef typename Stream::traits_type Tr; + const Ch zero = s.widen('0'); + const Ch one = s.widen('1'); + + return Tr::eq(c, one) || Tr::eq(c, zero); +} +template <typename Stream, typename Ch> +bool is_white_space(const Stream & s, Ch c) +{ + // NOTE: the using directive is to satisfy Borland 5.6.4 + // with its own library (STLport), which doesn't + // like std::isspace(c, loc) + using namespace std; + return isspace(c, s.getloc()); +} +#endif // defined BOOST_OLD_IOSTREAMS + + +template <typename Stream> +bool has_flags(const Stream& s, std::ios::iostate flags) +{ + return (s.rdstate() & flags) != std::ios::goodbit; +} + + +// constructors +// default (can't do this generically) + +template <typename Bitset> +struct bitset_test { + + typedef typename Bitset::block_type Block; + BOOST_STATIC_CONSTANT(int, bits_per_block = Bitset::bits_per_block); + + // from unsigned long + // + // Note: this is templatized so that we check that the do-the-right-thing + // constructor dispatch is working correctly. + // + template <typename NumBits, typename Value> + static void from_unsigned_long(NumBits num_bits, Value num) + { + // An object of size sz = num_bits is constructed: + // - the first m bit positions are initialized to the corresponding + // bit values in num (m being the smaller of sz and ulong_width) + // + // - any remaining bit positions are initialized to zero + // + + Bitset b(static_cast<typename Bitset::size_type>(num_bits), static_cast<unsigned long>(num)); + + // OK, we can now cast to size_type + typedef typename Bitset::size_type size_type; + const size_type sz = static_cast<size_type>(num_bits); + + BOOST_TEST(b.size() == sz); + + const std::size_t ulong_width = std::numeric_limits<unsigned long>::digits; + size_type m = sz; + if (ulong_width < sz) + m = ulong_width; + + size_type i = 0; + for ( ; i < m; ++i) + BOOST_TEST(b.test(i) == nth_bit(static_cast<unsigned long>(num), i)); + for ( ; i < sz; ++i) + BOOST_TEST(b.test(i) == 0); + } + + // from string + // + // Note: The corresponding function in dynamic_bitset (constructor + // from a string) has several default arguments. Actually we don't + // test the correct working of those defaults here (except for the + // default of num_bits). I'm not sure what to do in this regard. + // + // Note2: the default argument expression for num_bits doesn't use + // static_cast, to avoid a gcc 2.95.3 'sorry, not implemented' + // + template <typename Ch, typename Tr, typename Al> + static void from_string(const std::basic_string<Ch, Tr, Al>& str, + std::size_t pos, + std::size_t max_char, + std::size_t num_bits = (std::size_t)(-1)) + { + + std::size_t rlen = (std::min)(max_char, str.size() - pos); + + // The resulting size N of the bitset is num_bits, if + // that is different from the default arg, rlen otherwise. + // Put M = the smaller of N and rlen, then character + // position pos + M - 1 corresponds to bit position zero. + // Subsequent decreasing character positions correspond to + // increasing bit positions. + + const bool size_upon_string = num_bits == (std::size_t)(-1); + Bitset b = size_upon_string ? + Bitset(str, pos, max_char) + : Bitset(str, pos, max_char, num_bits); + + const std::size_t actual_size = size_upon_string? rlen : num_bits; + BOOST_TEST(b.size() == actual_size); + std::size_t m = (std::min)(num_bits, rlen); + std::size_t j; + for (j = 0; j < m; ++j) + BOOST_TEST(b[j] == (str[pos + m - 1 - j] == '1')); + // If M < N, remaining bit positions are zero + for (; j < actual_size; ++j) + BOOST_TEST(b[j] == 0); + + + } + + static void to_block_range(const Bitset & b /*, BlockOutputIterator result*/) + { + typedef typename Bitset::size_type size_type; + + Block sentinel = 0xF0; + int s = 8; // number of sentinels (must be *even*) + int offset = s/2; + std::vector<Block> v(b.num_blocks() + s, sentinel); + + boost::to_block_range(b, v.begin() + offset); + + assert(v.size() >= (size_type)s && (s >= 2) && (s % 2 == 0)); + // check sentinels at both ends + for(int i = 0; i < s/2; ++i) { + BOOST_TEST(v[i] == sentinel); + BOOST_TEST(v[v.size()-1-i] == sentinel); + } + + typename std::vector<Block>::const_iterator p = v.begin() + offset; + for(size_type n = 0; n < b.num_blocks(); ++n, ++p) { + typename Bitset::block_width_type i = 0; + for(; i < bits_per_block; ++i) { + size_type bit = n * bits_per_block + i; + BOOST_TEST(nth_bit(*p, i) == (bit < b.size()? b[bit] : 0)); + } + } + } + + // TODO from_block_range (below) should be splitted + + // PRE: std::equal(first1, last1, first2) == true + static void from_block_range(const std::vector<Block>& blocks) + { + { // test constructor from block range + Bitset bset(blocks.begin(), blocks.end()); + std::size_t n = blocks.size(); + for (std::size_t b = 0; b < n; ++b) { + typename Bitset::block_width_type i = 0; + for (; i < bits_per_block; ++i) { + std::size_t bit = b * bits_per_block + i; + BOOST_TEST(bset[bit] == nth_bit(blocks[b], i)); + } + } + BOOST_TEST(bset.size() == n * bits_per_block); + } + { // test boost::from_block_range + const typename Bitset::size_type n = blocks.size(); + Bitset bset(n * bits_per_block); + boost::from_block_range(blocks.begin(), blocks.end(), bset); + for (std::size_t b = 0; b < n; ++b) { + typename Bitset::block_width_type i = 0; + for (; i < bits_per_block; ++i) { + std::size_t bit = b * bits_per_block + i; + BOOST_TEST(bset[bit] == nth_bit(blocks[b], i)); + } + } + BOOST_TEST(n <= bset.num_blocks()); + } + } + + // copy constructor (absent from std::bitset) + static void copy_constructor(const Bitset& b) + { + Bitset copy(b); + BOOST_TEST(b == copy); + + // Changes to the copy do not affect the original + if (b.size() > 0) { + std::size_t pos = copy.size() / 2; + copy.flip(pos); + BOOST_TEST(copy[pos] != b[pos]); + } + } + + // copy assignment operator (absent from std::bitset) + static void copy_assignment_operator(const Bitset& lhs, const Bitset& rhs) + { + Bitset b(lhs); + b = rhs; + b = b; // self assignment check + BOOST_TEST(b == rhs); + + // Changes to the copy do not affect the original + if (b.size() > 0) { + std::size_t pos = b.size() / 2; + b.flip(pos); + BOOST_TEST(b[pos] != rhs[pos]); + } + } + + static void max_size(const Bitset& b) + { + BOOST_TEST(b.max_size() > 0); + } + +#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES + + // move constructor (absent from std::bitset) + static void move_constructor(const Bitset& b) + { + Bitset copy(boost::move(b)); + BOOST_TEST(b == copy); + } + + // move assignment operator (absent from std::bitset) + static void move_assignment_operator(const Bitset& lhs, const Bitset& rhs) + { + Bitset b(lhs); + Bitset c(rhs); + b = boost::move(c); + b = boost::move(b); // self assignment check + BOOST_TEST(b == rhs); + } + +#endif // BOOST_NO_CXX11_RVALUE_REFERENCES + + static void swap(const Bitset& lhs, const Bitset& rhs) + { + // bitsets must be swapped + Bitset copy1(lhs); + Bitset copy2(rhs); + copy1.swap(copy2); + + BOOST_TEST(copy1 == rhs); + BOOST_TEST(copy2 == lhs); + + // references must be stable under a swap + for(typename Bitset::size_type i = 0; i < lhs.size(); ++i) { + Bitset b1(lhs); + Bitset b2(rhs); + typename Bitset::reference ref = b1[i]; + bool x = ref; + if (i < b2.size()) + b2[i] = !x; // make sure b2[i] is different + b1.swap(b2); + BOOST_TEST(b2[i] == x); // now it must be equal.. + b2.flip(i); + BOOST_TEST(ref == b2[i]); // .. and ref must be into b2 + BOOST_TEST(ref == !x); + } + + } + + static void resize(const Bitset& lhs) + { + Bitset b(lhs); + + // Test no change in size + b.resize(lhs.size()); + BOOST_TEST(b == lhs); + + // Test increase in size + b.resize(lhs.size() * 2, true); + + std::size_t i; + for (i = 0; i < lhs.size(); ++i) + BOOST_TEST(b[i] == lhs[i]); + for (; i < b.size(); ++i) + BOOST_TEST(b[i] == true); + + // Test decrease in size + b.resize(lhs.size()); + for (i = 0; i < lhs.size(); ++i) + BOOST_TEST(b[i] == lhs[i]); + } + + static void clear(const Bitset& lhs) + { + Bitset b(lhs); + b.clear(); + BOOST_TEST(b.size() == 0); + } + + static void pop_back(const Bitset& lhs) + { + Bitset b(lhs); + b.pop_back(); + BOOST_TEST(b.size() == lhs.size() - 1); + for (std::size_t i = 0; i < b.size(); ++i) + BOOST_TEST(b[i] == lhs[i]); + + b.pop_back(); + BOOST_TEST(b.size() == lhs.size() - 2); + for (std::size_t j = 0; j < b.size(); ++j) + BOOST_TEST(b[j] == lhs[j]); + } + + static void append_bit(const Bitset& lhs) + { + Bitset b(lhs); + b.push_back(true); + BOOST_TEST(b.size() == lhs.size() + 1); + BOOST_TEST(b[b.size() - 1] == true); + for (std::size_t i = 0; i < lhs.size(); ++i) + BOOST_TEST(b[i] == lhs[i]); + + b.push_back(false); + BOOST_TEST(b.size() == lhs.size() + 2); + BOOST_TEST(b[b.size() - 1] == false); + BOOST_TEST(b[b.size() - 2] == true); + for (std::size_t j = 0; j < lhs.size(); ++j) + BOOST_TEST(b[j] == lhs[j]); + } + + static void append_block(const Bitset& lhs) + { + Bitset b(lhs); + Block value(128); + b.append(value); + BOOST_TEST(b.size() == lhs.size() + bits_per_block); + for (typename Bitset::block_width_type i = 0; i < bits_per_block; ++i) + BOOST_TEST(b[lhs.size() + i] == bool((value >> i) & 1)); + } + + static void append_block_range(const Bitset& lhs, const std::vector<Block>& blocks) + { + Bitset b(lhs), c(lhs); + b.append(blocks.begin(), blocks.end()); + for (typename std::vector<Block>::const_iterator i = blocks.begin(); + i != blocks.end(); ++i) + c.append(*i); + BOOST_TEST(b == c); + } + + // operator[] and reference members + // PRE: b[i] == bit_vec[i] + static void operator_bracket(const Bitset& lhs, const std::vector<bool>& bit_vec) + { + Bitset b(lhs); + std::size_t i, j, k; + + // x = b[i] + // x = ~b[i] + for (i = 0; i < b.size(); ++i) { + bool x = b[i]; + BOOST_TEST(x == bit_vec[i]); + x = ~b[i]; + BOOST_TEST(x == !bit_vec[i]); + } + Bitset prev(b); + + // b[i] = x + for (j = 0; j < b.size(); ++j) { + bool x = !prev[j]; + b[j] = x; + for (k = 0; k < b.size(); ++k) + if (j == k) + BOOST_TEST(b[k] == x); + else + BOOST_TEST(b[k] == prev[k]); + b[j] = prev[j]; + } + b.flip(); + + // b[i] = b[j] + for (i = 0; i < b.size(); ++i) { + b[i] = prev[i]; + for (j = 0; j < b.size(); ++j) { + if (i == j) + BOOST_TEST(b[j] == prev[j]); + else + BOOST_TEST(b[j] == !prev[j]); + } + b[i] = !prev[i]; + } + + // b[i].flip() + for (i = 0; i < b.size(); ++i) { + b[i].flip(); + for (j = 0; j < b.size(); ++j) { + if (i == j) + BOOST_TEST(b[j] == prev[j]); + else + BOOST_TEST(b[j] == !prev[j]); + } + b[i].flip(); + } + } + + //=========================================================================== + // bitwise operators + + // bitwise and assignment + + // PRE: b.size() == rhs.size() + static void and_assignment(const Bitset& b, const Bitset& rhs) + { + Bitset lhs(b); + Bitset prev(lhs); + lhs &= rhs; + // Clears each bit in lhs for which the corresponding bit in rhs is + // clear, and leaves all other bits unchanged. + for (std::size_t I = 0; I < lhs.size(); ++I) + if (rhs[I] == 0) + BOOST_TEST(lhs[I] == 0); + else + BOOST_TEST(lhs[I] == prev[I]); + } + + // PRE: b.size() == rhs.size() + static void or_assignment(const Bitset& b, const Bitset& rhs) + { + Bitset lhs(b); + Bitset prev(lhs); + lhs |= rhs; + // Sets each bit in lhs for which the corresponding bit in rhs is set, and + // leaves all other bits unchanged. + for (std::size_t I = 0; I < lhs.size(); ++I) + if (rhs[I] == 1) + BOOST_TEST(lhs[I] == 1); + else + BOOST_TEST(lhs[I] == prev[I]); + } + + // PRE: b.size() == rhs.size() + static void xor_assignment(const Bitset& b, const Bitset& rhs) + { + Bitset lhs(b); + Bitset prev(lhs); + lhs ^= rhs; + // Flips each bit in lhs for which the corresponding bit in rhs is set, + // and leaves all other bits unchanged. + for (std::size_t I = 0; I < lhs.size(); ++I) + if (rhs[I] == 1) + BOOST_TEST(lhs[I] == !prev[I]); + else + BOOST_TEST(lhs[I] == prev[I]); + } + + // PRE: b.size() == rhs.size() + static void sub_assignment(const Bitset& b, const Bitset& rhs) + { + Bitset lhs(b); + Bitset prev(lhs); + lhs -= rhs; + // Resets each bit in lhs for which the corresponding bit in rhs is set, + // and leaves all other bits unchanged. + for (std::size_t I = 0; I < lhs.size(); ++I) + if (rhs[I] == 1) + BOOST_TEST(lhs[I] == 0); + else + BOOST_TEST(lhs[I] == prev[I]); + } + + static void shift_left_assignment(const Bitset& b, std::size_t pos) + { + Bitset lhs(b); + Bitset prev(lhs); + lhs <<= pos; + // Replaces each bit at position I in lhs with the following value: + // - If I < pos, the new value is zero + // - If I >= pos, the new value is the previous value of the bit at + // position I - pos + for (std::size_t I = 0; I < lhs.size(); ++I) + if (I < pos) + BOOST_TEST(lhs[I] == 0); + else + BOOST_TEST(lhs[I] == prev[I - pos]); + } + + static void shift_right_assignment(const Bitset& b, std::size_t pos) + { + Bitset lhs(b); + Bitset prev(lhs); + lhs >>= pos; + // Replaces each bit at position I in lhs with the following value: + // - If pos >= N - I, the new value is zero + // - If pos < N - I, the new value is the previous value of the bit at + // position I + pos + std::size_t N = lhs.size(); + for (std::size_t I = 0; I < N; ++I) + if (pos >= N - I) + BOOST_TEST(lhs[I] == 0); + else + BOOST_TEST(lhs[I] == prev[I + pos]); + } + + + static void set_all(const Bitset& b) + { + Bitset lhs(b); + lhs.set(); + for (std::size_t I = 0; I < lhs.size(); ++I) + BOOST_TEST(lhs[I] == 1); + } + + static void set_one(const Bitset& b, std::size_t pos, bool value) + { + Bitset lhs(b); + std::size_t N = lhs.size(); + if (pos < N) { + Bitset prev(lhs); + // Stores a new value in the bit at position pos in lhs. + lhs.set(pos, value); + BOOST_TEST(lhs[pos] == value); + + // All other values of lhs remain unchanged + for (std::size_t I = 0; I < N; ++I) + if (I != pos) + BOOST_TEST(lhs[I] == prev[I]); + } else { + // Not in range, doesn't satisfy precondition. + } + } + + static void set_segment(const Bitset& b, std::size_t pos, + std::size_t len, bool value) + { + Bitset lhs(b); + std::size_t N = lhs.size(); + Bitset prev(lhs); + lhs.set(pos, len, value); + for (std::size_t I = 0; I < N; ++I) + { + if (I < pos || I >= pos + len) + BOOST_TEST(lhs[I] == prev[I]); + else + BOOST_TEST(lhs[I] == value); + } + } + + static void reset_all(const Bitset& b) + { + Bitset lhs(b); + // Resets all bits in lhs + lhs.reset(); + for (std::size_t I = 0; I < lhs.size(); ++I) + BOOST_TEST(lhs[I] == 0); + } + + static void reset_one(const Bitset& b, std::size_t pos) + { + Bitset lhs(b); + std::size_t N = lhs.size(); + if (pos < N) { + Bitset prev(lhs); + lhs.reset(pos); + // Resets the bit at position pos in lhs + BOOST_TEST(lhs[pos] == 0); + + // All other values of lhs remain unchanged + for (std::size_t I = 0; I < N; ++I) + if (I != pos) + BOOST_TEST(lhs[I] == prev[I]); + } else { + // Not in range, doesn't satisfy precondition. + } + } + + static void reset_segment(const Bitset& b, std::size_t pos, + std::size_t len) + { + Bitset lhs(b); + std::size_t N = lhs.size(); + Bitset prev(lhs); + lhs.reset(pos, len); + for (std::size_t I = 0; I < N; ++I) + { + if (I < pos || I >= pos + len) + BOOST_TEST(lhs[I] == prev[I]); + else + BOOST_TEST(!lhs[I]); + } + } + + static void operator_flip(const Bitset& b) + { + Bitset lhs(b); + Bitset x(lhs); + BOOST_TEST(~lhs == x.flip()); + } + + static void flip_all(const Bitset& b) + { + Bitset lhs(b); + std::size_t N = lhs.size(); + Bitset prev(lhs); + lhs.flip(); + // Toggles all the bits in lhs + for (std::size_t I = 0; I < N; ++I) + BOOST_TEST(lhs[I] == !prev[I]); + } + + static void flip_one(const Bitset& b, std::size_t pos) + { + Bitset lhs(b); + std::size_t N = lhs.size(); + if (pos < N) { + Bitset prev(lhs); + lhs.flip(pos); + // Toggles the bit at position pos in lhs + BOOST_TEST(lhs[pos] == !prev[pos]); + + // All other values of lhs remain unchanged + for (std::size_t I = 0; I < N; ++I) + if (I != pos) + BOOST_TEST(lhs[I] == prev[I]); + } else { + // Not in range, doesn't satisfy precondition. + } + } + + static void flip_segment(const Bitset& b, std::size_t pos, + std::size_t len) + { + Bitset lhs(b); + std::size_t N = lhs.size(); + Bitset prev(lhs); + lhs.flip(pos, len); + for (std::size_t I = 0; I < N; ++I) + { + if (I < pos || I >= pos + len) + BOOST_TEST(lhs[I] == prev[I]); + else + BOOST_TEST(lhs[I] != prev[I]); + } + } + + // empty + static void empty(const Bitset& b) + { + BOOST_TEST(b.empty() == (b.size() == 0)); + } + + // to_ulong() + static void to_ulong(const Bitset& lhs) + { + typedef unsigned long result_type; + std::size_t n = std::numeric_limits<result_type>::digits; + std::size_t sz = lhs.size(); + + bool will_overflow = false; + for (std::size_t i = n; i < sz; ++i) { + if (lhs.test(i) != 0) { + will_overflow = true; + break; + } + } + if (will_overflow) { + try { + (void)lhs.to_ulong(); + BOOST_TEST(false); // It should have thrown an exception + } catch (std::overflow_error & ex) { + // Good! + BOOST_TEST(!!ex.what()); + } catch (...) { + BOOST_TEST(false); // threw the wrong exception + } + } else { + result_type num = lhs.to_ulong(); + // Be sure the number is right + if (sz == 0) + BOOST_TEST(num == 0); + else { + for (std::size_t i = 0; i < sz; ++i) + BOOST_TEST(lhs[i] == (i < n ? nth_bit(num, i) : 0)); + } + } + } + + // to_string() + static void to_string(const Bitset& b) + { + std::string str; + boost::to_string(b, str); + BOOST_TEST(str.size() == b.size()); + for (std::size_t i = 0; i < b.size(); ++i) + BOOST_TEST(str[b.size() - 1 - i] ==(b.test(i)? '1':'0')); + } + + static void count(const Bitset& b) + { + std::size_t c = b.count(); + std::size_t actual = 0; + for (std::size_t i = 0; i < b.size(); ++i) + if (b[i]) + ++actual; + BOOST_TEST(c == actual); + } + + static void size(const Bitset& b) + { + BOOST_TEST(Bitset(b).set().count() == b.size()); + } + + static void capacity_test_one(const Bitset& lhs) + { + //empty bitset + Bitset b(lhs); + BOOST_TEST(b.capacity() == 0); + } + + static void capacity_test_two(const Bitset& lhs) + { + //bitset constructed with size "100" + Bitset b(lhs); + BOOST_TEST(b.capacity() >= 100); + b.resize(200); + BOOST_TEST(b.capacity() >= 200); + } + + static void reserve_test_one(const Bitset& lhs) + { + //empty bitset + Bitset b(lhs); + b.reserve(16); + BOOST_TEST(b.capacity() >= 16); + } + + static void reserve_test_two(const Bitset& lhs) + { + //bitset constructed with size "100" + Bitset b(lhs); + BOOST_TEST(b.capacity() >= 100); + b.reserve(60); + BOOST_TEST(b.size() == 100); + BOOST_TEST(b.capacity() >= 100); + b.reserve(160); + BOOST_TEST(b.size() == 100); + BOOST_TEST(b.capacity() >= 160); + } + + static void shrink_to_fit_test_one(const Bitset& lhs) + { + //empty bitset + Bitset b(lhs); + b.shrink_to_fit(); + BOOST_TEST(b.size() == 0); + BOOST_TEST(b.capacity() == 0); + } + + static void shrink_to_fit_test_two(const Bitset& lhs) + { + //bitset constructed with size "100" + Bitset b(lhs); + b.shrink_to_fit(); + BOOST_TEST(b.capacity() >= 100); + BOOST_TEST(b.size() == 100); + b.reserve(200); + BOOST_TEST(b.capacity() >= 200); + BOOST_TEST(b.size() == 100); + b.shrink_to_fit(); + BOOST_TEST(b.capacity() < 200); + BOOST_TEST(b.size() == 100); + } + + static void all(const Bitset& b) + { + BOOST_TEST(b.all() == (b.count() == b.size())); + bool result = true; + for(std::size_t i = 0; i < b.size(); ++i) + if(!b[i]) { + result = false; + break; + } + BOOST_TEST(b.all() == result); + } + + static void any(const Bitset& b) + { + BOOST_TEST(b.any() == (b.count() != 0)); + bool result = false; + for(std::size_t i = 0; i < b.size(); ++i) + if(b[i]) { + result = true; + break; + } + BOOST_TEST(b.any() == result); + } + + static void none(const Bitset& b) + { + bool result = true; + for(std::size_t i = 0; i < b.size(); ++i) { + if(b[i]) { + result = false; + break; + } + } + BOOST_TEST(b.none() == result); + + // sanity + BOOST_TEST(b.none() == !b.any()); + BOOST_TEST(b.none() == (b.count() == 0)); + } + + static void subset(const Bitset& a, const Bitset& b) + { + BOOST_TEST(a.size() == b.size()); // PRE + + bool is_subset = true; + if (b.size()) { // could use b.any() but let's be safe + for(std::size_t i = 0; i < a.size(); ++i) { + if(a.test(i) && !b.test(i)) { + is_subset = false; + break; + } + } + } + else { + // sanity + BOOST_TEST(a.count() == 0); + BOOST_TEST(a.any() == false); + + //is_subset = (a.any() == false); + } + + BOOST_TEST(a.is_subset_of(b) == is_subset); + } + + static void proper_subset(const Bitset& a, const Bitset& b) + { + // PRE: a.size() == b.size() + BOOST_TEST(a.size() == b.size()); + + bool is_proper = false; + + if (b.size() != 0) { + + // check it's a subset + subset(a, b); + + // is it proper? + for (std::size_t i = 0; i < a.size(); ++i) { + if (!a.test(i) && b.test(i)) { + is_proper = true; + // sanity + BOOST_TEST(a.count() < b.count()); + BOOST_TEST(b.any()); + } + } + } + + BOOST_TEST(a.is_proper_subset_of(b) == is_proper); + if (is_proper) + BOOST_TEST(b.is_proper_subset_of(a) != is_proper);// antisymmetry + } + + static void intersects(const Bitset& a, const Bitset& b) + { + bool have_intersection = false; + + typename Bitset::size_type m = a.size() < b.size() ? a.size() : b.size(); + for(typename Bitset::size_type i = 0; i < m && !have_intersection; ++i) + if(a[i] == true && b[i] == true) + have_intersection = true; + + BOOST_TEST(a.intersects(b) == have_intersection); + // also check commutativity + BOOST_TEST(b.intersects(a) == have_intersection); + } + + static void find_first(const Bitset& b) + { + // find first non-null bit, if any + typename Bitset::size_type i = 0; + while (i < b.size() && b[i] == 0) + ++i; + + if (i == b.size()) + BOOST_TEST(b.find_first() == Bitset::npos); // not found; + else { + BOOST_TEST(b.find_first() == i); + BOOST_TEST(b.test(i) == true); + } + + } + + static void find_next(const Bitset& b, typename Bitset::size_type prev) + { + BOOST_TEST(next_bit_on(b, prev) == b.find_next(prev)); + } + + static void operator_equal(const Bitset& a, const Bitset& b) + { + if (a == b) { + for (std::size_t I = 0; I < a.size(); ++I) + BOOST_TEST(a[I] == b[I]); + } else { + if (a.size() == b.size()) { + bool diff = false; + for (std::size_t I = 0; I < a.size(); ++I) + if (a[I] != b[I]) { + diff = true; + break; + } + BOOST_TEST(diff); + } + } + } + + static void operator_not_equal(const Bitset& a, const Bitset& b) + { + if (a != b) { + if (a.size() == b.size()) { + bool diff = false; + for (std::size_t I = 0; I < a.size(); ++I) + if (a[I] != b[I]) { + diff = true; + break; + } + BOOST_TEST(diff); + } + } else { + for (std::size_t I = 0; I < a.size(); ++I) + BOOST_TEST(a[I] == b[I]); + } + } + + static bool less_than(const Bitset& a, const Bitset& b) + { + + typedef BOOST_DEDUCED_TYPENAME Bitset::size_type size_type; + + size_type asize(a.size()); + size_type bsize(b.size()); + + if (!bsize) + { + return false; + } + else if (!asize) + { + return true; + } + else + { + + // Compare from most significant to least. + + size_type leqsize(std::min BOOST_PREVENT_MACRO_SUBSTITUTION(asize,bsize)); + size_type I; + for (I = 0; I < leqsize; ++I,--asize,--bsize) + { + + size_type i = asize-1; + size_type j = bsize-1; + + if (a[i] < b[j]) + return true; + else if (a[i] > b[j]) + return false; + // if (a[i] = b[j]) skip to next + } + return (a.size() < b.size()); + } + } + + static typename Bitset::size_type next_bit_on(const Bitset& b, typename Bitset::size_type prev) + { + // helper function for find_next() + // + + if (b.none() == true || prev == Bitset::npos) + return Bitset::npos; + + ++prev; + + if (prev >= b.size()) + return Bitset::npos; + + typename Bitset::size_type i = prev; + while (i < b.size() && b[i] == 0) + ++i; + + return i==b.size() ? Bitset::npos : i; + + } + + static void operator_less_than(const Bitset& a, const Bitset& b) + { + if (less_than(a, b)) + BOOST_TEST(a < b); + else + BOOST_TEST(!(a < b)); + } + + static void operator_greater_than(const Bitset& a, const Bitset& b) + { + if (less_than(a, b) || a == b) + BOOST_TEST(!(a > b)); + else + BOOST_TEST(a > b); + } + + static void operator_less_than_eq(const Bitset& a, const Bitset& b) + { + if (less_than(a, b) || a == b) + BOOST_TEST(a <= b); + else + BOOST_TEST(!(a <= b)); + } + + static void operator_greater_than_eq(const Bitset& a, const Bitset& b) + { + if (less_than(a, b)) + BOOST_TEST(!(a >= b)); + else + BOOST_TEST(a >= b); + } + + static void test_bit(const Bitset& b, std::size_t pos) + { + Bitset lhs(b); + std::size_t N = lhs.size(); + if (pos < N) { + BOOST_TEST(lhs.test(pos) == lhs[pos]); + } else { + // Not in range, doesn't satisfy precondition. + } + } + + static void test_set_bit(const Bitset& b, std::size_t pos, bool value) + { + Bitset lhs(b); + std::size_t N = lhs.size(); + if (pos < N) { + Bitset prev(lhs); + // Stores a new value in the bit at position pos in lhs. + BOOST_TEST(lhs.test_set(pos, value) == prev[pos]); + BOOST_TEST(lhs[pos] == value); + + // All other values of lhs remain unchanged + for (std::size_t I = 0; I < N; ++I) + if (I != pos) + BOOST_TEST(lhs[I] == prev[I]); + } else { + // Not in range, doesn't satisfy precondition. + } + } + + static void operator_shift_left(const Bitset& lhs, std::size_t pos) + { + Bitset x(lhs); + BOOST_TEST((lhs << pos) == (x <<= pos)); + } + + static void operator_shift_right(const Bitset& lhs, std::size_t pos) + { + Bitset x(lhs); + BOOST_TEST((lhs >> pos) == (x >>= pos)); + } + + // operator| + static + void operator_or(const Bitset& lhs, const Bitset& rhs) + { + Bitset x(lhs); + BOOST_TEST((lhs | rhs) == (x |= rhs)); + } + + // operator& + static + void operator_and(const Bitset& lhs, const Bitset& rhs) + { + Bitset x(lhs); + BOOST_TEST((lhs & rhs) == (x &= rhs)); + } + + // operator^ + static + void operator_xor(const Bitset& lhs, const Bitset& rhs) + { + Bitset x(lhs); + BOOST_TEST((lhs ^ rhs) == (x ^= rhs)); + } + + // operator- + static + void operator_sub(const Bitset& lhs, const Bitset& rhs) + { + Bitset x(lhs); + BOOST_TEST((lhs - rhs) == (x -= rhs)); + } + +//------------------------------------------------------------------------------ +// I/O TESTS + // The following tests assume the results of extraction (i.e.: contents, + // state and width of is, contents of b) only depend on input (the string + // str). In other words, they don't consider "unexpected" errors such as + // stream corruption or out of memory. The reason is simple: if e.g. the + // stream buffer throws, the stream layer may eat the exception and + // transform it into a badbit. But we can't trust the stream state here, + // because one of the things that we want to test is exactly whether it + // is set correctly. Similarly for insertion. + // + // To provide for these cases would require that the test functions know + // in advance whether the stream buffer and/or allocations will fail, and + // when; that is, we should write both a special allocator and a special + // stream buffer capable of throwing "on demand" and pass them here. + + // Seems overkill for these kinds of unit tests. + //------------------------------------------------------------------------- + + // operator<<( [basic_]ostream, + template <typename Stream> + static void stream_inserter(const Bitset & b, + Stream & s, + const char * file_name + ) + { +#if defined BOOST_OLD_IOSTREAMS + typedef char char_type; + typedef std::string string_type; + typedef ifstream corresponding_input_stream_type; +#else + typedef typename Stream::char_type char_type; + typedef std::basic_string<char_type> string_type; + typedef std::basic_ifstream<char_type> corresponding_input_stream_type; + + std::ios::iostate except = s.exceptions(); +#endif + + typedef typename Bitset::size_type size_type; + std::streamsize w = s.width(); + char_type fill_char = s.fill(); + std::ios::iostate oldstate = s.rdstate(); + bool stream_was_good = s.good(); + + bool did_throw = false; + try { + s << b; + } +#if defined BOOST_OLD_IOSTREAMS + catch(...) { + BOOST_TEST(false); + } +#else + catch (const std::ios_base::failure &) { + BOOST_TEST((except & s.rdstate()) != 0); + did_throw = true; + } catch (...) { + did_throw = true; + } +#endif + + BOOST_TEST(did_throw || !stream_was_good || (s.width() == 0)); + + if (!stream_was_good) { + BOOST_TEST(s.good() == false); + + // this should actually be oldstate == s.rdstate() + // but some implementations add badbit in the + // sentry constructor + // + BOOST_TEST((oldstate & s.rdstate()) == oldstate); + BOOST_TEST(s.width() == w); + } + else { + if(!did_throw) + BOOST_TEST(s.width() == 0); + // This test require that os be an output _and_ input stream. + // Of course dynamic_bitset's operator << doesn't require that. + + size_type total_len = w <= 0 || static_cast<size_type>(w) < b.size()? b.size() : static_cast<size_type>(w); + const string_type padding (total_len - b.size(), fill_char); + string_type expected; + boost::to_string(b, expected); + if ((s.flags() & std::ios::adjustfield) != std::ios::left) + expected = padding + expected; + else + expected = expected + padding; + + assert(expected.length() == total_len); + + // close, and reopen the file stream to verify contents + s.close(); + corresponding_input_stream_type is(file_name); + string_type contents; + std::getline(is, contents, char_type()); + BOOST_TEST(contents == expected); + } + } + + // operator>>( [basic_]istream + template <typename Stream, typename String> + static void stream_extractor(Bitset& b, + Stream& is, + String& str + ) + { + // save necessary info then do extraction + // + const std::streamsize w = is.width(); + Bitset a_copy(b); + bool stream_was_good = is.good(); + + bool did_throw = false; + +#if defined BOOST_OLD_IOSTREAMS + bool has_stream_exceptions = false; + is >> b; +#else + const std::ios::iostate except = is.exceptions(); + bool has_stream_exceptions = true; + try { + is >> b; + } + catch(const std::ios::failure &) { + did_throw = true; + } + + // postconditions + BOOST_TEST(except == is.exceptions()); // paranoid +#endif + //------------------------------------------------------------------ + + // postconditions + BOOST_TEST(b.size() <= b.max_size()); + if(w > 0) + BOOST_TEST(b.size() <= static_cast<typename Bitset::size_type>(w)); + + // throw if and only if required + if(has_stream_exceptions) { + const bool exceptional_state = has_flags(is, is.exceptions()); + BOOST_TEST(exceptional_state == did_throw); + } + + typedef typename String::size_type size_type; + typedef typename String::value_type Ch; + size_type after_digits = 0; + + if(!stream_was_good) { + BOOST_TEST(has_flags(is, std::ios::failbit)); + BOOST_TEST(b == a_copy); + BOOST_TEST(is.width() == (did_throw ? w : 0)); + } + else { + // stream was good(), parse the string; + // it may contain three parts, all of which are optional + // {spaces} {digits} {non-digits} + // opt opt opt + // + // The values of b.max_size() and is.width() may lead to + // ignore part of the digits, if any. + + size_type pos = 0; + size_type len = str.length(); + // {spaces} + for( ; pos < len && is_white_space(is, str[pos]); ++pos) + {} + size_type after_spaces = pos; + // {digits} or part of them + const typename Bitset::size_type max_digits = + w > 0 && static_cast<typename Bitset::size_type>(w) < b.max_size() + ? static_cast<typename Bitset::size_type>(w) : b.max_size(); + + for( ; pos < len && (pos - after_spaces) < max_digits; ++pos) { + if(!is_one_or_zero(is, str[pos])) + break; + } + after_digits = pos; + size_type num_digits = after_digits - after_spaces; + + // eofbit + if((after_digits == len && max_digits > num_digits )) + BOOST_TEST(has_flags(is, std::ios::eofbit)); + else + BOOST_TEST(!has_flags(is, std::ios::eofbit)); + + // failbit <=> there are no digits, except for the library + // issue explained below. + // + if(num_digits == 0) { + if(after_digits == len && has_stream_exceptions && + (is.exceptions() & std::ios::eofbit) != std::ios::goodbit) { + // This is a special case related to library issue 195: + // reaching eof when skipping whitespaces in the sentry ctor. + // The resolution says the sentry constructor should set *both* + // eofbit and failbit; but many implementations deliberately + // set eofbit only. See for instance: + // http://gcc.gnu.org/ml/libstdc++/2000-q1/msg00086.html + // + BOOST_TEST(did_throw); + + } + else { + BOOST_TEST(has_flags(is, std::ios::failbit)); + } + } + else + BOOST_TEST(!has_flags(is, std::ios::failbit)); + + + if(num_digits == 0 && after_digits == len) { + // The VC6 library has a bug/non-conformity in the sentry + // constructor. It uses code like + // // skip whitespaces... + // int_type _C = rdbuf()->sgetc(); + // while (!_Tr::eq_int_type(_Tr::eof(), _C) ... + // + // For an empty file the while statement is never "entered" + // and the stream remains in good() state; thus the sentry + // object gives "true" when converted to bool. This is worse + // than the case above, because not only failbit is not set, + // but no bit is set at all, end we end up clearing the + // bitset though there's nothing in the file to be extracted. + // Note that the dynamic_bitset docs say a sentry object is + // constructed and then converted to bool, thus we rely on + // what the underlying library does. + // +#if !defined(BOOST_DINKUMWARE_STDLIB) || (BOOST_DINKUMWARE_STDLIB >= 306) + BOOST_TEST(b == a_copy); +#else + BOOST_TEST(b.empty() == true); +#endif + } + else { + String sub = str.substr(after_spaces, num_digits); + BOOST_TEST(b == Bitset(sub)); + } + + // check width + BOOST_TEST(is.width() == 0 + || (after_digits == len && num_digits == 0 && did_throw)); + } + + + // clear the stream to allow further reading then + // retrieve any remaining chars with a single getline() + is.exceptions(std::ios::goodbit); + is.clear(); + String remainder; + std::getline(is, remainder, Ch()); + if(stream_was_good) + BOOST_TEST(remainder == str.substr(after_digits)); + else + BOOST_TEST(remainder == str); + + } + + +}; + + + +#endif // include guard diff --git a/src/boost/libs/dynamic_bitset/test/dyn_bitset_unit_tests1.cpp b/src/boost/libs/dynamic_bitset/test/dyn_bitset_unit_tests1.cpp new file mode 100644 index 00000000..c7227019 --- /dev/null +++ b/src/boost/libs/dynamic_bitset/test/dyn_bitset_unit_tests1.cpp @@ -0,0 +1,539 @@ +// ----------------------------------------------------------- +// Copyright (c) 2001 Jeremy Siek +// Copyright (c) 2003-2006 Gennaro Prota +// Copyright (c) 2014 Ahmed Charles +// Copyright (c) 2014 Riccardo Marcangelo +// +// Copyright (c) 2014 Glen Joseph Fernandes +// (glenjofe@gmail.com) +// +// 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) +// +// ----------------------------------------------------------- + +#include "bitset_test.hpp" +#include <boost/dynamic_bitset/dynamic_bitset.hpp> +#include <boost/limits.hpp> +#include <boost/config.hpp> + +#include <boost/config/workaround.hpp> + +#if !defined(BOOST_NO_CXX11_ALLOCATOR) +#include <cstdlib> + +template<class T> +class minimal_allocator { +public: + typedef T value_type; + + minimal_allocator() {} + + template <typename U> + minimal_allocator(const minimal_allocator<U>&) {} + + T* allocate(std::size_t n) { + void* p = std::malloc(sizeof(T) * n); + if (!p) { + throw std::bad_alloc(); + } + return static_cast<T*>(p); + } + + void deallocate(T* p, std::size_t) { + std::free(p); + } +}; +#endif + +#define BOOST_BITSET_TEST_COUNT(x) (sizeof(x)/sizeof(x[0])) + + +// Codewarrior 8.3 for Win fails without this. +// Thanks Howard Hinnant ;) +#if defined __MWERKS__ && BOOST_WORKAROUND(__MWERKS__, <= 0x3003) // 8.x +# pragma parse_func_templ off +#endif + + +template <typename Tests, typename String> +void run_string_tests(const String& s + BOOST_APPEND_EXPLICIT_TEMPLATE_TYPE(Tests) + ) +{ + + const std::size_t len = s.length(); + const std::size_t step = len/4 ? len/4 : 1; + + // bitset length determined by the string-related arguments + std::size_t i; + for (i = 0; i <= len/2 ; i += step) { + Tests::from_string(s, i, len/2); // len/2 - i bits + Tests::from_string(s, i, len); // len - i bits + Tests::from_string(s, i, 1 + len*2); // len - i bits + } + + // bitset length explicitly specified + for (i = 0; i <= len/2; i += step) { + for (std::size_t sz = 0; sz <= len*4; sz+= step*2) { + Tests::from_string(s, i, len/2, sz); + Tests::from_string(s, i, len, sz); + Tests::from_string(s, i, 1 + len*2, sz); + + } + } + +} + +// tests the do-the-right-thing constructor dispatch +template <typename Tests, typename T> +void run_numeric_ctor_tests( BOOST_EXPLICIT_TEMPLATE_TYPE(Tests) + BOOST_APPEND_EXPLICIT_TEMPLATE_TYPE(T) ) +{ + + const int bits_per_block = Tests::bits_per_block; + const int width = std::numeric_limits<T>::digits; + const T ma = (std::numeric_limits<T>::max)(); + const T mi = (std::numeric_limits<T>::min)(); + + int sizes[] = { + 0, 7*width/10, width, 13*width/10, 3*width, + 7*bits_per_block/10, bits_per_block, 13*bits_per_block/10, 3*bits_per_block + }; + + const T numbers[] = { + T(-1), T(-3), T(-8), T(-15), T(mi/2), T(mi), + T(0), T(1), T(3), T(8), T(15), T(ma/2), T(ma) + }; + + for (std::size_t s = 0; s < BOOST_BITSET_TEST_COUNT(sizes); ++s) { + for (std::size_t n = 0; n < BOOST_BITSET_TEST_COUNT(numbers); ++n ) { + + // can match ctor from ulong or templated one + Tests::from_unsigned_long(sizes[s], numbers[n]); + + typedef std::size_t compare_type; + const compare_type sz = sizes[s]; + // this condition is to be sure that size is representable in T, so + // that for signed T's we avoid implementation-defined behavior [if ma + // is larger than what std::size_t can hold then this is ok for our + // purposes: our sizes are anyhow < max(size_t)], which in turn could + // make the first argument of from_unsigned_long() a small negative, + // later converted to a very large unsigned. Example: signed 8-bit + // char (CHAR_MAX=127), bits_per_block=64, sz = 192 > 127. + const bool fits = + sz <= static_cast<compare_type>(ma); + + if (fits) { + // can match templated ctor only (so we test dispatching) + Tests::from_unsigned_long(static_cast<T>(sizes[s]), numbers[n]); + } + + } + } + +} + + +template <typename Block> +void run_test_cases( BOOST_EXPLICIT_TEMPLATE_TYPE(Block) ) +{ + typedef boost::dynamic_bitset<Block> bitset_type; + typedef bitset_test<bitset_type> Tests; + const int bits_per_block = bitset_type::bits_per_block; + + const std::string long_string = get_long_string(); + const Block all_1s = static_cast<Block>(-1); + + //===================================================================== + // Test construction from unsigned long + { + // NOTE: + // + // 1. keep this in sync with the numeric types supported + // for constructor dispatch (of course) + // 2. bool is tested separately; ugly and inelegant, but + // we don't have much time to think of a better solution + // which is likely to work on broken compilers + // + const int sizes[] = { + 0, 1, 3, + 7*bits_per_block/10, bits_per_block, 13*bits_per_block/10, 3*bits_per_block + }; + + const bool values[] = { false, true }; + + for (std::size_t s = 0; s < BOOST_BITSET_TEST_COUNT(sizes); ++s) { + for (std::size_t v = 0; v < BOOST_BITSET_TEST_COUNT(values); ++v) { + Tests::from_unsigned_long(sizes[s], values[v]); + Tests::from_unsigned_long(sizes[s] != 0, values[v]); + } + } + + run_numeric_ctor_tests<Tests, char>(); + +#if !defined(BOOST_NO_INTRINSIC_WCHAR_T) + run_numeric_ctor_tests<Tests, wchar_t>(); +#endif + + run_numeric_ctor_tests<Tests, signed char>(); + run_numeric_ctor_tests<Tests, short int>(); + run_numeric_ctor_tests<Tests, int>(); + run_numeric_ctor_tests<Tests, long int>(); + + run_numeric_ctor_tests<Tests, unsigned char>(); + run_numeric_ctor_tests<Tests, unsigned short>(); + run_numeric_ctor_tests<Tests, unsigned int>(); + run_numeric_ctor_tests<Tests, unsigned long>(); + +#if defined(BOOST_HAS_LONG_LONG) + run_numeric_ctor_tests<Tests, ::boost::long_long_type>(); + run_numeric_ctor_tests<Tests, ::boost::ulong_long_type>(); +#endif + + } + //===================================================================== + // Test construction from a string + { + + run_string_tests<Tests>(std::string("")); // empty string + run_string_tests<Tests>(std::string("1")); + + run_string_tests<Tests>(long_string); + +# if !defined BOOST_NO_STD_WSTRING + // I need to decide what to do for non "C" locales here. On + // one hand I should have better tests. On the other one + // I don't want tests for dynamic_bitset to cope with locales, + // ctype::widen, etc. (but that's what you deserve when you + // don't separate concerns at the library level) + // + run_string_tests<Tests>( + std::wstring(L"11111000000111111111010101010101010101010111111")); +# endif + + // Note that these are _valid_ arguments + Tests::from_string(std::string("x11y"), 1, 2); + Tests::from_string(std::string("x11"), 1, 10); + Tests::from_string(std::string("x11"), 1, 10, 10); + + } + //===================================================================== + // test from_block_range + { + std::vector<Block> blocks; + Tests::from_block_range(blocks); + } + { + std::vector<Block> blocks(3); + blocks[0] = static_cast<Block>(0); + blocks[1] = static_cast<Block>(1); + blocks[2] = all_1s; + Tests::from_block_range(blocks); + } + { + const unsigned int n = (std::numeric_limits<unsigned char>::max)(); + std::vector<Block> blocks(n); + for (typename std::vector<Block>::size_type i = 0; i < n; ++i) + blocks[i] = static_cast<Block>(i); + Tests::from_block_range(blocks); + } + //===================================================================== + // test to_block_range + { + bitset_type b; + Tests::to_block_range(b); + } + { + bitset_type b(1, 1ul); + Tests::to_block_range(b); + } + { + bitset_type b(long_string); + Tests::to_block_range(b); + } + + //===================================================================== + // Test copy constructor + { + boost::dynamic_bitset<Block> b; + Tests::copy_constructor(b); + } + { + boost::dynamic_bitset<Block> b(std::string("0")); + Tests::copy_constructor(b); + } + { + boost::dynamic_bitset<Block> b(long_string); + Tests::copy_constructor(b); + } + //===================================================================== + // Test copy assignment operator + { + bitset_type a, b; + Tests::copy_assignment_operator(a, b); + } + { + bitset_type a(std::string("1")), b(std::string("0")); + Tests::copy_assignment_operator(a, b); + } + { + bitset_type a(long_string), b(long_string); + Tests::copy_assignment_operator(a, b); + } + { + bitset_type a; + bitset_type b(long_string); // b greater than a, a empty + Tests::copy_assignment_operator(a, b); + } + { + bitset_type a(std::string("0")); + bitset_type b(long_string); // b greater than a + Tests::copy_assignment_operator(a, b); + } + +#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES + //===================================================================== + // Test move constructor + { + boost::dynamic_bitset<Block> b; + Tests::move_constructor(b); + } + { + boost::dynamic_bitset<Block> b(std::string("0")); + Tests::move_constructor(b); + } + { + boost::dynamic_bitset<Block> b(long_string); + Tests::move_constructor(b); + } + //===================================================================== + // Test move assignment operator + { + bitset_type a, b; + Tests::move_assignment_operator(a, b); + } + { + bitset_type a(std::string("1")), b(std::string("0")); + Tests::move_assignment_operator(a, b); + } + { + bitset_type a(long_string), b(long_string); + Tests::move_assignment_operator(a, b); + } + { + bitset_type a; + bitset_type b(long_string); // b greater than a, a empty + Tests::move_assignment_operator(a, b); + } + { + bitset_type a(std::string("0")); + bitset_type b(long_string); // b greater than a + Tests::move_assignment_operator(a, b); + } +#endif // BOOST_NO_CXX11_RVALUE_REFERENCES + //===================================================================== + // Test swap + { + bitset_type a; + bitset_type b(std::string("1")); + Tests::swap(a, b); + Tests::swap(b, a); + Tests::swap(a, a); + } + { + bitset_type a; + bitset_type b(long_string); + Tests::swap(a, b); + Tests::swap(b, a); + } + { + bitset_type a(std::string("0")); + bitset_type b(long_string); + Tests::swap(a, b); + Tests::swap(b, a); + Tests::swap(a, a); + Tests::swap(b, b); + } + //===================================================================== + // Test resize + { + boost::dynamic_bitset<Block> a; + Tests::resize(a); + } + { + boost::dynamic_bitset<Block> a(std::string("0")); + Tests::resize(a); + } + { + boost::dynamic_bitset<Block> a(std::string("1")); + Tests::resize(a); + } + { + boost::dynamic_bitset<Block> a(long_string); + Tests::resize(a); + } + //===================================================================== + // Test clear + { + boost::dynamic_bitset<Block> a; + Tests::clear(a); + } + { + boost::dynamic_bitset<Block> a(long_string); + Tests::clear(a); + } + //===================================================================== + // Test pop back + { + boost::dynamic_bitset<Block> a(std::string("01")); + Tests::pop_back(a); + } + { + boost::dynamic_bitset<Block> a(std::string("10")); + Tests::pop_back(a); + } + { + const int size_to_fill_all_blocks = 4 * bits_per_block; + boost::dynamic_bitset<Block> a(size_to_fill_all_blocks, 255ul); + Tests::pop_back(a); + } + { + boost::dynamic_bitset<Block> a(long_string); + Tests::pop_back(a); + } + //===================================================================== + // Test append bit + { + boost::dynamic_bitset<Block> a; + Tests::append_bit(a); + } + { + boost::dynamic_bitset<Block> a(std::string("0")); + Tests::append_bit(a); + } + { + boost::dynamic_bitset<Block> a(std::string("1")); + Tests::append_bit(a); + } + { + const int size_to_fill_all_blocks = 4 * bits_per_block; + boost::dynamic_bitset<Block> a(size_to_fill_all_blocks, 255ul); + Tests::append_bit(a); + } + { + boost::dynamic_bitset<Block> a(long_string); + Tests::append_bit(a); + } + //===================================================================== + // Test append block + { + boost::dynamic_bitset<Block> a; + Tests::append_block(a); + } + { + boost::dynamic_bitset<Block> a(std::string("0")); + Tests::append_block(a); + } + { + boost::dynamic_bitset<Block> a(std::string("1")); + Tests::append_block(a); + } + { + const int size_to_fill_all_blocks = 4 * bits_per_block; + boost::dynamic_bitset<Block> a(size_to_fill_all_blocks, 15ul); + Tests::append_block(a); + } + { + boost::dynamic_bitset<Block> a(long_string); + Tests::append_block(a); + } + //===================================================================== + // Test append block range + { + boost::dynamic_bitset<Block> a; + std::vector<Block> blocks; + Tests::append_block_range(a, blocks); + } + { + boost::dynamic_bitset<Block> a(std::string("0")); + std::vector<Block> blocks(3); + blocks[0] = static_cast<Block>(0); + blocks[1] = static_cast<Block>(1); + blocks[2] = all_1s; + Tests::append_block_range(a, blocks); + } + { + boost::dynamic_bitset<Block> a(std::string("1")); + const unsigned int n = (std::numeric_limits<unsigned char>::max)(); + std::vector<Block> blocks(n); + for (typename std::vector<Block>::size_type i = 0; i < n; ++i) + blocks[i] = static_cast<Block>(i); + Tests::append_block_range(a, blocks); + } + { + boost::dynamic_bitset<Block> a; + a.append(Block(1)); + a.append(Block(2)); + Block x[] = {3, 4, 5}; + std::size_t sz = sizeof(x) / sizeof(x[0]); + std::vector<Block> blocks(x, x + sz); + Tests::append_block_range(a, blocks); + } + { + boost::dynamic_bitset<Block> a(long_string); + std::vector<Block> blocks(3); + blocks[0] = static_cast<Block>(0); + blocks[1] = static_cast<Block>(1); + blocks[2] = all_1s; + Tests::append_block_range(a, blocks); + } + //===================================================================== + // Test bracket operator + { + boost::dynamic_bitset<Block> b1; + std::vector<bool> bitvec1; + Tests::operator_bracket(b1, bitvec1); + } + { + boost::dynamic_bitset<Block> b(std::string("1")); + std::vector<bool> bit_vec(1, true); + Tests::operator_bracket(b, bit_vec); + } + { + boost::dynamic_bitset<Block> b(long_string); + std::size_t n = long_string.size(); + std::vector<bool> bit_vec(n); + for (std::size_t i = 0; i < n; ++i) + bit_vec[i] = long_string[n - 1 - i] == '0' ? 0 : 1; + Tests::operator_bracket(b, bit_vec); + } +#if !defined(BOOST_NO_CXX11_ALLOCATOR) + { + typedef boost::dynamic_bitset<Block, + minimal_allocator<Block> > Bitset; + Bitset b; + bitset_test<Bitset>::max_size(b); + } +#endif + // Test copy-initialize with default constructor + { + boost::dynamic_bitset<Block> b[1] = {}; + (void)b; + } +} + +int +main() +{ + run_test_cases<unsigned char>(); + run_test_cases<unsigned short>(); + run_test_cases<unsigned int>(); + run_test_cases<unsigned long>(); +# ifdef BOOST_HAS_LONG_LONG + run_test_cases< ::boost::ulong_long_type>(); +# endif + + return boost::report_errors(); +} diff --git a/src/boost/libs/dynamic_bitset/test/dyn_bitset_unit_tests2.cpp b/src/boost/libs/dynamic_bitset/test/dyn_bitset_unit_tests2.cpp new file mode 100644 index 00000000..69a2edb3 --- /dev/null +++ b/src/boost/libs/dynamic_bitset/test/dyn_bitset_unit_tests2.cpp @@ -0,0 +1,383 @@ +// ----------------------------------------------------------- +// Copyright (c) 2001 Jeremy Siek +// Copyright (c) 2003-2006 Gennaro Prota +// Copyright (c) 2014 Ahmed Charles +// Copyright (c) 2018 Evgeny Shulgin +// +// 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) +// +// ----------------------------------------------------------- + +#include "bitset_test.hpp" +#include <boost/dynamic_bitset/dynamic_bitset.hpp> +#include <boost/config.hpp> + + +template <typename Block> +void run_test_cases( BOOST_EXPLICIT_TEMPLATE_TYPE(Block) ) +{ + typedef boost::dynamic_bitset<Block> bitset_type; + typedef bitset_test< bitset_type > Tests; + const int bits_per_block = bitset_type::bits_per_block; + + std::string long_string = get_long_string(); + + //===================================================================== + // Test operator&= + { + boost::dynamic_bitset<Block> lhs, rhs; + Tests::and_assignment(lhs, rhs); + } + { + boost::dynamic_bitset<Block> lhs(std::string("1")), rhs(std::string("0")); + Tests::and_assignment(lhs, rhs); + } + { + boost::dynamic_bitset<Block> lhs(long_string.size(), 0), rhs(long_string); + Tests::and_assignment(lhs, rhs); + } + { + boost::dynamic_bitset<Block> lhs(long_string.size(), 1), rhs(long_string); + Tests::and_assignment(lhs, rhs); + } + //===================================================================== + // Test operator |= + { + boost::dynamic_bitset<Block> lhs, rhs; + Tests::or_assignment(lhs, rhs); + } + { + boost::dynamic_bitset<Block> lhs(std::string("1")), rhs(std::string("0")); + Tests::or_assignment(lhs, rhs); + } + { + boost::dynamic_bitset<Block> lhs(long_string.size(), 0), rhs(long_string); + Tests::or_assignment(lhs, rhs); + } + { + boost::dynamic_bitset<Block> lhs(long_string.size(), 1), rhs(long_string); + Tests::or_assignment(lhs, rhs); + } + //===================================================================== + // Test operator^= + { + boost::dynamic_bitset<Block> lhs, rhs; + Tests::xor_assignment(lhs, rhs); + } + { + boost::dynamic_bitset<Block> lhs(std::string("1")), rhs(std::string("0")); + Tests::xor_assignment(lhs, rhs); + } + { + boost::dynamic_bitset<Block> lhs(std::string("0")), rhs(std::string("1")); + Tests::xor_assignment(lhs, rhs); + } + { + boost::dynamic_bitset<Block> lhs(long_string), rhs(long_string); + Tests::xor_assignment(lhs, rhs); + } + //===================================================================== + // Test operator-= + { + boost::dynamic_bitset<Block> lhs, rhs; + Tests::sub_assignment(lhs, rhs); + } + { + boost::dynamic_bitset<Block> lhs(std::string("1")), rhs(std::string("0")); + Tests::sub_assignment(lhs, rhs); + } + { + boost::dynamic_bitset<Block> lhs(std::string("0")), rhs(std::string("1")); + Tests::sub_assignment(lhs, rhs); + } + { + boost::dynamic_bitset<Block> lhs(long_string), rhs(long_string); + Tests::sub_assignment(lhs, rhs); + } + //===================================================================== + // Test operator<<= + { // case pos == 0 + std::size_t pos = 0; + { + boost::dynamic_bitset<Block> b; + Tests::shift_left_assignment(b, pos); + } + { + boost::dynamic_bitset<Block> b(std::string("1010")); + Tests::shift_left_assignment(b, pos); + } + { + boost::dynamic_bitset<Block> b(long_string); + Tests::shift_left_assignment(b, pos); + } + } + { + // test with both multiple and + // non multiple of bits_per_block + const int how_many = 10; + for (int i = 1; i <= how_many; ++i) { + std::size_t multiple = i * bits_per_block; + std::size_t non_multiple = multiple - 1; + boost::dynamic_bitset<Block> b(long_string); + + Tests::shift_left_assignment(b, multiple); + Tests::shift_left_assignment(b, non_multiple); + } + } + { // case pos == size()/2 + std::size_t pos = long_string.size() / 2; + boost::dynamic_bitset<Block> b(long_string); + Tests::shift_left_assignment(b, pos); + } + { // case pos >= n + std::size_t pos = long_string.size(); + boost::dynamic_bitset<Block> b(long_string); + Tests::shift_left_assignment(b, pos); + } + //===================================================================== + // Test operator>>= + { // case pos == 0 + std::size_t pos = 0; + { + boost::dynamic_bitset<Block> b; + Tests::shift_right_assignment(b, pos); + } + { + boost::dynamic_bitset<Block> b(std::string("1010")); + Tests::shift_right_assignment(b, pos); + } + { + boost::dynamic_bitset<Block> b(long_string); + Tests::shift_right_assignment(b, pos); + } + } + { + // test with both multiple and + // non multiple of bits_per_block + const int how_many = 10; + for (int i = 1; i <= how_many; ++i) { + std::size_t multiple = i * bits_per_block; + std::size_t non_multiple = multiple - 1; + boost::dynamic_bitset<Block> b(long_string); + + Tests::shift_right_assignment(b, multiple); + Tests::shift_right_assignment(b, non_multiple); + } + + } + { // case pos == size()/2 + std::size_t pos = long_string.size() / 2; + boost::dynamic_bitset<Block> b(long_string); + Tests::shift_right_assignment(b, pos); + } + { // case pos >= n + std::size_t pos = long_string.size(); + boost::dynamic_bitset<Block> b(long_string); + Tests::shift_right_assignment(b, pos); + } + //===================================================================== + // test b.set() + { + boost::dynamic_bitset<Block> b; + Tests::set_all(b); + } + { + boost::dynamic_bitset<Block> b(std::string("0")); + Tests::set_all(b); + } + { + boost::dynamic_bitset<Block> b(long_string); + Tests::set_all(b); + } + //===================================================================== + // Test b.set(pos) + { // case pos >= b.size() + boost::dynamic_bitset<Block> b; + Tests::set_one(b, 0, true); + Tests::set_one(b, 0, false); + } + { // case pos < b.size() + boost::dynamic_bitset<Block> b(std::string("0")); + Tests::set_one(b, 0, true); + Tests::set_one(b, 0, false); + } + { // case pos == b.size() / 2 + boost::dynamic_bitset<Block> b(long_string); + Tests::set_one(b, long_string.size()/2, true); + Tests::set_one(b, long_string.size()/2, false); + } + //===================================================================== + // Test b.set(pos, len) + { // case size is 1 + boost::dynamic_bitset<Block> b(std::string("0")); + Tests::set_segment(b, 0, 1, true); + Tests::set_segment(b, 0, 1, false); + } + { // case fill the whole set + boost::dynamic_bitset<Block> b(long_string); + Tests::set_segment(b, 0, b.size(), true); + Tests::set_segment(b, 0, b.size(), false); + } + { // case pos = size / 4, len = size / 2 + boost::dynamic_bitset<Block> b(long_string); + Tests::set_segment(b, b.size() / 4, b.size() / 2, true); + Tests::set_segment(b, b.size() / 4, b.size() / 2, false); + } + { // case pos = block_size / 2, len = size - block_size + boost::dynamic_bitset<Block> b(long_string); + Tests::set_segment(b, boost::dynamic_bitset<Block>::bits_per_block / 2, + b.size() - boost::dynamic_bitset<Block>::bits_per_block, true); + Tests::set_segment(b, boost::dynamic_bitset<Block>::bits_per_block / 2, + b.size() - boost::dynamic_bitset<Block>::bits_per_block, false); + } + { // case pos = 1, len = size - 2 + boost::dynamic_bitset<Block> b(long_string); + Tests::set_segment(b, 1, b.size() - 2, true); + Tests::set_segment(b, 1, b.size() - 2, false); + } + { // case pos = 3, len = 7 + boost::dynamic_bitset<Block> b(long_string); + Tests::set_segment(b, 3, 7, true); + Tests::set_segment(b, 3, 7, false); + } + //===================================================================== + // Test b.reset() + { + boost::dynamic_bitset<Block> b; + Tests::reset_all(b); + } + { + boost::dynamic_bitset<Block> b(std::string("0")); + Tests::reset_all(b); + } + { + boost::dynamic_bitset<Block> b(long_string); + Tests::reset_all(b); + } + //===================================================================== + // Test b.reset(pos) + { // case pos >= b.size() + boost::dynamic_bitset<Block> b; + Tests::reset_one(b, 0); + } + { // case pos < b.size() + boost::dynamic_bitset<Block> b(std::string("0")); + Tests::reset_one(b, 0); + } + { // case pos == b.size() / 2 + boost::dynamic_bitset<Block> b(long_string); + Tests::reset_one(b, long_string.size()/2); + } + //===================================================================== + // Test b.reset(pos, len) + { // case size is 1 + boost::dynamic_bitset<Block> b(std::string("0")); + Tests::reset_segment(b, 0, 1); + } + { // case fill the whole set + boost::dynamic_bitset<Block> b(long_string); + Tests::reset_segment(b, 0, b.size()); + } + { // case pos = size / 4, len = size / 2 + boost::dynamic_bitset<Block> b(long_string); + Tests::reset_segment(b, b.size() / 4, b.size() / 2); + } + { // case pos = block_size / 2, len = size - block_size + boost::dynamic_bitset<Block> b(long_string); + Tests::reset_segment(b, boost::dynamic_bitset<Block>::bits_per_block / 2, + b.size() - boost::dynamic_bitset<Block>::bits_per_block); + } + { // case pos = 1, len = size - 2 + boost::dynamic_bitset<Block> b(long_string); + Tests::reset_segment(b, 1, b.size() - 2); + } + { // case pos = 3, len = 7 + boost::dynamic_bitset<Block> b(long_string); + Tests::reset_segment(b, 3, 7); + } + //===================================================================== + // Test ~b + { + boost::dynamic_bitset<Block> b; + Tests::operator_flip(b); + } + { + boost::dynamic_bitset<Block> b(std::string("1")); + Tests::operator_flip(b); + } + { + boost::dynamic_bitset<Block> b(long_string); + Tests::operator_flip(b); + } + //===================================================================== + // Test b.flip() + { + boost::dynamic_bitset<Block> b; + Tests::flip_all(b); + } + { + boost::dynamic_bitset<Block> b(std::string("1")); + Tests::flip_all(b); + } + { + boost::dynamic_bitset<Block> b(long_string); + Tests::flip_all(b); + } + //===================================================================== + // Test b.flip(pos) + { // case pos >= b.size() + boost::dynamic_bitset<Block> b; + Tests::flip_one(b, 0); + } + { // case pos < b.size() + boost::dynamic_bitset<Block> b(std::string("0")); + Tests::flip_one(b, 0); + } + { // case pos == b.size() / 2 + boost::dynamic_bitset<Block> b(long_string); + Tests::flip_one(b, long_string.size()/2); + } + //===================================================================== + // Test b.flip(pos, len) + { // case size is 1 + boost::dynamic_bitset<Block> b(std::string("0")); + Tests::flip_segment(b, 0, 1); + } + { // case fill the whole set + boost::dynamic_bitset<Block> b(long_string); + Tests::flip_segment(b, 0, b.size()); + } + { // case pos = size / 4, len = size / 2 + boost::dynamic_bitset<Block> b(long_string); + Tests::flip_segment(b, b.size() / 4, b.size() / 2); + } + { // case pos = block_size / 2, len = size - block_size + boost::dynamic_bitset<Block> b(long_string); + Tests::flip_segment(b, boost::dynamic_bitset<Block>::bits_per_block / 2, + b.size() - boost::dynamic_bitset<Block>::bits_per_block); + } + { // case pos = 1, len = size - 2 + boost::dynamic_bitset<Block> b(long_string); + Tests::flip_segment(b, 1, b.size() - 2); + } + { // case pos = 3, len = 7 + boost::dynamic_bitset<Block> b(long_string); + Tests::flip_segment(b, 3, 7); + } +} + +int +main() +{ + run_test_cases<unsigned char>(); + run_test_cases<unsigned short>(); + run_test_cases<unsigned int>(); + run_test_cases<unsigned long>(); +# ifdef BOOST_HAS_LONG_LONG + run_test_cases< ::boost::ulong_long_type>(); +# endif + + return boost::report_errors(); +} diff --git a/src/boost/libs/dynamic_bitset/test/dyn_bitset_unit_tests3.cpp b/src/boost/libs/dynamic_bitset/test/dyn_bitset_unit_tests3.cpp new file mode 100644 index 00000000..5692c260 --- /dev/null +++ b/src/boost/libs/dynamic_bitset/test/dyn_bitset_unit_tests3.cpp @@ -0,0 +1,814 @@ +// ----------------------------------------------------------- +// Copyright (c) 2001 Jeremy Siek +// Copyright (c) 2003-2006 Gennaro Prota +// Copyright (c) 2014 Ahmed Charles +// Copyright (c) 2014 Riccardo Marcangelo +// +// 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) +// +// ----------------------------------------------------------- + +#include <assert.h> +#include "bitset_test.hpp" +#include <boost/dynamic_bitset/dynamic_bitset.hpp> +#include <boost/limits.hpp> +#include <boost/config.hpp> + +template <typename Block> +void run_test_cases( BOOST_EXPLICIT_TEMPLATE_TYPE(Block) ) +{ + // a bunch of typedefs which will be handy later on + typedef boost::dynamic_bitset<Block> bitset_type; + typedef bitset_test<bitset_type> Tests; + // typedef typename bitset_type::size_type size_type; // unusable with Borland 5.5.1 + + std::string long_string = get_long_string(); + std::size_t ul_width = std::numeric_limits<unsigned long>::digits; + + //===================================================================== + // Test b.empty() + { + bitset_type b; + Tests::empty(b); + } + { + bitset_type b(1, 1ul); + Tests::empty(b); + } + { + bitset_type b(bitset_type::bits_per_block + + bitset_type::bits_per_block/2, 15ul); + Tests::empty(b); + } + //===================================================================== + // Test b.to_long() + { + boost::dynamic_bitset<Block> b; + Tests::to_ulong(b); + } + { + boost::dynamic_bitset<Block> b(std::string("1")); + Tests::to_ulong(b); + } + { + boost::dynamic_bitset<Block> b(bitset_type::bits_per_block, + static_cast<unsigned long>(-1)); + Tests::to_ulong(b); + } + { + std::string str(ul_width - 1, '1'); + boost::dynamic_bitset<Block> b(str); + Tests::to_ulong(b); + } + { + std::string ul_str(ul_width, '1'); + boost::dynamic_bitset<Block> b(ul_str); + Tests::to_ulong(b); + } + { // case overflow + boost::dynamic_bitset<Block> b(long_string); + Tests::to_ulong(b); + } + //===================================================================== + // Test to_string(b, str) + { + boost::dynamic_bitset<Block> b; + Tests::to_string(b); + } + { + boost::dynamic_bitset<Block> b(std::string("0")); + Tests::to_string(b); + } + { + boost::dynamic_bitset<Block> b(long_string); + Tests::to_string(b); + } + //===================================================================== + // Test b.count() + { + boost::dynamic_bitset<Block> b; + Tests::count(b); + } + { + boost::dynamic_bitset<Block> b(std::string("0")); + Tests::count(b); + } + { + boost::dynamic_bitset<Block> b(std::string("1")); + Tests::count(b); + } + { + boost::dynamic_bitset<Block> b(8, 255ul); + Tests::count(b); + } + { + boost::dynamic_bitset<Block> b(long_string); + Tests::count(b); + } + //===================================================================== + // Test b.size() + { + boost::dynamic_bitset<Block> b; + Tests::size(b); + } + { + boost::dynamic_bitset<Block> b(std::string("0")); + Tests::size(b); + } + { + boost::dynamic_bitset<Block> b(long_string); + Tests::size(b); + } + //===================================================================== + // Test b.capacity() + { + boost::dynamic_bitset<Block> b; + Tests::capacity_test_one(b); + } + { + boost::dynamic_bitset<Block> b(100); + Tests::capacity_test_two(b); + } + //===================================================================== + // Test b.reserve() + { + boost::dynamic_bitset<Block> b; + Tests::reserve_test_one(b); + } + { + boost::dynamic_bitset<Block> b(100); + Tests::reserve_test_two(b); + } + //===================================================================== + // Test b.shrink_to_fit() + { + boost::dynamic_bitset<Block> b; + Tests::shrink_to_fit_test_one(b); + } + { + boost::dynamic_bitset<Block> b(100); + Tests::shrink_to_fit_test_two(b); + } + //===================================================================== + // Test b.all() + { + boost::dynamic_bitset<Block> b; + Tests::all(b); + Tests::all(~b); + Tests::all(b.set()); + Tests::all(b.reset()); + } + { + boost::dynamic_bitset<Block> b(std::string("0")); + Tests::all(b); + Tests::all(~b); + Tests::all(b.set()); + Tests::all(b.reset()); + } + { + boost::dynamic_bitset<Block> b(long_string); + Tests::all(b); + Tests::all(~b); + Tests::all(b.set()); + Tests::all(b.reset()); + } + //===================================================================== + // Test b.any() + { + boost::dynamic_bitset<Block> b; + Tests::any(b); + Tests::any(~b); + Tests::any(b.set()); + Tests::any(b.reset()); + } + { + boost::dynamic_bitset<Block> b(std::string("0")); + Tests::any(b); + Tests::any(~b); + Tests::any(b.set()); + Tests::any(b.reset()); + } + { + boost::dynamic_bitset<Block> b(long_string); + Tests::any(b); + Tests::any(~b); + Tests::any(b.set()); + Tests::any(b.reset()); + } + //===================================================================== + // Test b.none() + { + boost::dynamic_bitset<Block> b; + Tests::none(b); + Tests::none(~b); + Tests::none(b.set()); + Tests::none(b.reset()); + } + { + boost::dynamic_bitset<Block> b(std::string("0")); + Tests::none(b); + Tests::none(~b); + Tests::none(b.set()); + Tests::none(b.reset()); + } + { + boost::dynamic_bitset<Block> b(long_string); + Tests::none(b); + Tests::none(~b); + Tests::none(b.set()); + Tests::none(b.reset()); + } + //===================================================================== + // Test a.is_subset_of(b) + { + boost::dynamic_bitset<Block> a, b; + Tests::subset(a, b); + } + { + boost::dynamic_bitset<Block> a(std::string("0")), b(std::string("0")); + Tests::subset(a, b); + } + { + boost::dynamic_bitset<Block> a(std::string("1")), b(std::string("1")); + Tests::subset(a, b); + } + { + boost::dynamic_bitset<Block> a(long_string), b(long_string); + Tests::subset(a, b); + } + { + boost::dynamic_bitset<Block> a(long_string), b(long_string); + a[long_string.size()/2].flip(); + Tests::subset(a, b); + } + { + boost::dynamic_bitset<Block> a(long_string), b(long_string); + b[long_string.size()/2].flip(); + Tests::subset(a, b); + } + //===================================================================== + // Test a.is_proper_subset_of(b) + { + boost::dynamic_bitset<Block> a, b; + Tests::proper_subset(a, b); + } + { + boost::dynamic_bitset<Block> a(std::string("0")), b(std::string("0")); + Tests::proper_subset(a, b); + } + { + boost::dynamic_bitset<Block> a(std::string("1")), b(std::string("1")); + Tests::proper_subset(a, b); + } + { + boost::dynamic_bitset<Block> a(long_string), b(long_string); + Tests::proper_subset(a, b); + } + { + boost::dynamic_bitset<Block> a(long_string), b(long_string); + a[long_string.size()/2].flip(); + Tests::proper_subset(a, b); + } + { + boost::dynamic_bitset<Block> a(long_string), b(long_string); + b[long_string.size()/2].flip(); + Tests::proper_subset(a, b); + } + //===================================================================== + // Test intersects + { + bitset_type a; // empty + bitset_type b; + Tests::intersects(a, b); + } + { + bitset_type a; + bitset_type b(5, 8ul); + Tests::intersects(a, b); + } + { + bitset_type a(8, 0ul); + bitset_type b(15, 0ul); + b[9] = 1; + Tests::intersects(a, b); + } + { + bitset_type a(15, 0ul); + bitset_type b(22, 0ul); + a[14] = b[14] = 1; + Tests::intersects(a, b); + } + //===================================================================== + // Test find_first + { + // empty bitset + bitset_type b; + Tests::find_first(b); + } + { + // bitset of size 1 + bitset_type b(1, 1ul); + Tests::find_first(b); + } + { + // all-0s bitset + bitset_type b(4 * bitset_type::bits_per_block, 0ul); + Tests::find_first(b); + } + { + // first bit on + bitset_type b(1, 1ul); + Tests::find_first(b); + } + { + // last bit on + bitset_type b(4 * bitset_type::bits_per_block - 1, 0ul); + b.set(b.size() - 1); + Tests::find_first(b); + } + //===================================================================== + // Test find_next + { + // empty bitset + bitset_type b; + + // check + Tests::find_next(b, 0); + Tests::find_next(b, 1); + Tests::find_next(b, 200); + Tests::find_next(b, b.npos); + } + { + // bitset of size 1 (find_next can never find) + bitset_type b(1, 1ul); + + // check + Tests::find_next(b, 0); + Tests::find_next(b, 1); + Tests::find_next(b, 200); + Tests::find_next(b, b.npos); + } + { + // all-1s bitset + bitset_type b(16 * bitset_type::bits_per_block); + b.set(); + + // check + const typename bitset_type::size_type larger_than_size = 5 + b.size(); + for(typename bitset_type::size_type i = 0; i <= larger_than_size; ++i) { + Tests::find_next(b, i); + } + Tests::find_next(b, b.npos); + } + { + // a bitset with 1s at block boundary only + const int num_blocks = 32; + const int block_width = bitset_type::bits_per_block; + + bitset_type b(num_blocks * block_width); + typename bitset_type::size_type i = block_width - 1; + for ( ; i < b.size(); i += block_width) { + + b.set(i); + typename bitset_type::size_type first_in_block = i - (block_width - 1); + b.set(first_in_block); + } + + // check + const typename bitset_type::size_type larger_than_size = 5 + b.size(); + for (i = 0; i <= larger_than_size; ++i) { + Tests::find_next(b, i); + } + Tests::find_next(b, b.npos); + + } + { + // bitset with alternate 1s and 0s + const typename bitset_type::size_type sz = 1000; + bitset_type b(sz); + + typename bitset_type::size_type i = 0; + for ( ; i < sz; ++i) { + b[i] = (i%2 == 0); + } + + // check + const typename bitset_type::size_type larger_than_size = 5 + b.size(); + for (i = 0; i <= larger_than_size; ++i) { + Tests::find_next(b, i); + } + Tests::find_next(b, b.npos); + + } + //===================================================================== + // Test operator== + { + boost::dynamic_bitset<Block> a, b; + Tests::operator_equal(a, b); + } + { + boost::dynamic_bitset<Block> a(std::string("0")), b(std::string("0")); + Tests::operator_equal(a, b); + } + { + boost::dynamic_bitset<Block> a(std::string("1")), b(std::string("1")); + Tests::operator_equal(a, b); + } + { + boost::dynamic_bitset<Block> a(long_string), b(long_string); + Tests::operator_equal(a, b); + } + { + boost::dynamic_bitset<Block> a(long_string), b(long_string); + a[long_string.size()/2].flip(); + Tests::operator_equal(a, b); + } + { + boost::dynamic_bitset<Block> a(long_string), b(long_string); + b[long_string.size()/2].flip(); + Tests::operator_equal(a, b); + } + //===================================================================== + // Test operator!= + { + boost::dynamic_bitset<Block> a, b; + Tests::operator_not_equal(a, b); + } + { + boost::dynamic_bitset<Block> a(std::string("0")), b(std::string("0")); + Tests::operator_not_equal(a, b); + } + { + boost::dynamic_bitset<Block> a(std::string("1")), b(std::string("1")); + Tests::operator_not_equal(a, b); + } + { + boost::dynamic_bitset<Block> a(long_string), b(long_string); + Tests::operator_not_equal(a, b); + } + { + boost::dynamic_bitset<Block> a(long_string), b(long_string); + a[long_string.size()/2].flip(); + Tests::operator_not_equal(a, b); + } + { + boost::dynamic_bitset<Block> a(long_string), b(long_string); + b[long_string.size()/2].flip(); + Tests::operator_not_equal(a, b); + } + //===================================================================== + // Test operator< + { + boost::dynamic_bitset<Block> a, b; + Tests::operator_less_than(a, b); + } + { + boost::dynamic_bitset<Block> a(std::string("0")), b(std::string("0")); + Tests::operator_less_than(a, b); + } + { + boost::dynamic_bitset<Block> a(std::string("1")), b(std::string("1")); + Tests::operator_less_than(a, b); + } + { + boost::dynamic_bitset<Block> a(std::string("10")), b(std::string("11")); + Tests::operator_less_than(a, b); + } + { + boost::dynamic_bitset<Block> a(std::string("101")), b(std::string("11")); + Tests::operator_less_than(a, b); + } + { + boost::dynamic_bitset<Block> a(std::string("10")), b(std::string("111")); + Tests::operator_less_than(a, b); + } + { + boost::dynamic_bitset<Block> a(long_string), b(long_string); + Tests::operator_less_than(a, b); + } + { + boost::dynamic_bitset<Block> a(long_string), b(long_string); + a[long_string.size()/2].flip(); + Tests::operator_less_than(a, b); + } + { + boost::dynamic_bitset<Block> a(long_string), b(long_string); + b[long_string.size()/2].flip(); + Tests::operator_less_than(a, b); + } + // check for consistency with ulong behaviour when the sizes are equal + { + boost::dynamic_bitset<Block> a(3, 4ul), b(3, 5ul); + assert(a < b); + } + { + boost::dynamic_bitset<Block> a(3, 4ul), b(3, 4ul); + assert(!(a < b)); + } + { + boost::dynamic_bitset<Block> a(3, 5ul), b(3, 4ul); + assert(!(a < b)); + } + // when the sizes are not equal lexicographic compare does not necessarily correspond to ulong behavior + { + boost::dynamic_bitset<Block> a(4, 4ul), b(3, 5ul); + assert(a < b); + } + { + boost::dynamic_bitset<Block> a(3, 4ul), b(4, 5ul); + assert(!(a < b)); + } + { + boost::dynamic_bitset<Block> a(4, 4ul), b(3, 4ul); + assert(a < b); + } + { + boost::dynamic_bitset<Block> a(3, 4ul), b(4, 4ul); + assert(!(a < b)); + } + { + boost::dynamic_bitset<Block> a(4, 5ul), b(3, 4ul); + assert(a < b); + } + { + boost::dynamic_bitset<Block> a(3, 5ul), b(4, 4ul); + assert(!(a < b)); + } + //===================================================================== + // Test operator<= + { + boost::dynamic_bitset<Block> a, b; + Tests::operator_less_than_eq(a, b); + } + { + boost::dynamic_bitset<Block> a(std::string("0")), b(std::string("0")); + Tests::operator_less_than_eq(a, b); + } + { + boost::dynamic_bitset<Block> a(std::string("1")), b(std::string("1")); + Tests::operator_less_than_eq(a, b); + } + { + boost::dynamic_bitset<Block> a(long_string), b(long_string); + Tests::operator_less_than_eq(a, b); + } + { + boost::dynamic_bitset<Block> a(long_string), b(long_string); + a[long_string.size()/2].flip(); + Tests::operator_less_than_eq(a, b); + } + { + boost::dynamic_bitset<Block> a(long_string), b(long_string); + b[long_string.size()/2].flip(); + Tests::operator_less_than_eq(a, b); + } + // check for consistency with ulong behaviour + { + boost::dynamic_bitset<Block> a(3, 4ul), b(3, 5ul); + assert(a <= b); + } + { + boost::dynamic_bitset<Block> a(3, 4ul), b(3, 4ul); + assert(a <= b); + } + { + boost::dynamic_bitset<Block> a(3, 5ul), b(3, 4ul); + assert(!(a <= b)); + } + //===================================================================== + // Test operator> + { + boost::dynamic_bitset<Block> a, b; + Tests::operator_greater_than(a, b); + } + { + boost::dynamic_bitset<Block> a(std::string("0")), b(std::string("0")); + Tests::operator_greater_than(a, b); + } + { + boost::dynamic_bitset<Block> a(std::string("1")), b(std::string("1")); + Tests::operator_greater_than(a, b); + } + { + boost::dynamic_bitset<Block> a(long_string), b(long_string); + Tests::operator_greater_than(a, b); + } + { + boost::dynamic_bitset<Block> a(long_string), b(long_string); + a[long_string.size()/2].flip(); + Tests::operator_greater_than(a, b); + } + { + boost::dynamic_bitset<Block> a(long_string), b(long_string); + b[long_string.size()/2].flip(); + Tests::operator_greater_than(a, b); + } + // check for consistency with ulong behaviour + { + boost::dynamic_bitset<Block> a(3, 4ul), b(3, 5ul); + assert(!(a > b)); + } + { + boost::dynamic_bitset<Block> a(3, 4ul), b(3, 4ul); + assert(!(a > b)); + } + { + boost::dynamic_bitset<Block> a(3, 5ul), b(3, 4ul); + assert(a > b); + } + //===================================================================== + // Test operator<= + { + boost::dynamic_bitset<Block> a, b; + Tests::operator_greater_than_eq(a, b); + } + { + boost::dynamic_bitset<Block> a(std::string("0")), b(std::string("0")); + Tests::operator_greater_than_eq(a, b); + } + { + boost::dynamic_bitset<Block> a(std::string("1")), b(std::string("1")); + Tests::operator_greater_than_eq(a, b); + } + { + boost::dynamic_bitset<Block> a(long_string), b(long_string); + Tests::operator_greater_than_eq(a, b); + } + { + boost::dynamic_bitset<Block> a(long_string), b(long_string); + a[long_string.size()/2].flip(); + Tests::operator_greater_than_eq(a, b); + } + { + boost::dynamic_bitset<Block> a(long_string), b(long_string); + b[long_string.size()/2].flip(); + Tests::operator_greater_than_eq(a, b); + } + // check for consistency with ulong behaviour + { + boost::dynamic_bitset<Block> a(3, 4ul), b(3, 5ul); + assert(!(a >= b)); + } + { + boost::dynamic_bitset<Block> a(3, 4ul), b(3, 4ul); + assert(a >= b); + } + { + boost::dynamic_bitset<Block> a(3, 5ul), b(3, 4ul); + assert(a >= b); + } + //===================================================================== + // Test b.test(pos) + { // case pos >= b.size() + boost::dynamic_bitset<Block> b; + Tests::test_bit(b, 0); + } + { // case pos < b.size() + boost::dynamic_bitset<Block> b(std::string("0")); + Tests::test_bit(b, 0); + } + { // case pos == b.size() / 2 + boost::dynamic_bitset<Block> b(long_string); + Tests::test_bit(b, long_string.size()/2); + } + //===================================================================== + // Test b.test_set(pos) + { // case pos >= b.size() + boost::dynamic_bitset<Block> b; + Tests::test_set_bit(b, 0, true); + Tests::test_set_bit(b, 0, false); + } + { // case pos < b.size() + boost::dynamic_bitset<Block> b(std::string("0")); + Tests::test_set_bit(b, 0, true); + Tests::test_set_bit(b, 0, false); + } + { // case pos == b.size() / 2 + boost::dynamic_bitset<Block> b(long_string); + Tests::test_set_bit(b, long_string.size() / 2, true); + Tests::test_set_bit(b, long_string.size() / 2, false); + } + //===================================================================== + // Test b << pos + { // case pos == 0 + std::size_t pos = 0; + boost::dynamic_bitset<Block> b(std::string("1010")); + Tests::operator_shift_left(b, pos); + } + { // case pos == size()/2 + std::size_t pos = long_string.size() / 2; + boost::dynamic_bitset<Block> b(long_string); + Tests::operator_shift_left(b, pos); + } + { // case pos >= n + std::size_t pos = long_string.size(); + boost::dynamic_bitset<Block> b(long_string); + Tests::operator_shift_left(b, pos); + } + //===================================================================== + // Test b >> pos + { // case pos == 0 + std::size_t pos = 0; + boost::dynamic_bitset<Block> b(std::string("1010")); + Tests::operator_shift_right(b, pos); + } + { // case pos == size()/2 + std::size_t pos = long_string.size() / 2; + boost::dynamic_bitset<Block> b(long_string); + Tests::operator_shift_right(b, pos); + } + { // case pos >= n + std::size_t pos = long_string.size(); + boost::dynamic_bitset<Block> b(long_string); + Tests::operator_shift_right(b, pos); + } + //===================================================================== + // Test a & b + { + boost::dynamic_bitset<Block> lhs, rhs; + Tests::operator_and(lhs, rhs); + } + { + boost::dynamic_bitset<Block> lhs(std::string("1")), rhs(std::string("0")); + Tests::operator_and(lhs, rhs); + } + { + boost::dynamic_bitset<Block> lhs(long_string.size(), 0), rhs(long_string); + Tests::operator_and(lhs, rhs); + } + { + boost::dynamic_bitset<Block> lhs(long_string.size(), 1), rhs(long_string); + Tests::operator_and(lhs, rhs); + } + //===================================================================== + // Test a | b + { + boost::dynamic_bitset<Block> lhs, rhs; + Tests::operator_or(lhs, rhs); + } + { + boost::dynamic_bitset<Block> lhs(std::string("1")), rhs(std::string("0")); + Tests::operator_or(lhs, rhs); + } + { + boost::dynamic_bitset<Block> lhs(long_string.size(), 0), rhs(long_string); + Tests::operator_or(lhs, rhs); + } + { + boost::dynamic_bitset<Block> lhs(long_string.size(), 1), rhs(long_string); + Tests::operator_or(lhs, rhs); + } + //===================================================================== + // Test a^b + { + boost::dynamic_bitset<Block> lhs, rhs; + Tests::operator_xor(lhs, rhs); + } + { + boost::dynamic_bitset<Block> lhs(std::string("1")), rhs(std::string("0")); + Tests::operator_xor(lhs, rhs); + } + { + boost::dynamic_bitset<Block> lhs(long_string.size(), 0), rhs(long_string); + Tests::operator_xor(lhs, rhs); + } + { + boost::dynamic_bitset<Block> lhs(long_string.size(), 1), rhs(long_string); + Tests::operator_xor(lhs, rhs); + } + //===================================================================== + // Test a-b + { + boost::dynamic_bitset<Block> lhs, rhs; + Tests::operator_sub(lhs, rhs); + } + { + boost::dynamic_bitset<Block> lhs(std::string("1")), rhs(std::string("0")); + Tests::operator_sub(lhs, rhs); + } + { + boost::dynamic_bitset<Block> lhs(long_string.size(), 0), rhs(long_string); + Tests::operator_sub(lhs, rhs); + } + { + boost::dynamic_bitset<Block> lhs(long_string.size(), 1), rhs(long_string); + Tests::operator_sub(lhs, rhs); + } +} + + +int +main() +{ + run_test_cases<unsigned char>(); + run_test_cases<unsigned short>(); + run_test_cases<unsigned int>(); + run_test_cases<unsigned long>(); +# ifdef BOOST_HAS_LONG_LONG + run_test_cases< ::boost::ulong_long_type>(); +# endif + + return boost::report_errors(); +} diff --git a/src/boost/libs/dynamic_bitset/test/dyn_bitset_unit_tests4.cpp b/src/boost/libs/dynamic_bitset/test/dyn_bitset_unit_tests4.cpp new file mode 100644 index 00000000..413fddd0 --- /dev/null +++ b/src/boost/libs/dynamic_bitset/test/dyn_bitset_unit_tests4.cpp @@ -0,0 +1,338 @@ +// ----------------------------------------------------------- +// Copyright (c) 2001 Jeremy Siek +// Copyright (c) 2003-2006 Gennaro Prota +// +// 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) +// +// ----------------------------------------------------------- + +#include <fstream> +#include <string> +#include <cstddef> // for std::size_t +#include <stdexcept> // for std::logic_error +#include <assert.h> + +#include <boost/config.hpp> +#if !defined (BOOST_NO_STRINGSTREAM) +# include <sstream> +#endif + +#include "bitset_test.hpp" +#include <boost/dynamic_bitset/dynamic_bitset.hpp> +#include <boost/config/workaround.hpp> + + +// Codewarrior 8.3 for Win fails without this. +// Thanks Howard Hinnant ;) +#if defined __MWERKS__ && BOOST_WORKAROUND(__MWERKS__, <= 0x3003) // 8.x +# pragma parse_func_templ off +#endif + + +#if defined BOOST_NO_STD_WSTRING || defined BOOST_NO_STD_LOCALE +# define BOOST_DYNAMIC_BITSET_NO_WCHAR_T_TESTS +#endif + +#if !defined BOOST_DYNAMIC_BITSET_NO_WCHAR_T_TESTS +std::wstring widen_string( const std::string & str, + const std::locale & loc = std::locale() ) +{ + std::wstring result; + const std::string::size_type len = str.length(); + if(len != 0) { + + typedef std::ctype<wchar_t> ct_type; + typedef std::wstring::traits_type tr_type; + const ct_type & ct = BOOST_USE_FACET(ct_type, loc); + + result.resize(len); + for (std::size_t i = 0; i < len; ++i) + tr_type::assign(result[i], ct.widen(str[i])); + + } + return result; +} +#endif + +template <typename Block> +void run_test_cases( BOOST_EXPLICIT_TEMPLATE_TYPE(Block) ) +{ + + typedef boost::dynamic_bitset<Block> bitset_type; + typedef bitset_test<bitset_type> Tests; + + //===================================================================== + // Test stream operator<< + { + + // The test "variables" are: the stream type and its state, the + // exception mask, the width, the fill char and the padding side (left/right) + + std::ios::iostate masks[] = { + std::ios::goodbit, + std::ios::eofbit, + std::ios::failbit, + std::ios::eofbit | std::ios::failbit + }; + + static std::string strings[] = { + std::string(""), + std::string("0"), + std::string("1"), + std::string("11100"), + get_long_string() + }; + + char fill_chars[] = { '*', 'x', ' ' }; + + std::size_t num_masks = sizeof(masks) / sizeof(masks[0]); + std::size_t num_strings = sizeof(strings) / sizeof(strings[0]); + std::size_t num_chars = sizeof(fill_chars) / sizeof(fill_chars[0]); + + std::fstream not_good_stream("dynamic_bitset_tests - this file shouldn't exist", + std::ios::in); + + + for (std::size_t mi = 0; mi < num_masks; ++mi) { + for (std::size_t si = 0; si < num_strings; ++si) { + + std::streamsize slen = (std::streamsize)(strings[si].length()); + + assert( (std::numeric_limits<std::streamsize>::max)() + >=(std::streamsize)(1+slen*2) ); + + for (std::size_t ci = 0; ci < num_chars; ++ci) { + + // note how "negative widths" are tested too + const std::streamsize widths[] = { -1 - slen/2, 0, slen/2, 1 + slen*2 }; + std::size_t num_widths = sizeof(widths) / sizeof(widths[0]); + + for (std::size_t wi = 0; wi < num_widths; ++wi) { + std::streamsize w = widths[wi]; + { + // test 0 - stream !good() + if(not_good_stream.good()) + throw std::logic_error("Error in operator << tests" + " - please, double check"); + bitset_type b(strings[si]); + not_good_stream.width(w); + not_good_stream.fill(fill_chars[ci]); + try { not_good_stream.exceptions(masks[mi]); } catch(...) {} + + Tests::stream_inserter(b, not_good_stream, "<unused_string>"); + } + { + // test 1a - file stream + scoped_temp_file stf; + bitset_type b(strings[si]); + std::ofstream file(stf.path().string().c_str(), std::ios::trunc); + file.width(w); + file.fill(fill_chars[ci]); + file.exceptions(masks[mi]); + Tests::stream_inserter(b, file, stf.path().string().c_str()); + } + { + //NOTE: there are NO string stream tests + } +#if !defined (BOOST_DYNAMIC_BITSET_NO_WCHAR_T_TESTS) + { + // test 1b - wide file stream + scoped_temp_file stf; + bitset_type b(strings[si]); + std::wofstream file(stf.path().string().c_str()); + file.width(w); + file.fill(fill_chars[ci]); + file.exceptions(masks[mi]); + Tests::stream_inserter(b, file, stf.path().string().c_str()); + } +#endif + } + } + } + } // for (; mi..) + + } + + //===================================================================== + // Test stream operator>> + { + + // The test "variables" are: the stream type, the exception mask, + // the actual contents (and/or state) of the stream, and width. + // + // With few exceptions, each test case consists of writing a different + // assortment of digits and "whitespaces" to a text stream and then checking + // that what was written gets read back unchanged. That's NOT guaranteed by + // the standard, unless the assortment always ends with a '\n' and satisfies + // other conditions (see C99, 7.19.2/2), however it works in practice and is + // a good "real life" test. Some characters, such as '\v' and '\f', are not + // used exactly because they are the ones which will most likely give problems + // on some systems (for instance '\f' could actually be written as a sequence + // of new-lines, and we could never be able to read it back) + // + // Note how the bitset object is not initially empty. That helps checking + // that it isn't erroneously clear()ed by operator>>. + + + std::ios::iostate masks[] = { + std::ios::goodbit, + std::ios::eofbit, + std::ios::failbit, + std::ios::eofbit | std::ios::failbit + }; + + const std::string spaces = "\t\n "; //"\t\n\v\f "; + + const std::string long_string = get_long_string(); + /*const*/ static std::string strings[] = { + // NOTE: "const" gives the usual problems with Borland + // (in Tests::stream_extractor instantiation) + + +#if !(defined __BORLANDC__ \ + && BOOST_WORKAROUND(BOOST_RWSTD_VER, BOOST_TESTED_AT(0x20101))) + // Borland 5.5.1 with RW library crashes + // empty string + std::string(""), + // no bitset + spaces, +#endif + // no bitset + std::string("x"), + std::string("\t xyz"), + + // bitset of size 1 + std::string("0"), + std::string("1"), + + std::string(" 0 "), + std::string(" 1 "), + spaces + "1", + "1" + spaces, + spaces + "1" + spaces, + std::string(" x1x "), + std::string(" 1x "), + + // long bitset + long_string, + " " + long_string + " xyz", + spaces + long_string, + spaces + long_string + spaces + }; + + + //----------------------------------------------------- + + std::stringstream not_good_stream; + not_good_stream << "test"; + std::string sink; + not_good_stream >> sink; // now the stream should be in eof state + + const std::size_t num_masks = sizeof(masks) / sizeof(masks[0]); + const std::size_t num_strings = sizeof(strings) / sizeof(strings[0]); + + for (std::size_t mi = 0; mi < num_masks; ++mi) { + for (std::size_t si = 0; si < num_strings; ++si) { + + const std::streamsize slen = (std::streamsize)(strings[si].length()); + assert((std::numeric_limits<std::streamsize>::max)() >= (std::streamsize)(1+slen*2)); + + std::streamsize widths[] = { -1, 0, slen/2, slen, 1 + slen*2 }; + std::size_t num_widths = sizeof(widths) / sizeof(widths[0]); + + for(std::size_t wi = 0; wi < num_widths; ++wi) { + const std::streamsize w = widths[wi]; + + // test 0 - !good() stream + { + if(not_good_stream.good()) + throw std::logic_error("Error in operator >> tests" + " - please, double check"); + bitset_type b(1, 15ul); // note: b is not empty + not_good_stream.width(w); + try { not_good_stream.exceptions(masks[mi]); } catch(...) {} + std::string irrelevant; + Tests::stream_extractor(b, not_good_stream, irrelevant); + } + // test 1a - (narrow) file stream + { + scoped_temp_file stf; + bitset_type b(1, 255ul); + { + std::ofstream f(stf.path().string().c_str()); + f << strings[si]; + } + + std::ifstream f(stf.path().string().c_str()); + f.width(w); + f.exceptions(masks[mi]); + Tests::stream_extractor(b, f, strings[si]); + } +#if !defined(BOOST_NO_STRINGSTREAM) + // test 2a - stringstream + { + bitset_type b(1, 255ul); + std::istringstream stream(strings[si]); + stream.width(w); + stream.exceptions(masks[mi]); + Tests::stream_extractor(b, stream, strings[si]); + } +#endif + +#if !defined(BOOST_DYNAMIC_BITSET_NO_WCHAR_T_TESTS) + // test 1b - wchar_t file stream + { + scoped_temp_file stf; + std::wstring wstr = widen_string(strings[si]); + bitset_type b(1, 255ul); + { + std::basic_ofstream<wchar_t> of(stf.path().string().c_str()); + of << wstr; + } + + std::basic_ifstream<wchar_t> f(stf.path().string().c_str()); + f.width(w); + f.exceptions(masks[mi]); + Tests::stream_extractor(b, f, wstr); + } + // test 2b - wstringstream + { + bitset_type b(1, 255ul); + std::wstring wstr = widen_string(strings[si]); + + std::wistringstream wstream(wstr); + wstream.width(w); + wstream.exceptions(masks[mi]); + Tests::stream_extractor(b, wstream, wstr); + } +#endif // BOOST_DYNAMIC_BITSET_NO_WCHAR_T_TESTS + + } + } + + } // for ( mi = 0; ...) + + + } + //===================================================================== + // << Any other tests go here >> + // ..... + +} + + +int +main() +{ + run_test_cases<unsigned char>(); + run_test_cases<unsigned short>(); + run_test_cases<unsigned int>(); + run_test_cases<unsigned long>(); +# ifdef BOOST_HAS_LONG_LONG + run_test_cases< ::boost::ulong_long_type>(); +# endif + + return boost::report_errors(); +} diff --git a/src/boost/libs/dynamic_bitset/test/dyn_bitset_unit_tests5.cpp b/src/boost/libs/dynamic_bitset/test/dyn_bitset_unit_tests5.cpp new file mode 100644 index 00000000..79a60521 --- /dev/null +++ b/src/boost/libs/dynamic_bitset/test/dyn_bitset_unit_tests5.cpp @@ -0,0 +1,111 @@ +// ----------------------------------------------------------- +// Copyright (c) 2001 Jeremy Siek +// Copyright (c) 2003-2006 Gennaro Prota +// +// Copyright (c) 2015 Seth Heeren +// +// 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) +// +// ----------------------------------------------------------- + +#include "boost/config.hpp" +#if !defined (BOOST_NO_STRINGSTREAM) +# include <sstream> +#endif + +#include "bitset_test.hpp" +#include <boost/dynamic_bitset/serialization.hpp> +#include <boost/config/workaround.hpp> + + +// Codewarrior 8.3 for Win fails without this. +// Thanks Howard Hinnant ;) +#if defined __MWERKS__ && BOOST_WORKAROUND(__MWERKS__, <= 0x3003) // 8.x +# pragma parse_func_templ off +#endif + + +#if defined BOOST_NO_STD_WSTRING || defined BOOST_NO_STD_LOCALE +# define BOOST_DYNAMIC_BITSET_NO_WCHAR_T_TESTS +#endif + +#include <boost/archive/binary_oarchive.hpp> +#include <boost/archive/binary_iarchive.hpp> +#include <boost/archive/xml_oarchive.hpp> +#include <boost/archive/xml_iarchive.hpp> +#include <sstream> + +namespace { + template <typename Block> + struct SerializableType { + boost::dynamic_bitset<Block> x; + + private: + friend class boost::serialization::access; + template <class Archive> void serialize(Archive &ar, const unsigned int) { + ar & BOOST_SERIALIZATION_NVP(x); + } + }; + + template <typename Block, typename IArchive, typename OArchive> + void test_serialization( BOOST_EXPLICIT_TEMPLATE_TYPE(Block) ) + { + SerializableType<Block> a; + + for (int i=0; i<128; ++i) + a.x.resize(11*i, i%2); + +#if !defined (BOOST_NO_STRINGSTREAM) + std::stringstream ss; + + // test serialization + { + OArchive oa(ss); + oa << BOOST_SERIALIZATION_NVP(a); + } + + // test de-serialization + { + IArchive ia(ss); + SerializableType<Block> b; + ia >> BOOST_SERIALIZATION_NVP(b); + + assert(a.x == b.x); + } +#else +# error "TODO implement file-based test path?" +#endif + } + + template <typename Block> + void test_binary_archive( BOOST_EXPLICIT_TEMPLATE_TYPE(Block) ) { + test_serialization<Block, boost::archive::binary_iarchive, boost::archive::binary_oarchive>(); + } + + template <typename Block> + void test_xml_archive( BOOST_EXPLICIT_TEMPLATE_TYPE(Block) ) { + test_serialization<Block, boost::archive::xml_iarchive, boost::archive::xml_oarchive>(); + } +} + +template <typename Block> +void run_test_cases( BOOST_EXPLICIT_TEMPLATE_TYPE(Block) ) +{ + test_binary_archive<Block>(); + test_xml_archive<Block>(); +} + +int main() +{ + run_test_cases<unsigned char>(); + run_test_cases<unsigned short>(); + run_test_cases<unsigned int>(); + run_test_cases<unsigned long>(); +# ifdef BOOST_HAS_LONG_LONG + run_test_cases< ::boost::ulong_long_type>(); +# endif + + return boost::report_errors(); +} diff --git a/src/boost/libs/dynamic_bitset/test/test_ambiguous_set.cpp b/src/boost/libs/dynamic_bitset/test/test_ambiguous_set.cpp new file mode 100644 index 00000000..b51808a8 --- /dev/null +++ b/src/boost/libs/dynamic_bitset/test/test_ambiguous_set.cpp @@ -0,0 +1,30 @@ +// +// Copyright (C) 2018 James E. King III +// +// Permission to copy, use, modify, sell and distribute this software +// is granted provided this copyright notice appears in all copies. +// This software is provided "as is" without express or implied +// warranty, and with no claim as to its suitability for any purpose. +// +// 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) +// + +#include <boost/dynamic_bitset.hpp> +#include <boost/core/lightweight_test.hpp> + +int main(int, char*[]) +{ + boost::dynamic_bitset<> x(5); // all 0's by default + x.set(1, 2); + x.set(3, 1, true); + x.set(2, 1, false); + BOOST_TEST(!x.test(0)); + BOOST_TEST( x.test(1)); + BOOST_TEST(!x.test(2)); + BOOST_TEST( x.test(3)); + BOOST_TEST(!x.test(4)); + + return boost::report_errors(); +} diff --git a/src/boost/libs/dynamic_bitset/test/test_boost_hash.cpp b/src/boost/libs/dynamic_bitset/test/test_boost_hash.cpp new file mode 100644 index 00000000..85900642 --- /dev/null +++ b/src/boost/libs/dynamic_bitset/test/test_boost_hash.cpp @@ -0,0 +1,47 @@ +// +// Copyright (C) 2019 James E. King III +// +// Permission to copy, use, modify, sell and distribute this software +// is granted provided this copyright notice appears in all copies. +// This software is provided "as is" without express or implied +// warranty, and with no claim as to its suitability for any purpose. +// +// 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) +// + +#include <boost/config.hpp> +#include <boost/dynamic_bitset.hpp> +#include <boost/core/lightweight_test.hpp> +#include <set> + +int main(int, char*[]) +{ + typedef boost::dynamic_bitset<unsigned long> bitset_type; + const std::string long_string = + "01001110101110110101011010000000000011110101101111111111"; + const std::string long_string_prime_begin = + "11001110101110110101011010000000000011110101101111111111"; + const std::string long_string_prime_end = + "01001110101110110101011010000000000011110101101111111110"; + + bitset_type zeroes(long_string.size(), 0); + bitset_type stuff (long_string); + bitset_type stupb (long_string_prime_begin); + bitset_type stupe (long_string_prime_end); + bitset_type ones (long_string.size(), 1); + + boost::hash<bitset_type> bitset_hasher; + std::set<std::size_t> results; + results.insert(bitset_hasher(zeroes)); + results.insert(bitset_hasher(stuff)); + results.insert(bitset_hasher(stupb)); + results.insert(bitset_hasher(stupe)); + results.insert(bitset_hasher(ones)); + + // if any hash is identical to another there will be less than 5 + BOOST_TEST_EQ(results.size(), 5); + + return boost::report_errors(); +} diff --git a/src/boost/libs/dynamic_bitset/test/test_lowest_bit.cpp b/src/boost/libs/dynamic_bitset/test/test_lowest_bit.cpp new file mode 100644 index 00000000..a31f19d6 --- /dev/null +++ b/src/boost/libs/dynamic_bitset/test/test_lowest_bit.cpp @@ -0,0 +1,28 @@ +// +// Copyright (C) 2018 James E. King III +// +// Permission to copy, use, modify, sell and distribute this software +// is granted provided this copyright notice appears in all copies. +// This software is provided "as is" without express or implied +// warranty, and with no claim as to its suitability for any purpose. +// +// 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) +// + +#include <boost/cstdint.hpp> +#include <boost/dynamic_bitset/detail/lowest_bit.hpp> +#include <boost/core/lightweight_test.hpp> + +int main(int, char*[]) +{ + for (boost::int32_t i = 1; i < 32; ++i) { + BOOST_TEST_EQ(i, boost::detail::lowest_bit(1u << i)); + } + + BOOST_TEST_EQ(2, boost::detail::lowest_bit(123456788)); + BOOST_TEST_EQ(30, boost::detail::lowest_bit(static_cast<boost::int64_t>(1507208177123328))); + + return boost::report_errors(); +} diff --git a/src/boost/libs/dynamic_bitset/test/test_std_hash.cpp b/src/boost/libs/dynamic_bitset/test/test_std_hash.cpp new file mode 100644 index 00000000..2910b23f --- /dev/null +++ b/src/boost/libs/dynamic_bitset/test/test_std_hash.cpp @@ -0,0 +1,39 @@ +// +// Copyright (C) 2019 James E. King III +// +// Permission to copy, use, modify, sell and distribute this software +// is granted provided this copyright notice appears in all copies. +// This software is provided "as is" without express or implied +// warranty, and with no claim as to its suitability for any purpose. +// +// 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) +// + +#include <boost/config.hpp> +#include <boost/dynamic_bitset.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <functional> +#include <unordered_set> + +int main(int, char*[]) +{ + typedef boost::dynamic_bitset<unsigned long> bitset_type; + const std::string long_string = + "01001110101110110101011010000000000011110101101111111111"; + + bitset_type zeroes(long_string.size(), 0); + bitset_type stuff (long_string); + bitset_type ones (long_string.size(), 1); + + std::unordered_set<bitset_type> bitsets; + bitsets.insert(zeroes); + bitsets.insert(stuff); + bitsets.insert(ones); + + BOOST_TEST_EQ(bitsets.size(), 3); + + return boost::report_errors(); +} |