carray2d.h
Go to the documentation of this file.
00001 /* carray2d.h
00002  */
00003 #ifndef OSL_CARRAY2D_H
00004 #define OSL_CARRAY2D_H
00005 #include "osl/player.h"
00006 #include <algorithm>
00007 #include <cstddef>
00008 #include <cassert>
00009 
00010 namespace osl
00011 {
00012   namespace misc
00013   {
00014   template <typename T, size_t Capacity2>
00015   struct CArray2dProxy
00016   {
00017     T* a;
00018     explicit CArray2dProxy(T *ia) : a(ia)
00019     {
00020     }
00021     T& operator[](size_t j) const
00022     {
00023       assert(j < Capacity2);
00024       return a[j];
00025     }
00026     T& operator[] (Player p) const
00027     {
00028       return operator[](playerToIndex(p));
00029     }
00030   };
00031 
00039   template <typename T, size_t Capacity1, size_t Capacity2>
00040   class CArray2d
00041   {
00042   public:
00044     T elements[Capacity1][Capacity2];
00045   public:
00046     typedef CArray2d<T,Capacity1,Capacity2> array_t;
00047     typedef CArray2dProxy<T,Capacity2> proxy_t;
00048     typedef CArray2dProxy<const T,Capacity2> const_proxy_t;
00049     
00050     const proxy_t operator[] (size_t i) 
00051     {
00052       assert(i < Capacity1);
00053       return proxy_t(elements[i]);
00054     }
00055     T& operator()(size_t i, size_t j)
00056     {
00057       assert(i < Capacity1);
00058       assert(j < Capacity2);
00059       return elements[i][j];
00060     }
00061     
00062     const const_proxy_t operator[] (size_t i) const
00063     {
00064       assert(i < Capacity1);
00065       return const_proxy_t(elements[i]);
00066     }
00067     
00068     void fill(T value=T()){
00069       for (size_t j=0; j<Capacity1; j++)
00070         std::fill(&elements[j][0], &elements[j][Capacity2], value);
00071     }
00072     const T& operator()(size_t i, size_t j) const
00073     {
00074       assert(i < Capacity1);
00075       assert(j < Capacity2);
00076       return elements[i][j];
00077     }
00078 
00079     static size_t capacity1() { return Capacity1; }
00080     static size_t capacity2() { return Capacity2; }
00081     static size_t size1() { return Capacity1; }
00082     static size_t size2() { return Capacity2; }
00083 
00084     const proxy_t operator[] (Player p)
00085     {
00086       return operator[](playerToIndex(p));
00087     }
00088     const const_proxy_t operator[] (Player p) const
00089     {
00090       return operator[](playerToIndex(p));
00091     }
00092 
00093     bool operator==(const CArray2d& other) const
00094     {
00095       return std::equal(&elements[0][0], &elements[0][0]+size1()*size2(),
00096                         &other.elements[0][0]);
00097     }
00098   };
00099   } // namespace osl
00100   using misc::CArray2d;
00101 } // namespace osl
00102 
00103 
00104 #endif /* _FIXED_CAPACITY_VECTOR_H */
00105 // ;;; Local Variables:
00106 // ;;; mode:c++
00107 // ;;; c-basic-offset:2
00108 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines