Array.h
Go to the documentation of this file.
1 // cwchessboard -- A C++ chessboard tool set
2 //
3 //! @file Array.h This file contains the definition of class Array.
4 //
5 // Copyright (C) 2008, by
6 //
7 // Carlo Wood, Run on IRC <carlo@alinoe.com>
8 // RSA-1024 0x624ACAD5 1997-01-26 Sign & Encrypt
9 // Fingerprint16 = 32 EC A7 B6 AC DB 65 A6 F6 F6 55 DD 1C DC FF 61
10 //
11 // This program is free software: you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation, either version 2 of the License, or
14 // (at your option) any later version.
15 //
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program. If not, see <http://www.gnu.org/licenses/>.
23 
24 #ifndef ARRAY_H
25 #define ARRAY_H
26 
27 #ifndef USE_PCH
28 #endif
29 
30 #include "Color.h"
31 #include "Index.h"
32 
33 #define DEBUG_ARRAY_RANGE_CHECK 0
34 
35 #if DEBUG_ARRAY_RANGE_CHECK
36 #include <cassert>
37 #endif
38 
39 namespace cwchess {
40 
41 template<typename T>
42 struct ArrayCode {
43  T M_array[16];
44 
45  T& operator[](Code const& code)
46  {
47 #if DEBUG_ARRAY_RANGE_CHECK
48  assert(code() < 16);
49 #endif
50  return M_array[code()];
51  }
52  T const& operator[](Code const& code) const
53  {
54 #if DEBUG_ARRAY_RANGE_CHECK
55  assert(code() < 16);
56 #endif
57  return M_array[code()];
58  }
59 
60  T& operator[](Color const& color)
61  {
62 #if DEBUG_ARRAY_RANGE_CHECK
63  assert(color() == 0 || color() == 8);
64 #endif
65  return M_array[color()];
66  }
67  T const& operator[](Color const& color) const
68  {
69 #if DEBUG_ARRAY_RANGE_CHECK
70  assert(color() == 0 || color() == 8);
71 #endif
72  return M_array[color()];
73  }
74 };
75 
76 template<typename T>
77 struct ArrayColor {
78  T M_array[2];
79 
80  T& operator[](Code const& code)
81  {
82 #if DEBUG_ARRAY_RANGE_CHECK
83  assert(code() < 16);
84 #endif
85  return M_array[code() >> 3];
86  }
87  T const& operator[](Code const& code) const
88  {
89 #if DEBUG_ARRAY_RANGE_CHECK
90  assert(code() < 16);
91 #endif
92  return M_array[code() >> 3];
93  }
94 
95  T& operator[](Color const& color)
96  {
97 #if DEBUG_ARRAY_RANGE_CHECK
98  assert(color.index() < 2);
99 #endif
100  return M_array[color.index()];
101  }
102  T const& operator[](Color const& color) const
103  {
104 #if DEBUG_ARRAY_RANGE_CHECK
105  assert(color.index() < 2);
106 #endif
107  return M_array[color.index()];
108  }
109 };
110 
111 template<typename T>
112 struct ArrayIndex {
113  T M_array[64];
114 
115  T& operator[](Index const& index)
116  {
117 #if DEBUG_ARRAY_RANGE_CHECK
118  assert(index() < 64);
119 #endif
120  return M_array[index()];
121  }
122  T const& operator[](Index const& index) const
123  {
124 #if DEBUG_ARRAY_RANGE_CHECK
125  assert(index() < 64);
126 #endif
127  return M_array[index()];
128  }
129 };
130 
131 } // namespace cwchess
132 
133 #endif // ARRAY_H
A namespace for all chess related objects that are not related to the GUI.
Definition: Array.h:39
This file contains the declaration of class Index.
uint8_t index(void) const
Return a number that can be used as array index.
Definition: Color.h:136
The index of a chess square.
Definition: Index.h:211
A chess piece type including color.
Definition: Code.h:92
This file contains the declaration of class Color.
A color (black or white).
Definition: Color.h:67

Copyright © 2006 - 2010 Carlo Wood.  All rights reserved.