00001 00002 /* 00003 The Extended Integer (XInt) Library 00004 A fast, portable C++ library for multi-precision integer math 00005 Copyright 2010 by Chad Nelson 00006 00007 Distributed under the Boost Software License, Version 1.0. 00008 See accompanying file LICENSE_1_0.txt or copy at 00009 http://www.boost.org/LICENSE_1_0.txt 00010 00011 See http://www.boost.org/libs/xint for library home page. 00012 */ 00013 00020 #ifndef BOOST_INCLUDED_XINT_MAGNITUDE_MANAGER_HPP 00021 #define BOOST_INCLUDED_XINT_MAGNITUDE_MANAGER_HPP 00022 00023 namespace boost { 00024 namespace xint { 00025 namespace detail { 00027 00028 template <bitsize_t Bits, bool Secure, class Allocator> 00029 class magnitude_manager_t { 00030 public: 00031 typedef allocator_t<Bits, Secure, Allocator> Alloc; 00032 00033 magnitude_manager_t(): raw_data(zerodata()) { inc(); } 00034 explicit magnitude_manager_t(std::size_t length, bool readonly = false): 00035 raw_data(Alloc::alloc(length, readonly)) { inc(); } 00036 magnitude_manager_t(const magnitude_manager_t<Bits, Secure, Allocator>& 00037 copy): raw_data(copy.raw_data) { inc(); } 00038 template <bitsize_t B, bool S, class A> 00039 magnitude_manager_t(const magnitude_manager_t<B, S, A>& copy): 00040 raw_data(copy.raw_data) { inc(); } 00041 ~magnitude_manager_t() { dec(); } 00042 00043 magnitude_manager_t& operator=(const magnitude_manager_t<Bits, Secure, 00044 Allocator>& copy) { reset(copy.raw_data); return *this; } 00045 00046 bool is_nan() const { return (raw_data == 0); } 00047 void make_nan() { dec(); raw_data = 0; } 00048 00049 void resize_and_uniquify(std::size_t newsize = 0, realloc::strategy strategy 00050 = realloc::copy) { raw_data = Alloc::realloc_and_uniquify(raw_data, 00051 newsize, strategy); } 00052 00053 bool same_storage(const magnitude_manager_t<Bits, Secure, Allocator> n) 00054 const { return raw_data == n.raw_data; } 00055 00056 // Do not call these on a NaN 00057 std::size_t max_length() const { return raw_data->max_length; } 00058 bool is_unique() const { return (raw_data->copies() == 1); } 00059 digit_t *digits() { return raw_data->digits; } 00060 const digit_t *digits() const { return raw_data->digits; } 00061 void trim() { fixedlength_t<Bits>::trim(raw_data); } 00062 00063 void _swap(magnitude_manager_t<Bits, Secure, Allocator>& i2) { 00064 using std::swap; 00065 swap(raw_data, i2.raw_data); 00066 } 00067 00068 private: 00069 void reset(magnitude_t *p) { if (raw_data != p) { dec(); raw_data = p; 00070 inc(); } } 00071 void inc() { if (raw_data) Alloc::attach(raw_data); } 00072 void dec() { if (raw_data) Alloc::detach(raw_data); } 00073 static magnitude_t *zerodata() { static magnitude_manager_t z(1, true); 00074 return z.raw_data; } 00075 00076 magnitude_t *raw_data; 00077 00078 template <bitsize_t B, bool S, class A> 00079 friend class magnitude_manager_t; 00080 }; 00081 00082 template <bitsize_t Bits, bool Secure, class Allocator> 00083 void swap(magnitude_manager_t<Bits, Secure, Allocator>& p1, 00084 magnitude_manager_t<Bits, Secure, Allocator>& p2) 00085 { 00086 p1._swap(p2); 00087 } 00088 00090 } // namespace detail 00091 } // namespace xint 00092 } // namespace boost 00093 00094 #endif // BOOST_INCLUDED_XINT_MAGNITUDE_MANAGER_HPP
© Copyright Chad Nelson, 2010-2011. 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)