annotate toolboxes/FullBNT-1.0.7/KPMtools/hash_lookup.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function [val, found, Nentries] = hash_lookup(key, fname)
wolffd@0 2 % HASH_LOOKUP Lookup a key in a hash table stored in a file using linear search
wolffd@0 3 % function [val, found, Nentries] = hash_lookup(key, filename)
wolffd@0 4 %
wolffd@0 5 % Example:
wolffd@0 6 % If htbl.mat does not exist,
wolffd@0 7 % [val,found,N] = hash_lookup('foo', 'htbl')
wolffd@0 8 % returns found val = [], found = 0, N = 0
wolffd@0 9 % hash_add('foo', 42, 'htbl')
wolffd@0 10 % hash_add('bar', [1:10], 'htbl')
wolffd@0 11 % [val,found,N] = hash_lookup('foo', 'htbl')
wolffd@0 12 % now returns val = 42, found = 1, N = 2
wolffd@0 13 %
wolffd@0 14 % Type 'delete htbl' to delete the file/ reset the hashtable
wolffd@0 15
wolffd@0 16
wolffd@0 17 val = [];
wolffd@0 18 found = 0;
wolffd@0 19
wolffd@0 20 if exist(fname, 'file')==0
wolffd@0 21 % new hashtable
wolffd@0 22 Nentries = 0;
wolffd@0 23 else
wolffd@0 24 %hashtable = importdata(fname);
wolffd@0 25 load(fname);
wolffd@0 26 Nentries = length(hashtable.key);
wolffd@0 27 for i=1:Nentries
wolffd@0 28 if isequal(hashtable.key{i}, key)
wolffd@0 29 val = hashtable.value{i};
wolffd@0 30 found = 1;
wolffd@0 31 break;
wolffd@0 32 end
wolffd@0 33 end
wolffd@0 34 end