No BSD License  

Highlights from
Simle Hashtable

3.71429

3.7 | 7 ratings Rate this file 16 Downloads (last 30 days) File Size: 4.23 KB File ID: #6514

Simle Hashtable

by

 

08 Dec 2004 (Updated )

Simple hashtable class.

| Watch this File

File Information
Description

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.

Acknowledgements

This file inspired Simple Hashtable Repackaged and Use A Hash Table.

MATLAB release MATLAB 7.0.1 (R14SP1)
Other requirements Developed and tested on 7.0.1 (R14SP1), OS Windows XP SP2
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (11)
06 Oct 2011 Erik

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

26 Sep 2011 Abhinav Gaur

I agree with Shawn

23 Mar 2011 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".

22 Nov 2010 Scott

Its much faster to use the hash table class of java

16 Sep 2008 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')

24 Apr 2007 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

21 Nov 2006 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.

10 Mar 2006 Theo Alexandrov

Thanks!

28 Sep 2005 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).

28 Mar 2005 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..

14 Dec 2004 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.

Contact us