Public Member Functions | Private Member Functions | Private Attributes

chomp::homology::chain< euclidom > Class Template Reference

This class defines objects which represent chains as finite sequences of elements identified by integral numbers with coefficients in a given Euclidean domain. More...

#include <chains.h>

List of all members.

Public Member Functions

 chain ()
 The default constructor.
 chain (const chain< euclidom > &c)
 The copy constructor.
chain< euclidom > & operator= (const chain< euclidom > &c)
 The assignment operator.
 ~chain ()
 The destructor.
int size () const
 Returns the size of the chain, that is, the number of elements with non-zero coefficients.
bool empty () const
 Returns true if the chain is empty (zero), false otherwise.
euclidom getcoefficient (int n=-1) const
 Finds and returns the coefficient in front of the given element.
int findnumber (int n) const
 Find the position of an element with the given identifier.
euclidom coef (int i) const
 Returns the coefficient in front of the i-th element in the chain.
int num (int i) const
 Returns the number (identifier) of the i-th element in the chain.
bool contains_non_invertible () const
 Determines if the chain contains a non-invertible coefficient.
int findbest (chain< euclidom > *table=NULL) const
 Finds the best element in the chain for reduction, that is, the element with minimal value of delta.
chain< euclidom > & add (int n, euclidom e)
 Adds an element algebraically to the chain.
chain< euclidom > & remove (int n)
 Removes an element with the given identifier from the chain.
chain< euclidom > & add (const chain< euclidom > &other, euclidom e, int number=-1, chain< euclidom > *table=NULL)
 Adds one chain to another with a given coefficient.
chain< euclidom > & swap (chain< euclidom > &other, int number=-1, int othernumber=-1, chain< euclidom > *table=NULL)
 Swaps one chain with another.
chain< euclidom > & take (chain< euclidom > &c)
 Takes data from another chain. Destroys the other chain.
chain< euclidom > & multiply (euclidom e, int number=-1)
 Multiplies one or all the coefficients in the chain by the given number.
outputstreamshow (outputstream &out, const char *label=NULL) const
 Shows the chain to the output stream.
std::ostream & show (std::ostream &out, const char *label=NULL) const
 Shows the chain to the standard output stream.

Private Member Functions

chain< euclidom > & insertpair (int i, int n, euclidom e)
 Inserts one chain element at the given position.
chain< euclidom > & removepair (int i)
 Removes one chain element at the given position.
chain< euclidom > & swapnumbers (int number1, int number2)
 Swaps two numbers (identifiers) in the chain.
bool allocated () const
 Checks if the tables have been allocated depending on the value of their length.

Private Attributes

int len
 The length of the list and the length of the table.
union {
   struct {
      int *   n
      euclidom *   e
   }   t
   struct {
      int *   n
      euclidom *   e
   }   x
}; 
 Elements of the list sorted according to the identifier.

Detailed Description

template<class euclidom>
class chomp::homology::chain< euclidom >

This class defines objects which represent chains as finite sequences of elements identified by integral numbers with coefficients in a given Euclidean domain.

Definition at line 101 of file chains.h.


Constructor & Destructor Documentation

template<class euclidom >
chomp::homology::chain< euclidom >::chain (  )  [inline]

The default constructor.

Definition at line 246 of file chains.h.

References chomp::homology::chain< euclidom >::len.

{
        len = 0;
        return;
} /* chain<euclidom>::chain */

template<class euclidom >
chomp::homology::chain< euclidom >::chain ( const chain< euclidom > &  c  )  [inline]

The copy constructor.

Definition at line 253 of file chains.h.

References chomp::homology::chain< euclidom >::allocated(), chomp::homology::chain< euclidom >::e, chomp::homology::chain< euclidom >::len, chomp::homology::chain< euclidom >::n, chomp::homology::chain< euclidom >::t, and chomp::homology::chain< euclidom >::x.

{
        // copy the length of the chain
        len = c. len;

        // allocate new tables if necessary and copy the data
        if (allocated ())
        {
                t. n = new int [len];
                t. e = new euclidom [len];
                if (!t. n || !t. e)
                        throw "Not enough memory to create a chain copy.";
                for (int i = 0; i < len; ++ i)
                {
                        t. n [i] = c. t. n [i];
                        t. e [i] = c. t. e [i];
                }
        }
        else
        {
                for (int i = 0; i < len; ++ i)
                {
                        x. n [i] = c. x. n [i];
                        x. e [i] = c. x. e [i];
                }
        }
        return;
} /* chain<euclidom>::chain */

template<class euclidom >
chomp::homology::chain< euclidom >::~chain (  )  [inline]

The destructor.

Definition at line 321 of file chains.h.

References chomp::homology::chain< euclidom >::allocated(), chomp::homology::chain< euclidom >::e, chomp::homology::chain< euclidom >::n, and chomp::homology::chain< euclidom >::t.

{
        if (allocated ())
        {
                delete [] t. n;
                delete [] t. e;
        }
        return;
} /* chain<euclidom>::~chain */


Member Function Documentation

template<class euclidom >
chain< euclidom > & chomp::homology::chain< euclidom >::add ( int  n,
euclidom  e 
) [inline]

Adds an element algebraically to the chain.

Definition at line 690 of file chains.h.

References chomp::homology::chain< euclidom >::allocated(), chomp::homology::chain< euclidom >::insertpair(), chomp::homology::chain< euclidom >::len, chomp::homology::chain< euclidom >::removepair(), chomp::homology::chain< euclidom >::t, and chomp::homology::chain< euclidom >::x.

Referenced by chomp::homology::chain< euclidom >::add().

{
        // if the coefficient is zero, ignore the pair
        if (e == 0)
                return *this;
        bool a = allocated ();
        int *tntab = a ? t. n : x. n;
        euclidom *tetab = a ? t. e : x. e;

        // find the position in the table for adding this pair
        int i = 0;
        while ((i < len) && (tntab [i] < n))
                ++ i;

        // if an element with this identifier was found, add the coefficients
        if ((i < len) && (tntab [i] == n))
        {
                // add the coefficient
                tetab [i] += e;

                // if the coefficient became zero, remove this pair
                if (tetab [i] == 0)
                        return removepair (i);

                // otherwise we are done
                else
                        return *this;
        }

        // otherwise insert this pair into the chain
        return insertpair (i, n, e);

} /* chain<euclidom>::add */

template<class euclidom >
chain< euclidom > & chomp::homology::chain< euclidom >::add ( const chain< euclidom > &  other,
euclidom  e,
int  number = -1,
chain< euclidom > *  table = NULL 
) [inline]

Adds one chain to another with a given coefficient.

If the chain is a row of a matrix, then its number and the table of columns must be given for proper modification. If this is a column, its number and columns must be given.

Definition at line 743 of file chains.h.

References chomp::homology::chain< euclidom >::add(), chomp::homology::chain< euclidom >::allocated(), chomp::homology::chain< euclidom >::len, chomp::homology::chain< euclidom >::t, and chomp::homology::chain< euclidom >::x.

{
        // if the coefficient is zero or the other chain is zero,
        // then there is nothing to do
        if ((e == 0) || !other. len)
                return *this;

        // prepare big tables for the new chain
        int tablen = len + other. len;
        int *bigntab = new int [tablen];
        euclidom *bigetab = new euclidom [tablen];
        if (!bigntab || !bigetab)
                throw "Not enough memory to add chains.";

        // prepare the counters of elements of the two input chains
        // and of the output chain
        int i = 0, j = 0, k = 0;

        // determine the actual tables to be processed
        bool a = allocated ();
        bool oa = other. allocated ();
        const int *tntab = a ? t. n : x. n;
        const euclidom *tetab = a ? t. e : x. e;
        const int *ontab = oa ? other. t. n : other. x. n;
        const euclidom *oetab = oa ? other. t. e : other. x. e;

        // go through both input chains and compute the output chain
        while ((i < len) || (j < other. len))
        {
                if (i >= len)
                {
                        bigntab [k] = ontab [j];
                        bigetab [k] = e * oetab [j ++];
                        if (table)
                        {
                                table [bigntab [k]]. add (number,
                                        bigetab [k]);
                        }
                        ++ k;
                }
                else if ((j >= other. len) || (tntab [i] < ontab [j]))
                {
                        bigntab [k] = tntab [i];
                        bigetab [k ++] = tetab [i ++];
                }
                else if (tntab [i] > ontab [j])
                {
                        bigntab [k] = ontab [j];
                        bigetab [k] = e * oetab [j ++];
                        if (table)
                        {
                                table [bigntab [k]]. add (number,
                                        bigetab [k]);
                        }
                        ++ k;
                }
                else // if (tntab [i] == ontab [j])
                {
                        bigntab [k] = tntab [i];
                        euclidom addelem = e * oetab [j ++];
                        bigetab [k] = tetab [i ++] + addelem;
                        euclidom zero;
                        zero = 0;
                        if (bigetab [k] != zero)
                        {
                                if (table)
                                {
                                        table [bigntab [k]]. add (number,
                                                addelem);
                                }
                                ++ k;
                        }
                        else if (table)
                        {
                                table [bigntab [k]]. remove (number);
                        }
                }
        }

        // release the old tables if they are useless now
        if (a && ((k != len) || (k == tablen)))
        {
                delete [] t. n;
                delete [] t. e;
        }

        // use the previous tables and release the big table if beneficial
        if (a && (k == len) && (k != tablen))
        {
                for (int i = 0; i < len; ++ i)
                {
                        t. n [i] = bigntab [i];
                        t. e [i] = bigetab [i];
                }
                delete [] bigntab;
                delete [] bigetab;
                return *this;
        }

        len = k;

        // if the new tables don't have to be allocated, only copy the data
        if (!allocated ())
        {
                for (int i = 0; i < len; ++ i)
                {
                        x. n [i] = bigntab [i];
                        x. e [i] = bigetab [i];
                }
                delete [] bigntab;
                delete [] bigetab;
                return *this;
        }

        // if the big tables cannot be used, allocate new tables
        if (len != tablen)
        {
                t. n = new int [len];
                t. e = new euclidom [len];
                if (!t. n || !t. e)
                        throw "Cannot shorten a sum of chains.";
                for (int i = 0; i < len; ++ i)
                {
                        t. n [i] = bigntab [i];
                        t. e [i] = bigetab [i];
                }
                delete [] bigntab;
                delete [] bigetab;
        }

        // otherwise, simply use the big tables
        else
        {
                t. n = bigntab;
                t. e = bigetab;
        }

        return *this;
} /* chain<euclidom>::add */

template<class euclidom >
bool chomp::homology::chain< euclidom >::allocated (  )  const [inline, private]
template<class euclidom >
euclidom chomp::homology::chain< euclidom >::coef ( int  i  )  const [inline]

Returns the coefficient in front of the i-th element in the chain.

Definition at line 389 of file chains.h.

References chomp::homology::chain< euclidom >::allocated(), chomp::homology::chain< euclidom >::e, chomp::homology::chain< euclidom >::len, chomp::homology::chain< euclidom >::t, and chomp::homology::chain< euclidom >::x.

{
        if (i >= len)
                throw "Wrong coefficient requested from a chain.";
        return (allocated () ? t. e : x. e) [i];
} /* chain<euclidom>::coef */

template<class euclidom >
bool chomp::homology::chain< euclidom >::contains_non_invertible (  )  const [inline]

Determines if the chain contains a non-invertible coefficient.

Returns true if yes, false if not.

Definition at line 405 of file chains.h.

References chomp::homology::chain< euclidom >::allocated(), chomp::homology::chain< euclidom >::e, chomp::homology::chain< euclidom >::len, chomp::homology::chain< euclidom >::t, and chomp::homology::chain< euclidom >::x.

{
        if (allocated ())
        {
                for (int i = 0; i < len; ++ i)
                {
                        if (t. e [i]. delta () > 1)
                                return true;
                }
        }
        else
        {
                for (int i = 0; i < len; ++ i)
                {
                        if (x. e [i]. delta () > 1)
                                return true;
                }
        }
        return false;
} /* chain<euclidom>::contains_non_invertible */

template<class euclidom >
bool chomp::homology::chain< euclidom >::empty (  )  const [inline]

Returns true if the chain is empty (zero), false otherwise.

Definition at line 338 of file chains.h.

References chomp::homology::chain< euclidom >::len.

{
        return !len;
} /* chain<euclidom>::empty */

template<class euclidom >
int chomp::homology::chain< euclidom >::findbest ( chain< euclidom > *  table = NULL  )  const [inline]

Finds the best element in the chain for reduction, that is, the element with minimal value of delta.

IF the given table is given, then additionally an element with the shortest chain length in the table is searched for. Returns the actual number of this element in the chain (not its identifier) or -1 if the chain is empty (zero).

Definition at line 427 of file chains.h.

References chomp::homology::chain< euclidom >::allocated(), chomp::homology::chain< euclidom >::e, chomp::homology::chain< euclidom >::len, chomp::homology::chain< euclidom >::n, chomp::homology::chain< euclidom >::size(), chomp::homology::chain< euclidom >::t, and chomp::homology::chain< euclidom >::x.

{
        // if the chain is so short that the answer is obvious, return it
        if (len <= 1)
                return (len - 1);

        // find the number which has the smallest delta function value
        int this_delta, best_delta = -1;
        int best_i = 0;

        // go through the whole table
        bool a = allocated ();
        const int *tntab = a ? t. n : x. n;
        const euclidom *tetab = a ? t. e : x. e;
        int i;
        for (i = 0; i < len; ++ i)
        {
                // compute the value of the function delta
                this_delta = tetab [i]. delta ();

                // if the value is the smallest possible
                // and no further analysis was required, finish here
                if (!table && (this_delta == 1))
                        return i;

                // if this delta is better, remember it
                if (!i || (this_delta < best_delta))
                {
                        best_delta = this_delta;
                        best_i = i;
                }
        }

        // if no further analysis is required, return the result just now
        if (!table)
                return best_i;

        // analyse which element has the shortest corresponding chain
        int this_length, best_length =
                table [tntab [best_i]]. size ();
        for (i = best_i + 1; i < len; ++ i)
        {
                if (tetab [i]. delta () == best_delta)
                {
                        this_length =
                                table [tntab [i]]. size ();
                        if (best_length > this_length)
                        {
                                best_length = this_length;
                                best_i = i;
                        }
                }
        }

        return best_i;
} /* chain<euclidom>::findbest */

template<class euclidom >
int chomp::homology::chain< euclidom >::findnumber ( int  n  )  const [inline]

Find the position of an element with the given identifier.

Returns -1 if not found.

Definition at line 374 of file chains.h.

References chomp::homology::chain< euclidom >::allocated(), chomp::homology::chain< euclidom >::len, chomp::homology::chain< euclidom >::t, and chomp::homology::chain< euclidom >::x.

{
        bool a = allocated ();
        const int *tntab = a ? t. n : x. n;
        for (int i = 0; i < len; ++ i)
        {
                if (tntab [i] == n)
                        return i;
                else if (tntab [i] > n)
                        return -1;
        }
        return -1;
} /* chain<euclidom>::findnumber */

template<class euclidom >
euclidom chomp::homology::chain< euclidom >::getcoefficient ( int  n = -1  )  const

Finds and returns the coefficient in front of the given element.

If the identifier is negative, then returns the first nonzero coefficient or 0 if none.

Definition at line 344 of file chains.h.

References chomp::homology::chain< euclidom >::allocated(), chomp::homology::chain< euclidom >::e, chomp::homology::chain< euclidom >::len, chomp::homology::chain< euclidom >::t, and chomp::homology::chain< euclidom >::x.

{
        bool a = allocated ();
        const euclidom *tetab = a ? t. e : x. e;
        if (n < 0)
        {
                if (len > 0)
                        return tetab [0];
                else
                {
                        euclidom zero;
                        zero = 0;
                        return zero;
                }
        }

        const int *tntab = a ? t. n : x. n;
        int i = 0;
        while ((i < len) && (tntab [i] < n))
                ++ i;
        if ((i >= len) || (tntab [i] != n))
        {
                euclidom zero;
                zero = 0;
                return zero;
        }
        return tetab [i];
} /* chain<euclidom>::getcoefficient */

template<class euclidom >
chain< euclidom > & chomp::homology::chain< euclidom >::insertpair ( int  i,
int  n,
euclidom  e 
) [inline, private]

Inserts one chain element at the given position.

Definition at line 488 of file chains.h.

Referenced by chomp::homology::chain< euclidom >::add().

{
        // remember if the table was previously allocated or not
        bool a = allocated ();

        // increase the length
        ++ len;

        // determine if the new table should be allocated or not
        bool na = allocated ();

        // if a new table has to be allocated, do it
        if (na)
        {
                // allocate a new table
                int *newntab = new int [len];
                euclidom *newetab = new euclidom [len];
                if (!newntab || !newetab)
                        throw "Cannot add an element to a chain.";

                // determine the addresses of the old tables
                int *oldntab = a ? t. n : x. n;
                euclidom *oldetab = a ? t. e : x. e;

                // copy the old data and insert the new pair
                int j;
                for (j = 0; j < i; ++ j)
                {
                        newntab [j] = oldntab [j];
                        newetab [j] = oldetab [j];
                }
                newntab [i] = n;
                newetab [i] = e;
                for (j = i + 1; j < len; ++ j)
                {
                        newntab [j] = oldntab [j - 1];
                        newetab [j] = oldetab [j - 1];
                }

                // release the previous tables if they were allocated
                if (a)
                {
                        delete [] t. n;
                        delete [] t. e;
                }

                // take the new tables to the data structure
                t. n = newntab;
                t. e = newetab;
        }

        // otherwise just insert the new element at the appropriate position
        else // if (!na && !a)
        {
                for (int j = len - 1; j > i; -- j)
                {
                        x. n [j] = x. n [j - 1];
                        x. e [j] = x. e [j - 1];
                }
                x. n [i] = n;
                x. e [i] = e;
        }

        return *this;
} /* chain<euclidom>::insertpair */

template<class euclidom >
chain< euclidom > & chomp::homology::chain< euclidom >::multiply ( euclidom  e,
int  number = -1 
) [inline]

Multiplies one or all the coefficients in the chain by the given number.

Definition at line 1020 of file chains.h.

References chomp::homology::chain< euclidom >::allocated(), chomp::homology::chain< euclidom >::len, chomp::homology::chain< euclidom >::removepair(), chomp::homology::chain< euclidom >::t, and chomp::homology::chain< euclidom >::x.

{
        // check if the tables have been allocated or not
        bool a = allocated ();
        int *tntab = a ? t. n : x. n;
        euclidom *tetab = a ? t. e : x. e;

        // if there is only one element to be multiplied, find it and do it
        if (number >= 0)
        {
                for (int i = 0; i < len; ++ i)
                {
                        if (tntab [i] == number)
                        {
                                if (e == 0)
                                        removepair (i);
                                else
                                {
                                        tetab [i] *= e;
                                //      if (tetab [i] == 0)
                                //              removepair (i);
                                }
                                return *this;
                        }
                }
        }

        // if the entire chain has to be multiplied by a non-zero number...
        else if (e != 0)
        {
                for (int i = 0; i < len; ++ i)
                {
                        tetab [i] *= e;
                        if (tetab [i] == 0)
                                removepair (i);
                }
        }

        // otherwise, if the chain has to be made zero, clean it
        else
        {
                if (a)
                {
                        delete [] t. n;
                        delete [] t. e;
                }
                len = 0;
        }

        return *this;
} /* chain<euclidom>::multiply */

template<class euclidom >
int chomp::homology::chain< euclidom >::num ( int  i  )  const [inline]

Returns the number (identifier) of the i-th element in the chain.

Definition at line 397 of file chains.h.

References chomp::homology::chain< euclidom >::allocated(), chomp::homology::chain< euclidom >::len, chomp::homology::chain< euclidom >::n, chomp::homology::chain< euclidom >::t, and chomp::homology::chain< euclidom >::x.

{
        if (i >= len)
                throw "Wrong number requested from a chain.";
        return (allocated () ? t. n : x. n) [i];
} /* chain<euclidom>::num */

template<class euclidom >
chain< euclidom > & chomp::homology::chain< euclidom >::operator= ( const chain< euclidom > &  c  )  [inline]

The assignment operator.

Definition at line 284 of file chains.h.

{
        // first release allocated tables if any
        if (allocated ())
        {
                delete [] t. n;
                delete [] t. e;
        }

        // copy the length of the chain
        len = c. len;

        // allocate new tables if necessary and copy the data
        if (allocated ())
        {
                t. n = new int [len];
                t. e = new euclidom [len];
                if (!t. n || !t. e)
                        throw "Not enough memory to create a chain copy =.";
                for (int i = 0; i < len; ++ i)
                {
                        t. n [i] = c. t. n [i];
                        t. e [i] = c. t. e [i];
                }
        }
        else
        {
                for (int i = 0; i < len; ++ i)
                {
                        x. n [i] = c. x. n [i];
                        x. e [i] = c. x. e [i];
                }
        }
        return *this;
} /* chain<euclidom>::operator = */

template<class euclidom >
chain< euclidom > & chomp::homology::chain< euclidom >::remove ( int  n  ) 

Removes an element with the given identifier from the chain.

Definition at line 725 of file chains.h.

References chomp::homology::chain< euclidom >::allocated(), chomp::homology::chain< euclidom >::len, chomp::homology::chain< euclidom >::removepair(), chomp::homology::chain< euclidom >::t, and chomp::homology::chain< euclidom >::x.

{
        bool a = allocated ();
        int *tntab = a ? t. n : x. n;

        // find the element of the chain to be removed
        int i = 0;
        while ((i < len) && (tntab [i] < n))
                ++ i;

        // if found, then remove it
        if ((i < len) && (tntab [i] == n))
                return removepair (i);

        return *this;
} /* chain<euclidom>::remove */

template<class euclidom >
chain< euclidom > & chomp::homology::chain< euclidom >::removepair ( int  i  )  [inline, private]

Removes one chain element at the given position.

Definition at line 555 of file chains.h.

References chomp::homology::chain< euclidom >::allocated(), chomp::homology::chain< euclidom >::len, chomp::homology::chain< euclidom >::t, and chomp::homology::chain< euclidom >::x.

Referenced by chomp::homology::chain< euclidom >::add(), chomp::homology::chain< euclidom >::multiply(), and chomp::homology::chain< euclidom >::remove().

{
        // remember if the table was previously allocated or not
        bool a = allocated ();

        // decrease the length
        if (len)
                -- len;

        // determine if the new table should be allocated or not
        bool na = allocated ();

        // allocate the new tables if necessary
        if (na)
        {
                int *newntab = new int [len];
                euclidom *newetab = new euclidom [len];
                if (!newntab || !newetab)
                        throw "Cannot remove a pair from a chain.";

                // copy the data form the previous tables
                int j;
                for (j = 0; j < i; ++ j)
                {
                        newntab [j] = t. n [j];
                        newetab [j] = t. e [j];
                }
                for (j = i; j < len; ++ j)
                {
                        newntab [j] = t. n [j + 1];
                        newetab [j] = t. e [j + 1];
                }
                delete [] t. n;
                delete [] t. e;
                t. n = newntab;
                t. e = newetab;
        }

        // otherwise, copy the data from the previous tables
        else
        {
                int *oldntab = a ? t. n : x. n;
                euclidom *oldetab = a ? t. e : x. e;

                // copy the data form the previous tables
                int j;
                for (j = 0; a && (j < i); ++ j)
                {
                        x. n [j] = oldntab [j];
                        x. e [j] = oldetab [j];
                }
                for (j = i; j < len; ++ j)
                {
                        x. n [j] = oldntab [j + 1];
                        x. e [j] = oldetab [j + 1];
                }

                // release the old tables if necessary
                if (a)
                {
                        delete [] oldntab;
                        delete [] oldetab;
                }
        }

        return *this;
} /* chain<euclidom>::removepair */

template<class euclidom >
outputstream & chomp::homology::chain< euclidom >::show ( outputstream out,
const char *  label = NULL 
) const [inline]

Shows the chain to the output stream.

Uses a given label for indicating identifiers of elements in the chain.

Definition at line 1073 of file chains.h.

References chomp::homology::chain< euclidom >::allocated(), chomp::homology::chain< euclidom >::len, chomp::homology::chain< euclidom >::t, and chomp::homology::chain< euclidom >::x.

Referenced by chomp::homology::chain< euclidom >::show().

{
        if (len <= 0)
                out << "0";
        bool a = allocated ();
        const int *tntab = a ? t. n : x. n;
        const euclidom *tetab = a ? t. e : x. e;
        for (int i = 0; i < len; ++ i)
        {
                euclidom e = tetab [i];
                int n = tntab [i] + 1;

                if (e == 1)
                        out << (i ? " + " : "") <<
                                (label ? label : "") << n;
                else if (-e == 1)
                        out << (i ? " - " : "-") <<
                                (label ? label : "") << n;
                else
                        out << (i ? " + " : "") << e << " * " <<
                                (label ? label : "") << n;
        }
        return out;
} /* chain<euclidom>::show */

template<class euclidom >
std::ostream & chomp::homology::chain< euclidom >::show ( std::ostream &  out,
const char *  label = NULL 
) const [inline]

Shows the chain to the standard output stream.

Uses a given label for indicating identifiers of elements in the chain.

Definition at line 1100 of file chains.h.

References chomp::homology::chain< euclidom >::show().

{
        outputstream tout (out);
        show (tout, label);
        return out;
} /* chain<euclidom>::show */

template<class euclidom >
int chomp::homology::chain< euclidom >::size (  )  const [inline]

Returns the size of the chain, that is, the number of elements with non-zero coefficients.

Definition at line 332 of file chains.h.

References chomp::homology::chain< euclidom >::len.

Referenced by chomp::homology::chain< euclidom >::findbest().

{
        return len;
} /* chain<euclidom>::size */

template<class euclidom >
chain< euclidom > & chomp::homology::chain< euclidom >::swap ( chain< euclidom > &  other,
int  number = -1,
int  othernumber = -1,
chain< euclidom > *  table = NULL 
) [inline]

Swaps one chain with another.

If the chain is a row of a matrix, then its number, the number of the other row and the table of columns must be given for proper modification; if this is a column, its number and columns must be given

Definition at line 885 of file chains.h.

References chomp::homology::chain< euclidom >::allocated(), chomp::homology::chain< euclidom >::len, chomp::homology::swapelements(), chomp::homology::chain< euclidom >::swapnumbers(), chomp::homology::chain< euclidom >::t, and chomp::homology::chain< euclidom >::x.

{
        // check which chains where allocated
        bool a = allocated ();
        bool oa = other. allocated ();

        // swap the data of the chains
        if (a && oa)
        {
                swapelements (t. n, other. t. n);
                swapelements (t. e, other. t. e);
        }
        else if (!a && !oa)
        {
                // common variable for interations (required by MSVC++)
                int i;

                // swap the data in the common area of both chains
                for (i = 0; (i < len) && (i < other. len); ++ i)
                {
                        swapelements (x. n [i], other. x. n [i]);
                        swapelements (x. e [i], other. x. e [i]);
                }

                // copy the remaining portion of the data
                for (i = len; i < other. len; ++ i)
                {
                        x. n [i] = other. x. n [i];
                        x. e [i] = other. x. e [i];
                }
                for (i = other. len; i < len; ++ i)
                {
                        other. x. n [i] = x. n [i];
                        other. x. e [i] = x. e [i];
                }
        }
        else if (a) // && !oa
        {
                int *tempn = t. n;
                euclidom *tempe = t. e;
                for (int i = 0; i < other. len; ++ i)
                {
                        x. n [i] = other. x. n [i];
                        x. e [i] = other. x. e [i];
                }
                other. t. n = tempn;
                other. t. e = tempe;
        }
        else // if (oa) // && !a
        {
                int *tempn = other. t. n;
                euclidom *tempe = other. t. e;
                for (int i = 0; i < len; ++ i)
                {
                        other. x. n [i] = x. n [i];
                        other. x. e [i] = x. e [i];
                }
                t. n = tempn;
                t. e = tempe;
        }

        // swap the lengths of the chains (do not swap 'a' with 'oa')
        swapelements (len, other. len);

        if (!table)
                return *this;

        // change the numbers in every relevant entry of the table
        int *tntab = oa ? t. n : x. n;
        int *ontab = a ? other. t. n : other. x. n;
        int i = 0, j = 0;
        while ((i < len) || (j < other. len))
        {
                // determine which table entry should be modified
                int n;
                if (i >= len)
                        n = ontab [j ++];
                else if (j >= other. len)
                        n = tntab [i ++];
                else if (tntab [i] < ontab [j])
                        n = tntab [i ++];
                else if (ontab [j] < tntab [i])
                        n = ontab [j ++];
                else
                {
                        n = tntab [i ++];
                        ++ j;
                //      ++ i;
                //      ++ j;
                //      continue;
                }

                // swap numbers in that table entry
                table [n]. swapnumbers (othernumber, number);
        }

        return *this;
} /* chain<euclidom>::swap */

template<class euclidom >
chain< euclidom > & chomp::homology::chain< euclidom >::swapnumbers ( int  number1,
int  number2 
) [inline, private]

Swaps two numbers (identifiers) in the chain.

Definition at line 624 of file chains.h.

References chomp::homology::chain< euclidom >::allocated(), chomp::homology::chain< euclidom >::len, chomp::homology::swapelements(), chomp::homology::chain< euclidom >::t, and chomp::homology::chain< euclidom >::x.

Referenced by chomp::homology::chain< euclidom >::swap().

{
        // if the numbers are the same, do nothing
        if (number1 == number2)
                return *this;

        // force the first number be less than the second number
        if (number1 > number2)
        {
                int tempnumber = number1;
                number1 = number2;
                number2 = tempnumber;
        }

        // determine the true tables to be processed
        bool a = allocated ();
        int *tntab = a ? t. n : x. n;
        euclidom *tetab = a ? t. e : x. e;

        // find both numbers or the positions they should be at
        int i1 = 0, i2 = 0;
        while ((i1 < len) && (tntab [i1] < number1))
                ++ i1;
        while ((i2 < len) && (tntab [i2] < number2))
                ++ i2;

        // if the first number was found...
        if ((i1 < len) && (tntab [i1] == number1))
        {
                // if both numbers were found, exchange their coefficients
                if ((i2 < len) && (tntab [i2] == number2))
                        swapelements (tetab [i1], tetab [i2]);
                // if only the first was found, move it to the new position
                else
                {
                        euclidom temp = tetab [i1];
                        for (int i = i1 + 1; i < i2; ++ i)
                        {
                                tntab [i - 1] = tntab [i];
                                tetab [i - 1] = tetab [i];
                        }
                        tntab [i2 - 1] = number2;
                        tetab [i2 - 1] = temp;
                }
        }

        // otherwise if the second number only was found, move it to its pos.
        else if ((i2 < len) && (tntab [i2] == number2))
        {
                euclidom temp = tetab [i2];
                for (int i = i2; i > i1; -- i)
                {
                        tntab [i] = tntab [i - 1];
                        tetab [i] = tetab [i - 1];
                }
                tntab [i1] = number1;
                tetab [i1] = temp;
        }

        return *this;
} /* chain<euclidom>::swapnumbers */

template<class euclidom >
chain< euclidom > & chomp::homology::chain< euclidom >::take ( chain< euclidom > &  c  )  [inline]

Takes data from another chain. Destroys the other chain.

Definition at line 986 of file chains.h.

References chomp::homology::chain< euclidom >::allocated(), chomp::homology::chain< euclidom >::len, chomp::homology::chain< euclidom >::t, and chomp::homology::chain< euclidom >::x.

{
        // release the current tables if they were allocated
        if (allocated ())
        {
                delete [] t. n;
                delete [] t. e;
        }

        // if the other tables were allocated, take them
        if (c. allocated ())
        {
                t. n = c. t. n;
                t. e = c. t. e;
        }

        // otherwise copy the data from the internal other tables
        else
        {
                for (int i = 0; i < c. len; ++ i)
                {
                        x. n [i] = c. x. n [i];
                        x. e [i] = c. x. e [i];
                }
        }

        // copy the length and reset the other length
        len = c. len;
        c. len = 0;

        return *this;
} /* chain<euclidom>::take */


Member Data Documentation

union { ... } [private]

Elements of the list sorted according to the identifier.

If there are very few of them, they are kept in the space normally reserved for the addresses. Otherwise, an array is allocated in the memory.

template<class euclidom>
euclidom* chomp::homology::chain< euclidom >::e
template<class euclidom>
int chomp::homology::chain< euclidom >::len [private]
template<class euclidom>
int* chomp::homology::chain< euclidom >::n
struct { ... } chomp::homology::chain< euclidom >::t
struct { ... } chomp::homology::chain< euclidom >::x

The documentation for this class was generated from the following file: