Home » U++ Library support » U++ Core » Can we have a ValueMap(Unique) ?
Can we have a ValueMap(Unique) ? [message #42920] |
Mon, 14 April 2014 15:52 |
mingodad
Messages: 53 Registered: February 2008 Location: Spain
|
Member |
|
|
Today I spent a lot of time to figure out why some code was not working and I found that it was because I have added the same key more than once on a ValueMap, I was expecting getting back the last added value but no and till I found this problem a lot of time passed (I learned a bit more about U++ internals) but it was not fun.
Looking a the ValueMap implementation the ideal place to do it would be on the function "Add" but it is not virtual so I was thinking that adding a new type "VALUEMAPUNIQUE_V" and modify the "Add" function to:
void ValueMap::Add(const Value& key, const Value& value) {
Data& d = Clone();
if(data->GetType() == VALUEMAPUNIQUE_V)
{
///check if key already exists and throw an Exception
}
d.key.Add(key);
d.value.Add(value);
}
What U++ users think about have a ValueMap variant like this ?
Cheers !
|
|
|
Re: Can we have a ValueMap(Unique) ? [message #42928 is a reply to message #42920] |
Mon, 14 April 2014 20:29 |
|
mirek
Messages: 13975 Registered: November 2005
|
Ultimate Member |
|
|
mingodad wrote on Mon, 14 April 2014 13:52Today I spent a lot of time to figure out why some code was not working and I found that it was because I have added the same key more than once on a ValueMap, I was expecting getting back the last added value but no and till I found this problem a lot of time passed (I learned a bit more about U++ internals) but it was not fun.
Looking a the ValueMap implementation the ideal place to do it would be on the function "Add" but it is not virtual so I was thinking that adding a new type "VALUEMAPUNIQUE_V" and modify the "Add" function to:
void ValueMap::Add(const Value& key, const Value& value) {
Data& d = Clone();
if(data->GetType() == VALUEMAPUNIQUE_V)
{
///check if key already exists and throw an Exception
}
d.key.Add(key);
d.value.Add(value);
}
What U++ users think about have a ValueMap variant like this ?
Cheers !
Use Set instead of Add...
That said, there is another problem with ValueMap following VectorMap semantics: ordering of elements is meaningful.
Thus, if you have
ValueMap a, b;
a("x", 1)("y", 2);
b("y", 2)("x", 1);
then
a != b;
to solve that:
a.IsSame(b) // unordered compare
that said, it is not completely unlikely that we introduce some new type that has these issues altered..
Mirek
|
|
|
Goto Forum:
Current Time: Fri Mar 29 12:20:17 CET 2024
Total time taken to generate the page: 0.01093 seconds
|