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   template <typename T, typename T2, size_t C>
00032   inline bool operator==(const CArray2dProxy<T,C>& l, const CArray2dProxy<T2,C>& r)
00033   {
00034     return std::equal(l.a, l.a+C, r.a);
00035   }
00043   template <typename T, size_t Capacity1, size_t Capacity2>
00044   class CArray2d
00045   {
00046   public:
00048     T elements[Capacity1][Capacity2];
00049   public:
00050     typedef CArray2d<T,Capacity1,Capacity2> array_t;
00051     typedef CArray2dProxy<T,Capacity2> proxy_t;
00052     typedef CArray2dProxy<const T,Capacity2> const_proxy_t;
00053     
00054     const proxy_t operator[] (size_t i) 
00055     {
00056       assert(i < Capacity1);
00057       return proxy_t(elements[i]);
00058     }
00059     T& operator()(size_t i, size_t j)
00060     {
00061       assert(i < Capacity1);
00062       assert(j < Capacity2);
00063       return elements[i][j];
00064     }
00065     
00066     const const_proxy_t operator[] (size_t i) const
00067     {
00068       assert(i < Capacity1);
00069       return const_proxy_t(elements[i]);
00070     }
00071     
00072     void fill(T value=T()){
00073       for (size_t j=0; j<Capacity1; j++)
00074         std::fill(&elements[j][0], &elements[j][Capacity2], value);
00075     }
00076     const T& operator()(size_t i, size_t j) const
00077     {
00078       assert(i < Capacity1);
00079       assert(j < Capacity2);
00080       return elements[i][j];
00081     }
00082 
00083     static size_t capacity1() { return Capacity1; }
00084     static size_t capacity2() { return Capacity2; }
00085     static size_t size1() { return Capacity1; }
00086     static size_t size2() { return Capacity2; }
00087 
00088     const proxy_t operator[] (Player p)
00089     {
00090       return operator[](playerToIndex(p));
00091     }
00092     const const_proxy_t operator[] (Player p) const
00093     {
00094       return operator[](playerToIndex(p));
00095     }
00096 
00097     bool operator==(const CArray2d& other) const
00098     {
00099       return std::equal(&elements[0][0], &elements[0][0]+size1()*size2(),
00100                         &other.elements[0][0]);
00101     }
00102   };
00103   } // namespace osl
00104   using misc::CArray2d;
00105 } // namespace osl
00106 
00107 
00108 #endif /* _FIXED_CAPACITY_VECTOR_H */
00109 // ;;; Local Variables:
00110 // ;;; mode:c++
00111 // ;;; c-basic-offset:2
00112 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines