[964] | 1 | /*
|
---|
| 2 | Copyright (C) 2005-2006 Feeling Software Inc.
|
---|
| 3 | MIT License: http://www.opensource.org/licenses/mit-license.php
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /**
|
---|
| 7 | @file FMArray.h
|
---|
| 8 | The file contains the vector class, which improves on the standard C++ vector class.
|
---|
| 9 | */
|
---|
| 10 |
|
---|
| 11 | /**
|
---|
| 12 | A dynamically-sized array.
|
---|
| 13 | Built on top of the standard C++ vector class, this class improves on the interface
|
---|
| 14 | by adding useful extra functionality, such as constructor that takes in a constant-sized array,
|
---|
| 15 | comparison which a constant-sized array, erase and find functions that take in a value, etc.
|
---|
| 16 |
|
---|
| 17 | @ingroup FMath
|
---|
| 18 | */
|
---|
| 19 |
|
---|
| 20 | #ifndef _FM_ARRAY_H_
|
---|
| 21 | #define _FM_ARRAY_H_
|
---|
| 22 |
|
---|
| 23 | template <class T>
|
---|
| 24 | class FCOLLADA_EXPORT vector : public std::vector<T>
|
---|
| 25 | {
|
---|
| 26 | public:
|
---|
| 27 | /** Default constructor. */
|
---|
| 28 | vector() : std::vector<T>() {}
|
---|
| 29 |
|
---|
| 30 | /** Constructor: Builds a dynamically-sized array of the wanted size.
|
---|
| 31 | @param size The wanted size of the array. */
|
---|
| 32 | vector(size_t size) : std::vector<T>(size) {}
|
---|
| 33 |
|
---|
| 34 | /** Constructor: Builds a dynamically-sized array of the wanted size.
|
---|
| 35 | @param size The wanted size of the array
|
---|
| 36 | @param defaultValue The default value to use for all the entries of the array. */
|
---|
| 37 | vector(size_t size, const T& defaultValue) : std::vector<T>(size, defaultValue) {}
|
---|
| 38 |
|
---|
| 39 | /** Copy constructor.
|
---|
| 40 | @param copy The dynamically-sized array to copy the values from. */
|
---|
| 41 | vector(const std::vector& copy) : std::vector<T>(copy) {}
|
---|
| 42 |
|
---|
| 43 | /** Constructor: Builds a dynamically-sized array from a constant-sized array.
|
---|
| 44 | @param values A constant-sized array of floating-point values.
|
---|
| 45 | @param count The size of the constant-sized array. */
|
---|
| 46 | vector(const T* values, size_t count) : std::vector<T>()
|
---|
| 47 | {
|
---|
| 48 | resize(count);
|
---|
| 49 | memcpy(&at(0), values, count * sizeof(T));
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | /** Retrieves an iterator for a given element.
|
---|
| 53 | @param value The value, contained within the list, to search for.
|
---|
| 54 | @return An iterator to this element. The end() iterator
|
---|
| 55 | is returned if the value is not found. */
|
---|
| 56 | template <typename _T>
|
---|
| 57 | inline iterator find(const _T& value) { return std::find(begin(), end(), value); }
|
---|
| 58 | template <typename _T>
|
---|
| 59 | inline const_iterator find(const _T& value) const { return std::find(begin(), end(), value); } /**< See above. */
|
---|
| 60 |
|
---|
| 61 | /** Removes the value at the given position within the list.
|
---|
| 62 | @param it The list position for the value to remove. */
|
---|
| 63 | inline void erase(iterator it) { std::vector<T>::erase(it); }
|
---|
| 64 |
|
---|
| 65 | /** Removes the value at the given position within the list.
|
---|
| 66 | @param it The list position for the value to remove. */
|
---|
| 67 | inline void erase(iterator first, iterator last) { std::vector<T>::erase(first, last); }
|
---|
| 68 |
|
---|
| 69 | /** Removes a value contained within the list, once.
|
---|
| 70 | @param value The value, contained within the list, to erase from it.
|
---|
| 71 | @return Whether the value was found and erased from the list. */
|
---|
| 72 | inline bool erase(const T& value) { iterator it = find(value); if (it != end()) { erase(it); return true; } return false; }
|
---|
| 73 |
|
---|
| 74 | /** Removes an indexed value contained within the list.
|
---|
| 75 | @param index The index of the value to erase. */
|
---|
| 76 | inline void erase(size_t index) { erase(begin() + index); }
|
---|
| 77 |
|
---|
| 78 | /** Releases a value contained within a list.
|
---|
| 79 | Use this function only if your vector contains pointers
|
---|
| 80 | and you are certain that there is no duplicate pointers within the list.
|
---|
| 81 | @param value The value, contained within the list, to release.
|
---|
| 82 | @return Whether the value was found and released. */
|
---|
| 83 | inline bool release(const T& value) { iterator it = find(value); if (it != end()) { erase(it); delete value; return true; } return false; }
|
---|
| 84 |
|
---|
| 85 | /** Retrieves whether the list contains a given value.
|
---|
| 86 | @return Whether the list contains a given value. */
|
---|
| 87 | inline bool contains(const T& value) const { const_iterator it = find(value); return it != end(); }
|
---|
| 88 | };
|
---|
| 89 |
|
---|
| 90 | /** Returns whether a dynamically-sized array is equivalent to a constant-sized array.
|
---|
| 91 | @param dl A dynamically-sized array.
|
---|
| 92 | @param cl A constant-sized array.
|
---|
| 93 | @param count The size of the constant-sized array.
|
---|
| 94 | @return Whether the two arrays are equivalent. */
|
---|
| 95 | template <typename T>
|
---|
| 96 | inline bool IsEquivalent(const vector<T>& dl, const T* cl, size_t count)
|
---|
| 97 | {
|
---|
| 98 | if (dl.size() != count) return false;
|
---|
| 99 | bool equivalent = true;
|
---|
| 100 | for (size_t i = 0; i < count && equivalent; ++i) equivalent = IsEquivalent(dl.at(i), cl[i]);
|
---|
| 101 | return equivalent;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | #endif // _FM_ARRAY_H_ |
---|