Category : C++ Source Code
Archive   : BLFMATH.ZIP
Filename : VECTOR.H

 
Output of file : VECTOR.H contained in archive : BLFMATH.ZIP
/////////////////////////////////////////////////////////////
// vector.h: Fixed-length shared vector template.
// Copyright(c) 1993 Azarona Software. All rights reserved.
/////////////////////////////////////////////////////////////
#ifndef H_VECTOR
#define H_VECTOR
#include "range.h"
#include "vecptr.h"

#define INLINE

enum SliceType { SHARED, COPIED };

// Vector representation class. Private to the vector class.

template class Vector; // Forward template declaration

template
class VecRep {
// WARNING: VecRep objects are meant to be allocated only
// dynamically. The only exception is null_rep, which is
// a special null version of a VecRep object. Only one
// of these should be created.
#ifdef NO_BORLAND_BUG
private:
friend class Vector;
#else
public:
#endif
unsigned alloclen;
unsigned refcnt;
TYPE data[1];
VecRep(unsigned d);
void *operator new(size_t n, unsigned d);
void operator delete(void *p);
public: // So we can set up null_rep
static VecRep null_rep;
VecRep();
~VecRep();
};

// You need to call this for each type of vector, to
// set up the special null VecRep object. Do it only
// once though.

#define INITNULLVEC(TYPE) VecRep VecRep::null_rep;


// The vector class

template class Matrix; // Forward template declaration

template
class Vector {
protected:
friend class Matrix;
VecRep *rep; // Pointer to shared vector data
TYPE *start; // Pointer to logical start of data
unsigned len; // Number of logical elements
unsigned stride; // Stride (offset to next logical element)
int Alloc(unsigned n);
void Bind(const Vector &v);
void Unbind();
void NewBinding(const Vector &v);
public:
Vector(unsigned n = 0, const TYPE *s = 0);
Vector(const Vector &v);
Vector(const Vector &v, SliceType styp,
unsigned n=0, unsigned str=1, unsigned ofs=0);
~Vector();
#ifndef NO_RANGE_CHECK
unsigned CheckIndx(unsigned i) const;
#endif
// We'll provide both forms of subscripting
TYPE &operator[](unsigned i);
const TYPE &operator[](unsigned i) const;
void CopyN(const TYPE *src, unsigned n);
void Copy(const Vector &v);
void Share(const Vector &v);
void SetElements(const TYPE &x);
Vector &operator=(const Vector &v);
Vector &operator=(const TYPE &x);
int IsNull() const;
int IsUnique() const;
Vector Clone() const;
int EnsureUnique();
unsigned Length() const;
unsigned Stride() const;
// Low-level hooks
VecPtr PtrToAll();
VecPtr PtrToAll() const;
};

template
INLINE VecRep::VecRep()
// Constructor to form a null VecRep object.
// This object should only be allocated statically.
// NOTE: TYPE's default constructor called implicitly.
{
alloclen = 1;
refcnt = 1;
}

template
INLINE Vector::~Vector()
// Destructor unbinds this vector from the data
// it was sharing.
{
Unbind();
}


template
INLINE TYPE &Vector::operator[](unsigned i)
// For non-const vectors.
{
return start[CHECK(i)*stride];
}


template
INLINE const TYPE &Vector::operator[](unsigned i) const
// For const vectors.
{
return start[CHECK(i)*stride];
}

template
INLINE Vector &Vector::operator=(const Vector &v)
// Share semantics used for assignment.
{
if (this != &v) Share(v); // Note trap for assignment to self
return *this;
}

template
INLINE Vector &Vector::operator=(const TYPE &x)
// Sets each element of the vector to the value x.
{
SetElements(x);
return *this;
}

template
INLINE int Vector::IsNull() const
// Returns true if the vector references null_rep.
{
return rep == &VecRep::null_rep;
}

template
INLINE int Vector::IsUnique() const
// Returns true if vector has only reference to shared data,
// or the shared data is null_rep.
{
return rep->refcnt == 1 || IsNull();
}


template
INLINE unsigned Vector::Length() const
// Return the logical number of elements of the vector.
{
return len;
}

template
INLINE unsigned Vector::Stride() const
// Return the vector's stride (ie. offset between
// each logical element.)
{
return stride;
}

template
INLINE VecPtr Vector::PtrToAll()
// Returns a vector pointer to the beginning of
// this vector's elements.
{
return VecPtr(start, stride);
}

template
INLINE VecPtr Vector::PtrToAll() const
// Returns a vector pointer to the beginning of
// this vector's elements.
{
return VecPtr(start, stride);
}

#undef INLINE

// Whether or not we should include the non-line methods for
// our class templates here is implementation dependent.

#include "vector.mth"

#endif


  3 Responses to “Category : C++ Source Code
Archive   : BLFMATH.ZIP
Filename : VECTOR.H

  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/