📄 vector.h
字号:
/*************************************************************************** * * Oxford Semiconductor Proprietary and Confidential Information * * Copyright: Oxford Semiconductor Ltd, 2003 * * Description: * * // {{{ * $Log: Vector.h,v $ * Revision 1.7 2005/01/26 12:09:35 ford * resize was able to call operator= on unconstructed memory: this could include * side effects, notably on referance counted objects. Now fixed to use correct * placement new operation. * * Revision 1.6 2004/12/21 17:28:04 ford * fixed build warning. * * Revision 1.5 2004/12/16 16:03:48 ford * corrected vector range insert. * * Revision 1.4 2004/11/10 12:35:53 ford * fixes build issues. * * Revision 1.3 2004/11/08 17:00:43 ford * corrected typo error on range erase. * * Revision 1.2 2004/11/04 12:54:59 ford * unicode changes. * * Revision 1.1 2004/06/28 14:52:17 clarke * Moved from Misc * * Revision 1.39 2004/06/23 11:00:06 dennett * Commented out some asserts because video player expects to run out of memmory. * * Revision 1.38 2004/05/13 10:50:21 clarke * Made empty() const * * Revision 1.37 2004/05/12 16:17:36 ford * modified to include more media information. * * Revision 1.36 2004/04/23 11:20:27 dennett * Fixed erase. * * Revision 1.35 2004/04/22 17:03:48 ford * nulled buffer_ in ctor to allow for non-allocating construction. * * Revision 1.34 2004/04/22 13:39:01 ford * no longer alloc for zero sized vectors. * * Revision 1.33 2004/04/19 13:13:16 dennett * REmoved some pokeouts. * * Revision 1.32 2004/04/19 08:50:00 ford * Added asserts and memcpy implementation * * Revision 1.31 2004/04/19 07:21:20 dennett * Now include necessary header file. * * Revision 1.30 2004/04/16 11:44:51 dennett * Fixed it. * * Revision 1.29 2004/04/16 11:38:02 dennett * Fixed it. * * Revision 1.28 2004/04/16 11:10:26 ford * added front, back and end. * * Revision 1.27 2004/04/16 10:50:09 ford * added include for memcpy. * * Revision 1.26 2004/04/16 10:47:22 ford * added range based erase. * * Revision 1.25 2004/03/16 19:48:41 ford * fixed resize for ctorless Vector. * * Revision 1.24 2004/03/12 17:30:59 ford * allowed loki custom POD trait to be defined, and recognised by Vector. Included * ASSERT when memory allocation fialure in Vector, and refleected allocation * in max_size. added resize to the interface. * * Revision 1.23 2004/03/11 11:54:24 andy * added operator== * * Revision 1.22 2004/03/01 11:43:32 ford * added allocation assert, resize, and maxsize allocation feedback. * * Revision 1.21 2003/11/12 16:09:31 andy * added pop_back() * * Revision 1.20 2003/10/29 16:01:11 ford * added path to Loki for Vector, added specialisation for primitives * * Revision 1.19 2003/10/17 13:53:44 dennett * Stopped shadowing warning. * * Revision 1.18 2003/09/23 08:00:16 paul * *** empty log message *** * // }}} * ***************************************************************************/#ifndef VECTOR_H#define VECTOR_H#include <iterator>#include <string.h>#include "Loki/TypeTraits.h"#include "Utils/assert.h"#include "Utils/helpers.h"#include "SysLib/xdbg.h"#include "Allocators.h"namespace oxsemi{ // define a template that determinds if type 'U' needs a ctor call when used // in a container... template < typename U > struct NeedsCtor // {{{ { static const bool result = !( Loki::TypeTraits<U>::isPointer || Loki::TypeTraits<U>::isFundamental || Loki::TypeTraits<U>::isPOD ); }; // }}} // Vector takes two template parameters - the first is the contianed type, and // the second is a static const boolean that the implementation is specialised // on. The specialisation is for bUseCtor == false, and is implemented such that // the ctor is never explicitly called on contained elements. This parameter // has been defaulted to an expression derived from the type traits of the // first parameter 'T', so that for primitve types and pointers, the no ctor // specialisation is automaticaally picked. For custom POD types, this // specialisation can also be used, either by explicitly providing the second // parameter as false when defining the Vector, or by using Loki's 'IsCustomUnsignedInt' // template or similar to include it in the automatic set. template < typename T, typename Allocator = allocator::Default<T>, bool bUseCtor = NeedsCtor<T>::result > class Vector // {{{ { public: typedef T value_type; typedef T* iterator; typedef const T* const_iterator; typedef T& reference; typedef const T& const_reference; typedef std::size_t size_type; typedef std::ptrdiff_t difference_type; typedef std::reverse_iterator<iterator> reverse_iterator; typedef std::reverse_iterator<const_iterator> const_reverse_iterator; /** * @ctor * creates a lightweight vector style object with space reserved for * maxSize elements. * @param maxSize is a size_type that describes the number of objects * that we wish storage to be reserved for. This will be reflected by * max_size(), and this should be checked against the requested size * *BEFORE THE OBJECT IS USED**. An assert is also included to trap * memory allocation failure in debug builds. */ explicit Vector(size_type maxSize); /** * @ctor * creates a lightweight vector style object with space reserved for * the larger of maxSize and other.max_size() elements. The body of * the construction in the event of successsful allocation will then * duplicate the data of 'other' in the new object. * @param other is the object from which data will be copied when the * vector is established. * @param maxSize is a size_type that describes the number of objects * that we wish storage to be reserved for. If the max_size() of 'other' * is greater, then this size will be requested instead. The result will * be reflected by max_size(), and this should be checked against the * requested size and/or other.max_size() * *BEFORE THE OBJECT IS USED**. * An assert is also included to trap memory allocation failure in debug * builds. */ Vector(const Vector<T, Allocator, bUseCtor>& other, size_type maxSize = 0); Vector<T, Allocator, bUseCtor>& operator=(const Vector<T, Allocator, bUseCtor>& other); ~Vector(); reference operator[](size_type index); const_reference operator[](size_type index) const; reference at(size_type index); const_reference at(size_type index) const; reference front(); const_reference front() const; reference back(); const_reference back() const; iterator begin(); const_iterator begin() const; void clear(); bool empty() const; iterator end(); const_iterator end() const; iterator erase( iterator itr ); iterator erase( iterator first, iterator last ); /** * expand - Although this implementation does not support automatic * resizing or reallocation, the interface is extended beyond that of * standard STL with this method that allows the maximum storage size * to be increased. This is an expensive operation, and involves the * use of the copy constructor, and Swap. Accordingly, in the event of * an allocation failure, the object will remain untouched, and a debug * ASSERT will be raised. * @param maxSize is a size_type that describes the number of objects * that we wish storage to be reserved for. If the max_size() of 'other' * is greater, then this size will be requested instead. The result will * be reflected by max_size(), and this should be checked against the * requested size and/or other.max_size() **BEFORE THE OBJECT IS USED**. * An assert is also included to trap memory allocation failure in debug * builds. */ void expand(size_type maxSize); /** * resize is a method that allows the 'size()' of the object to be altered. * It achieves this in two ways. If the size is greater than size_, the * elements upto size are constructed, and size is set accordingly. If * the newSize is less than size_, the elemnts between newSize and size_ are * destroyed, and the size_ is trimmed back. THIS OPERATION DOES NOT * RANGE CHECK MAX_SIZE - it is required that the user observes max_size. * @param newSize is a size_type that specifies the new size_ of the * object. */ void resize( size_type newSize ); /** * resize is a method that allows the 'size()' of the object to be altered. * It achieves this in two ways. If the size is greater than size_, the * elements upto size are constructed, and size is set accordingly. If * the newSize is less than size_, the elemnts between newSize and size_ are * destroyed, and the size_ is trimmed back. THIS OPERATION DOES NOT * RANGE CHECK MAX_SIZE - it is required that the user observes max_size. * @param newSize is a size_type that specifies the new size_ of the * object. * @param fill_value is a value_type that describes the value which should * be copied into the slots when size_ is increased. */ void resize( size_type newSize, value_type fill_value ); iterator insert( iterator position, const_reference value ); iterator insert( iterator position, const_iterator first, const_iterator last ); //bool isComplete() const { return buffer_; } size_type max_size() const; void push_back(const_reference value); void pop_back(); reverse_iterator rbegin(); const_reverse_iterator rbegin() const; reverse_iterator rend(); const_reverse_iterator rend() const; size_type size() const; private: size_type maxSize_; size_type size_; T* buffer_; void Swap( Vector<T, Allocator, bUseCtor>& other ); }; // }}} // Specialisation for types that do not require construction ( pointers, primitives, POD ) template < typename T, typename Allocator > class Vector<T, Allocator, false> // {{{ { public: typedef T value_type; typedef T* iterator; typedef T const * const_iterator; typedef T& reference; typedef T const & const_reference; typedef std::size_t size_type; typedef std::ptrdiff_t difference_type; typedef std::reverse_iterator<iterator> reverse_iterator; typedef std::reverse_iterator<const_iterator> const_reverse_iterator; /** * @ctor * creates a lightweight vector style object with space reserved for * maxSize elements. * @param maxSize is a size_type that describes the number of objects * that we wish storage to be reserved for. This will be reflected by * max_size(), and this should be checked against the requested size * *BEFORE THE OBJECT IS USED**. An assert is also included to trap * memory allocation failure in debug builds. */ explicit Vector(size_type maxSize); /** * @ctor * creates a lightweight vector style object with space reserved for * the larger of maxSize and other.max_size() elements. The body of * the construction in the event of successsful allocation will then * duplicate the data of 'other' in the new object. * @param other is the object from which data will be copied when the * vector is established. * @param maxSize is a size_type that describes the number of objects * that we wish storage to be reserved for. If the max_size() of 'other' * is greater, then this size will be requested instead. The result will * be reflected by max_size(), and this should be checked against the * requested size and/or other.max_size() * *BEFORE THE OBJECT IS USED**. * An assert is also included to trap memory allocation failure in debug * builds. */ Vector(const Vector<T, Allocator, false>& other, size_type maxSize = 0); Vector<T, Allocator, false>& operator=(const Vector<T, Allocator, false>& other); ~Vector(); reference operator[](size_type index); const_reference operator[](size_type index) const; reference at(size_type index); const_reference at(size_type index) const; reference front(); const_reference front() const; reference back(); const_reference back() const; iterator begin(); const_iterator begin() const;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -