From 4f30f522c43bbc9822721b071d59ec194b71af6a Mon Sep 17 00:00:00 2001 From: Timo Ewalds Date: Wed, 23 Nov 2011 20:47:30 +0100 Subject: [PATCH] Remove some the old children library as well as old time code, replace #ifndefs with #pragma once to avoid collisions on Macs, spinlocks aren't needed in single threaded mode --- assert2.h | 5 +-- board.h | 5 +-- children.h | 83 ---------------------------------------------- compacttree.h | 7 +--- depthstats.h | 5 +-- game.h | 5 +-- gtp.h | 15 ++++----- hashset.h | 7 ++-- havannahgtp.h | 5 +-- lbdist.h | 5 +-- log.h | 5 +-- move.h | 5 +-- player.h | 5 +-- solver.h | 5 +-- solverab.h | 5 +-- solverpns_tt.h | 5 +-- string.h | 5 +-- thread.h | 13 ++++---- time.h | 11 +----- timer.h | 5 +-- types.h | 6 +--- weightedrandtree.h | 5 +-- zobrist.h | 5 +-- 23 files changed, 33 insertions(+), 189 deletions(-) delete mode 100644 children.h diff --git a/assert2.h b/assert2.h index 8b67d91..26bf701 100644 --- a/assert2.h +++ b/assert2.h @@ -1,6 +1,5 @@ -#ifndef _ASSERT2_H_ -#define _ASSERT2_H_ +#pragma once #include #include "string.h" @@ -10,5 +9,3 @@ ? __ASSERT_VOID_CAST (0) \ : __assert_fail ((string(__STRING(expr)) + "; " + (str)).c_str(), __FILE__, __LINE__, __ASSERT_FUNCTION)) -#endif - diff --git a/board.h b/board.h index b23459c..be9fcf8 100644 --- a/board.h +++ b/board.h @@ -1,6 +1,5 @@ -#ifndef _BOARD_H_ -#define _BOARD_H_ +#pragma once #include #include @@ -875,5 +874,3 @@ mutable uint8_t ringdepth; //when doing a ring search, what depth was this posit } }; -#endif - diff --git a/children.h b/children.h deleted file mode 100644 index c752aa9..0000000 --- a/children.h +++ /dev/null @@ -1,83 +0,0 @@ - -#ifndef _CHILDREN_H_ -#define _CHILDREN_H_ - -#include "thread.h" - -template class Children { - static const int LOCK = 1; //needs to be cast to (Node *) at usage point - - uint32_t _num; //can be smaller, but will be padded anyway. - Node * _children; -public: - typedef Node * iterator; - Children() : _num(0), _children(NULL) { } - Children(int n) : _num(0), _children(NULL) { alloc(n); } - ~Children() { assert_empty(); } - - void assert_consistent() const { assert((_num == 0) || (_children > (Node *) LOCK)); } - void assert_empty() const { assert((_num == 0) && (_children == NULL)); } - - bool lock() { return CAS(_children, (Node *) NULL, (Node *) LOCK); } - bool unlock() { return CAS(_children, (Node *) LOCK, (Node *) NULL); } - - void atomic_set(Children & o){ - assert(CAS(_children, (Node *) LOCK, o._children)); //undoes the lock - assert(CAS(_num, 0, o._num)); //keeps consistency - } - - unsigned int alloc(unsigned int n){ - assert(_num == 0); - assert(_children == NULL); - assert(n > 0); - - _num = n; - _children = new Node[_num]; - - return _num; - } - void neuter(){ - _children = 0; - _num = 0; - } - unsigned int dealloc(){ - assert_consistent(); - - int n = _num; - if(_children){ - Node * temp = _children; //CAS! - neuter(); - delete[] temp; - } - return n; - } - void swap(Children & other){ - Children temp = other; - other = *this; - *this = temp; - temp.neuter(); //to avoid problems with the destructor of temp - } - unsigned int num() const { - assert_consistent(); - return _num; - } - bool empty() const { - return num() == 0; - } - Node & operator[](unsigned int offset){ - assert(_children > (Node *) LOCK); - assert(offset >= 0 && offset < _num); - return _children[offset]; - } - Node * begin() const { - assert_consistent(); - return _children; - } - Node * end() const { - assert_consistent(); - return _children + _num; - } -}; - -#endif - diff --git a/compacttree.h b/compacttree.h index 8cbf582..da2d42c 100644 --- a/compacttree.h +++ b/compacttree.h @@ -1,8 +1,5 @@ - - -#ifndef _COMPACTTREE_H_ -#define _COMPACTTREE_H_ +#pragma once #include #include //for memmove @@ -499,5 +496,3 @@ template class CompactTree { } }; -#endif - diff --git a/depthstats.h b/depthstats.h index f2bfe1a..df35282 100644 --- a/depthstats.h +++ b/depthstats.h @@ -1,6 +1,5 @@ -#ifndef _DEPTHSTATS_H_ -#define _DEPTHSTATS_H_ +#pragma once #include #include @@ -59,5 +58,3 @@ struct DepthStats { } }; -#endif - diff --git a/game.h b/game.h index 686d429..67e589f 100644 --- a/game.h +++ b/game.h @@ -1,6 +1,5 @@ -#ifndef _GAME_H_ -#define _GAME_H_ +#pragma once #include "board.h" #include "move.h" @@ -77,5 +76,3 @@ class HavannahGame { } }; -#endif - diff --git a/gtp.h b/gtp.h index 9bfd001..78d6d1c 100644 --- a/gtp.h +++ b/gtp.h @@ -1,6 +1,5 @@ -#ifndef _GTP_H_ -#define _GTP_H_ +#pragma once #include "string.h" @@ -26,7 +25,7 @@ struct GTPResponse { response = r; rtrim(response); } - + GTPResponse(string r){ GTPResponse(true, r); } @@ -102,11 +101,11 @@ class GTPclient { if(longest_cmd < name.length()) longest_cmd = name.length(); } - + void newcallback(const GTPCallback & a){ callbacks.push_back(a); } - + int find_callback(const string & name){ for(unsigned int i = 0; i < callbacks.size(); i++) if(callbacks[i].name == name) @@ -155,7 +154,7 @@ class GTPclient { string line(buf); trim(line); - + if(line.length() == 0 || line[0] == '#') continue; @@ -211,7 +210,7 @@ class GTPclient { running = false; return true; } - + GTPResponse gtp_list_commands(vecstr args, bool showdesc){ string ret = "\n"; for(unsigned int i = 0; i < callbacks.size(); i++){ @@ -226,5 +225,3 @@ class GTPclient { } }; -#endif - diff --git a/hashset.h b/hashset.h index 8584dbc..3a76dc6 100644 --- a/hashset.h +++ b/hashset.h @@ -1,6 +1,5 @@ -#ifndef _HASHSET_H_ -#define _HASHSET_H_ +#pragma once #include "zobrist.h" @@ -32,7 +31,7 @@ class HashSet { i = (i+1) & mask; table[i] = h; } - + bool exists(hash_t h){ for(unsigned int i = h & mask; table[i]; i = (i+1) & mask) if(table[i] == h) @@ -53,5 +52,3 @@ class HashSet { } }; -#endif - diff --git a/havannahgtp.h b/havannahgtp.h index 3be9b64..fac9634 100644 --- a/havannahgtp.h +++ b/havannahgtp.h @@ -1,6 +1,5 @@ -#ifndef _HAVANNAHGTP_H_ -#define _HAVANNAHGTP_H_ +#pragma once #include "gtp.h" #include "game.h" @@ -223,5 +222,3 @@ class HavannahGTP : public GTPclient { GTPResponse gtp_solve_pnstt_clear(vecstr args); }; -#endif - diff --git a/lbdist.h b/lbdist.h index 310b223..4060cf3 100644 --- a/lbdist.h +++ b/lbdist.h @@ -1,4 +1,5 @@ +#pragma once /* Compute a rough lower bound on the number of additional moves needed to win given this position @@ -9,8 +10,6 @@ Increase distance when crossing an opponent virtual connection? Decrease distance when crossing your own virtual connection? */ -#ifndef _LBDIST_H_ -#define _LBDIST_H_ #include "board.h" #include "move.h" @@ -201,5 +200,3 @@ class LBDists { } }; -#endif - diff --git a/log.h b/log.h index a345e15..32c8861 100644 --- a/log.h +++ b/log.h @@ -1,6 +1,5 @@ -#ifndef _LOG_H_ -#define _LOG_H_ +#pragma once #include #include @@ -9,5 +8,3 @@ inline void logerr(std::string str){ fprintf(stderr, "%s", str.c_str()); } -#endif - diff --git a/move.h b/move.h index 1bb6f66..d4a6185 100644 --- a/move.h +++ b/move.h @@ -1,6 +1,5 @@ -#ifndef _MOVE_H_ -#define _MOVE_H_ +#pragma once #include #include @@ -89,5 +88,3 @@ struct PairMove { PairMove(MoveSpecial A) : a(Move(A)), b(M_UNKNOWN) { } }; -#endif - diff --git a/player.h b/player.h index a56ca16..d23ced7 100644 --- a/player.h +++ b/player.h @@ -1,6 +1,5 @@ -#ifndef __PLAYER_H_ -#define __PLAYER_H_ +#pragma once #include #include @@ -438,5 +437,3 @@ class Player { Node * return_move(Node * node, int toplay) const; }; -#endif - diff --git a/solver.h b/solver.h index e91e2ce..7be3063 100644 --- a/solver.h +++ b/solver.h @@ -1,6 +1,5 @@ -#ifndef _SOLVER_H_ -#define _SOLVER_H_ +#pragma once #include #include "board.h" @@ -65,5 +64,3 @@ class Solver { }; -#endif - diff --git a/solverab.h b/solverab.h index 921810d..862c189 100644 --- a/solverab.h +++ b/solverab.h @@ -1,6 +1,5 @@ -#ifndef _SOLVERAB_H_ -#define _SOLVERAB_H_ +#pragma once #include @@ -71,5 +70,3 @@ class SolverAB : public Solver { void tt_set(const Board & board, int val); }; -#endif - diff --git a/solverpns_tt.h b/solverpns_tt.h index 30094d3..d4dff96 100644 --- a/solverpns_tt.h +++ b/solverpns_tt.h @@ -1,6 +1,5 @@ -#ifndef _SOLVERPNS_TT_H_ -#define _SOLVERPNS_TT_H_ +#pragma once #include @@ -130,5 +129,3 @@ class SolverPNSTT : public Solver { PNSNode * tt(const Board & board, Move move); }; -#endif - diff --git a/string.h b/string.h index b61dca5..55f069d 100644 --- a/string.h +++ b/string.h @@ -1,6 +1,5 @@ -#ifndef _STRING_H_ -#define _STRING_H_ +#pragma once #include #include @@ -31,5 +30,3 @@ void rtrim(std::string & str); vecstr explode(const std::string & str, const std::string & sep); std::string implode(const vecstr & vec, const std::string & sep); -#endif - diff --git a/thread.h b/thread.h index e271a5a..bf33a8b 100644 --- a/thread.h +++ b/thread.h @@ -1,8 +1,5 @@ - - -#ifndef _MY_THREAD_H_ -#define _MY_THREAD_H_ +#pragma once #include #include @@ -118,8 +115,13 @@ class SpinLock { public: SpinLock() : taken(0) { } void lock() { while(!trylock()) ; } +#ifdef SINGLE_THREAD + bool trylock(){ return true; } + void unlock() { }; +#else bool trylock(){ return !__sync_lock_test_and_set(&taken, 1); } void unlock() { __sync_lock_release(&taken); } +#endif }; //object wrapper around pthread rwlock @@ -231,6 +233,3 @@ class Barrier { } }; - -#endif - diff --git a/time.h b/time.h index fa4c39c..5f6005c 100644 --- a/time.h +++ b/time.h @@ -1,16 +1,9 @@ -#ifndef _TIME_H_ -#define _TIME_H_ +#pragma once #include #include -inline int time_msec(){ - struct timeval time; - gettimeofday(&time, NULL); - return (time.tv_sec*1000 + time.tv_usec/1000); -} - class Time { double t; public: @@ -43,5 +36,3 @@ class Time { bool operator != (const Time & a) const { return t != a.t; } }; -#endif - diff --git a/timer.h b/timer.h index ab6c058..da4e1e1 100644 --- a/timer.h +++ b/timer.h @@ -1,6 +1,5 @@ -#ifndef _TIMER_H_ -#define _TIMER_H_ +#pragma once #include #include @@ -64,5 +63,3 @@ class Timer { } }; -#endif - diff --git a/types.h b/types.h index 428cba8..48f8409 100644 --- a/types.h +++ b/types.h @@ -1,7 +1,5 @@ - -#ifndef _TYPES_H_ -#define _TYPES_H_ +#pragma once #include @@ -32,5 +30,3 @@ typedef uint64_t u64; #error Unknown word size #endif -#endif - diff --git a/weightedrandtree.h b/weightedrandtree.h index fda9351..4a00fa7 100644 --- a/weightedrandtree.h +++ b/weightedrandtree.h @@ -1,6 +1,5 @@ -#ifndef _WEIGHTED_RAND_TREE_H_ -#define _WEIGHTED_RAND_TREE_H_ +#pragma once #include #include "xorshift.h" @@ -115,5 +114,3 @@ class WeightedRandTree { } }; -#endif - diff --git a/zobrist.h b/zobrist.h index b2a7f92..e7e2d81 100644 --- a/zobrist.h +++ b/zobrist.h @@ -1,6 +1,5 @@ -#ifndef _ZOBRIST_H_ -#define _ZOBRIST_H_ +#pragma once #include "types.h" @@ -41,5 +40,3 @@ class Zobrist { } }; -#endif -