File Exchange

image thumbnail

Simle Hashtable

version 1.0 (4.23 KB) by

Simple hashtable class.

3.71429
7 Ratings

5 Downloads

Updated

No License

This is a simple hashtable class. Keys must be a string (strcmp is used to located entries), and the data can be any type. The internal structure is two cell lists, one for the keys and one for the data.

Following is the hashtable Contents:

@HASHTABLE

 Files
   clear - Clear hash table
   display - Display a hash table object
   elements - Get all hash table elements
   get - Get data from the hash table
   hashtable - Constructor for HashTable class
   isempty - Check to see if the hash is empty
   iskey - Check to see if the hash is currently using a key
   keys - Get all the keys currently being used in the hash
   put - Put data in the hash table
   remove - Remove element from the hash
   values - Get all data contained in the hash table

As previously stated, this is a very simple structure. No attempt has been made for optimization, and 'put' and 'get' can only handle one entry at a time. The intent is to provide a structure that overcomes MATLAB struct field name limitations.

Comments and Ratings (11)

Erik

Erik (view profile)

I believe containers.Map uses a binary tree structure and therefore is log(n) as opposed to O(1).

Abhinav Gaur

I agree with Shawn

Shawn Hershey

I've been having trouble finding MATLAB built-in support for hash tables and a coworker recently pointed me to this:

http://www.mathworks.com/help/techdoc/matlab_prog/brqqo5e-1.html

which describes MATLAB's containers.Map data structure.

I can't see anything in the documentation that describes the underlying data structures but tests seem to imply O(1) insertion and lookup.

I wanted to mention that here since this page is currently the second hit for the Google search "MATLAB hashtable".

Scott

Scott (view profile)

Its much faster to use the hash table class of java

Tim Johnson

One could also use the Hashtable class of Java, e.g.,

ht = java.util.Hashtable;
ht.put('key1',[1 2 3]);
ht.get('key')
ht.containsKey('key')

Ryan Rifkin

1. This is not actually a hash table at all. It is an associative array (lookup table). A good lookup table in Matlab is a very useful thing to have, but a hashtable implies some effort to provide O(1) access times --- this code simply does a string comparison on all the keys, and is therefore O(n). It's great for small tables, but it doesn't scale too well.

2. The constructor is buggy. As downloaded, on Matlab R14 on linux, I get a complaint because the constructor tries to assign class 'HashTable' rather than 'hashtable'. Additoinally, the one argument constructor makes no sense, referring to a non-existant variable 'keys'. These bugs are both easily fixable.

3. One can improve this package by adding subsasgn and subsrefs methods. After this, we can say foo('bar') = 5 instead of put(foo,'bar',5):

function hash = subsasgn(hash,index,val)

switch index.type
case '()'
  if (length(index.subs) ~= 1)
    error('Only single indexing is supported.');
  end
  hash = put(hash,index.subs{1},val);
case '.'
  hash = put(hash,index.subs,val);
otherwise
  error('Invalid type.')
end

function val = subsref(hash,index)

switch index.type
case '()'
  if (length(index.subs) ~= 1)
    error('Only single indexing is supported.');
  end
  val = get(hash,index.subs{1});
case '.'
  val = get(hash,index.subs);
otherwise
  error('Invalid type.')
end

Mike Fero

Hallelujah!

Matt, thanks for saving me a lot of trouble. It is such pain to not have a simple hash table in MATLAB.

Theo Alexandrov

Thanks!

Fernando Guevara

Pure matlab hashtable implementation, maybe not the fastest, but allows to store any matlab objects (structs etc...), not only arrays like java.util.Hashtable does.

 Saving the hashtable to disk does not crash matlab (java.util.Hashtable does for some reason in Linux).

Vineet Jain

It's a good tool for using hashing matlab. Most of the useful functions in hashing are thier in the code and the code is well designed.
Thanks..

Joe Hicklin

I'd like to know how this compares with using the built-in hashtable: h = java.util.Hashtable
as far as speed and memory are concerned.

MATLAB Release
MATLAB 7.0.1 (R14SP1)
Acknowledgements

Inspired: Simple Hashtable - Repackaged, Use a hash table

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video