Daniel@0: function [val, found, Nentries] = hash_lookup(key, fname) Daniel@0: % HASH_LOOKUP Lookup a key in a hash table stored in a file using linear search Daniel@0: % function [val, found, Nentries] = hash_lookup(key, filename) Daniel@0: % Daniel@0: % Example: Daniel@0: % If htbl.mat does not exist, Daniel@0: % [val,found,N] = hash_lookup('foo', 'htbl') Daniel@0: % returns found val = [], found = 0, N = 0 Daniel@0: % hash_add('foo', 42, 'htbl') Daniel@0: % hash_add('bar', [1:10], 'htbl') Daniel@0: % [val,found,N] = hash_lookup('foo', 'htbl') Daniel@0: % now returns val = 42, found = 1, N = 2 Daniel@0: % Daniel@0: % Type 'delete htbl' to delete the file/ reset the hashtable Daniel@0: Daniel@0: Daniel@0: val = []; Daniel@0: found = 0; Daniel@0: Daniel@0: if exist(fname, 'file')==0 Daniel@0: % new hashtable Daniel@0: Nentries = 0; Daniel@0: else Daniel@0: %hashtable = importdata(fname); Daniel@0: load(fname); Daniel@0: Nentries = length(hashtable.key); Daniel@0: for i=1:Nentries Daniel@0: if isequal(hashtable.key{i}, key) Daniel@0: val = hashtable.value{i}; Daniel@0: found = 1; Daniel@0: break; Daniel@0: end Daniel@0: end Daniel@0: end