Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9308693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:41:23+00:00 2026-06-19T00:41:23+00:00

The problem looks simple enough, basically I have a sequence of sequences, something like:

  • 0

The problem looks simple enough, basically I have a sequence of sequences, something like:

typedef mpl::vector<
  mpl::vector<mpl::_1, mpl::_2>,
  mpl::vector<mpl::_1, mpl::_2, mpl::_3>,
  mpl::vector<mpl::_2, mpl::_1>,
  mpl::vector<mpl::_2, mpl::_2>,
  mpl::vector<mpl::_2, mpl::_2, mpl::_3>
> seq;

What I would like to do is to transform this to a trie, the end result being something like:

mpl::map<
  mpl::pair<mpl::_1, 
    mpl::map<
      mpl::pair<mpl::_2,
        mpl::map<
          mpl::pair<TERMINAL, T>,
          mpl::pair<mpl::_3,
            mpl::map<
              mpl::pair<TERMINAL, T>
            >
          >
        >
      >
    >
  >
  mpl::pair<mpl::_2, 
    mpl::map<
      mpl::pair<mpl::_1,
        mpl::map<
          mpl::pair<TERMINAL, T>
        >
      >,
      mpl::pair<mpl::_2,
        mpl::map<
          mpl::pair<TERMINAL, T>,
          mpl::pair<mpl::_3,
            mpl::map<
              mpl::pair<TERMINAL, T>
            >
          >
        >
      >
    >
  >
>

So, the question is, is this possible (I’m thinking it’s not)? If it is possible, which dark spells have I missed?

EDIT: In case the above transformation from sequence of sequences to a trie is not clear, let me see if I can state it in plain English (often more difficult.) Basically each sequence within the main sequence is composed of some types (_1, _2 etc.) The transformed version is trie where common prefixes are collapsed. May be the attached picture helps..

resulting tree

EDIT2: Thanks to @Yakk, hopefully now the question is clearer…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T00:41:25+00:00Added an answer on June 19, 2026 at 12:41 am

    There you go:

    struct Terminal;
    
    template < typename Trie, typename First, typename Last,
               typename Enable = void >
    struct insertInTrie_impl
    {
        typedef typename
            mpl::deref<First>::type key;
    
        typedef typename 
            mpl::at<
                Trie,
                key
            >::type subTrieOrVoid; // would be easier if "at" supported Default
    
        typedef typename
            mpl::if_<
                boost::is_same< subTrieOrVoid, mpl::void_ >,
                mpl::map<>,
                subTrieOrVoid
            >::type subTrie;
    
        typedef typename
            mpl::insert<
                Trie,
                mpl::pair<
                    key, typename
                    insertInTrie_impl<
                        subTrie, typename
                        mpl::next<First>::type,
                        Last
                    >::type
                >
            >::type type;
    };
    
    template < typename Trie, typename First, typename Last >
    struct insertInTrie_impl< Trie, First, Last, typename 
        boost::enable_if< boost::is_same<First, Last> >::type >
        : mpl::insert<
            Trie,
            mpl::pair< Terminal, Terminal >
            // I'm not sure what you want in your terminal node
        >
    {};
    
    template < typename Trie, typename Seq >
    struct insertInTrie
        : insertInTrie_impl< 
            Trie, typename 
            mpl::begin<Seq>::type, typename 
            mpl::end<Seq>::type
        >
    {};
    
    
    template < typename SeqOfSeq >
    struct constructTrie
        : mpl::fold< 
            SeqOfSeq,
            mpl::map<>,
            insertInTrie< mpl::_1, mpl::_2 >
        >
    {};
    

    insertInTrie_impl is a recursive metafunction that inserts a sequence into an existing trie, using iterators. insertInTrie accepts a sequence an calls insertInTrie_impl. constructTrie applies insertInTrie to all sequences in the given sequence, starting with an empty trie.

    In pseudo-code, this reads as follow:

    Trie insertInTrie_impl(trie, first, last)
    {
        if (first == last)
        {
            trie.insert(Terminal, Terminal);
            return trie;
        }
    
        key = *first;
    
        subTrie = trie[key];
        if (subTrie = void) // key not found
        {
            subTrie = emptyTrie;
        }
    
        trie.insert(key, insertInTrie_impl(subTrie, ++first, last))
    
        return trie;
    }
    
    Trie insertInTrie(trie, seq)
    {
        return insertInTrie_impl(trie, seq.begin(), seq.end();
    }
    
    Trie constructTrie(seqOfSeq)
    {
        return fold(seqOfSeq, emptyTrie, insertInTrie);
    }
    

    Finally, a sample use:

    int main()
    {
        typedef mpl::vector<
            mpl::vector<mpl::_1, mpl::_2>,
            mpl::vector<mpl::_1, mpl::_2, mpl::_3>,
            mpl::vector<mpl::_2, mpl::_1>,
            mpl::vector<mpl::_2, mpl::_2>,
            mpl::vector<mpl::_2, mpl::_2, mpl::_3>
        > seqOfSeq;
    
        typedef constructTrie< seqOfSeq >::type bigTrie;
    
        BOOST_MPL_ASSERT(( 
            mpl::has_key<
                mpl::at< 
                    mpl::at< 
                        bigTrie, 
                        mpl::_1
                    >::type, 
                    mpl::_2
                >::type, 
                Terminal
            > ));
        BOOST_MPL_ASSERT(( 
            mpl::has_key<
                mpl::at< 
                    mpl::at< 
                        mpl::at< 
                            bigTrie, 
                            mpl::_1
                        >::type,
                        mpl::_2
                    >::type, 
                    mpl::_3
                >::type, 
                Terminal
            > ));
        BOOST_MPL_ASSERT(( 
            mpl::has_key<
                mpl::at< 
                    mpl::at< 
                        bigTrie, 
                        mpl::_2
                    >::type,
                    mpl::_2
                >::type, 
                Terminal
            > ));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It looks like a problem which could have simple solution, but I haven't found
Problem: I have a string that looks like this: [1=>2,3,4][5=>6,7,8][9=>10,11,12][13=>14,15][16=>17,18] Question: How can you
It looks like POSIXlt allows millisecond precision specification, but I have a problem when
I have a problem. It looks like ARC synchronizes my property with child class.
I use Spring MVC [version: 2.5] and Security[version: 2.0.4]. My problem looks like that:
UPDATE It looks like this problem has been quietly fixed in iOS 4.3. Up
Strange problem. My Query looks like SELECT DISTINCT ID, `etcetc`, `if/elses over muliple joined
It looks like $smth is not a function is a very common problem with
So, I have a page that looks like the following: alt text http://img704.imageshack.us/img704/5973/croppercapture3.png This
This should be simple enough, but somehow my brain stopped working. I have two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.