Chapter 4. Basic Classes

Table of Contents
Classes
Example Code
EST_KVL example code

Classes

Container Classes

Hash Tables

class EST_HashFunctions{}

class EST_HashFunctions

This is just a convinient place to put some useful hash functions

DefaultHash()
static unsigned int DefaultHash(const void *data, size_t size, unsigned int n)

A generally useful hash function.

StringHash()
static unsigned int StringHash(const EST_String &key, unsigned int size)

A hash function for strings.

class EST_Hash_Pair{}

template <class K, class V> class EST_Hash_Pair

This class is used in hash tables to hold a key value pair. Not much to say beyond that.

k;
K k

The key

v;
V v

The value

class EST_THash{}

template <class K, class V> class EST_THash(: protected EST_HashFunctions

An open hash table. The number of buckets should be set to allow enough space that there are relatively few entries per bucket on average.

EST_THash()
EST_THash(int size, unsigned int (*hash_function)(const K &key, unsigned int size)= NULL)

Create a table with the given number of buckets. Optionally setting a custom hash function.

EST_THash()
EST_THash(const EST_THash<K, V> &from)

Create a copy

~EST_THash()
~EST_THash(void)

Destroy the table.

clear()
void clear(void)

Empty the table.

num_entries()
unsigned int num_entries(void) const

Return the total number of entries in the table.

present()
int present(const K &key) const

Does the key have an entry?

val()
V& val(const K &key, int &found) const

Return the value associated with the key. found is set to whether such an entry was found.

val()
V& val(const K &key) const

Return the value associated with the key.

copy()
void copy(const EST_THash<K, V> &from)

Copy all entries

map()
void map(void (*func)(K&, V&))

Apply func to each entry in the table.

add_item()
int add_item(const K &key, const V &value, int no_search = 0)

Add an entry to the table.

remove_item()
int remove_item(const K &rkey, int quiet = 0)

Remove an entry from the table.

operator = ()
EST_THash <K,V> & operator = (const EST_THash<K, V> &from)

Assignment is a copy operation

dump()
void dump(ostream &stream, int all=0)

Print the table to stream in a human readable format.

Pair Iteration

This iterator steps through the table returning key-value pairs.

union IPointer_s{}

struct IPointer_s

A position in the table is given by a bucket number and a pointer into the bucket

skip_blank()
void skip_blank(IPointer &ip) const

Shift to point at something.

point_to_first()
void point_to_first(IPointer &ip) const

Go to start of the table.

move_pointer_forwards()
void move_pointer_forwards(IPointer &ip) const

Move pointer forwards, at the end of the bucket, move down.

points_to_something()
bool points_to_something(const IPointer &ip) const

We are at the end if the pointer ever becomes NULL

points_at()
EST_Hash_Pair <K, V> & points_at(const IPointer &ip)

Return the contents of this entry.

V;
friend class EST_TStructIterator EST_THash K V IPointer EST_Hash_Pair KV

The iterator must be a friend to access this private interface.

Entry;
typedef EST_Hash_Pair <K, V> Entry

An entry returned by the iterator is a key value pair.

Entries;
typedef EST_TStructIterator < EST_THash<K, V>, IPointer, EST_Hash_Pair<K, V> > Entries

Give the iterator a sensible name.

Key Iteration

This iterator steps through the table returning just keys.

union IPointer_k_s{}

struct IPointer_k_s

A position in the table is given by a bucket number and a pointer into the bucket

skip_blank()
void skip_blank(IPointer_k &ip) const

Shift to point at something.

point_to_first()
void point_to_first(IPointer_k &ip) const

Go to start of the table.

move_pointer_forwards()
void move_pointer_forwards(IPointer_k &ip) const

Move pointer forwards, at the end of the bucket, move down.

points_to_something()
bool points_to_something(const IPointer_k &ip) const

We are at the end if the pointer ever becomes NULL

points_at()
K& points_at(const IPointer_k &ip)

Return the key of this entry.

K;
friend class EST_TIterator EST_THash K V IPointer_kK

The iterator must be a friend to access this private interface.

KeyEntry;
typedef K KeyEntry

An entry returned by this iterator is just a key.

KeyEntries;
typedef EST_TIterator < EST_THash<K, V>, IPointer_k, K > KeyEntries

Give the iterator a sensible name.

class EST_TStringHash{}

template <class V> class EST_TStringHash(: public EST_THash<EST_String, V>

A specialised hash table for when the key is an EST_String.

This is just a version of EST_THash which has a different default hash function.

EST_TStringHash()
EST_TStringHash(int size)

Create a string hash table of size buckets.

Entry;
typedef EST_Hash_Pair <EST_String, V> Entry

An entry returned by the iterator is a key value pair.

Entries;
typedef EST_TStructIterator < EST_THash<EST_String, V>, IPointer, EST_Hash_Pair<EST_String, V> > Entries

Give the iterator a sensible name.

class EST_IMatrix{}

class EST_IMatrix(: public EST_TSimpleMatrix<int>

A matrix class for integers. {\tt EST_IMatrix x} should be used instead of {\tt int **x} wherever possible.

EST_IMatrix()

EST_IMatrix(int m, int n)

size constructor

EST_IMatrix()

EST_IMatrix(EST_IMatrix &a)

copy constructor

EST_IMatrix()

EST_IMatrix(EST_IMatrix &a, int b)

CHECK - what does this do???

EST_IMatrix()

EST_IMatrix()

default constructor

class EST_TMatrix{}

(R,C) template <class T> class EST_TMatrix(: public EST_TVector<T>

Template Matrix class.

This is an extension of the EST_TVector class to two dimensions.

EST_TMatrix()

EST_TMatrix()

default constructor

EST_TMatrix()

EST_TMatrix(const EST_TMatrix<T> &m)

copy constructor

EST_TMatrix()

EST_TMatrix(int rows, int cols)

"size" constructor

EST_TMatrix()

EST_TMatrix(int rows, int cols, T *memory, int offset=0, int free_when_destroyed=0)

construct from memory supplied by caller

~EST_TMatrix()

~EST_TMatrix()

EST_TMatrix

access

Basic access methods for matrices.

num_rows()
int num_rows() const

return number of rows

num_columns()
int num_columns() const

return number of columns

a_no_check()
INLINE const T& a_no_check(int row, int col) const

const access with no bounds check, care recommend

a_no_check()
INLINE T& a_no_check(int row, int col)

access with no bounds check, care recommend

a_check()
const T& a_check(int row, int col) const

const element access function

a_check()
T& a_check(int row, int col)

non-const element access function

operator()
const T& operator operator(int row, int col) const

const element access operator

operator()
T& operator operator(int row, int col)

non-const element access operator

resize()

void resize(int rows, int cols, int set=1)

resize matrix. If {\tt set=1}, then the current values in the matirx are preserved up to the new size {\tt n}. If the new size exceeds the old size, the rest of the matrix is filled with the {\tt def_val}

fill()

void fill(const T &v)

fill matrix with value v

operator=()

EST_TMatrix& operator=(const EST_TMatrix &s)

assignment operator

add_rows()

EST_TMatrix& add_rows(const EST_TMatrix &s)

The two versions of what might have been operator +=

Sub-Matrix/Vector Extraction

All of these return matrecies and vectors which share memory with the original, so altering values them alters the original.

row()
void row(EST_TVector<T> &rv, int r, int start_c=0, int len=-1)

Make the vector {\tt rv} a window onto row {\tt r}

column()
void column(EST_TVector<T> &cv, int c, int start_r=0, int len=-1)

Make the vector {\tt cv} a window onto column {\tt c}

sub_matrix()
void sub_matrix(EST_TMatrix<T> &sm, int r=0, int numr=EST_ALL, int c=0, int numc=EST_ALL)

Make the matrix {\tt sm} a window into this matrix.

Copy in and out

Copy data between buffers and the matrix.

copy_row()
void copy_row(int r, T *buf, int offset=0, int num=-1) const

Copy row {\tt r} of matrix to {\tt buf}. {\tt buf} should be pre-malloced to the correct size.

copy_column()
void copy_column(int c, T *buf, int offset=0, int num=-1) const

Copy column {\tt c} of matrix to {\tt buf}. {\tt buf} should be pre-malloced to the correct size.

set_row()
void set_row(int n, const T *buf, int offset=0, int num=-1)

Copy buf into row {\tt n} of matrix

set_column()
void set_column(int n, const T *buf, int offset=0, int num=-1)

Copy buf into column {\tt n} of matrix.

set_memory()
void set_memory(T *buffer, int offset, int rows, int columns, int free_when_destroyed=0)

For when you absolutely have to have access to the memory

io

Matrix file io.

load()
EST_read_status load(const class EST_String &filename)

load Matrix from file - Not currently implemented.

save()
EST_write_status save(const class EST_String &filename) const

save Matrix to file {\tt filename}

operator << ()
friend ostream& operator << (ostream &st, const EST_TMatrix<T> &a)

print matirx.

p_num_rows;

unsigned int p_num_rows

Visible shape

p_row_step;

unsigned int p_row_step

How to access the memory

fast_a_m()

INLINE const T& fast_a_m(int r, int c) const

quick method for returning {\tt x[m][n]}

set_values()

void set_values(const T *data, int r_step, int c_step, int start_r, int num_r, int start_c, int num_c )

Get and set values from array

copy()

void copy(const EST_TMatrix<T> &a)

private resize and copy function.

copy_data()

void copy_data(const EST_TMatrix<T> &a)

just copy data, no resizing, no size check.

just_resize()

void just_resize(int new_rows, int new_cols, T** old_vals)

resize the memory and reset the bounds, but don't set values.

default_vals()

void default_vals()

sets data and length to default values (0 in both cases).

class EST_FMatrix{}

class EST_FMatrix(: public EST_TSimpleMatrix<float>

A matrix class for floating point numbers. EST_FMatrix x should be used instead of float **x wherever possible.

EST_FMatrix()

EST_FMatrix(int m, int n)

size constructor

EST_FMatrix()

EST_FMatrix(const EST_FMatrix &a)

copy constructor

EST_FMatrix()

EST_FMatrix(const EST_FMatrix &a, int b)

CHECK - what does this do???

EST_FMatrix()

EST_FMatrix()

default constructor

save()

EST_write_status save(const EST_String &filename, const EST_String &type = EST_FMatrix::default_file_type )

Save in file (ascii or binary)

load()

EST_read_status load(const EST_String &filename)

Load from file (ascii or binary as defined in file)

est_save()

EST_write_status est_save(const EST_String &filename, const EST_String &type)

Save in file in est format

est_load()

EST_read_status est_load(const EST_String &filename)

Load from file in est format (binary/ascii defined in file itself)

copyin()

void copyin(float **x, int rows, int cols)

Copy 2-d array {\tt x} of size {\tt rows x cols} into matrix.

operator+=()

EST_FMatrix& operator+=(const EST_FMatrix &a)

Add elements of 2 same sized matrices.

operator-=()

EST_FMatrix& operator-=(const EST_FMatrix &a)

Subtract elements of 2 same sized matrices.

operator*=()

EST_FMatrix& operator*=(const float f)

elementwise multiply by scalar

operator/=()

EST_FMatrix& operator/=(const float f)

elementwise divide by scalar

operator*()

friend EST_FMatrix operator*(const EST_FMatrix &a, const float x)

Multiply all elements of matrix by {\tt x}.

operator*()

friend EST_FVector operator*(const EST_FMatrix &a, const EST_FVector &v)

Multiply matrix by vector.

operator*()

friend EST_FVector operator*(const EST_FVector &v, const EST_FMatrix &a)

Multiply vector by matrix

operator*()

friend EST_FMatrix operator*(const EST_FMatrix &a, const EST_FMatrix &b)

Multiply matrix by matrix.

class EST_TList{}

template <class T> class EST_TList(: public EST_UList

A Template doubly linked list class. This class contains doubly linked lists of a type denoted by {\tt T}. A pointer of type \Ref{EST_Litem} is used to access items in the list. The class supports a variety of ways of adding, removing and accessing items in the list. For examples of how to operate lists, see \Ref{list_example}.

Iteration through the list is performed using a pointer of type \Ref{EST_Litem}. See \Ref{Iteration} for example code.

Constructor functions

EST_TList()
EST_TList()

default constructor

EST_TList()
EST_TList(const EST_TList<T> &l)

copy constructor

Access functions for reading and writing items.

See \Ref{EST_TList_Accessing} for examples.

item()
T& item(const EST_Litem *p)

return the value associated with the EST_Litem pointer. This has the same functionality as the overloaded () operator.

item()
const T& item(const EST_Litem *p) const

return a const value associated with the EST_Litem pointer

nth()
T& nth(int n)

return the Nth value

nth()
const T& nth(int n) const

return a const Nth value

first()
const T& first() const

return const reference to first item in list

last()
const T& last() const

return const reference to last item in list

first()
T& first()

return reference to first item in list

last()
T& last()

return reference to last item in list

operator()
const T& operator operator(const EST_Litem *ptr) const

return const reference to item in list pointed to by {\tt ptr}

operator()
T& operator operator(const EST_Litem *ptr)

return const reference to item in list pointed to by {\tt ptr}

Removing items in a list.

more.

remove()
EST_Litem* remove(EST_Litem *ptr)

remove item pointed to by {\tt ptr}, return pointer to previous item. See \Ref{Removing} for example code.

remove_nth()
EST_Litem* remove_nth(int n)

remove nth item, return pointer to previous item

Adding items to a list.

In all cases, a complete copy of the item is made and added to the list. See \Ref{Addition} for examples.

append()
void append(const T &item)

add item onto end of list

prepend()
void prepend(const T &item)

add item onto start of list

insert_after()
EST_Litem* insert_after(EST_Litem *ptr, const T &item)

add {\tt item} after position given by {\tt ptr}, return pointer to added item

insert_before()
EST_Litem* insert_before(EST_Litem *ptr, const T &item)

add {\tt item} before position given by {\tt ptr}, return pointer to added item

Exchange

exchange()
void exchange(EST_Litem *a, EST_Litem *b)

exchange 1

exchange()
void exchange(int i, int j)

exchange 2

exchange_contents()
static void exchange_contents(EST_Litem *a, EST_Litem *b)

exchange 3

General functions

operator=()
EST_TList <T> & operator=(const EST_TList<T> &a)

make full copy of list

operator +=()
EST_TList <T> & operator +=(const EST_TList<T> &a)

Add list onto end of existing list

operator << ()
friend ostream& operator << (ostream &st, EST_TList<T> const &list)

print list

clear()
void clear(void)

remove all items in list

class EST_DVector{}

class EST_DVector(: public EST_TSimpleVector<double>

A vector class for double precision floating point numbers. {\tt EST_DVector x} should be used instead of {\tt float *x} wherever possible.

EST_DVector()

EST_DVector(int n)

Size constructor.

EST_DVector()

EST_DVector(const EST_DVector &a)

Copy constructor.

EST_DVector()

EST_DVector()

Default constructor.

operator*=()

EST_DVector& operator*=(const EST_DVector &s)

elementwise multiply

operator+=()

EST_DVector& operator+=(const EST_DVector &s)

elementwise add

operator*=()

EST_DVector& operator*=(const double d)

elementwise multiply by scalar

operator/=()

EST_DVector& operator/=(const double d)

elementwise divide by scalar

save()

EST_write_status save(const EST_String &filename, const EST_String &type)

save vector to file filename.

load()

EST_read_status load(const EST_String &filename)

load vector from file filename.

est_load()

EST_read_status est_load(const EST_String &filename)

Load from file in est format (binary/ascii defined in file itself)

class EST_FVector{}

class EST_FVector(: public EST_TSimpleVector<float>

A vector class for floating point numbers. {\tt EST_FVector x} should be used instead of {\tt float *x} wherever possible.

EST_FVector()

EST_FVector(int n)

Size constructor.

EST_FVector()

EST_FVector(const EST_FVector &a)

Copy constructor.

EST_FVector()

EST_FVector()

Default constructor.

operator*=()

EST_FVector& operator*=(const EST_FVector &s)

elementwise multiply

operator+=()

EST_FVector& operator+=(const EST_FVector &s)

elementwise add

operator*=()

EST_FVector& operator*=(const float f)

elementwise multiply by scalar

operator/=()

EST_FVector& operator/=(const float f)

elementwise divide by scalar

save()

EST_write_status save(const EST_String &filename, const EST_String &type)

save vector to file filename.

load()

EST_read_status load(const EST_String &filename)

load vector from file filename.

est_load()

EST_read_status est_load(const EST_String &filename)

Load from file in est format (binary/ascii defined in file itself)

class EST_TSimpleVector{}

template <class T> class EST_TSimpleVector(: public EST_TVector<T>

A derived class from EST_TVector which is used for containing simple types, such as float or int

EST_TSimpleMatrix()

EST_TSimpleMatrix(void)

default constructor

p_memory;

T* p_memory

Pointer to the start of the vector. The start of allocated memory is p_memory-p_offset.

p_num_columns;

unsigned int p_num_columns

Visible shape

p_offset;

unsigned int p_offset

How to access the memory

vcell_pos()

INLINE unsigned int vcell_pos(unsigned int c, unsigned int cs) const

The memory access rule, in one place for easy reference

fast_a_v()

INLINE const T& fast_a_v(int c) const

quick method for returning

set_values()

void set_values(const T *data, int step, int start_c, int num_c)

Get and set values from array

copy()

void copy(const EST_TVector<T> &a)

private copy function, called from all other copying functions.

copy_data()

void copy_data(const EST_TVector<T> &a)

just copy data, no resizing, no size check.

just_resize()

void just_resize(int new_cols, T** old_vals)

resize the memory and reset the bounds, but don't set values.

default_vals()

void default_vals()

sets data and length to default values (0 in both cases).

EST_TVector()

EST_TVector()

default constructor

EST_TVector()

EST_TVector(const EST_TVector<T> &v)

copy constructor

EST_TVector()

EST_TVector(int n)

"size" constructor - make vector of size n.

EST_TVector()

EST_TVector(int, T *memory, int offset=0, int free_when_destroyed=0)

construct from memory supplied by caller

~EST_TVector()

~EST_TVector()

destructor.

def_val;

static const T* def_val

default value, used for filling matrix after resizing

error_return;

static T* error_return

A reference to this variable is returned if you try and access beyond the bounds of the matrix. The vaue is undefined, but you can check for the reference you get having the same address as this variable to test for an error.

resize()

void resize(int n, int set=1)

resize vector. If set=1, then the current values in the vector are preserved up to the new length n. If the new length exceeds the old length, the rest of the vector is filled with the def_val

memory()

const T* memory() const

For when you absolutely have to have access to the memory

access

Basic access methods for vectors.

num_columns()
int num_columns() const

number of items in vector.

length()
int length() const

number of items in vector.

n()
int n() const

number of items in vector.

a_no_check()
INLINE const T& a_no_check(int n) const

read-only const access operator: without bounds checking

a_no_check()
INLINE T& a_no_check(int n)

read/write non-const access operator: without bounds checking

a_no_check_1()
INLINE const T& a_no_check_1(int n) const

read-only const access operator: without bounds checking

a_no_check_1()
INLINE T& a_no_check_1(int n)

read/write non-const access operator: without bounds checking

a_check()
const T& a_check(int n) const

read-only const access operator: with bounds checking

a_check()
T& a_check(int n)

read/write non-const access operator: with bounds checking

operator()
const T& operator operator(int n) const

read-only const access operator: return reference to nth member

operator [] ()
T& operator [] (int n)

read/write non const access operator: return reference to nth member

operator=()

EST_TVector& operator=(const EST_TVector &s)

assignment operator

fill()

void fill(const T &v)

Fill entire array will value v.

empty()

void empty()

Fill vector with default value

save()

EST_write_status save(const EST_String &filename)

Save vector to file filename.

operator == ()

int operator == (const EST_TVector &v) const

is true if vectors are equal size and all elements are equal.

operator != ()

int operator != (const EST_TVector &v) const

is true if vectors are not equal size or a single elements isn't equal.

copy_section()

void copy_section(T* dest, int offset=0, int num=-1) const

Copy data in and out. Subclassed by SimpleVector for speed.

sub_vector()

void sub_vector(EST_TVector<T> &sv, int start_c=0, int len=-1)

Create a sub vector.

operator << ()

friend ostream& operator << (ostream &st, const EST_TVector<T> &m)

print out vector.

T;

friend class EST_TMatrixT

Matrix must be friend to set up subvectors

EST_TSimpleMatrix()

EST_TSimpleMatrix(int m, int n)

size constructor

EST_TSimpleMatrix()

EST_TSimpleMatrix(const EST_TSimpleMatrix<T> &m)

copy constructor

copy()

void copy(const EST_TSimpleMatrix<T> &a)

copy one matrix into another

resize()

void resize(int rows, int cols, int set=1)

resize matrix

operator=()

EST_TSimpleMatrix <T> & operator=(const EST_TSimpleMatrix<T> &s)

assignment operator

EST_TSimpleVector()

EST_TSimpleVector()

default constructor

EST_TSimpleVector()

EST_TSimpleVector(int n)

"size" constructor

resize()

void resize(int n, int set=1)

resize vector

operator=()

EST_TSimpleVector& operator=(const EST_TSimpleVector<T> &s)

assignment operator

zero()

void zero(void)

Fill entire array with 0 bits.

empty()

void empty(void)

Fill vector with default value

class EST_TrieNode{}

class EST_TrieNode

An internal class for \Ref{EST_StringTrie} used to hold represent the node in an string index tree.

This basically represents a 128-branching node (on for each character) plus a contents field for strings ending at this point.

EST_TrieNode()

EST_TrieNode(const int width)

lookup()

void* lookup(const unsigned char *key) const

Find the contents for given string, 0 if no current contents

add()

void add(const unsigned char *key, void *item)

add {\tt item} for {\tt key} overwriting previous contents

copy_into()

void copy_into(EST_StringTrie &trie, const EST_String &path) const

copy all entries in trie node into trie

class EST_TDeque{}

template <class T> class EST_TDeque

Double ended queue.

Iter;

typedef EST_TIterator <Container, IPointer, Entry> Iter

Name for an itterator like this

EST_TIterator()

EST_TIterator()

Create an iterator not associated with any specific container.

EST_TIterator()

EST_TIterator(const Container &over)

Create an iterator ready to run over the given container.

operator = ()

Iter& operator = (const Iter &orig)

Copy an iterator by assignment

operator = ()

Iter& operator = (const Container &over)

Assigning a container to an iterator sets it ready to start.

begin()

void begin(