Cinnamon  1.0
chess engine
namespaces.cpp
Go to the documentation of this file.
00001 #include "namespaces.h"
00002 using namespace _bits;
00003 namespace _memory {
00004 
00005 #ifdef _WIN32
00006 
00007 void* _mmap(string fileName) {
00008     void* blob;
00009     HANDLE hfile= CreateFile( fileName.c_str(),FILE_SHARE_READ,0x00000001,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
00010     if( hfile == INVALID_HANDLE_VALUE )
00011         return NULL;
00012     HANDLE map_handle = CreateFileMapping( hfile, NULL, PAGE_READONLY , 0, 0, 0);
00013     if( !map_handle) {
00014         CloseHandle(hfile);
00015         return NULL;
00016     }
00017     blob = (void*) MapViewOfFile( map_handle,  FILE_MAP_READ, 0, 0, 0 );
00018     CloseHandle(hfile);
00019     CloseHandle(map_handle);
00020     return blob;
00021 }
00022 
00023 void _munmap(void *blob,int fileSize) {
00024     UnmapViewOfFile(blob);
00025 }
00026 
00027 #else
00028 
00029 void* _mmap(string fileName) {
00030     void* blob;
00031     FILE* bookBlob = fopen(fileName.c_str(), "rb");
00032     if (!bookBlob)
00033         return NULL;
00034     blob =(void*)mmap(0, _file::fileSize(fileName), PROT_READ, MAP_PRIVATE, fileno(bookBlob), 0);
00035     fclose(bookBlob);
00036     return blob;
00037 }
00038 
00039 void _munmap(void *blob,int fileSize) {
00040     munmap(blob,fileSize);
00041 }
00042 
00043 #endif
00044 
00045 }
00046 
00047 namespace _time {
00048 
00049 int diffTime(struct timeb t1,struct timeb t2) {
00050     return 1000 * (t1.time - t2.time) + t1.millitm - t2.millitm;
00051 }
00052 
00053 string getLocalTime() {
00054     struct tm *current;
00055     time_t now;
00056     time(&now);
00057     current = localtime(&now);
00058     char tt[30];
00059     sprintf(tt,"%i-%i-%i %i:%i:%i", current->tm_year+1900,current->tm_mon,current->tm_mday, current->tm_hour, current->tm_min, current->tm_sec);
00060     return string(tt);
00061 }
00062 }
00063 
00064 namespace _string {
00065 void trimRight(string& str) {
00066     string::size_type pos = str.find_last_not_of( " " );
00067     str.erase( pos + 1 );
00068 }
00069 
00070 void replace(string& f,char c1,char c2) {
00071     for(unsigned i=0; i<f.size(); i++)
00072         if(f[i]==c1)f[i]=c2;
00073 }
00074 
00075 void replace(string& f,string s1,string s2) {
00076     int a;
00077     while((a=f.find(s1))!=string::npos) {
00078         f.replace(a, s1.size(), s2);
00079     };
00080 }
00081 }
00082 
00083 namespace _file {
00084 int fileSize(const string& FileName) {
00085     struct stat file;
00086     if (!stat(FileName.c_str(), &file))
00087         return file.st_size;
00088     return -1;
00089 }
00090 }
00091 
00092 namespace _bits {
00093 
00094 u64** LINK_ROOKS;
00095 void _free() {
00096     for(int i=0; i<64; i++) {
00097         free(LINK_ROOKS[i]);
00098     }
00099     free(LINK_ROOKS);
00100 }
00101 
00102 void init() {
00103     //LINK_ROOKS
00104     LINK_ROOKS=(u64**)malloc(64*sizeof(u64*));
00105     for(int i=0; i<64; i++) {
00106         LINK_ROOKS[i]=(u64*)malloc(64*sizeof(u64));
00107     }
00108     int from,to;
00109     for(int i=0; i<64; i++) {
00110         for(int j=0; j<64; j++) {
00111             u64 t=0;
00112             if(RANK[i] & RANK[j]) { //rank
00113                 from=min(i,j);
00114                 to=max(i,j);
00115                 for(int k=from+1; k<=to-1; k++) {
00116                     t|=POW2[k];
00117                 }
00118             } else if(FILE_[i] & FILE_[j]) { //file
00119                 from=min(i,j);
00120                 to=max(i,j);
00121                 for(int k=from+8; k<=to-8; k+=8) {
00122                     t|=POW2[k];
00123                 }
00124             }
00125             if(!t) t=0xffffffffffffffffULL;
00126             LINK_ROOKS[i][j]=t;
00127         }
00128     }
00129 }
00130 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines