[comp.lang.c++] PD classes for matrix/vector ...

mls@cs.purdue.EDU (Michael Schneider) (08/05/89)

Does anyone know of any good matrix/vector classes in the public domain

Mike Schneider

ttwang@polyslo.CalPoly.EDU (Thomas Wang) (08/05/89)

mls@cs.purdue.EDU (Michael Schneider) writes:

>Does anyone know of any good matrix/vector classes in the public domain

>Mike Schneider
/*@start*/
dummy
/*@regen #ifndef FLEX@@1 */
dummy
/*@regen #define FLEX@@1 */

/* Author:  Thomas Wang
   Content: generic flexible vector,
   Date:    Jun 10, 1989
   Usage:

 t_flex_char anarray(37); // array expansion factor is 37

 anarray.setsize(26); // set array size at 26
 anarray[25] = 'a';

 t_flex_char second; // default array expansion factor is 16

 second.setsize(10); // set array size at 10
 second[9] = 'b';

*/

@#include "@@2"

class t_flex_@@1 {
  int max;                  // total size allocated including unused space
  int sz;                   // current size of the array
  int ntable;               // number of tables used
  int tabsize[30];          // size of each successive table
  @@1* table[30];           // 30 tables, max size 16 ^ 30
  void err(int);            // error reporting subroutine
public:
  t_flex_@@1();             // default 0 sized array, expansion size = 16
  t_flex_@@1(int);          // constructor with expansion specified
  ~t_flex_@@1();            // destructor
  @@1& operator[](int);     // array access operator
  @@1* getptr(int);         // array pointer access
  int setsize(int);         // set the size of an array, return new size
  int inc_size(int);        // increase array size by amount specified
  int dec_size(int);        // decrease array size by amount specified
  int size() { return sz; } // return the size of the array
  int setexp(int);          // if no storage is allocated, set expansion size
  int exp()                 // get the expansion factor
  { return tabsize[0]; }
  void operator = (t_flex_@@1&); // copy the whole array
};

/*@regen #endif */
dummy
/*@end*/


/*@start*/
/* Author:  Thomas Wang
   Content: implementation of flexible vector,
   Date:    jun 10, 1989
*/
@#include <stream.h>
@#include "@@2"

// report error, then go into endless loop
void t_flex_@@1::err(int code)
{
  cerr << "t_flex_@@1 error " << code << "!!!\n";
  while (1) { }
}

// increase array size by amount specified
int t_flex_@@1::inc_size(int want)
{
  int temp;
  temp = tabsize[ntable];
  sz += want;
  while (max < want)
  {
    table[ntable] = new @@1 [temp]; // allocate table
    max += temp; // increase total storage count
    temp += temp; // array chunks grows by factor of 2
    tabsize[++ntable] = temp; // store size of this table
  }
  return sz;
}

// decrease array size by size specified
int t_flex_@@1::dec_size(int want)
{
  sz -= want;
  while ((ntable > 0) && (max - tabsize[ntable - 1] >= want))
  {
    --ntable;               // decrease number of tables
    delete[tabsize[ntable]] table[ntable];   // delete a table
    max -= tabsize[ntable]; // decrease total storage count
  }
  return sz;
}

// set array size
int t_flex_@@1::setsize(int want)
{
  if (want < 0) return sz;
  if (want > sz) // increase size
    return inc_size(want - sz);
  else // decrease size
    return dec_size(sz - want);
}

// array access operator
@@1& t_flex_@@1::operator [] (int ind)
{
  int tabptr = 0;
  if ((ind < 0) || (ind >= sz))
    err(ind);
  while (1)
  {
    if (ind < tabsize[tabptr])
      return table[tabptr][ind];
    ind -= tabsize[tabptr++];
  }
}

// array pointer access
@@1* t_flex_@@1::getptr(int ind)
{
  int tabptr = 0;
  if ((ind < 0) || (ind >= sz))
    err(ind);
  while (1)
  {
    if (ind < tabsize[tabptr])
      return & table[tabptr][ind];
    ind -= tabsize[tabptr++];
  }
}

t_flex_@@1::t_flex_@@1()
{
  ntable = sz = max = 0;
  tabsize[0] = 16;
}

t_flex_@@1::t_flex_@@1(int s)
{
  ntable = sz = max = 0;
  tabsize[0] = (s > 0) : s ? 1;
}

// destructor for flexible array
t_flex_@@1::~t_flex_@@1()
{
  for (int n = 0; n < ntable; ++n)
    delete[tabsize[n]] table[n];
}

void t_flex_@@1::operator = (t_flex_@@1& second)
{
  int n;
  for (n=0; n< ntable; ++n)
    delete[tabsize[n]] table[n];
  tabsize[0] = second.tabsize[0];
  sz = max = ntable = 0;
  setsize(second.size());
  for (n=0; n < second.size(); ++n) // copy
    (*this)[n] = second[n];
}

int t_flex_@@1::setexp(int num)
{
  if (ntable == 0)
    return tabsize[0] = (num > 0) : num ? 1;
  return tabsize[0];
}
/*@end*/