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 next 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

Re: array vs. array's items.id sorting/searching [message #26681 is a reply to message #26680] Wed, 19 May 2010 11:52 Go to previous messageGo to next message
fudadmin is currently offline  fudadmin
Messages: 1321
Registered: November 2005
Location: Kaunas, Lithuania
Ultimate Contributor
Administrator
Have you had a look at GridCtrl GridSort.cpp?
Re: array vs. array's items.id sorting/searching [message #26688 is a reply to message #26680] Wed, 19 May 2010 13:12 Go to previous messageGo to next message
fudadmin is currently offline  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.
Re: array vs. array's items.id sorting/searching [message #26710 is a reply to message #26688] Thu, 20 May 2010 09:44 Go to previous messageGo to next message
qwerty is currently offline  qwerty
Messages: 130
Registered: May 2006
Experienced Member
Yes, it's quite possible... not changed my code, but that 'static' trick is nice, I'll remember that

thank you
Re: array vs. array's items.id sorting/searching [message #26713 is a reply to message #26710] Thu, 20 May 2010 14:58 Go to previous messageGo to next message
fudadmin is currently offline  fudadmin
Messages: 1321
Registered: November 2005
Location: Kaunas, Lithuania
Ultimate Contributor
Administrator
qwerty wrote on Thu, 20 May 2010 08:44

Yes, it's quite possible... not changed my code, but that 'static' trick is nice, I'll remember that

thank you


thanks for noticing! Smile

btw, this trick with that code serves as internal shared clipboard amongst Grid1 instances. It enables a user to copy with e.g Shift_Ctrl_C from one database with duplicates and paste into another without duplicates with customizable column selection sets. Of course you need more lines of code.

querty, maybe a bit off topic:
but... my general advice for newbies (and not only) - use GridCtrl with QuickSearch enabled for debugging purposes. It is much faster to explore and find things than using text logs.

And... a kind of my patented idea... Smile with this code it is possible to have/find/use unique strings from debug output.

And another one. Use and learn from GridCtrl everywhere unless speed really matters. And even then use GridCtrl. It's the best thing after the wheel invention. Believe me.
Sorry, maybe too much off topic... Smile


Re: array vs. array's items.id sorting/searching [message #26714 is a reply to message #26713] Thu, 20 May 2010 16:21 Go to previous messageGo to next message
qwerty is currently offline  qwerty
Messages: 130
Registered: May 2006
Experienced Member
little bit disapointig is, that I am still newbie a few years in upp Very Happy

thanx for advices, I am curious what I will find

[Updated on: Thu, 20 May 2010 16:24]

Report message to a moderator

Re: array vs. array's items.id sorting/searching [message #30328 is a reply to message #26714] Fri, 24 December 2010 23:10 Go to previous message
alendar is currently offline  alendar
Messages: 47
Registered: January 2010
Location: Idaho, USA
Member
I'm snagging this static wrapper idea, I had a problem with the VectorMap sitting as an attribute, this really helps!

Thanks!


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


Current Time: Fri Apr 19 11:51:22 CEST 2024

Total time taken to generate the page: 0.03480 seconds