Boost logo

Boost :

From: Gennaro Prota (gennaro_prota_at_[hidden])
Date: 2004-04-14 02:34:14


On Tue, 13 Apr 2004 08:46:57 -0400, David Abrahams
<dave_at_[hidden]> wrote:

>Gennaro Prota <gennaro_prota_at_[hidden]> writes:
>
>> On Sat, 10 Apr 2004 10:34:49 -0400, David Abrahams
>> <dave_at_[hidden]> wrote:
>>
>>>Gennaro Prota <gennaro_prota_at_[hidden]> writes:
>>>
>>>> Hi everybody,
>>>>
>>>> as I have mentioned in some post here, a new version of
>>>> dynamic_bitset is in the sandbox.
>>>
>>>Neat! I just noticed that the old one is missing an empty() member
>>>function.
>>
>> Yes. I noticed that but was not sure whether adding it or not (just to
>> avoid cluttering the interface with too many "utility" functions).
>> I've added it now, with tests (wow :)).
>>
>>>> I've finished everything 4/5 days ago (including new and more tests)
>>>> and thought to move it to the main repository, as Jeremy asked too.
>>>
>>>What does your change do?
>>
>> Hmm. Here's an extremely condensed (not exhaustive) summary of
>> changes, future directions and open issues. Sorry if it's terse and
>> incomplete, but I don't have time right now.
>
><snip>
>
>Wow, that's terse!?!? I'm *very* impressed.

Thanks :) BTW, I forgot to say that the stream extractor has also
different semantics: formerly, it extracted at most b.size()
characters; now it enlarges the dynamic_bitset as needed. The docs
have a section named 'Changes from previous versions'. I also added
the following functions: find_first, find_next, get_allocator(),
max_size(), intersects() and empty().

About the stream extractor: the fixed-size semantics it had before
were chosen to make it possible for users to write template functions
like this:

  template <class Bitset>
  void foo(Bitset& b) {
  cin >> b;
  // etc...
  }

and use them with both std::bitset and boost::dynamic_bitset. Of
course this was at the cost of sacrificing the natural semantics for a
dynamic structure. After some discussions with Jeremy we agreed to
adopt the dynamic behavior by default and require a little more work
to reuse the template above. In particular you can recycle foo() with
a little wrapper:

  template <typename T>
  class fix_size
  {
    T& m_t;

    public:
    fix_size(T & t):m_t(t) {}

    template <typename T>
    friend std::istream & operator>>(std::istream & is,
                                     fix_size<T>& obj);
  };

  template <typename T>
  std::istream & operator>>(std::istream & is,
                            fix_size<T>& obj)
  {
    [width saver object here...]

    return is >> std::setw(obj.m_t.size()) >> obj.m_t;
  }

This is basically a proxy for the actual bitset type. You can use it
up in the call chain, where you actually construct the bitset to be
passed to the generic functions (note that it's usable for both
dynamic and static bitsets and has no effects on the latter):

  typedef boost::dynamic_bitset<> bitset_type;
  bitset_type b(4);

  fix_size<bitset_type> proxy(b);
  foo(proxy);
  std::cout << b;

If one is reusing a foo function that also calls bitset functions
other than the extractor (e.g. any() or count(), etc.) the wrapper
becomes a little more complex but is still feasible; it just has more
boilerplate code.

Genny.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk