3

In Octave, containers.Map objects are handle objects that can be passed around by reference but always refer to the same object [1]. However, the only test for "equality" between containers.Map objects is isequal, which only tests if the objects' contents are equal. I need a test for equality that tells me if 2 variables refer to the same containers.Map object. The following code demonstrates the problem:

user@computer:~$ octave -q octave:1> original_map = containers.Map({'name'}, {'Alice'}); octave:2> copy_map = original_map; octave:3> new_map = containers.Map({'name'}, {'Alice'}); octave:4> isequal(original_map, copy_map) ans = 1 octave:5> isequal(original_map, new_map) ans = 1 octave:6> original_map('name') = 'Bob'; octave:7> isequal(original_map, copy_map) ans = 1 octave:8> isequal(original_map, new_map) ans = 0 octave:9> eq(new_map, original_map) error: eq method not defined for containers.Map class 

I am looking for a function/operator - call it eqtest - such that eqtest(original_map, copy_map) returns true and eqtest(original_map, new_map) returns false regardless of whether original_map and new_map have the same contents.

The best I have come up with on my own is:

function equality = eqtest(map1, map2) map1('testKey') = 'Alpha'; map2('testKey') = 'Beta'; equality = isequal(map1, map2); remove(map1, 'testKey'); remove(map2, 'testKey'); end 

This is horribly fragile and breaks if "UniformValues" is true and/or the map1/map2 values are not both char.

[1] - This is equivalent behavior to Matlab. However, Matlab allows the use of eq (or ==) to test if 2 containers.Map objects are the same object. Search Google on "octave test containers.map same object" and Google AI will give you abjectly wrong information on the topic.

1
  • Could you just add an id key to the map when you create it which you specify as a unique identifier for that specific map? Then you can just compare the IDs Commented Oct 27 at 14:16

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.