annotate _FullBNT/KPMtools/hash_lookup.m @ 9:4ea6619cb3f5 tip

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