- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# D:/Programmation/Sources/Tensor2/Utils/include/frost_utils_array.h

Go to the documentation of this file.

    00001 // Warning : If you need to use this file, include frost_utils_types.h
    00002 namespace Frost
    00003 {
    00004     /// Base type : array
    00005     /** This class is an improved version of C arrays.
    00006     *   It comes with a few features that make it simpler
    00007     *   to use.
    00008     */
    00009     template<class T, default_uint N = 0>
    00010     class s_array
    00011     {
    00012     public :
    00013
    00014         typedef T*                      iterator;
    00015         typedef const T*                const_iterator;
    00016         typedef s_range<iterator>       range;
    00017         typedef s_range<const_iterator> const_range;
    00018
    00019         s_array()
    00020         {
    00021         }
    00022
    00023         s_array(const T& mInitValue)
    00024         {
    00025             for (default_uint i = 0; i < N; ++i)
    00026             {
    00027                 lArray_[i] = mInitValue;
    00028             }
    00029         }
    00030
    00031         template<class U, class C>
    00032         s_array(const s_ctnr_t<U, C>& mContainer)
    00033         {
    00034             default_uint i;
    00035             for (i = 0; i < N; ++i)
    00036             {
    00037                 if (i >= mContainer.GetSize().Get())
    00038                     break;
    00039
    00040                 lArray_[i] = mContainer[i];
    00041             }
    00042             for (; i < N; ++i)
    00043             {
    00044                 lArray_[i] = T();
    00045             }
    00046         }
    00047
    00048         #ifdef CPP_0X
    00049             /// Initializer list constructor.
    00050             /** \param mList Brace enclosed element list
    00051             *   \note This constructor uses a C++0x feature.<br>
    00052             *         It allows : s_array<s_int, 4> v = {1, 2, 3, 4};<br>
    00053             *         If the initializer_list is too long, only the N first
    00054             *         values are stored into the s_array.<br>
    00055             *         If it is too short, the s_array is filled with T's
    00056             *         default constructor.
    00057             */
    00058             s_array(std::initializer_list<T> mList)
    00059             {
    00060                 default_uint i = 0;
    00061                 for (const T* p = mList.begin(); (p != mList.end()) && (i < N); ++p, ++i)
    00062                     lArray_[i] = *p;
    00063                 for (; i < N; ++i)
    00064                 {
    00065                     lArray_[i] = T();
    00066                 }
    00067             }
    00068         #endif
    00069
    00070         s_array(const s_array& mValue)
    00071         {
    00072             for (default_uint i = 0; i < N; ++i)
    00073                 lArray_[i] = mValue.lArray_[i];
    00074         }
    00075
    00076         /// Returns the underlying C array.
    00077         /** \return The underlying C array
    00078         */
    00079         T* GetClassicArray()
    00080         {
    00081             return lArray_;
    00082         }
    00083
    00084         void Set(const T& mValue)
    00085         {
    00086             for (default_uint i = 0; i < N; ++i)
    00087             {
    00088                 lArray_[i] = mValue;
    00089             }
    00090         }
    00091
    00092         void Set(const T (&lArray)[N])
    00093         {
    00094             for (default_uint i = 0; i < N; ++i)
    00095             {
    00096                 lArray_[i] = lArray[i];
    00097             }
    00098         }
    00099
    00100         void Set(const T* lArray)
    00101         {
    00102             for (default_uint i = 0; i < N; ++i)
    00103             {
    00104                 lArray_[i] = lArray[i];
    00105             }
    00106         }
    00107
    00108         void Set(const s_nullptr& pPtr)
    00109         {
    00110             for (default_uint i = 0; i < N; ++i)
    00111             {
    00112                 lArray_[i] = pPtr;
    00113             }
    00114         }
    00115
    00116         s_uint_t<default_uint> GetSize() const
    00117         {
    00118             return N;
    00119         }
    00120
    00121         const T& Get(const s_uint_t<default_uint>& uiIndex) const
    00122         {
    00123             return lArray_[uiIndex.Get()];
    00124         }
    00125
    00126         iterator Begin()
    00127         {
    00128             return lArray_;
    00129         }
    00130
    00131         const_iterator Begin() const
    00132         {
    00133             return lArray_;
    00134         }
    00135
    00136         iterator End()
    00137         {
    00138             return lArray_ + N;
    00139         }
    00140
    00141         const_iterator End() const
    00142         {
    00143             return lArray_ + N;
    00144         }
    00145
    00146         iterator begin()
    00147         {
    00148             return lArray_;
    00149         }
    00150
    00151         const_iterator begin() const
    00152         {
    00153             return lArray_;
    00154         }
    00155
    00156         iterator end()
    00157         {
    00158             return lArray_ + N;
    00159         }
    00160
    00161         const_iterator end() const
    00162         {
    00163             return lArray_ + N;
    00164         }
    00165
    00166         T& operator [] (const s_uint_t<default_uint>& uiIndex)
    00167         {
    00168             return lArray_[uiIndex.Get()];
    00169         }
    00170
    00171         const T& operator [] (const s_uint_t<default_uint>& uiIndex) const
    00172         {
    00173             return lArray_[uiIndex.Get()];
    00174         }
    00175
    00176         T& operator [] (const default_uint& uiIndex)
    00177         {
    00178             return lArray_[uiIndex];
    00179         }
    00180
    00181         const T& operator [] (const default_uint& uiIndex) const
    00182         {
    00183             return lArray_[uiIndex];
    00184         }
    00185
    00186         s_bool operator == (const s_array& mValue) const
    00187         {
    00188             for (default_uint i = 0; i < N; ++i)
    00189             {
    00190                 if (lArray_[i] != mValue[i])
    00191                     return false;
    00192             }
    00193
    00194             return true;
    00195         }
    00196
    00197         s_bool operator != (const s_array& mValue) const
    00198         {
    00199             for (default_uint i = 0; i < N; ++i)
    00200             {
    00201                 if (lArray_[i] != mValue[i])
    00202                     return true;
    00203             }
    00204
    00205             return false;
    00206         }
    00207
    00208         s_array& operator = (const s_array& mValue)
    00209         {
    00210             for (default_uint i = 0; i < N; ++i)
    00211                 lArray_[i] = mValue.lArray_[i];
    00212
    00213             return *this;
    00214         }
    00215
    00216         template<class U, class C>
    00217         s_array& operator = (const s_ctnr_t<U, C>& mContainer)
    00218         {
    00219             default_uint i;
    00220             for (i = 0; (i < N) && (i < mContainer.GetSize().Get()); ++i)
    00221             {
    00222                 lArray_[i] = mContainer[i];
    00223             }
    00224             for (; i < N; ++i)
    00225             {
    00226                 lArray_[i] = T();
    00227             }
    00228
    00229             return *this;
    00230         }
    00231
    00232     private :
    00233
    00234         T lArray_[N];
    00235     };
    00236
    00237     /** \cond NOT_REMOVE_FROM_DOC
    00238     */
    00239     template<class T, default_uint N> class StringConverter< string_element, s_array<T, N> >
    00240     {
    00241     public :
    00242
    00243         typedef s_str::string string;
    00244
    00245         static string Convert(const s_array<T, N>& mArray)
    00246         {
    00247             string sTemp = "(";
    00248             for (default_uint i = 0; i < N; ++i)
    00249             {
    00250                 if (i != 0)
    00251                     sTemp += ", " + StringConverter<string_element, T>::Convert(mArray[i]);
    00252                 else
    00253                     sTemp += StringConverter<string_element, T>::Convert(mArray[i]);
    00254             }
    00255             sTemp += ")";
    00256
    00257             return sTemp;
    00258         }
    00259     };
    00260     /** \endcond
    00261     */
    00262 }
    00263