Direct comparing of two QVariant variables
-
[quote author="deisik" date="1326662003"][quote author="Volker" date="1326661563"]And what do you want to do with the void pointer? That is as opaque as the QVariant itself...[/quote]
I can ask you another question, how QVariant itself stores its value data type?
[/quote]
http://qt.gitorious.org/qt/qt/blobs/4.8/src/corelib/kernel/qvariant.h#line358
-
[quote author="Andre" date="1326662334"]A void* caries less information than QVariant. I don't see how that would bring you any closer to a solution. [/quote]
This information about data type is known beside QVariant (read saved somewhere else) - this is the whole point of my question. How to use it for comparison and whether it is possible in c++ at all is another question
-
[quote author="Volker" date="1326662484"]http://qt.gitorious.org/qt/qt/blobs/4.8/src/corelib/kernel/qvariant.h#line358[/quote]
I expected to see something like that
-
[quote author="deisik" date="1326662787"][quote author="Andre" date="1326662334"]A void* caries less information than QVariant. I don't see how that would bring you any closer to a solution. [/quote]
This information about data type is known beside QVariant (read saved somewhere else) - this is the whole point of my question. How to use it for comparison and whether it is possible in c++ at all is another question[/quote]
You put void pointers into the game, so it's your turn to show how it eases solving the problem, not ours. Our opinion is known (void pointers make matters worse). The other constraints, set by C++, have already been pointed out.
[quote author="deisik" date="1326662903"][quote author="Volker" date="1326662484"]http://qt.gitorious.org/qt/qt/blobs/4.8/src/corelib/kernel/qvariant.h#line358[/quote]
I expected to see something like that
[/quote]
Reading source code is the best way of understanding things. What else would you have hoped to see instead of your expectations?
-
[quote author="Volker" date="1326663446"]
You put void pointers into the game, so it's your turn to show how it eases solving the problem, not ours. Our opinion is known (void pointers make matters worse). The other constraints, set by C++, have already been pointed out.[/quote]Ok, I will make a try
[quote author="Volker" date="1326663446"]Reading source code is the best way of understanding things. What else would you have hoped to see instead of your expectations?[/quote]
To hope for something out of nothing is the most popular form of hope. Black magic indeed
-
[quote author="Volker" date="1326663446"]
You put void pointers into the game, so it's your turn to show how it eases solving the problem, not ours[/quote]My try as promised. I am interested in comparing ints, floats and strings and as I said earlier data type for each value is known. This means that we know beforehand the length the byte array representing this or that value takes in memory. So the algorithm for comparing two values is to regard them as, say, 64 bits integers, filling the higher missing bits of the shorter values (copy of) with zeroes (to clean up garbage)
This technique should work even for real numbers. In fact, interpreting the bit-pattern of a floating-point number as integer is one of the methods for comparing floats and doubles (as a side note, you cannot correctly compare two floats by comparing them "out of the box" as you suggested in the code snippet, but I think you know this yourself)
There still remains a minor problem with negative numbers but as it happens I am not much interested in them (in my case there are none), furthermore I guess this can be fixed somehow
Now you give me a void pointer to a QVariant value and I show you the code
-
[quote]I am interested in comparing ints, floats and strings and as I said earlier data type for each value is known.[/quote]
Do you mean that the variants you want to compare are only of these three types? An arbitrary string or a string representing a number as well? (In the former case, how do you compare it f.i. against a number?)
-
[quote author="peppe" date="1326672292"]Do you mean that the variants you want to compare are only of these three types? An arbitrary string or a string representing a number as well? (In the former case, how do you compare it f.i. against a number?)[/quote]
There can be no comparison between defferent types (imagine a table where you compare only between values of the same column). And yes, columns of this imaginary table are almost all kinds of unsigned base types but they are all interpreted as quint64 integer for saving in a binary file (well, this is a waste of space but it saves time when retrieving them and when we come to file sizes over a few GBs it makes no difference - you just save the value in place of otherwise a 64 bit pointer)
[quote author="peppe" date="1326672292"]An arbitrary string or a string representing a number as well? (In the former case, how do you compare it f.i. against a number?)[/quote]
Yes, arbitrary text strings. In case there is a "number" inside it is valid to consider it as text. So "75" would actually be 55 53 as a byte array
There are also QDateTimes but they are converted to uints with QDateTime::toTime_t(). In fact, all these types pass in QVariant as raw bytes (by QByteArray) and converted to real int, floats, strings and dates only for representation through QAbstractItemModel and building indexes (where comparison comes into play). It is a matter of interpretation
-
I am still wondering how this byte comparison is going to help you achieve the goal you stated earlier:
[quote author="deisik" date="1326624029"]How can this help me find out which value is greater and which is less?[/quote]
How, you think, is a byte-level comparision going to help you compare two QStrings stored in that QVariant union? I mean: you do realize that the actual character array is not stored within the QVariant, right?
-
[quote author="Andre" date="1326703007"]How, you think, is a byte-level comparision going to help you compare two QStrings stored in that QVariant union? I mean: you do realize that the actual character array is not stored within the QVariant, right?
[/quote]Right, it is stored somewhere in memory, so rises the question of a void* pointer to this character array. You seem to be missing the whole point of what I say
-
Then, I guess we just don't understand each other. I wish you all the luck with your attempts to efficiently compare QVariants, but I don't think I can contribute to this discussion anymore. Please let us know if you find an efficient solution, I would be interested to see it.
-
[quote author="Andre" date="1326707128"]Then, I guess we just don't understand each other. I wish you all the luck with your attempts to efficiently compare QVariants, but I don't think I can contribute to this discussion anymore. Please let us know if you find an efficient solution, I would be interested to see it. [/quote]
I have already shown a clean solution which I am going to pick up myself. You probably confuse a pointer to a QVariant object as such with a pointer to data which this object wraps around and to which it should provide this pointer (as a void*) - I talk about the latter
-
[quote author="deisik" date="1326707907"]I have already shown a clean solution which I am going to pick up myself. You probably confuse a pointer to a QVariant object as such with a pointer to data which this object wraps around and should provide as a void* - I talk about the latter[/quote]
If I understood correctly, you want to use the pointer to the memory to do the comparison, right?
How do you know, how to interpret the raw memory segment? The memory representation of a double is different from an int is different from .... So you again need the information to do correct comparison. Where is the benefit? -
[quote author="Gerolf" date="1326708249"]If I understood correctly, you want to use the pointer to the memory to do the comparison, right?[/quote]
Yes, you got it right
[quote author="Gerolf" date="1326708249"]How do you know, how to interpret the raw memory segment?[/quote]
There is no need to interpret this segment in the sense you mean by "interpreting". You just need to know its length, that's all
[quote author="Gerolf" date="1326708249"]The memory representation of a double is different from an int is different from .... So you again need the information to do correct comparison. Where is the benefit?[/quote]
I have already answered this question above - just read about how doubles and floats are accurately compared to each other
-
[quote author="Gerolf" date="1326709312"]a double is represented by 8 bytes, mantisse and exponent[/quote]
Yes, 64 bits
[quote author="Gerolf" date="1326709312"]They have influence on how to read each other. If you do a byte compare, that does not work.[/quote]
It will. Just use 64 bits integers. Check yourself
-
If you do things like this, please leave a note for the next developer to start debugging right here if the application starts to crash somewhen.
@
void lessThan(void* left, void* right)
{
return (quint64)left < (quint64)right;
}
@
Another rule one might stick to: even if it is possible, it doesn't mean that it should be done that way. -
[quote author="Lukas Geyer" date="1326717017"]If you do things like this, please leave a note for the next developer to start debugging right here if the application starts to crash somewhen.
@
void lessThan(void* left, void* right)
{
return (quint64)left < (quint64)right;
}
@
Another rule one might stick to: even if it is possible, it doesn't mean that it should be done that way.[/quote]If the code is like this, it might crash if the value is not 64 bit long and not enough memory to read is there. Additionally, it might bring problems with little / big endianess, right?