An mbed-friendly version of dynamic arrays Main difference in the code from my original version is the inability to use template files with the mbed compiler.
Revision 0:537664265ba6, committed 2014-01-27
- Comitter:
- Fuzball
- Date:
- Mon Jan 27 08:20:47 2014 +0000
- Commit message:
- Initial commit
Changed in this revision
darray.cpp | Show annotated file Show diff for this revision Revisions of this file |
darray.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 537664265ba6 darray.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/darray.cpp Mon Jan 27 08:20:47 2014 +0000 @@ -0,0 +1,144 @@ +// Author: Chris Yan +// Date: March 9, 2011 +// File: darray.cpp +// Desc: Implementation for the darray class. +///////////////////////////////////////////////////////////////////////// +#ifndef __WHYNOMBEDTEMPLATES +#define __WHYNOMBEDTEMPLATES +#include "darray.h" +#include <cassert> + +template<typename T> +darray<T>::darray() +{ + cap = 0; + length = 0; + arr = 0; +} + +template<typename T> +darray<T>::darray( const darray& d):cap(d.cap),length(d.length),arr(0) +{ + arr = new T[cap]; + for( int i = 0; i < length; ++i) + arr[i] = d.arr[i]; +} + +template<typename T> +darray<T>::~darray() +{ + delete[] this->arr; + cap = length = 0; + arr = 0; +} + +template<typename T> +const T& darray<T>::operator [] (unsigned int idx) const +{ + assert( idx < length ); + return arr[idx]; +} + +template<typename T> +T& darray<T>::operator [] (unsigned int idx) +{ + assert( idx < length ); + return arr[idx]; +} + +template<typename T> +void darray<T>::pop_back() +{ + assert( length > 0 ); + --length; +} + +template<typename T> +void darray<T>::push_back( const T& entry ) +{ + if( length < cap ) + arr[length++] = entry; + else + { + T* temp = new T[length + 5]; + + for( int i = 0; i < length; ++i ) + temp[i] = arr[i]; + + temp[length] = entry; + delete[] arr; + arr = temp; + cap += 5; + ++length; + std::cout << "nAddress of Arr: " << &arr << '\n'; + } +} + +template<typename T> +darray<T>& darray<T>::operator = (const darray &d) +{ + if(this != &d) + { + //delete[] this; + this = d; + } + return *this; +} + + +template<typename T> +void darray<T>::reserve(unsigned newcap) +{ + if(cap >= newcap) + return; + + T* temp = new T[newcap]; + for( int i = 0; i < length; ++i ) + temp[i] = arr[i]; + + delete[] arr; + arr = temp; + cap = newcap; +} + +//operator += () + +/* +darray operator + (const darray& d1, const darray& d2) +{ + darray temp; + temp += d1; + temp += d2; + return temp; +} +*/ + +/* +std::ostream& operator << (std::ostream& out, const darray& d) +{ + out << '('; + int i; + for( i = 0; i < (d.length-1); ++i ) + out << d.arr[i] << ' '; + return out << d.arr[i] << ')'; +} +*/ + +template <typename T> +void darray<T>::sort(darray<T>& elems) +{ + for(int top=elems.size()-1; top>0; --top) + for(int k=0; k<top; ++k) + if(elems[k] > elems[k+1]) + swap(elems[k], elems[k+1]); +} + +template <typename T> +void darray<T>::swap(T& elem1, T& elem2) +{ + T temp = elem1; + elem1 = elem2; + elem2 = temp; +} + +#endif \ No newline at end of file
diff -r 000000000000 -r 537664265ba6 darray.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/darray.h Mon Jan 27 08:20:47 2014 +0000 @@ -0,0 +1,94 @@ +// Author: Chris Yan +// Date: March 9, 2011 +// File: darray.h +// Desc: Header for the darray (dynamicalrific array) class. +///////////////////////////////////////////////////////////////////////// + +#ifndef _darray +#define _darray + +#include <iostream> + +template<typename T> +class darray +{ +public: + // Constructors + + // Pre: none + // Post: default constructor is called and default darray results + darray(); + + // Pre: darray passed to the copy constructor must have = operator + // Post: darray passed is copied into the new darray + darray( const darray &); + + // Destructor + + // Pre: computer is turned on + // Post: darray is tilded and destroyed + ~darray(); + + // Constant members + + // Pre: none + // Post: length of darray is returned + unsigned size() const { return length; } + + // Pre: none + // Post: total capacity of darray is returned + unsigned capacity() const { return cap; } + + // Pre: index accessed must exist or cassert will fail + // Post: a reference to the value at that index is returned + const T& operator [] ( unsigned int ) const; + + // Modifiers + + // Pre: darray must have a length > 0, otherwise cassert will fail + // Post: length of darray is decreased by one + void pop_back(); + + // Pre: Value passed must be a valid type and have = operator assigned + // Post: darray is expanded and the type, T, is added to the end of the darray + void push_back( const T & ); + + // Pre: index accessed must exist or cassert will fail + // Post: a non-constant reference to the value at that index is returned + T& operator [] ( unsigned int ); + + // Pre: none + // Post: amount of type * unsigned is reserved in memory + void reserve(unsigned); + + // Pre: darray must have more than one item or memory-out-of-bounds thingy will go nuts on you + // Post: list is sorted... ... ...backwards + void sort( darray& ); + + // Pre: Parameters one and two should most functionally not equal each other... + // Post: Values at param 1 and param 2 are exchanged and proper locations in the darray are exchanged as well + void swap( T&, T& ); + + // Operators + + // Pre: both sides must be valid darrays? + // Post: darray1 now ='s darray2. Huzzah! + darray& operator = (const darray&); + + // Friends + // darrays have no friends, instead, they hold work-relationships with various types + +private: + + // length and capacity of the darray + unsigned length, cap; + // Pointer to the array of types, T. + T* arr; + +}; + +// template include +#include "darray.cpp" + +// #endif +#endif \ No newline at end of file