Boost logo

Boost :

From: Yitzhak Sapir (yitzhaks_at_[hidden])
Date: 2002-10-07 14:00:51


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>());
>
> For exact syntax consult MPL list specification.
>
> Gennadiy.

I consulted, and it seems it should be list_c? Anyhow, there is
unfortunately no way to easily generate an array of ints from 2, 3, 5, 7,
11. And I will use this point to ask/learn of mpl in an area in which
I am curious. And that is, I can't seem to find how I would make any
class type S such that
int S<mpl::ct_list<2,3,5,7,11> >::value[] = {2, 3, 5, 7, 11};

It would be nice if it could be done. Without it, the iterator that feeds
into the list constructor has to travel upon the initialization list each
time * is called. (The iterator that feeds into the list is a run time
construct, that has to convert the mpl compile-time construct into a run
time function). In mpl, I understand this would generate something that
if written in code would look like:

int operator*()
{
 switch (current_index) {
 case 0: return 2; // mpl::at_c<0, Sequence>::value;
 case 1: return 3; //etc.
 }
 throw out_of_range();
}

The traversal through the switch is usually a linear operation. This
means that construction of the list requires traversal through n items
each of which does traversal through n items, or a quadratic operation.

Using more intelligent switch() construction this could be done in n log
n. But if arrays could be generated from the sequence, it would have been
an O(n) operation.

I tried writing an iterator adaptor that would handle this. But then I
found that in order to support both mpl and non-mpl iterators I'd have to
be a little tricky.

I still think that the way I posted should be available. For example, for
inserting values into a map which requires feeding in an initialization
list of pairs. Is there a way to specify a pair in mpl? The integral
constant case is probably a frequently used case but it's not the
only case.


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