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.
darray.h@0:537664265ba6, 2014-01-27 (annotated)
- Committer:
- Fuzball
- Date:
- Mon Jan 27 08:20:47 2014 +0000
- Revision:
- 0:537664265ba6
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Fuzball | 0:537664265ba6 | 1 | // Author: Chris Yan |
Fuzball | 0:537664265ba6 | 2 | // Date: March 9, 2011 |
Fuzball | 0:537664265ba6 | 3 | // File: darray.h |
Fuzball | 0:537664265ba6 | 4 | // Desc: Header for the darray (dynamicalrific array) class. |
Fuzball | 0:537664265ba6 | 5 | ///////////////////////////////////////////////////////////////////////// |
Fuzball | 0:537664265ba6 | 6 | |
Fuzball | 0:537664265ba6 | 7 | #ifndef _darray |
Fuzball | 0:537664265ba6 | 8 | #define _darray |
Fuzball | 0:537664265ba6 | 9 | |
Fuzball | 0:537664265ba6 | 10 | #include <iostream> |
Fuzball | 0:537664265ba6 | 11 | |
Fuzball | 0:537664265ba6 | 12 | template<typename T> |
Fuzball | 0:537664265ba6 | 13 | class darray |
Fuzball | 0:537664265ba6 | 14 | { |
Fuzball | 0:537664265ba6 | 15 | public: |
Fuzball | 0:537664265ba6 | 16 | // Constructors |
Fuzball | 0:537664265ba6 | 17 | |
Fuzball | 0:537664265ba6 | 18 | // Pre: none |
Fuzball | 0:537664265ba6 | 19 | // Post: default constructor is called and default darray results |
Fuzball | 0:537664265ba6 | 20 | darray(); |
Fuzball | 0:537664265ba6 | 21 | |
Fuzball | 0:537664265ba6 | 22 | // Pre: darray passed to the copy constructor must have = operator |
Fuzball | 0:537664265ba6 | 23 | // Post: darray passed is copied into the new darray |
Fuzball | 0:537664265ba6 | 24 | darray( const darray &); |
Fuzball | 0:537664265ba6 | 25 | |
Fuzball | 0:537664265ba6 | 26 | // Destructor |
Fuzball | 0:537664265ba6 | 27 | |
Fuzball | 0:537664265ba6 | 28 | // Pre: computer is turned on |
Fuzball | 0:537664265ba6 | 29 | // Post: darray is tilded and destroyed |
Fuzball | 0:537664265ba6 | 30 | ~darray(); |
Fuzball | 0:537664265ba6 | 31 | |
Fuzball | 0:537664265ba6 | 32 | // Constant members |
Fuzball | 0:537664265ba6 | 33 | |
Fuzball | 0:537664265ba6 | 34 | // Pre: none |
Fuzball | 0:537664265ba6 | 35 | // Post: length of darray is returned |
Fuzball | 0:537664265ba6 | 36 | unsigned size() const { return length; } |
Fuzball | 0:537664265ba6 | 37 | |
Fuzball | 0:537664265ba6 | 38 | // Pre: none |
Fuzball | 0:537664265ba6 | 39 | // Post: total capacity of darray is returned |
Fuzball | 0:537664265ba6 | 40 | unsigned capacity() const { return cap; } |
Fuzball | 0:537664265ba6 | 41 | |
Fuzball | 0:537664265ba6 | 42 | // Pre: index accessed must exist or cassert will fail |
Fuzball | 0:537664265ba6 | 43 | // Post: a reference to the value at that index is returned |
Fuzball | 0:537664265ba6 | 44 | const T& operator [] ( unsigned int ) const; |
Fuzball | 0:537664265ba6 | 45 | |
Fuzball | 0:537664265ba6 | 46 | // Modifiers |
Fuzball | 0:537664265ba6 | 47 | |
Fuzball | 0:537664265ba6 | 48 | // Pre: darray must have a length > 0, otherwise cassert will fail |
Fuzball | 0:537664265ba6 | 49 | // Post: length of darray is decreased by one |
Fuzball | 0:537664265ba6 | 50 | void pop_back(); |
Fuzball | 0:537664265ba6 | 51 | |
Fuzball | 0:537664265ba6 | 52 | // Pre: Value passed must be a valid type and have = operator assigned |
Fuzball | 0:537664265ba6 | 53 | // Post: darray is expanded and the type, T, is added to the end of the darray |
Fuzball | 0:537664265ba6 | 54 | void push_back( const T & ); |
Fuzball | 0:537664265ba6 | 55 | |
Fuzball | 0:537664265ba6 | 56 | // Pre: index accessed must exist or cassert will fail |
Fuzball | 0:537664265ba6 | 57 | // Post: a non-constant reference to the value at that index is returned |
Fuzball | 0:537664265ba6 | 58 | T& operator [] ( unsigned int ); |
Fuzball | 0:537664265ba6 | 59 | |
Fuzball | 0:537664265ba6 | 60 | // Pre: none |
Fuzball | 0:537664265ba6 | 61 | // Post: amount of type * unsigned is reserved in memory |
Fuzball | 0:537664265ba6 | 62 | void reserve(unsigned); |
Fuzball | 0:537664265ba6 | 63 | |
Fuzball | 0:537664265ba6 | 64 | // Pre: darray must have more than one item or memory-out-of-bounds thingy will go nuts on you |
Fuzball | 0:537664265ba6 | 65 | // Post: list is sorted... ... ...backwards |
Fuzball | 0:537664265ba6 | 66 | void sort( darray& ); |
Fuzball | 0:537664265ba6 | 67 | |
Fuzball | 0:537664265ba6 | 68 | // Pre: Parameters one and two should most functionally not equal each other... |
Fuzball | 0:537664265ba6 | 69 | // Post: Values at param 1 and param 2 are exchanged and proper locations in the darray are exchanged as well |
Fuzball | 0:537664265ba6 | 70 | void swap( T&, T& ); |
Fuzball | 0:537664265ba6 | 71 | |
Fuzball | 0:537664265ba6 | 72 | // Operators |
Fuzball | 0:537664265ba6 | 73 | |
Fuzball | 0:537664265ba6 | 74 | // Pre: both sides must be valid darrays? |
Fuzball | 0:537664265ba6 | 75 | // Post: darray1 now ='s darray2. Huzzah! |
Fuzball | 0:537664265ba6 | 76 | darray& operator = (const darray&); |
Fuzball | 0:537664265ba6 | 77 | |
Fuzball | 0:537664265ba6 | 78 | // Friends |
Fuzball | 0:537664265ba6 | 79 | // darrays have no friends, instead, they hold work-relationships with various types |
Fuzball | 0:537664265ba6 | 80 | |
Fuzball | 0:537664265ba6 | 81 | private: |
Fuzball | 0:537664265ba6 | 82 | |
Fuzball | 0:537664265ba6 | 83 | // length and capacity of the darray |
Fuzball | 0:537664265ba6 | 84 | unsigned length, cap; |
Fuzball | 0:537664265ba6 | 85 | // Pointer to the array of types, T. |
Fuzball | 0:537664265ba6 | 86 | T* arr; |
Fuzball | 0:537664265ba6 | 87 | |
Fuzball | 0:537664265ba6 | 88 | }; |
Fuzball | 0:537664265ba6 | 89 | |
Fuzball | 0:537664265ba6 | 90 | // template include |
Fuzball | 0:537664265ba6 | 91 | #include "darray.cpp" |
Fuzball | 0:537664265ba6 | 92 | |
Fuzball | 0:537664265ba6 | 93 | // #endif |
Fuzball | 0:537664265ba6 | 94 | #endif |