octave-bug-tracker
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Octave-bug-tracker] [bug #56594] [Patch] containers.Map does not auto-c


From: Markus Ebner
Subject: [Octave-bug-tracker] [bug #56594] [Patch] containers.Map does not auto-convert differing numeric key-types
Date: Mon, 8 Jul 2019 16:40:24 -0400 (EDT)
User-agent: Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0

URL:
  <https://savannah.gnu.org/bugs/?56594>

                 Summary: [Patch] containers.Map does not auto-convert
differing numeric key-types
                 Project: GNU Octave
            Submitted by: seijikun
            Submitted on: Mon 08 Jul 2019 08:40:22 PM UTC
                Category: Octave Function
                Severity: 3 - Normal
                Priority: 5 - Normal
              Item Group: Matlab Compatibility
                  Status: None
             Assigned to: None
         Originator Name: 
        Originator Email: 
             Open/Closed: Open
         Discussion Lock: Any
                 Release: 5.1.0
        Operating System: GNU/Linux

    _______________________________________________________

Details:

There is code in the Octave containers.Map implementation, that compares types
by taking into account, that they are numerics. But the key is then not
converted into the actual numerical type, which is stored within the Map. This
leads to different hashes and the key is therefore not found.

The following code-sample works in Matlab:

key = double([1,2,3]);
val = {"One", "Two", "Three"};
m = containers.Map(key, val);
m(uint32(1))


But fails in Octave with:

error: containers.Map: specified key <1> does not exist
error: called from
    subsref at line 391 column 13


Adding something to a map with a different numerical key-type seems to
reinterpret the type, which leads to funny keys:

key = double([1,2,3]);
val = {"One", "Two", "Three"};
m = containers.Map(key, val);
m(uint32(1)) = "Four";
m.keys
ans =
{
  [1,1] =   2.1220e-314
  [1,2] =  1
  [1,3] =  2
  [1,4] =  3
}




My attempt at fixing this bug:

diff --git a/scripts/+containers/Map.m b/scripts/+containers/Map.m
index a0bcf3b..191ac5f 100644
--- a/scripts/+containers/Map.m
+++ b/scripts/+containers/Map.m
@@ -386,7 +386,7 @@ classdef Map < handle
                                         || ! isscalar (key))))
             error ("containers.Map: specified key type does not match the
type of this container");
           endif
-          enckey = encode_keys (this, key);
+          enckey = encode_keys (this, cast (key, this.KeyType));
           if (! isfield (this.map, enckey))
             error ("containers.Map: specified key <%s> does not exist",
                    strtrim (disp (key)));
@@ -423,7 +423,7 @@ classdef Map < handle
             endif
             val = feval (this.ValueType, val);
           endif
-          key = encode_keys (this, key);
+          key = encode_keys (this, cast (key, this.KeyType));
           if (isfield (this.map, key))
             this.map.(key) = val;
           else
@@ -722,6 +722,22 @@ endclassdef
 %!   assert (m.keys (), {key});
 %! endfor
 
+## Test using mixed numerical keys (subsref)
+%!test
+%! key = double([1,2,3]);
+%! val = {"One", "Two", "Three"};
+%! m = containers.Map(key, val);
+%! assert(m(uint32(1)), 'One');
+
+## Test using mixed numerical keys (subsasgn)
+%!test
+%! key = double([1,2,3]);
+%! val = {"One", "Two", "Three"};
+%! m = containers.Map(key, val);
+%! m(uint32(1)) = 'Four';
+%! assert(m.Count, uint64(3));
+%! assert(keys (m), {1, 2, 3});
+
 ## Test sort order of keys and values
 %!test
 %! key = {"d","a","b"};





    _______________________________________________________

Reply to this item at:

  <https://savannah.gnu.org/bugs/?56594>

_______________________________________________
  Message sent via Savannah
  https://savannah.gnu.org/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]