/* Copyright (C) 2005-2006 Feeling Software Inc. MIT License: http://www.opensource.org/licenses/mit-license.php */ /** @file FMArray.h The file contains the vector class, which improves on the standard C++ vector class. */ /** A dynamically-sized array. Built on top of the standard C++ vector class, this class improves on the interface by adding useful extra functionality, such as constructor that takes in a constant-sized array, comparison which a constant-sized array, erase and find functions that take in a value, etc. @ingroup FMath */ #ifndef _FM_ARRAY_H_ #define _FM_ARRAY_H_ template class FCOLLADA_EXPORT vector : public std::vector { public: /** Default constructor. */ vector() : std::vector() {} /** Constructor: Builds a dynamically-sized array of the wanted size. @param size The wanted size of the array. */ vector(size_t size) : std::vector(size) {} /** Constructor: Builds a dynamically-sized array of the wanted size. @param size The wanted size of the array @param defaultValue The default value to use for all the entries of the array. */ vector(size_t size, const T& defaultValue) : std::vector(size, defaultValue) {} /** Copy constructor. @param copy The dynamically-sized array to copy the values from. */ vector(const std::vector& copy) : std::vector(copy) {} /** Constructor: Builds a dynamically-sized array from a constant-sized array. @param values A constant-sized array of floating-point values. @param count The size of the constant-sized array. */ vector(const T* values, size_t count) : std::vector() { resize(count); memcpy(&at(0), values, count * sizeof(T)); } /** Retrieves an iterator for a given element. @param value The value, contained within the list, to search for. @return An iterator to this element. The end() iterator is returned if the value is not found. */ template inline iterator find(const _T& value) { return std::find(begin(), end(), value); } template inline const_iterator find(const _T& value) const { return std::find(begin(), end(), value); } /**< See above. */ /** Removes the value at the given position within the list. @param it The list position for the value to remove. */ inline void erase(iterator it) { std::vector::erase(it); } /** Removes the value at the given position within the list. @param it The list position for the value to remove. */ inline void erase(iterator first, iterator last) { std::vector::erase(first, last); } /** Removes a value contained within the list, once. @param value The value, contained within the list, to erase from it. @return Whether the value was found and erased from the list. */ inline bool erase(const T& value) { iterator it = find(value); if (it != end()) { erase(it); return true; } return false; } /** Removes an indexed value contained within the list. @param index The index of the value to erase. */ inline void erase(size_t index) { erase(begin() + index); } /** Releases a value contained within a list. Use this function only if your vector contains pointers and you are certain that there is no duplicate pointers within the list. @param value The value, contained within the list, to release. @return Whether the value was found and released. */ inline bool release(const T& value) { iterator it = find(value); if (it != end()) { erase(it); delete value; return true; } return false; } /** Retrieves whether the list contains a given value. @return Whether the list contains a given value. */ inline bool contains(const T& value) const { const_iterator it = find(value); return it != end(); } }; /** Returns whether a dynamically-sized array is equivalent to a constant-sized array. @param dl A dynamically-sized array. @param cl A constant-sized array. @param count The size of the constant-sized array. @return Whether the two arrays are equivalent. */ template inline bool IsEquivalent(const vector& dl, const T* cl, size_t count) { if (dl.size() != count) return false; bool equivalent = true; for (size_t i = 0; i < count && equivalent; ++i) equivalent = IsEquivalent(dl.at(i), cl[i]); return equivalent; } #endif // _FM_ARRAY_H_