X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=src%2Fstlplus%2Fpersistence%2Fpersistent_bitset.tpp;fp=src%2Fstlplus%2Fpersistence%2Fpersistent_bitset.tpp;h=e6b4d542241ddfdac48feb4954e5aa5a111c8f38;hb=6b0a0d0efafe34d48ab344fca3b479553bd4e62c;hp=0000000000000000000000000000000000000000;hpb=85783316365181491a3e3c0c63659972477cebba;p=chaz%2Fyoink diff --git a/src/stlplus/persistence/persistent_bitset.tpp b/src/stlplus/persistence/persistent_bitset.tpp new file mode 100644 index 0000000..e6b4d54 --- /dev/null +++ b/src/stlplus/persistence/persistent_bitset.tpp @@ -0,0 +1,61 @@ +//////////////////////////////////////////////////////////////////////////////// + +// Author: Andy Rushton +// Copyright: (c) Southampton University 1999-2004 +// (c) Andy Rushton 2004-2009 +// License: BSD License, see ../docs/license.html + +//////////////////////////////////////////////////////////////////////////////// +#include "persistent_int.hpp" + +namespace stlplus +{ + + //////////////////////////////////////////////////////////////////////////////// + // format: data msB first, packed into bytes with lowest index at the byte's lsb + + // Note: the interface does not provide access to the internal storage and yet + // to be efficient the std::bitset must be packed as bytes. Thus I have to do it the + // hard way. + + template + void dump_bitset(dump_context& context, const std::bitset& data) + throw(persistent_dump_failed) + { + size_t bits = data.size(); + size_t bytes = (bits+7)/8; + for (size_t B = bytes; B--; ) + { + unsigned char ch = 0; + for (size_t b = 0; b < 8; b++) + { + size_t bit = B*8+b; + if (bit < bits && data.test(bit)) + ch |= (0x01 << b); + } + dump_unsigned_char(context,ch); + } + } + + template + void restore_bitset(restore_context& context, std::bitset& data) + throw(persistent_restore_failed) + { + size_t bits = data.size(); + size_t bytes = (bits+7)/8; + for (size_t B = bytes; B--; ) + { + unsigned char ch = 0; + restore_unsigned_char(context,ch); + for (size_t b = 0; b < 8; b++) + { + size_t bit = B*8+b; + if (bit >= bits) break; + data.set(bit, ch & (0x01 << b) ? true : false); + } + } + } + + //////////////////////////////////////////////////////////////////////////////// + +} // end namespace stlplus