#include "classes.h" // Header for vector class //-------------------------------------------------------------------------------------------------------------------- // FlexVec::~FlexVec is used to free up any information left in the list. //-------------------------------------------------------------------------------------------------------------------- FlexVec::~FlexVec() { vlist.clear(); } // FlexVec::~FlexVec //-------------------------------------------------------------------------------------------------------------------- // FlexVec::operator[ ] returns the integer value associated with the given subscript. If no // such value exists in the list, a new pair is inserted whose integer value is 0. //-------------------------------------------------------------------------------------------------------------------- int &FlexVec::operator[ ]( int subscript ) { Pair newpair(subscript, 0); // Item which may be inserted into list. bool retval = false; // Found a match? list::iterator i; // Index into the list. for ( i = vlist.begin(); i != vlist.end(); ++ i ) { // If list was empty, loop terminates immediately. if ( (*i).getKey() == subscript ) { // Check if have found right element. retval = true; break; } // if } // for if ( ! retval ) { // If false, didn't find element to match, so insert empty one. vlist.push_back(newpair); i = --(vlist.end()); // Make sure we know where the new element is. } // if return (*i).getValue(); } // FlexVec::operator[ ] //-------------------------------------------------------------------------------------------------------------------- // FlexVec::Status returns the status of the vector so far. //-------------------------------------------------------------------------------------------------------------------- void FlexVec::Status( int &ne, int &minI, int &maxI ) { list::iterator i; // Index into the list. ne = 0; // Initialize the number of entries to 0. // If list was empty, i loop terminates immediately. for ( i = vlist.begin(); i != vlist.end(); ++ i ) { if ( (*i).getValue() != 0 ) { // Check if have found non-zero element if ( ne == 0 ) { // If first non-zero element, initialize indices. minI = (*i).getKey(); maxI = minI; } else { if ( (*i).getKey() < minI ) minI = (*i).getKey(); if ( (*i).getKey() > maxI ) maxI = (*i).getKey(); } // if ne++; } else { // else delete element. if ( i == --(vlist.end()) ) { // Check first if is last element in list. vlist.pop_back(); break; } else vlist.erase(i); } // if } // for } // FlexVec::Status