Open GoogleCodeExporter opened 9 years ago
Err, my apologies, this isn't a defect but enchancement request.
Original comment by iridian....@googlemail.com
on 3 Feb 2015 at 7:31
See Issue 60. Equality depends on schema, which is not one of yaml-cpp's
better-defined concepts.
Original comment by jbe...@gmail.com
on 3 Feb 2015 at 2:08
Hi I've been using this function (Link) for a while to check equality of 2 Nodes. I haven't extensively tested it, but it works fine on simple YAML files.
Is this a step in the right direction? If yes, can you point me to some YAML files for extensive testing?
@kunaltyagi there are a few issues with your link.
1) It only checks maps with string keys (maps are allowed to have arbitrary keys) 2) It doesn't compare tags 3) See #60 for discussion of equality in YAML. You're testing exact comparisons, but YAML allows an application to consider two nodes equal based on application-specific tags, with some being automatically applied for all YAML files. For example
are considered equal.
@jbeder [Edit] Updated the gist to compare values first typecasted as bool, then int then double and finally std::string.
As far as I could understand the code, all scalar values are stored in std::string m_scalar (node_data.h:108). How do I get the type of data (bool, int, double, float, string, etc.) from it? I don't think that is possible.
So, I went ahead and added a check for equality of tags also. However, as i went through the specification, I realized that yaml-cpp has the Map Keys also as a node. So, should even the keys be checked for equality? Currently, I'm converting the key to std::string and checking if such key exists in the second node. I also added a check for identity (since identity implies equality)
Yes, keys should be checked for equality.
This is difficult for 2 reasons:
std::string
is 'wrong' as per YAML 1.2, but due to above 2 reasons the only method to compare 2 maps with n
keys would require O(n^2)
time, not linear. I will post the O(n^2) version also, but it is bad, just saying.
[Edit]: Updated Gist[1]: I created 2 nodes from a file, added different values in a new map, and got the first and seconds iterators of both maps different despite the keys being the same since the nodes are organised by std::lesser<node*>
and node std::lesser<node>
. Will a function like bool compare(Node, Node)
but substituting <
for ==
be enough in the map declaration as the comparator?
After looking at node_data::get
and node::equals
functions, I think that the compare function can be made linear in time IFF the ==
operator is changed from identity to equality.
Also I'm facing a rather curious problem. The following snippet outputs nothing. Does this stem from the fact that memories are merged in operator[]
or that ==
is identity and returned false in the get function?
YAML::Node node1 = YAML::LoadFile('test.yaml');
YAML::Node node2 = YAML::LoadFile('test.yaml');
std::cout << node2[node1.begin()->first];
@kunaltyagi I'm not sure what you mean by changing operator ==
from identity to equality (since that seems what you're trying to compute).
Also: nodes from different calls to Load
or LoadFile
are unrelated.
@kunaltyagi Even if maps aren't ordered by keys, I see a simple O(N) implementation:
@jleben I have a personal issue against allocating memory (especially since hashmaps require particularly large pool of memory) for operator ==
, but for the sake of argument, let's try.
O(N)
in best or in worst case O(N*N + N))
to insert N
items in a set (or to check and insert). Assume LHS keys are now inserted.O(N)
to O(N*N)
to check and remove.size
is non-zero, LHS != RHS, else LHS == RHSConcerns:
std::unordered_map
itself for storing keys? If we do that, then two lookups, one from LHS to RHS and another from RHS to LHS are sufficient (This is valid even if we don't use hash maps, just that the cost would be O(N*log(N))
std::unordered_map, std::map, google::sparse_hash_map, google::dense_hash_map, std::lower_bound
might be a decent strategy but might be too much work for a small functionality that hasn't seen a lot of interest (from maintainer perspective)
Original issue reported on code.google.com by
iridian....@googlemail.com
on 3 Feb 2015 at 7:28