Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include "managedarray.h"
- template <typename T>
- class FastList : public ManagedArray<T> {
- private:
- int count; // how many elements are actually used
- public:
- inline FastList() : ManagedArray<T>(),count(0){}
- inline ~FastList(){}
- inline int size() { return count; }
- // re-allocates data to be 1 larger, and adds the given value to the end
- void addToEnd(const T value) {
- if (count >= ManagedArray<T>::size()) {
- if(size() == 0) {
- ManagedArray<T>::setSize(2);
- } else {
- ManagedArray<T>::setSize(size() + 10); // TODO talk about why *2 is bad
- }
- }
- ManagedArray<T>::set(count++, value); // add 1 to count AFTER the function call is done.
- }
- // inserts a value into the given index by reallocating to a bigger array and
- // copying every value value into the new array with the new value in its index
- void insert(const int index, const T value) {
- addToEnd(value);
- for(int i = count-1; i > index; --i) {
- ManagedArray<T>::set(i, ManagedArray<T>::get(i-1));
- }
- ManagedArray<T>::set(index, value);
- }
- // removes the value at the given index by reallocating to a smaller array
- // and copying every value *except* the removed value into the new array
- void removeAt(const int index) {
- for(int i = index; i < count; ++i) {
- ManagedArray<T>::set(i, ManagedArray<T>::get(i+1));
- }
- count--;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement