A basic optimization request...
Hey, I'm still moderatly new to C++, I've played with it for several months only.
I'm trying to write some c++ code to record chess pieces in memory, and I will be using a 64-bit model, I did some reading, and it became apparent that I could represent the 64 squares of a chess board in a 64 bit register, such that one register could hold the location of all white pawns, or another all black queens.
I liked this, it fit in well with the way I pictured my 64-bit processor working.
I wrote a simple file, it is basically just to hold the data, I'll extend it later, but I have some questions, mostly about memory management and optimization.
First - I'll list my code...
BoardData.h
Code:
class BoardData
{
struct BitBoard
{
unsigned long long WhitePawns;
unsigned long long WhiteKnights;
unsigned long long WhiteBishops;
unsigned long long WhiteRooks;
unsigned long long WhiteQueens;
unsigned long long WhiteKing;
unsigned long long WhitePieces;
unsigned long long BlackPawns;
unsigned long long BlackKnights;
unsigned long long BlackBishops;
unsigned long long BlackRooks;
unsigned long long BlackQueens;
unsigned long long BlackKing;
unsigned long long BlackPieces;
unsigned char CastlingFlags;
/* The CastlingFlags are four bits 0-3 that contain true/false information about the availability of castling.
* bit 0 = White King Side Rook (White Short Castle)
* bit 1 = White Queen Side Rook (White Long Castle)
* bit 2 = Black King Side Rook (Black Short Castle)
* bit 3 = Black Queen Side Rook (Black Long Castle)
*/
unsigned char EnPassantPhantomPawn;
/* A value stored in bits 0-6 represents a square of the board (0-63) assuming that 0 is the White Queen Side
* Rook, going left to right and then down the board.
* bit 7 is a true/false flag indicating the pressence of the en'Passant Phantom Pawn, if this bit is false,
* bits 0-6 are unused.
*/
};
public:
BoardData() { Clear(); };
~BoardData() { Destroy(); }
void Clear();
void Destroy();
void* GetHandle();
private:
BitBoard thisInstance;
};
and BoardData.cpp
Code:
#include "BoardData.h"
void BoardData::Clear()
{
thisInstance.WhitePawns=thisInstance.BlackPawns=0ULL;
thisInstance.WhiteKnights=thisInstance.BlackKnights=0ULL;
thisInstance.WhiteBishops=thisInstance.BlackBishops=0ULL;
thisInstance.WhiteRooks=thisInstance.BlackRooks=0ULL;
thisInstance.WhiteQueens=thisInstance.BlackQueens=0ULL;
thisInstance.WhiteKing=thisInstance.BlackKing=0ULL;
thisInstance.WhitePieces=thisInstance.BlackPieces=0ULL;
thisInstance.CastlingFlags=15;
thisInstance.EnPassantPhantomPawn=0;
return;
}
void BoardData::Destroy()
{
delete GetHandle();
return;
}
void* BoardData::GetHandle()
{
return &thisInstance;
}
My hope is that this code can go into memory with a minimum of fuss, while I do not plan to be caching a lot of these structures, I would imagine that during the recursion process I do take a few board thumbnails as I go.
My model is speed priority, memory secondary. As I just said, memory is still of a high profile concern.
What would be better, calling this as a static library, a dynamic dll or just inserting it as a class in my main program? I'll elaborate on a few things, load time is unimportant as a speed consideration, speed is only required for improving iterations.
Can anyone just looking at the above perhaps suggest a better way of handling this data?
Finally, (and slightly off-topic) I'm using Visual Studio 2008 Professional, I made a 64bit profile for the code, I'm sure there is an easy way to remove the default Win32 profile, (guess I'm being slightly dumb asking here, as I will solve this before I get a reply) how do I remove that reference to the Win32 profile...
P.S: Perhaps I lack confidence, but I'm going to ask anyway, does anyone see any apparent faults with my code? I've been more mucking around with C#, and void* and delete commands are not something I've delved into much.
EDIT: I'd like to add that for storing the board I intend to use 32 bytes to hold the physical component + a byte for castle flags, and a byte for the en'Passant Phantom Pawn, this storage will be used in transposition tables. The above description is purely for accelerated processing, an expanded form.
Another query I have with the void*, will I be able to access the data in the struct using that void*, like say I wanted to extract the flags for castling, would I need to redeclare the struct in the main code block?
For example, say I went with the idea of making this a DLL, my guess is that I would need to make the struct extern (as well as all the public functions I suppose...)
2nd Edit: I've totally rearranged my code into a factory, looking back at the above now that I've ventured on a little I see my whole approach was possibly way out of whack.
With that struct, when it comes to delete, do I just use delete to destory it, or should I use the array variant of delete?