[chuck-users] My solution: HyperArray class

David Powers cyborgk at gmail.com
Sat Apr 22 17:05:06 EDT 2006


Hello, my solution to the array problem was to create a new class,
"HyperArray", which has functions for insert, remove, remove duplicates. It
holds an array, currently of size 1024, but the methods make it appear as if
it's an array of variable length.

So, I'm going to have the Cell class inherit from HyperArray, and simply add
methods that are specific to manipulating arrays of notes (or whatever) in
serial fashion.

If anyone wants to test this class out more, I'd appreciate it. There may be
some things wrong, because when I used the get() method internally, in the
shuffle function, it crashed check for reasons I don't really understand.

Here's the code:

//HyperArray

public class HyperArray
{
  //initialize
  1024 => int max;
  0 => int hyperLen;
  float hyperA[max];

  //set
  fun void set(float a[])
  {
    a.cap() => hyperLen;
    for (0 => int i; i<hyperLen; i++)
    {
      a[i] => hyperA[i];
    }
  }

  //get
  fun float[] get()
  {
    float temp[hyperLen];
    for (0 => int i; i<hyperLen; i++)
    {
      hyperA[i] => temp[i] ;
    }
    return temp;
  }

  //print
  fun void print()
  {
    for (0 => int i; i<hyperLen; i++)
    {
      <<< hyperA[i] >>> ;
    }
  }

  //remove
  fun void remove (int index)
  {
    hyperLen--;
    for (0 => int i; i < hyperLen; i++)
    {
      if (i==index || i>index)
      {
        hyperA[i+1] => hyperA[i];
      }
    }
  }

  //insert
  fun void insert (int index, float item)
  {
    hyperLen++;
    get() @=> float temp[];
    for (0 => int i; i < hyperLen; i++)
    {
      if (i==index)
      {
        item => hyperA[i];
      }
      if (i>index)
      {
        temp[i-1] => hyperA[i];
      }
    }
  }

  //sort
  fun float[] sort()
  {
  float temp;
  for (0 => int x ; x < hyperLen-1; x++)
    {
    for (0 => int y; y < hyperLen-x-1; y++)
      {
          if (hyperA[y] > hyperA[y+1])
          {
              hyperA[y] => temp;
              hyperA[y+1] => hyperA[y];
              temp => hyperA[y+1];
          }
      }
    }
  return hyperA;
  }

  //remove duplicates
  fun float[] removeDup()
  {
    int purge[hyperLen];
    0 => int purgeIndex;
    for (0 => int i; i<hyperLen-1; i++)
    {
      for (i+1 => int j; j<hyperLen;j++)
      {
        if (hyperA[i]==hyperA[j])
        {
          j => purge[purgeIndex];
          purgeIndex++;
        }
      }
    }
    for (purgeIndex => int k; k>0; k--)
    {
     remove(purge[k-1]);
    }
    return hyperA;
  }

  //reverse
  fun float[] reverse()
  {
    float newA[hyperLen];
    hyperLen - 1 => int counter;
    for (0 => int i; i<hyperLen; i++)
    {
      hyperA[i] => newA[counter];
      counter--;
    }
    set(newA);
    return(hyperA);
  }

  //shuffle
  fun float[] shuffle(int times)
  {
    float newA[hyperLen];
    for (0 => int i; i<hyperLen; i++)
    {
      hyperA[i] => newA[i] ;
    }
    //--- Shuffle elements by randomly exchanging each with one other.
    for (0 => int i; i<times; i++)
    {
      for (0 => int j; j<hyperLen; j++)
      {
        std.rand2(0,hyperLen-1) => int r;  // generate a random position
        newA[j] => float temp;
        newA[r] => newA[j];
        temp => newA[r];
      }
    }
    set(newA);
    return(hyperA);
  }

}

fun void testShiz()
{
  HyperArray booyah;
  <<<"Set and print array">>>;
  booyah.set([5.0,2.0,3.0,4.0,5.0,1.0,2.0,3.0]);
  booyah.print();
  <<<"Remove item at index 3 and print HyperArray">>>;
  booyah.remove(3);
  booyah.print();
  <<<"insert value 4.0 at index 3 and print HyperArray">>>;
  booyah.insert(3,4.0);
  booyah.print();
  <<<"Remove duplicates and print HyperArray">>>;
  booyah.removeDup();
  booyah.print();
  <<<"Sort and print HyperArray">>>;
  booyah.sort();
  booyah.print();
  <<<"Reverse and print HyperArray">>>;
  booyah.reverse();
  booyah.print();
  <<<"Shuffle and print HyperArray">>>;
  booyah.shuffle(3);
  booyah.print();
  <<<"Create new array using HyperArray get method, print new array">>>;
  booyah.get() @=> float theNewArray[];
  for (0 => int i; i<theNewArray.cap();i++)
  {
    <<<theNewArray[i]>>>;
  }
}

testShiz();
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.cs.princeton.edu/pipermail/chuck-users/attachments/20060422/2f85ab63/attachment.htm 
-------------- next part --------------
//HyperArray

public class HyperArray
{
  //initialize
  1024 => int max;
  0 => int hyperLen;
  float hyperA[max];

  //set
  fun void set(float a[])
  {
    a.cap() => hyperLen;
    for (0 => int i; i<hyperLen; i++)
    {
      a[i] => hyperA[i];
    }
  }
  
  //get
  fun float[] get()
  {
    float temp[hyperLen];
    for (0 => int i; i<hyperLen; i++)
    {
      hyperA[i] => temp[i] ;
    }
    return temp;
  }
  
  //print
  fun void print()
  {
    for (0 => int i; i<hyperLen; i++)
    {
      <<< hyperA[i] >>> ;
    }
  }
  
  //remove
  fun void remove (int index)
  {
    hyperLen--;
    for (0 => int i; i < hyperLen; i++)
    {
      if (i==index || i>index)
      {
        hyperA[i+1] => hyperA[i];
      }
    }
  }

  //insert
  fun void insert (int index, float item)
  {
    hyperLen++;
    get() @=> float temp[];
    for (0 => int i; i < hyperLen; i++)
    {
      if (i==index)
      {
        item => hyperA[i];
      }
      if (i>index)
      {
        temp[i-1] => hyperA[i];
      }
    }
  }

  //sort
  fun float[] sort()
  {
  float temp;
  for (0 => int x ; x < hyperLen-1; x++)
    {
    for (0 => int y; y < hyperLen-x-1; y++)
      {
          if (hyperA[y] > hyperA[y+1])
          {
              hyperA[y] => temp;
              hyperA[y+1] => hyperA[y];
              temp => hyperA[y+1];
          }
      }
    }
  return hyperA;
  }

  //remove duplicates
  fun float[] removeDup()
  {
    int purge[hyperLen];
    0 => int purgeIndex;
    for (0 => int i; i<hyperLen-1; i++)
    {
      for (i+1 => int j; j<hyperLen;j++)
      {
        if (hyperA[i]==hyperA[j])
        {
          j => purge[purgeIndex];
          purgeIndex++;
        }
      }
    }
    for (purgeIndex => int k; k>0; k--)
    {
     remove(purge[k-1]);
    }
    return hyperA;
  }

  //reverse
  fun float[] reverse()
  {
    float newA[hyperLen];
    hyperLen - 1 => int counter;
    for (0 => int i; i<hyperLen; i++)
    {
      hyperA[i] => newA[counter];
      counter--;
    }
    set(newA);
    return(hyperA);
  }

  //shuffle
  fun float[] shuffle(int times)
  {
    float newA[hyperLen];
    for (0 => int i; i<hyperLen; i++)
    {
      hyperA[i] => newA[i] ;
    }
    //--- Shuffle elements by randomly exchanging each with one other.
    for (0 => int i; i<times; i++)
    {
      for (0 => int j; j<hyperLen; j++)
      {
        std.rand2(0,hyperLen-1) => int r;  // generate a random position
        newA[j] => float temp;
        newA[r] => newA[j];
        temp => newA[r];
      }
    }
    set(newA);
    return(hyperA);
  }

}

fun void testShiz()
{
  HyperArray booyah;
  <<<"Set and print array">>>;
  booyah.set([5.0,2.0,3.0,4.0,5.0,1.0,2.0,3.0]);
  booyah.print();
  <<<"Remove item at index 3 and print HyperArray">>>;
  booyah.remove(3);
  booyah.print();
  <<<"insert value 4.0 at index 3 and print HyperArray">>>;
  booyah.insert(3,4.0);
  booyah.print();
  <<<"Remove duplicates and print HyperArray">>>;
  booyah.removeDup();
  booyah.print();
  <<<"Sort and print HyperArray">>>;
  booyah.sort();
  booyah.print();
  <<<"Reverse and print HyperArray">>>;
  booyah.reverse();
  booyah.print();
  <<<"Shuffle and print HyperArray">>>;
  booyah.shuffle(3);
  booyah.print();
  <<<"Create new array using HyperArray get method, print new array">>>;
  booyah.get() @=> float theNewArray[];
  for (0 => int i; i<theNewArray.cap();i++)
  {
    <<<theNewArray[i]>>>;
  }
}

testShiz();



More information about the chuck-users mailing list