Search code examples
matlabmergehashtable

Matlab: How to merge two hashtables?


I want to add a method to my hashtable.m that accepts a second hashtable and returns the union of the hashtables. If a keys exists in both hashtables, I want to use the value of the second hashtable.

I tried the following but cannot get the expected output, would really appreciate any help:

   function hash_union = hashtables_union(hash1, hash2)
            hash_union = {};
            hash1 = hashtable();
            hash2 = hashtable();
            keys1 = hash1.keys;
            keys2 = hash2.keys;  
            data1 = hash1.data; 
            data2 = hash2.data;


            for i = 1:length(keys1) 


                if iskey(hash2,hash1.keys{i})
                   remove(hash1,hash1.keys{i})
                else 
                   put(hash2,hash1.keys{i},hash1.data{i})

                end
                hash_union = hash2;

            end 

the hashtables I want to merge:

hash1 = ('1', 'Omri') , ('2', 'Noa') , ('4', 'Yossi') hash2 = ('2', 'Alon') , ('3', 'Tamar') , ('5', 'Dafna')

the entire class is :

classdef hashtable

    properties
        keys;
        data;
    end

    methods

        % Constructor
        function hash = hashtable(varargin)
            if nargin == 1 && isa(varargin{1},'hashtable')
                hash = varargin{1};
            elseif nargin == 0
                hash.keys = {};
                hash.data = {};
            elseif nargin == 2
                hash.keys = varargin{1};
                hash.data = varargin{2};
            else
                error('hashtable: Invalid input');
            end
        end % constructor

        % Clear hash table
        function hash = clear(hash)
            hash.keys = {};
            hash.data = {};
        end;


        function data = elements(hash)
            data(:,1) = hash.keys;
            data(:,2) = hash.data;
        end;


        function data = get(hash,key)
            index = find(strcmp(hash.keys,key));
            if isempty(index)
                data = {};
            else
                data = hash.data{index};
            end
        end

        % Check to see if the hash is empty
        function bool = isempty(hash)
            bool = isempty(hash.keys);
        end

        %ISKEY Check to see if the hash is currently using a key
        function bool = iskey(hash,key)
            index = find(strcmp(hash.keys,key));
            bool = ~isempty(index);
        end

        % KEYS Get all the keys currently being used in the hash
        function k = getkeys(hash)
            k = hash.keys;
        end;

        % Put data in the hash table
        function hash = put(hash,key,data)
            index = find(strcmp(hash.keys,key));
            if isempty(index)
                if isempty(hash.keys)
                    hash.keys{1} = key;
                    hash.data{1} = data;
                else
                    hash.keys{end+1} = key;
                    hash.data{end+1} = data;
                end
            else
                hash.data{index} = data;
            end
        end

        % Remove element from the hash
        function hash = remove(hash,key)
            index = find(strcmp(hash.keys,key));
            if ~isempty(index)
                hash.keys = {hash.keys{1:index-1} hash.keys{index+1:end}};
                hash.data = {hash.data{1:index-1} hash.data{index+1:end}};
            end
        end

        % VALUES Get all data contained in the hash table
        function data = values(hash)
            data = hash.data;
        end

        % Display a hash table object
        function disp(hash)
            if(length(inputname(1)) ~= 0)
                    disp(' ');
                fprintf('%s =\n', inputname(1));
            end         
            fprintf('HashTable\n');
            if isempty(hash)
                fprintf('\tEmpty\n\n' );
            else
                fprintf('Elements:\n');
                disp(elements(hash));
            end

        end

        function hash_union = hashtables_union(hash1, hash2)
            hash_union = {};
            hash1 = hashtable();
            hash2 = hashtable();
            keys1 = hash1.keys;
            keys2 = hash2.keys;  
            data1 = hash1.data; 
            data2 = hash2.data;


            for i = 1:length(keys1) 


                if iskey(hash2,hash1.keys{i})
                   remove(hash1,hash1.keys{i})
                else 
                   put(hash2,hash1.keys{i},hash1.data{i})

                end
                hash_union = hash2;

            end
        end

Solution

  • Your hashtable class is not inheriting from handle. That means it is a pass-by-value/Copy-On-Write object, not a pass-by-reference object. And you need to capture the updated values returned by its methods to see the updates they do.

    Something like this:

    if iskey(hash2,hash1.keys{i})
       hash1 = remove(hash1,hash1.keys{i});
    else 
       hash2 = put(hash2,hash1.keys{i},hash1.data{i});
    end