Main Page | Namespace List | Compound List | File List | Compound Members | File Members

kvector< MYTYPE > Class Template Reference

The kvector class. More...

List of all members.

Public Member Functions

 kvector (void)
 default constructor

 kvector (int)
 set initial vector size to size

 ~kvector (void)
void add (MYTYPE x)
void show (void)
 print the current data elements in the vector

int get_length (void)
MYTYPE & access (int index)
 access

MYTYPE & operator[] (int index)
 overloaded array access operator

iterator begin ()
 returns initial iterator for the kvector

iterator end ()
 returns end iterator for the kvector


Protected Attributes

MYTYPE * vptr
 pointer to current vector array

int count
 current number of used slots in vector

int length
 current number of slots in vector


Friends

ostream & operator<< (ostream &os, kvector< MYTYPE > &k)
 overloaded output operator


Detailed Description

template<class MYTYPE>
class kvector< MYTYPE >

The kvector class.

We are making a vector class. A vector is like an array but the length can vary. We will use templates so that it can work with an array of any type and make it longer as needed. This is similar to the way the Vector class from the STL works, but much simpler. I have also added some other methods and operators to better support the Vector data type.

Definition at line 31 of file kvector.cpp.


Constructor & Destructor Documentation

template<class MYTYPE>
kvector< MYTYPE >::kvector void   ) 
 

default constructor

Definition at line 151 of file kvector.cpp.

References kvector< MYTYPE >::count, DEFSIZE, kvector< MYTYPE >::length, and kvector< MYTYPE >::vptr.

00152 {
00153         length = DEFSIZE;
00154         count = 0;
00155         if( (vptr = new MYTYPE[length]) == NULL) {
00156                 cerr << "No memory for vector of length " << length << endl;
00157         }
00158 } // constructor

template<class MYTYPE>
kvector< MYTYPE >::kvector int   ) 
 

set initial vector size to size

Definition at line 162 of file kvector.cpp.

References kvector< MYTYPE >::count, kvector< MYTYPE >::length, and kvector< MYTYPE >::vptr.

00163 {
00164         length = size;
00165         count = 0;
00166         if( (vptr = new MYTYPE[length]) == NULL) {
00167                 cerr << "No memory for vector of length " << length << endl;
00168         }
00169 } // 1 arg constructor

template<class MYTYPE>
kvector< MYTYPE >::~kvector void   )  [inline]
 

Definition at line 35 of file kvector.cpp.

References kvector< MYTYPE >::vptr.

00035 { if(vptr != NULL) delete [] vptr;}


Member Function Documentation

template<class MYTYPE>
MYTYPE& kvector< MYTYPE >::access int  index  )  [inline]
 

access

the access function can be used on either side of the = since it returns a reference to the array member being accessed.

Definition at line 45 of file kvector.cpp.

References kvector< MYTYPE >::length, and kvector< MYTYPE >::vptr.

Referenced by vector_float(), and vector_int().

00046                 {
00047                         if((index >= 0) && (index < length))
00048                                 return vptr[index];
00049                         else
00050                                 throw "Bad index";
00051                 }

template<class MYTYPE>
void kvector< MYTYPE >::add MYTYPE  x  ) 
 

adds the integer x to the next slot in the vector if the vector is full, it allocates a new array, copies the values, and adjusts the pointers

count is the number of used slots length is the number of slots if the count and length are equal, then the array is full and we need to allocate a new bigger array. We make the array bigger in chunks to avoid having to run this code too often. after the new array is created, we copy the old array elements to the new array. Changing the internal kvector array pointer (vptr) to point to the new array completes the work. We can delete the old array now. length is changed to the new array size. Whether or not the array was extended, we add the parameter x to the array.

Definition at line 190 of file kvector.cpp.

References kvector< MYTYPE >::count, INCR, kvector< MYTYPE >::length, and kvector< MYTYPE >::vptr.

Referenced by vector_float(), and vector_int().

00191 {
00192         /*! count is the number of used slots
00193             length is the number of slots
00194                 if the count and length are equal, then the array
00195                 is full and we need to allocate a new bigger array.
00196                 We make the array bigger in chunks to avoid having to run this code
00197                 too often.
00198                 after the new array is created, we copy the old array elements 
00199                 to the new array.
00200                 Changing the internal kvector array pointer (vptr) to
00201                 point to the new array completes the work.
00202                 We can delete the old array now.
00203                 length is changed to the new array size.
00204                 Whether or not the array was extended, we add the
00205                 parameter x to the array.
00206         */
00207         if( count == length) {  // make new array
00208                 cout << "making new array" << endl;
00209                 MYTYPE * tptr = new MYTYPE[length + INCR];
00210                 if(tptr == NULL) { // this is fatal
00211                         cerr << "No memory for new array" << endl;
00212                         throw ("no memory");
00213                 }
00214                 // copy the old array elements to the new array.
00215                 for(int i=0; i < count; i++) tptr[i] = vptr[i];
00216 //              delete vptr;  // we don't need this anymore
00217                 vptr = tptr;  // make the new array the current array
00218                 length += INCR;
00219         }
00220         vptr[count++] = x;
00221 } // add

template<class MYTYPE>
iterator kvector< MYTYPE >::begin  )  [inline]
 

returns initial iterator for the kvector

this creates a new iterator object and initializes it to the beginning of the underlying array (vptr). It then returns the iterator. Note that x is a pointer because new returns a pointer, but we have to return the actual iterator object because that is what is expected of begin().

Definition at line 125 of file kvector.cpp.

References kvector< MYTYPE >::vptr.

Referenced by vector_float(), and vector_int().

00125                                  {
00126                                 iterator  *x = new iterator(vptr);
00127                                 return *x;
00128                 }

template<class MYTYPE>
iterator kvector< MYTYPE >::end  )  [inline]
 

returns end iterator for the kvector

This also creates a new iterator and points it at the memory just after the end of the array. it does this by getting the address of the last cell in the array &vptr[length-1] is the address of the last element in the array Then we add 1 to get the address of the next place after the end of the array.

Definition at line 138 of file kvector.cpp.

References kvector< MYTYPE >::length, and kvector< MYTYPE >::vptr.

Referenced by vector_float(), and vector_int().

00138                                { 
00139                                 iterator  *x = new iterator( (MYTYPE *)(&vptr[length-1]) + 1);
00140                                 return *x;
00141                 }

template<class MYTYPE>
int kvector< MYTYPE >::get_length void   )  [inline]
 

Definition at line 39 of file kvector.cpp.

References kvector< MYTYPE >::length.

Referenced by vector_float(), and vector_int().

00039 { return length;}

template<class MYTYPE>
MYTYPE& kvector< MYTYPE >::operator[] int  index  )  [inline]
 

overloaded array access operator

makes our vector look like an array since we can access the elements using the [] operator. It also checks that we are not trying to access something we shouldn't. Note that it is very similar to the access() method

Definition at line 59 of file kvector.cpp.

References kvector< MYTYPE >::length, and kvector< MYTYPE >::vptr.

00060                 {
00061                         if((index >= 0) && (index < length))
00062                                 return vptr[index];
00063                         else
00064                                 throw "Bad index";
00065                 }

template<class MYTYPE>
void kvector< MYTYPE >::show void   ) 
 

print the current data elements in the vector

Definition at line 174 of file kvector.cpp.

References kvector< MYTYPE >::count, and kvector< MYTYPE >::vptr.

Referenced by vector_float(), and vector_int().

00175 {
00176         cout << "vector = (";
00177         for(int i = 0; i < count; i++) {
00178                 cout << vptr[i];
00179                 if(i+1 != count) cout << ","; // don't print trailing comma
00180         }
00181         cout << ")" << endl;
00182 } // show


Friends And Related Function Documentation

template<class MYTYPE>
ostream& operator<< ostream &  os,
kvector< MYTYPE > &  k
[friend]
 

overloaded output operator

prints the contents in a line. Assumes the base type (MYTYPE) has a << operator defined.

Definition at line 71 of file kvector.cpp.

00072                 {
00073                         for(int i=0; i < k.get_length(); i++) 
00074                                         os << k[i] << " ";
00075                         os << endl;
00076                         return os;
00077                 }


Member Data Documentation

template<class MYTYPE>
int kvector< MYTYPE >::count [protected]
 

current number of used slots in vector

Definition at line 145 of file kvector.cpp.

Referenced by kvector< MYTYPE >::add(), kvector< MYTYPE >::kvector(), and kvector< MYTYPE >::show().

template<class MYTYPE>
int kvector< MYTYPE >::length [protected]
 

current number of slots in vector

Definition at line 146 of file kvector.cpp.

Referenced by kvector< MYTYPE >::access(), kvector< MYTYPE >::add(), kvector< MYTYPE >::end(), kvector< MYTYPE >::get_length(), kvector< MYTYPE >::kvector(), and kvector< MYTYPE >::operator[]().

template<class MYTYPE>
MYTYPE* kvector< MYTYPE >::vptr [protected]
 

pointer to current vector array

Definition at line 144 of file kvector.cpp.

Referenced by kvector< MYTYPE >::access(), kvector< MYTYPE >::add(), kvector< MYTYPE >::begin(), kvector< MYTYPE >::end(), kvector< MYTYPE >::kvector(), kvector< MYTYPE >::operator[](), kvector< MYTYPE >::show(), and kvector< MYTYPE >::~kvector().


The documentation for this class was generated from the following file:
Generated on Wed Sep 24 15:58:42 2003 for kvector by doxygen 1.3.3