about summary refs log tree commit diff
path: root/sourcecodes/bnt-master/KPMtools/hash_lookup.m
diff options
context:
space:
mode:
Diffstat (limited to 'sourcecodes/bnt-master/KPMtools/hash_lookup.m')
-rw-r--r--sourcecodes/bnt-master/KPMtools/hash_lookup.m34
1 files changed, 34 insertions, 0 deletions
diff --git a/sourcecodes/bnt-master/KPMtools/hash_lookup.m b/sourcecodes/bnt-master/KPMtools/hash_lookup.m
new file mode 100644
index 00000000..847c6d58
--- /dev/null
+++ b/sourcecodes/bnt-master/KPMtools/hash_lookup.m
@@ -0,0 +1,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