Boost logo

Boost :

From: Yitzhak Sapir (yitzhaks_at_[hidden])
Date: 2002-10-08 12:56:31


On Mon, 7 Oct 2002, Rozental, Gennadiy wrote:

> > Why not make an iterator adaptor (initialization_iterator?)
> > that holds a list of values with which to initialize. And
> > then pass it to any of the STL containers. (I can post my
> > implementation if it's interesting):
> >
> > std::list<int> primes(
> > make_init_iterator<int>(2)(3)(5)(7)(11)(),
> > make_init_iterator<int>());
>
> I like this idea. But proposed interface look like a waste since all of the
> argument are know at compile time.
>
> I would rather preper:
>
> std::list<int> primes(
> make_init_iterator<int, mpl::ct_list<2,3,5,7,11> >(),
> make_init_iterator<int>());
>

I couldn't use this exact format because:
1) I couldn't specialize make_init_iterator<int> so it would work for the
regular case, and make_init_iterator<int, list> so it would work for the
compile time case.
2) VC 6.5 has a bug when given a function like this. (It generates the
same function call for both the end iterator generator and the begin
iterator generator).

But learning mpl was a nice challenge so instead, I created something that
works as follows:
  std::list<int> l3(
    ct_init_iterator<boost::mpl::list_c<int, 2, 3, 5, 7>, int>::type(),
    ct_init_iterator<boost::mpl::list_c<int>, int>::type()
  );
  std::list<int> l4(
    ct_init_iterator<boost::mpl::vector_c<int, 2, 3, 5, 7, 11, 13> >::type(),
    ct_init_iterator<boost::mpl::vector_c<int> >::type()
  );

It's quite annoying that I don't have the equivalent of Cont::value_type
for list_c/vector_c/range_c. I needed it in the case of the empty list
too so I'd know what kind of end-marker iterator to generate. Also why
does list_c allow specifying a type but defaults all parameters to long?
Couldn't list_c<class T, T c1, T c2, etc.> have been used? (Like in
integral_c?) Anyhow, given that all the parameters default to long, I
made the ct_init_iterator have the second parameter default to long too,
so you don't really have to specify the value type of the list.

The compile time complexity is O(1) for operator* and O(1) for operator++,
because I build compile-time static data structures to hold each value of
the iteration. But the iterator still can't be random access.

I also simplified the form for pairs:
  map_t m3(
    make_init_iterator<map_t::value_type>(1, "once")(2, "twice")
      (3, "thrice")(),
    make_init_iterator<map_t::value_type>());

And the regular list form remains:
  std::list<int> l(
    make_init_iterator<int>(2)(3)(5)(7)(11)(),
    make_init_iterator<int>());

Also, there's a little bug in the at.hpp documentation. The documentation
suggests that the format is at<N, Sequence> whereas the format seems to be
at<Sequence, N>.

And for the pair optimization, I need a "is_pair". That would have been
nice to have in type traits.




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