www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.announce - AA maker template

Here is a little template I wrote to create an AA given an array of keys and 
an array of values.  It's put in the public domain, so if you have a use for 
it, go ahead.  I'm relatively new to templates, so if I've missed something 
let me know; it seems to check out fine.
// Array maker template.
// Written by Ameer Armaly and placed in to the public domain.

class MakeAAException: Exception
  {
  this(char[] msg)
    {
    super(msg);
    }
  }
template makeAA(Tk, Tv)
  {
  // Tk is the key type, Tv is the value type
  Tv[Tk] makeAA(Tk[] keys, Tv[] values)
    {
    // There have to be the same number of keys as values.
    if(keys.length != values.length)
      throw new MakeAAException("The number of keys and values do not 
match.");
    else
      {
      Tv[Tk] AA;
      foreach(i, v; values)
        AA[keys[i]] = v;
      return AA;
      }
    }
  } 
Jun 05 2006