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  |
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 )
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
|
|
|
Goto Forum:
Current Time: Mon Apr 28 19:39:03 CEST 2025
Total time taken to generate the page: 0.00625 seconds
|