about summary refs log tree commit diff
path: root/sourcecodes/bnt-master/KPMtools/hash_lookup.m
blob: 847c6d587a56ee97708e3bd818c90d1d8f8d2310 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
function [val, found, Nentries] = hash_lookup(key, fname)
% HASH_LOOKUP Lookup a key in a hash table stored in a file using linear search
% function [val, found, Nentries] = hash_lookup(key, filename)
%
% Example:
% If htbl.mat does not exist,
%   [val,found,N] = hash_lookup('foo', 'htbl')
% returns found val = [], found = 0, N = 0
%   hash_add('foo', 42, 'htbl')
%   hash_add('bar', [1:10], 'htbl')
%   [val,found,N] = hash_lookup('foo', 'htbl')
% now returns val = 42, found = 1, N = 2
%
% Type 'delete htbl' to delete the file/ reset the hashtable


val = [];
found = 0;

if exist(fname, 'file')==0
  % new hashtable
  Nentries = 0;
else
  %hashtable = importdata(fname);
  load(fname);
  Nentries = length(hashtable.key);
  for i=1:Nentries
    if isequal(hashtable.key{i}, key)
      val = hashtable.value{i};
      found = 1;
      break;
    end
  end
end