|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r64410 - in sandbox/SOC/2010/bits_and_ints: boost/integer libs/integer/test
From: muriloufg_at_[hidden]
Date: 2010-07-28 09:24:38
Author: murilov
Date: 2010-07-28 09:24:37 EDT (Wed, 28 Jul 2010)
New Revision: 64410
URL: http://svn.boost.org/trac/boost/changeset/64410
Log:
Added find_first_one_string function wich finds the first string of ones in an integral value of a given length
Added:
sandbox/SOC/2010/bits_and_ints/boost/integer/find_first_one_string.hpp (contents, props changed)
sandbox/SOC/2010/bits_and_ints/libs/integer/test/find_first_one_string_test.cpp (contents, props changed)
Added: sandbox/SOC/2010/bits_and_ints/boost/integer/find_first_one_string.hpp
==============================================================================
--- (empty file)
+++ sandbox/SOC/2010/bits_and_ints/boost/integer/find_first_one_string.hpp 2010-07-28 09:24:37 EDT (Wed, 28 Jul 2010)
@@ -0,0 +1,74 @@
+// Boost integer/isqrt.hpp header file ------------------------------//
+
+// (C) Copyright Murilo Adriano Vasconcelos 2010.
+// Distributed under the Boost Software License, Version 1.0. (See
+// accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt
+
+// See http://www.boost.org for updates, documentation, and revision history.
+
+#ifndef BOOST_FFOS_INCLUDED
+#define BOOST_FFOS_INCLUDED
+
+#include <boost/utility/enable_if.hpp>
+#include <boost/type_traits/is_unsigned.hpp>
+#include <boost/math/policies/policy.hpp>
+#include <boost/math/policies/error_handling.hpp>
+#include <boost/integer/count_leading_zeros.hpp>
+
+namespace boost {
+
+/*
+ * This function finds the first occurrence of a string of
+ * ones in `value` with a given lenght `size`.
+ *
+ * `size` must be greater than 0 and less than or equal the
+ * length in bits of type `T`. If these requirements are
+ * no met, one error is raised according to the policy
+ * used.
+ *
+ * See the math::policies documentation for more details about
+ * policies.
+ *
+ * `T` must be an unsigned type.
+ */
+
+template <typename T, typename Policy>
+typename enable_if<is_unsigned<T>, int>::type
+find_first_one_string(T value, unsigned size, const Policy& pol)
+{
+
+ if (size == 0 || size > (sizeof(T) << 3)) {
+ return math::policies::raise_domain_error(
+ "boost::find_first_one_string(%1%, %1%)",
+ "find_first_one_string requires size greater than 0 "
+ "and less than or equal the size in bits of value "
+ "size=%1% given,",
+ size, pol
+ );
+ }
+
+ int shift;
+ while (size > 1) {
+ shift = size >> 1;
+ value &= (value << shift);
+ size -= shift;
+ }
+
+ unsigned ret = count_leading_zeros(value);
+
+ if (ret < (sizeof(T) << 3)) return ret;
+
+ return -1; // not found
+}
+
+template <typename T>
+typename enable_if<is_unsigned<T>, int>::type
+find_first_one_string(T value, int size)
+{
+ return find_first_one_string(value, size, math::policies::policy<>());
+}
+
+}
+
+#endif
\ No newline at end of file
Added: sandbox/SOC/2010/bits_and_ints/libs/integer/test/find_first_one_string_test.cpp
==============================================================================
--- (empty file)
+++ sandbox/SOC/2010/bits_and_ints/libs/integer/test/find_first_one_string_test.cpp 2010-07-28 09:24:37 EDT (Wed, 28 Jul 2010)
@@ -0,0 +1,43 @@
+// Boost integer/find_first_one_string.hpp test program -------//
+// (C) Copyright Murilo Adriano Vasconcelos 2010.
+// Distributed under the Boost Software License, Version 1.0. (See
+// accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt
+
+
+#include <iostream>
+#include <cmath>
+#include <limits>
+#include <boost/cstdint.hpp>
+#include <boost/detail/lightweight_test.hpp>
+#include <boost/integer/find_first_one_string.hpp>
+
+int main()
+{
+ using boost::find_first_one_string;
+
+ BOOST_TEST((find_first_one_string((uint32_t)0, 1) == -1));
+ BOOST_TEST((find_first_one_string((uint32_t)0, 2) == -1));
+ BOOST_TEST((find_first_one_string((uint32_t)0, 3) == -1));
+ BOOST_TEST((find_first_one_string((uint32_t)0, 30) == -1));
+ BOOST_TEST((find_first_one_string((uint32_t)0, 31) == -1));
+ BOOST_TEST((find_first_one_string((uint32_t)0x0F, 4) == 28));
+ BOOST_TEST((find_first_one_string((uint32_t)0x1F, 4) == 27));
+ BOOST_TEST((find_first_one_string((uint32_t)0xFFFFFFFF, 1) == 0));
+ BOOST_TEST((find_first_one_string((uint32_t)0xFFFFFFFF, 10) == 0));
+ BOOST_TEST((find_first_one_string((uint32_t)0xFFFFFFFF, 31) == 0));
+ BOOST_TEST((find_first_one_string((uint32_t)0xFFFFFFFF, 32) == 0));
+ BOOST_TEST((find_first_one_string((uint32_t)0x0000FFFF, 16) == 16));
+ BOOST_TEST((find_first_one_string((uint32_t)0x0000FFFF, 20) == -1));
+
+#ifndef BOOST_HAS_NO_INT64_T
+ BOOST_TEST((find_first_one_string((uint64_t)0, 1) == -1));
+ BOOST_TEST((find_first_one_string((uint64_t)0, 20) == -1));
+ BOOST_TEST((find_first_one_string((uint64_t)0xFFFF0000FFFF, 16) == 16));
+ BOOST_TEST((find_first_one_string((uint64_t)0xFFFF0000FF0FFFFF, 17) == 44));
+ BOOST_TEST((find_first_one_string((uint64_t)0xFFFFFFFFFFFFFFFF, 64) == 0));
+ BOOST_TEST((find_first_one_string((uint64_t)0xFFFFFFFFFFFFFFFF, 63) == 0));
+#endif
+
+ return boost::report_errors();
+}
\ No newline at end of file
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk