Click here to Skip to main content
12,358,497 members (61,099 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C++
Can someone please explain exactly what is happening in this code? It is intended to find all the possible arrangements of operations. A detailed breakdown and explanation would be appreciated. Thanks much.

/** Recursion for enumeration of all possible transaction schedules */
void gen_all_sch_r(const xa_coll_t& xa_coll,
    vector<xa_t::const_iterator>& xac_pos,
    vector<xa_op_t>& sch)
{
    size_t num_xa = xa_coll.size();
 
    //if(xac_pos.empty())
    //{
    //    xac_pos = vector<xa_coll_t::const_iterator>(num_xa);
    //}

    /*
    vector<size_t> v(4);
    iota(v.begin(), v.end(), 0);
    print_coll(cout, v.begin(), v.end(), " ", "\n");
    while(next_permutation(v.begin(), v.end()))
    {
        print_coll(cout, v.begin(), v.end(), " ", "\n");
    }
    //copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
    */
 
    // Have all transaction operations been added to the schedule?
    bool all_xa_op_used = true;
    for(size_t i = 0; i < num_xa; i++)
    {
        if(xac_pos[i] != xa_coll[i].end())
        {
            all_xa_op_used = false;
        }
    }
 
    if(all_xa_op_used)
    {
        // We have found another schedule
        cout << "SCHEDULE" << endl;
        cout << sch;
 
        // TODO: Is this schedule serializable? Use graphs and cycles.
    }
    else
    {
        // Still need to recurse one level lower to continue filling
        // the schedule
        for(size_t i = 0; i < num_xa; i++)
        {
            if(xac_pos[i] != xa_coll[i].end())
            {
                sch.push_back(*xac_pos[i]);
                xac_pos[i]++;
                gen_all_sch_r(xa_coll, xac_pos, sch);
                xac_pos[i]--;
                sch.pop_back();
            }
        }
    }
 
}
 
void gen_all_sch(const xa_coll_t& xa_coll)
{
    vector<xa_op_t> sch;
    vector<xa_t::const_iterator> xac_pos(xa_coll.size());
 
    for(size_t pos = 0; pos < xa_coll.size(); pos++)
    {
        xac_pos[pos] = xa_coll[pos].cbegin();
    }
 
    gen_all_sch_r(xa_coll, xac_pos, sch);
}
Posted 23-Feb-13 18:48pm
wayten336
Updated 23-Feb-13 22:18pm
v2
Comments
Richard MacCutchan 24-Feb-13 3:32am
   
Given the lack of comments in the code and the fact that we have no information about where it came from, or what it is about, it will not be easy.

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Do you have any idea how much work explaining code line by line is?
Every single line needs a paragraph of explanation! For example:
int next = r.Next();
Create a new variable called "next" which can hold a integer value. From the previously declared Random instance "r", call the "Next" method to get a new random number, and assign it to the "next" variable.

Can you imagine how long it would take us to explain even a very short code fragment like your example, line by line?

No. It is not going to happen. If you have a specific problem, then ask a question about it. But think first - would you want to sit down for 45 minutes and type up a line-by-line description for no good reason?
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160621.1 | Last Updated 24 Feb 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100