Home » Community » Newbie corner » SortedIndex and Less
SortedIndex and Less [message #40728] |
Tue, 10 September 2013 17:34 |
keltor
Messages: 73 Registered: February 2012
|
Member |
|
|
Hey there,
I am finally stepping into NTL and so far I like it a lot, much nicer than STL, I think. I have encountered a hurdle though.
I would like to sort a class. I provide what I thought was enough data to appease the NTL gods, but it seems I need some more stuff.
Here's a sample of my simple test program:
struct rho : Moveable<rho> {
int x,y,z;
rho(int x,int y,int z) : x(x), y(y), z(z) {}
rho() {}
}r;
unsigned GetHashValue(const rho& p)
{
return CombineHash(p.x, p.x, p.z);
}
bool operator==(const rho& a, const rho& b)
{
return a.x == b.x && a.y == b.y && a.z == b.z;
}
bool operator < (const rho& a, const rho& b){
return a.x == b.x ? (a.y == b.y ? a.z < b.z : a.y < b.y) : a.x < b.x;
}
Index<rho> works fine (without the operator < part), but SortedIndex<rho> does not. I looked at the StdLess<T> code and it seems that all it does is to provide a simple return a < b operation. What am I missing?
Thanks,
Kel
|
|
|
Re: SortedIndex and Less [message #40827 is a reply to message #40728] |
Sat, 21 September 2013 19:12 |
|
mirek
Messages: 14155 Registered: November 2005
|
Ultimate Member |
|
|
Not sure what went wrong, I have tried this:
#include <Core/Core.h>
using namespace Upp;
struct rho : Moveable<rho> {
int x,y,z;
rho(int x,int y,int z) : x(x), y(y), z(z) {}
rho() {}
String ToString() const { return AsString(x) + ' ' + AsString(y) + ' ' + AsString(z); }
};
bool operator < (const rho& a, const rho& b){
return a.x == b.x ? (a.y == b.y ? a.z < b.z : a.y < b.y) : a.x < b.x;
}
CONSOLE_APP_MAIN{
StdLogSetup(LOG_FILE);
SortedIndex<rho> data;
data.Add(rho(1, 2, 3));
data.Add(rho(1, 1, 1));
data.Add(rho(1, 2, 0));
DDUMPC(data);
}
and it seems to work fine...
Note: you can use CombineCompare helper:
bool operator < (const rho& a, const rho& b){
return CombineCompare(a.x, b.x)(a.y, b.y)(a.z, b.z) < 0;
}
Mirek
|
|
|
|
Re: SortedIndex and Less [message #40860 is a reply to message #40728] |
Tue, 24 September 2013 17:15 |
keltor
Messages: 73 Registered: February 2012
|
Member |
|
|
Thanks to the example above, I have found why I was getting an error. I pretty much wrote the same code, but I also had the following line in my test:
data.FindAdd(rho(1, 2, 3));
which gives an error. But the compiler points at the definition of data instead of at that line, so I thought that my object had something missing.
However, this opens a new question: how to fix this? It looks as if both FindAdd and Add have the same paradigm, yet I get
c:\upp\uppsrc\core\InVector.hpp(237) : error C2664: 'rho &Upp::Vector<T>::Add(const T &)' : cannot convert parameter 1 from 'int' to 'const
rho &'
with
[
T=rho
]
Reason: cannot convert from 'int' to 'const rho'
Is this a bug, or am I doing something stupid here?
Edit: Incidentally, I am now using build 6254. Not the latest, I know, but not very old either.
[Updated on: Tue, 24 September 2013 17:17] Report message to a moderator
|
|
|
|
Re: SortedIndex and Less [message #40863 is a reply to message #40728] |
Wed, 25 September 2013 08:12 |
keltor
Messages: 73 Registered: February 2012
|
Member |
|
|
OK, sorry. But really all I did was to add that line to the bottom of your code. Here's the full program:
#include <Core/Core.h>
using namespace Upp;
struct rho : Moveable<rho> {
int x,y,z;
rho(int x,int y,int z) : x(x), y(y), z(z) {}
rho() {}
String ToString() const { return AsString(x) + ' ' + AsString(y) + ' ' + AsString(z); }
};
bool operator < (const rho& a, const rho& b){
return a.x == b.x ? (a.y == b.y ? a.z < b.z : a.y < b.y) : a.x < b.x;
}
CONSOLE_APP_MAIN{
StdLogSetup(LOG_FILE);
SortedIndex<rho> data;
data.Add(rho(1, 2, 3));
data.Add(rho(1, 1, 1));
data.Add(rho(1, 2, 0));
data.FindAdd(rho(1, 2, 3));
DDUMPC(data);
}
Thanks for the help Mirek
[Updated on: Wed, 25 September 2013 08:27] Report message to a moderator
|
|
|
|
|
Goto Forum:
Current Time: Wed Dec 04 16:27:30 CET 2024
Total time taken to generate the page: 0.02338 seconds
|