Hashtable

I know there will be a hashtable object included in NETMF 4.1, but does anyone know if there is an implementation anywhere for 4.0?

I see none possible with out going to 4.1. I could guess that you could code your own in place of upgrading to the beta.

I already have the beta SDK installed, but it is only useful enough to allow me to use Visual Studio 2010 to develop for the FEZ. As far as I know, the OEM (RE: GHI) has to actually upgrade the runtime on the unit in the form of a firmware update.

What I am trying to do is to just find the best way to pass an “array” of parameters. I originally coded a weather API in PHP, in which I used an associative array to carry the weather information. I am trying to port that over to NETMF, but I’m not sure what the best way to pass the weather params around is. Maybe a struct?

The original API is here… The code is fully commented and pretty easy to understand.
http://www.chrisseto.com/wordpress/?p=17

Yes we have to support 4.1, which we will do once it is out.

Looking through the NETMF ref more, do you think a Dictionary might be an appropriate way to do this?

A dictionary is a fancy name for a hashtable. :wink:

So, as it turns out, dictionaries are not sup[ported in NETMF, either.

So, what are my other options, other than making a struct and manually putting a bunch of string properties in?

All I need to be able to do is get values their respective string keys. Any ideas?

How many entries in the table?

Are there any deletes from the table?

Is the table built at initialization time or dynamically as the program runs?

Anywhere from 10-15 entries.
No deletes
Dynamically built from an XML feed. The program will have no knowledge of what will go in the “array” at initialize time

Also, every key and value will be a string, if that helps.

Thanks Mike! :smiley:

With 10-15 entries do you really need a hashtable? A linked list and a sequential search might be work for you.

A hashtable is usually used with many more entries. There is an overhead in building and looking up entries in a hashtable. At a small number of entries it might take longer with a hashtable.

I suggest you build a class which encapsulates the functionality of a hashtable, but uses a linked list internally. If it turns out that you need something that is faster, you just have to change the class internals.

I like to get a program working in the simplest manner, and then later go back and tune program for performance. I have found that usually the performance bottlenecks are not where I thought they were initially.

The only thing I need is to be able to get values by a string key, IE, NOT an int key (array). I also want it to programmically build the list. What do you think of this, from Stackoverflow?

EDIT: I just realized, this uses LINQ, which I assume is not available on NETMF… Hmm…

class HashTable
{
    private List<KeyValuePair<int, List>> _table = 
        new List<KeyValuePair<int, List>>();

    private void Set(string key, T value)
    {
        var hashcode = key.GetHashCode();
        List l;
        if(!_table.TryGetValue(hashcode, out l))
        {
            l = new List();
            _table.Add(hashcode, l);
        }

        T o;
        if(l.TryGetValue(key, out o))
        {
            if (o != value)
                l.Single(x => x.Key == key).Value = o;
        }
        else
            l.Add(new KeyValuePair(key, value));
    }

    private T Get(string key)
    {
        List l;
        object o;
        if(!(_table.TryGetValue(hashcode, out l) && 
            !l.TryGetValue(key, out o)))
        {
            throw new ArgumentOutOfRangeException("key");
        }

        return o;
    }

public bool TryGetValue(this List list, 
    TKey key, out TValue value)
{
    var query = list.Where(x => x.Key == key);        
    value = query.SingleOrDefault().Value;
    return query.Any();
}

}

Chris:

take a look at this code. See if works for you.

If you have any problem following the code I can add comments.

using System;
using Microsoft.SPOT;
using System.Collections;

namespace HashTable
{
    public class Program
    {
        public class LookupTable
        {
            private class Entry
            {
                public String Key { get; set; }
                public String Value { get; set; }

                public Entry(String key, String value)
                {
                    Key = key;
                    Value = value;
                }

                public override bool Equals(object obj)
                {
                    return ((String)obj).Equals(Key);

                }

                public override int GetHashCode()
                {
                    return base.GetHashCode();
                }
                
            }

            private ArrayList table = new ArrayList();

            public LookupTable()
            {
            }

            public String this[String key]
            {
                get
                {
                    int index = table.IndexOf(key);
                    if (index == -1)
                        return null;
                    else
                        return ((Entry)table[index]).Value;
                }

                set
                {
                    if (table.Count == 0)
                    {
                        table.Add(new Entry(key, value));
                    }
                    else
                    {
                        int index = table.IndexOf(key);
                        if (index == -1)
                        {
                            table.Add(new Entry(key, value));
                        }
                        else
                        {
                            ((Entry)table[index]).Value = value;
                        }
                    }
                }
            }

            public bool ContainsKey(String key)
            {
                return table.IndexOf(key) != -1;
            }
        }

        public static void Main()
        {
            LookupTable myTable = new LookupTable();

            myTable["key1"] = "Value1";
            myTable["key2"] = "Value2";     

            String rtn = myTable["key1"];
            Debug.Print("key1 returned " + ((rtn == null)?"null":rtn));

            rtn = myTable["key2"];
            Debug.Print("key2 returned " + ((rtn == null) ? "null" : rtn));

            rtn = myTable["missing"];
            Debug.Print("missing returned " + ((rtn == null) ? "null" : rtn));

            // replacing prior value
            myTable["key2"] = "New Value2";
            rtn = myTable["key2"];
            Debug.Print("key2 returned " + ((rtn == null) ? "null" : rtn));

            Debug.Print("ContainsKey[\"key1\"] returns " + myTable.ContainsKey("key1").ToString());
            Debug.Print("ContainsKey[\"missing\"] returns " + myTable.ContainsKey("missing").ToString());
        }

    }
}

Hi Mike,

I can follow that just fine, thanks a whole lot, I really appreciate it! :smiley: