image thumbnail
from Order book simulation by Dimitri Shvorob
(a naive artificial stock market)

CharKeyHashtable
classdef CharKeyHashtable < handle
   
    properties (SetAccess = private)
       Keys
       Vals
    end    
    
    methods 
        
       function put(obj,key,val)
           if isempty(obj)
              obj.Keys = {key};
              obj.Vals = {val};
           else
              obj.Keys{end+1} = key;
              obj.Vals{end+1} = val;
           end
       end
       
       function[val] = get(obj,key)
           i = obj.index(key);
           if isempty(i)
              val = [];
           else
              val = obj.Vals{i};
           end
       end
       
       function[out] = iskey(obj,key)
           out = ~isempty(obj.index(key));
       end
       
       function[out] = isempty(obj)
           out = isempty(obj.Keys);
       end
       
    end
    
    methods (Access = private)
        
        function[out]= index(obj,key)
            out = [];
            for i = 1:length(obj.Keys)
                if strcmp(key,obj.Keys{i})
                   out = i;
                   break
                end
            end
        end        
        
    end
    
end

Contact us at files@mathworks.com