Category : C++ Source Code
Archive   : BLFMATH.ZIP
Filename : TSTVEC.CPP

 
Output of file : TSTVEC.CPP contained in archive : BLFMATH.ZIP
///////////////////////////////////////////////////////////////
// tstvec.cpp: Vector test program using integer vector math.
// Copyright(c) 1993 Azarona Software. All rights reserved.
///////////////////////////////////////////////////////////////
#include
#include "vector.h"

//
// Some sample vector math routines
//

template
void operator+=(Vector &v, const TYPE &x)
// Adds x to every element of v.
// ASSUMES TYPE has '+=' defined.
{
VecPtr elemptr(v.PtrToAll());
for (unsigned i = 0; i *elemptr += x;
elemptr++;
}
}

template
void Add(Vector &r, const Vector &a, const Vector &b)
// Adds vectors a and b together, storing result in r.
// The number of elements added is determined by the
// shortest length of all three vectors.
// ASSUMES that TYPE has '+' defined.
{
unsigned rlen = r.Length();
unsigned slen = a.Length();
if (rlen > slen) rlen = slen;
slen = b.Length();
if (rlen > slen) rlen = slen;
VecPtr rptr(r.PtrToAll());
VecPtr aptr(a.PtrToAll());
VecPtr bptr(b.PtrToAll());
for (unsigned i = 0; i *rptr = *aptr + *bptr;
rptr++;
aptr++;
bptr++;
}
}

template
Vector operator+(const Vector &a, const Vector &b)
// Adds vectors a and b together, returning the result (which
// has its data allocated on the heap.) The number of elements
// created is determined by the shortest length a and b.
{
unsigned rlen = a.Length();
unsigned slen = b.Length();
if (rlen > slen) rlen = slen;
Vector result(rlen);
Add(result, a, b);
return result;
}


template
void PrtVec(const Vector &v)
// Print out the elements of a vector
{
VecPtr cursor(v.PtrToAll());
unsigned i, len = v.Length();
for (i = 0; i cout << *cursor << ' ';
cursor++;
}
cout << '\n';
}

// BC++ needs the next statements, which generate functions
// for doing operations on vectors

void Add(Vector &r, const Vector &a, const Vector &b);
void operator+=(Vector &v, const int &x);
void PrtVec(const Vector &v);

// You need one of these statements for each type of vector:

INITNULLVEC(int);

// A low-level C array we'll copy

const int arr_size = 7;
int arr[arr_size] = {0, 1, 2, 3, 4, 5, 6};

void test()
{
cout << "Creating a vector\n";

Vector myvec(arr_size, arr);
cout << "myvec: "; PrtVec(myvec);

cout << "Creating a shared slice of every other element\n";

Vector everyother(myvec, SHARED, 4, 2);
cout << "everyother: "; PrtVec(everyother);

cout << "Creating a copied slice of every other element\n";

Vector cpslice(myvec, COPIED, 4, 2);
cout << "cpslice: "; PrtVec(cpslice);

cout << "Adding 10 to every other element\n";

everyother += 10;
cout << "myvec: "; PrtVec(myvec);
cout << "everyother: "; PrtVec(everyother);
cout << "cpslice: "; PrtVec(cpslice);

cout << "Adding all odd elements to even elements\n";

Vector odd(myvec, SHARED, 3, 2, 1);
Vector even(myvec, SHARED, 4, 2, 0);

cout << "Result: "; PrtVec(odd + even);
cout << "myvec: "; PrtVec(myvec);

cout << "Testing consistency checks for stride, length, and offset\n";

Vector tstslice(everyother, SHARED, 4, 2, 1);
cout << "everyother: "; PrtVec(everyother);
cout << "tstslice: "; PrtVec(tstslice);
}

main()
{
test();
return 0;
}



  3 Responses to “Category : C++ Source Code
Archive   : BLFMATH.ZIP
Filename : TSTVEC.CPP

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/