carray2d.h Source File

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
carray2d.h
Go to the documentation of this file.
1 /* carray2d.h
2 */
3 #ifndef OSL_CARRAY2D_H
4 #define OSL_CARRAY2D_H
5 #include "osl/player.h"
6 #include <algorithm>
7 #include <cstddef>
8 #include <cassert>
9
10 namespace osl
11 {
12 namespace misc
13 {
14 template <typename T, size_t Capacity2>
15 struct CArray2dProxy
16 {
17 T* a;
18 explicit CArray2dProxy(T *ia) : a(ia)
19 {
20 }
21 T& operator[](size_t j) const
22 {
23 assert(j < Capacity2);
24 return a[j];
25 }
26 T& operator[] (Player p) const
27 {
28 return operator[](playerToIndex(p));
29 }
30 };
31 template <typename T, typename T2, size_t C>
32 inline bool operator==(const CArray2dProxy<T,C>& l, const CArray2dProxy<T2,C>& r)
33 {
34 return std::equal(l.a, l.a+C, r.a);
35 }
43 template <typename T, size_t Capacity1, size_t Capacity2>
44 class CArray2d
45 {
46 public:
48 T elements[Capacity1][Capacity2];
49 public:
50 typedef CArray2d<T,Capacity1,Capacity2> array_t;
51 typedef CArray2dProxy<T,Capacity2> proxy_t;
52 typedef CArray2dProxy<const T,Capacity2> const_proxy_t;
53
54 const proxy_t operator[] (size_t i)
55 {
56 assert(i < Capacity1);
57 return proxy_t(elements[i]);
58 }
59 T& operator()(size_t i, size_t j)
60 {
61 assert(i < Capacity1);
62 assert(j < Capacity2);
63 return elements[i][j];
64 }
65
66 const const_proxy_t operator[] (size_t i) const
67 {
68 assert(i < Capacity1);
69 return const_proxy_t(elements[i]);
70 }
71
72 void fill(T value=T()){
73 for (size_t j=0; j<Capacity1; j++)
74 std::fill(&elements[j][0], &elements[j][Capacity2], value);
75 }
76 const T& operator()(size_t i, size_t j) const
77 {
78 assert(i < Capacity1);
79 assert(j < Capacity2);
80 return elements[i][j];
81 }
82
83 static size_t capacity1() { return Capacity1; }
84 static size_t capacity2() { return Capacity2; }
85 static size_t size1() { return Capacity1; }
86 static size_t size2() { return Capacity2; }
87
88 const proxy_t operator[] (Player p)
89 {
90 return operator[](playerToIndex(p));
91 }
92 const const_proxy_t operator[] (Player p) const
93 {
94 return operator[](playerToIndex(p));
95 }
96
97 bool operator==(const CArray2d& other) const
98 {
99 return std::equal(&elements[0][0], &elements[0][0]+size1()*size2(),
100 &other.elements[0][0]);
101 }
102 };
103 } // namespace osl
104 using misc::CArray2d;
105 } // namespace osl
106
107
108 #endif /* _FIXED_CAPACITY_VECTOR_H */
109 // ;;; Local Variables:
110 // ;;; mode:c++
111 // ;;; c-basic-offset:2
112 // ;;; End: