Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Extra libraries, Code snippets, applications etc. » U++ users applications in progress and useful code snippets, including reference examples! » array vs. array's items.id sorting/searching
array vs. array's items.id sorting/searching [message #26680] Wed, 19 May 2010 11:32 Go to previous message
qwerty is currently offline  qwerty
Messages: 130
Registered: May 2006
Experienced Member
Hello,

I have such a piece of code:
    class Item {
    public:
        int id;
        void do(int);
    };

    Array<Item> items;
    Array<int>  data;


    /*
     * ... filling arrays with items here ...
     */

//section ONE
{
    ArrayMap<int, int> idx;
    for(int i = 0; i < items.GetCount(); ++i) {
        idx.Add(i, items[i].id);
    }

    Vector<int> idx_order = GetSortOrder(idx.GetValues));
    int ni = 0;	    // count of the items with unique id
    for(int i = 1; i < idx_order.GetCount(); ++i) {
        ni += items[idx_order[i - 1]].id ==
              items[idx_order[i    ]].id ? 0 : 1;
    }
}

// section TWO
    for(int d = 0; d < data.GetCount(); ++d) {
        for(int i = 0; i < items.GetCount(); ++i) {
            if(items[i].id == d) {
                items[i].do(d);
            }
	}
    }


section ONE:
I am doing this, because I want the count of unique 'items' by the their 'id', as it can be seen. Is there better approach? (need persistency, thus Array)

optional: sorting the array by their's item's property was for me interesting topic too(by one/two functions of course Wink)

section TWO:
There is redundancy. Bad. I belive, that it can be solved better using upp without adding anything to Item. Maybee.

thank you very much for your optional suggestions

[Updated on: Wed, 19 May 2010 11:47]

Report message to a moderator

 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Just one instance of application running (SingleApp)
Next Topic: Fix for examples/CodeMetric
Goto Forum:
  


Current Time: Wed May 15 04:24:15 CEST 2024

Total time taken to generate the page: 0.01676 seconds