array.h

Go to the documentation of this file.
00001 /*
00002 Copyright (C) 2001-2006, William Joseph.
00003 All Rights Reserved.
00004 
00005 This file is part of GtkRadiant.
00006 
00007 GtkRadiant is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2 of the License, or
00010 (at your option) any later version.
00011 
00012 GtkRadiant is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with GtkRadiant; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 */
00021 
00022 #if !defined(INCLUDED_CONTAINER_ARRAY_H)
00023 #define INCLUDED_CONTAINER_ARRAY_H
00024 
00025 #include <cstddef>
00026 #include <algorithm>
00027 
00028 #include "memory/allocator.h"
00029 
00038 template < typename Element, typename Allocator = DefaultAllocator<Element> >
00039 class Array : public Allocator {
00040     std::size_t m_size;
00041     Element* m_data;
00042 
00043     Element* construct(std::size_t size) {
00044         return New<Element, Allocator>(*this).vector(size);
00045     }
00046     template<typename T1>
00047     Element* construct(std::size_t size, const T1& value) {
00048         return New<Element, Allocator>(*this).vector(size, value);
00049     }
00050     void destroy(Element* data, std::size_t size) {
00051         Delete<Element, Allocator>(*this).vector(data, size);
00052     }
00053 
00054 public:
00055     typedef Element value_type;
00056     typedef value_type* iterator;
00057     typedef const value_type* const_iterator;
00058 
00059     Array()
00060             : m_size(0), m_data(0) {
00061     }
00062     Array(std::size_t size)
00063             : m_size(size), m_data(construct(size)) {
00064     }
00065     template<typename T1>
00066     Array(std::size_t size, const T1& value)
00067             : m_size(size), m_data(construct(size, value)) {
00068     }
00069     Array(const Array& other)
00070             : Allocator(other), m_size(other.size()), m_data(construct(m_size)) {
00071         std::copy(other.begin(), other.end(), begin());
00072     }
00073     template<typename Iterator>
00074     Array(Iterator start, Iterator finish)
00075             : m_size(std::distance(start, finish)), m_data(construct(m_size)) {
00076         std::copy(start, finish, begin());
00077     }
00078     ~Array() {
00079         destroy(m_data, m_size);
00080     }
00081 
00082     Array& operator=(const Array& other) {
00083         if (other.size() == size()) {
00084             std::copy(other.begin(), other.end(), begin());
00085         } else {
00086             Array temp(other);
00087             temp.swap(*this);
00088         }
00089         return *this;
00090     }
00091 
00092     void swap(Array& other) {
00093         std::swap(m_size, other.m_size);
00094         std::swap(m_data, other.m_data);
00095     }
00096 
00097     iterator begin() {
00098         return m_data;
00099     }
00100     const_iterator begin() const {
00101         return m_data;
00102     }
00103     iterator end() {
00104         return m_data + m_size;
00105     }
00106     const_iterator end() const {
00107         return m_data + m_size;
00108     }
00109 
00110     value_type& operator[](std::size_t index) {
00111 #if defined(DEBUG)
00112         ASSERT_MESSAGE(index < size(), "array index out of bounds");
00113 #endif
00114         return m_data[index];
00115     }
00116     const value_type& operator[](std::size_t index) const {
00117 #if defined(DEBUG)
00118         ASSERT_MESSAGE(index < size(), "array index out of bounds");
00119 #endif
00120         return m_data[index];
00121     }
00122     value_type* data() {
00123         return m_data;
00124     }
00125     const value_type* data() const {
00126         return m_data;
00127     }
00128     std::size_t size() const {
00129         return m_size;
00130     }
00131     bool empty() const {
00132         return m_size == 0;
00133     }
00134 
00135     void resize(std::size_t count) {
00136         if (count != size()) {
00137             Array temp(count);
00138             temp.swap(*this);
00139         }
00140     }
00141     void resize(std::size_t count, const value_type& value) {
00142         if (count != size()) {
00143             Array temp(count, value);
00144             temp.swap(*this);
00145         }
00146     }
00147 };
00148 
00149 namespace std {
00152 template<typename Element, typename Allocator>
00153 inline void swap(Array<Element, Allocator>& self, Array<Element, Allocator>& other) {
00154     self.swap(other);
00155 }
00156 }
00157 
00158 #endif

Generated by  doxygen 1.6.2