Mercurial > hg > camir-ismir2012
annotate toolboxes/FullBNT-1.0.7/KPMtools/hash_lookup.m @ 0:cc4b1211e677 tip
initial commit to HG from
Changeset:
646 (e263d8a21543) added further path and more save "camirversion.m"
author | Daniel Wolff |
---|---|
date | Fri, 19 Aug 2016 13:07:06 +0200 |
parents | |
children |
rev | line source |
---|---|
Daniel@0 | 1 function [val, found, Nentries] = hash_lookup(key, fname) |
Daniel@0 | 2 % HASH_LOOKUP Lookup a key in a hash table stored in a file using linear search |
Daniel@0 | 3 % function [val, found, Nentries] = hash_lookup(key, filename) |
Daniel@0 | 4 % |
Daniel@0 | 5 % Example: |
Daniel@0 | 6 % If htbl.mat does not exist, |
Daniel@0 | 7 % [val,found,N] = hash_lookup('foo', 'htbl') |
Daniel@0 | 8 % returns found val = [], found = 0, N = 0 |
Daniel@0 | 9 % hash_add('foo', 42, 'htbl') |
Daniel@0 | 10 % hash_add('bar', [1:10], 'htbl') |
Daniel@0 | 11 % [val,found,N] = hash_lookup('foo', 'htbl') |
Daniel@0 | 12 % now returns val = 42, found = 1, N = 2 |
Daniel@0 | 13 % |
Daniel@0 | 14 % Type 'delete htbl' to delete the file/ reset the hashtable |
Daniel@0 | 15 |
Daniel@0 | 16 |
Daniel@0 | 17 val = []; |
Daniel@0 | 18 found = 0; |
Daniel@0 | 19 |
Daniel@0 | 20 if exist(fname, 'file')==0 |
Daniel@0 | 21 % new hashtable |
Daniel@0 | 22 Nentries = 0; |
Daniel@0 | 23 else |
Daniel@0 | 24 %hashtable = importdata(fname); |
Daniel@0 | 25 load(fname); |
Daniel@0 | 26 Nentries = length(hashtable.key); |
Daniel@0 | 27 for i=1:Nentries |
Daniel@0 | 28 if isequal(hashtable.key{i}, key) |
Daniel@0 | 29 val = hashtable.value{i}; |
Daniel@0 | 30 found = 1; |
Daniel@0 | 31 break; |
Daniel@0 | 32 end |
Daniel@0 | 33 end |
Daniel@0 | 34 end |