summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/dynamic_bitset/example
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-27 18:24:20 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-27 18:24:20 +0000
commit483eb2f56657e8e7f419ab1a4fab8dce9ade8609 (patch)
treee5d88d25d870d5dedacb6bbdbe2a966086a0a5cf /src/boost/libs/dynamic_bitset/example
parentInitial commit. (diff)
downloadceph-483eb2f56657e8e7f419ab1a4fab8dce9ade8609.tar.xz
ceph-483eb2f56657e8e7f419ab1a4fab8dce9ade8609.zip
Adding upstream version 14.2.21.upstream/14.2.21upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/boost/libs/dynamic_bitset/example')
-rw-r--r--src/boost/libs/dynamic_bitset/example/Jamfile26
-rw-r--r--src/boost/libs/dynamic_bitset/example/example1.cpp35
-rw-r--r--src/boost/libs/dynamic_bitset/example/example2.cpp32
-rw-r--r--src/boost/libs/dynamic_bitset/example/example3.cpp71
-rw-r--r--src/boost/libs/dynamic_bitset/example/timing_tests.cpp142
5 files changed, 306 insertions, 0 deletions
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;
+}
+