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
|
|
|
|
Re: array vs. array's items.id sorting/searching [message #26688 is a reply to message #26680] |
Wed, 19 May 2010 13:12   |
 |
fudadmin
Messages: 1321 Registered: November 2005 Location: Kaunas, Lithuania
|
Ultimate Contributor Administrator |
|
|
For my own GridCtrl class I have written a code that copies/pastes only unique column values (useful for databases...). Something like this:
//Grid1.h
VectorMap<String, int>& Unique(); //Value might be better?
//Grid1.cpp
VectorMap<String, int>& Grid1::Unique()
{
static VectorMap<String, int> x;
return x;
}
void Grid1::PumpInCol()
{
int col=GetSelCol();
Unique().Clear();
for (int r=0; r< GetRowCount(); r++)
Unique().GetAdd(AsString(Get(r,col)), 0)++;
SelectCells(col,col,-1,-1);
}
void Grid1::PumpOutCol()
{
Clear();
Vector<int> order = UPP::GetSortOrder( Unique().GetKeys() );
for(int i = 0; i < order.GetCount(); i++)
{
Add();
String mylist = Unique().GetKey(order[i]);
// SetLast(0, i+1);
SetLast(1, atoi(mylist));
// SetLast(2, Unique()[order[i]]); // 0 everywhere
// SetLast(1, AsString(Unique()[i]));
SetLast(2, mylist.GetCount());
}
void Grid1::PumpOutColM()
{
for(int i = 0; i < Unique().GetCount(); i++)
{
Add();
SetLast(0, i+1);
SetLast(1, Unique().GetKey(i));
SetLast(2, Unique()[i]);
}
}
Maybe it would give you some ideas.
|
|
|
|
|
|
|
Goto Forum:
Current Time: Mon Apr 28 19:18:07 CEST 2025
Total time taken to generate the page: 0.01071 seconds
|