From: Marco Costalba Date: Sun, 19 Aug 2012 09:20:15 +0000 (+0100) Subject: Prefer size_t over int for array sizes X-Git-Url: https://git.sesse.net/?p=stockfish;a=commitdiff_plain;h=4b19430103ac75b574a6b269db447d359814b603 Prefer size_t over int for array sizes Align to standard library conventions. No functional change. Signed-off-by: Marco Costalba --- diff --git a/src/benchmark.cpp b/src/benchmark.cpp index 5800a37b..c3a41913 100644 --- a/src/benchmark.cpp +++ b/src/benchmark.cpp @@ -120,7 +120,7 @@ void benchmark(const Position& current, istream& is) { if (limitType == "perft") { - int64_t cnt = Search::perft(pos, limits.depth * ONE_PLY); + size_t cnt = Search::perft(pos, limits.depth * ONE_PLY); cerr << "\nPerft " << limits.depth << " leaf nodes: " << cnt << endl; nodes += cnt; } diff --git a/src/movegen.h b/src/movegen.h index 65d346b4..84d5d35e 100644 --- a/src/movegen.h +++ b/src/movegen.h @@ -45,7 +45,7 @@ struct MoveList { void operator++() { cur++; } bool end() const { return cur == last; } Move move() const { return cur->move; } - int size() const { return int(last - mlist); } + size_t size() const { return last - mlist; } private: MoveStack mlist[MAX_MOVES]; diff --git a/src/search.cpp b/src/search.cpp index 8f1964dd..a3ad7be1 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -198,24 +198,23 @@ void Search::init() { /// Search::perft() is our utility to verify move generation. All the leaf nodes /// up to the given depth are generated and counted and the sum returned. -int64_t Search::perft(Position& pos, Depth depth) { +size_t Search::perft(Position& pos, Depth depth) { - StateInfo st; - int64_t cnt = 0; - - MoveList ml(pos); - - // At the last ply just return the number of moves (leaf nodes) + // At the last ply just return the number of legal moves (leaf nodes) if (depth == ONE_PLY) - return ml.size(); + return MoveList(pos).size(); + StateInfo st; + size_t cnt = 0; CheckInfo ci(pos); - for ( ; !ml.end(); ++ml) + + for (MoveList ml(pos); !ml.end(); ++ml) { pos.do_move(ml.move(), st, ci, pos.move_gives_check(ml.move(), ci)); cnt += perft(pos, depth - ONE_PLY); pos.undo_move(ml.move()); } + return cnt; } @@ -1541,7 +1540,7 @@ split_point_start: // At split points actual search starts from here int t = SearchTime.elapsed(); int selDepth = 0; - for (int i = 0; i < Threads.size(); i++) + for (size_t i = 0; i < Threads.size(); i++) if (Threads[i].maxPly > selDepth) selDepth = Threads[i].maxPly; diff --git a/src/search.h b/src/search.h index c573807b..d580b546 100644 --- a/src/search.h +++ b/src/search.h @@ -98,7 +98,7 @@ extern Position RootPosition; extern Time SearchTime; extern void init(); -extern int64_t perft(Position& pos, Depth depth); +extern size_t perft(Position& pos, Depth depth); extern void think(); } // namespace Search diff --git a/src/thread.cpp b/src/thread.cpp index 0fbcc1fc..0a8bacf7 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -55,7 +55,7 @@ Thread::Thread(Fn fn) { lock_init(sleepLock); cond_init(sleepCond); - for (int j = 0; j < MAX_SPLITPOINTS_PER_THREAD; j++) + for (size_t j = 0; j < MAX_SPLITPOINTS_PER_THREAD; j++) lock_init(splitPoints[j].lock); if (!thread_create(handle, start_routine, this)) @@ -213,7 +213,7 @@ void ThreadPool::init() { ThreadPool::~ThreadPool() { - for (int i = 0; i < size(); i++) + for (size_t i = 0; i < size(); i++) delete threads[i]; delete timer; @@ -232,7 +232,7 @@ void ThreadPool::read_uci_options() { maxThreadsPerSplitPoint = Options["Max Threads per Split Point"]; minimumSplitDepth = Options["Min Split Depth"] * ONE_PLY; useSleepingThreads = Options["Use Sleeping Threads"]; - int requested = Options["Threads"]; + size_t requested = Options["Threads"]; assert(requested > 0); @@ -253,7 +253,7 @@ void ThreadPool::read_uci_options() { void ThreadPool::wake_up() const { - for (int i = 0; i < size(); i++) + for (size_t i = 0; i < size(); i++) { threads[i]->maxPly = 0; threads[i]->do_sleep = false; @@ -269,7 +269,7 @@ void ThreadPool::wake_up() const { void ThreadPool::sleep() const { - for (int i = 1; i < size(); i++) // Main thread will go to sleep by itself + for (size_t i = 1; i < size(); i++) // Main thread will go to sleep by itself threads[i]->do_sleep = true; // to avoid a race with start_searching() } @@ -279,7 +279,7 @@ void ThreadPool::sleep() const { bool ThreadPool::available_slave_exists(Thread* master) const { - for (int i = 0; i < size(); i++) + for (size_t i = 0; i < size(); i++) if (threads[i]->is_available_to(master)) return true; @@ -344,7 +344,7 @@ Value ThreadPool::split(Position& pos, Stack* ss, Value alpha, Value beta, lock_grab(sp->lock); lock_grab(splitLock); - for (int i = 0; i < size() && !Fake; ++i) + for (size_t i = 0; i < size() && !Fake; ++i) if (threads[i]->is_available_to(master)) { sp->slavesMask |= 1ULL << i; diff --git a/src/thread.h b/src/thread.h index 73be23f1..d1328daa 100644 --- a/src/thread.h +++ b/src/thread.h @@ -67,14 +67,11 @@ struct SplitPoint { class Thread { - Thread(const Thread&); // Only declared to disable the default ones - Thread& operator=(const Thread&); // that are not suitable in this case. - - typedef void (Thread::* Fn) (); + typedef void (Thread::* Fn) (); // Pointer to member function public: Thread(Fn fn); - ~Thread(); + ~Thread(); void wake_up(); bool cutoff_occurred() const; @@ -88,7 +85,7 @@ public: SplitPoint splitPoints[MAX_SPLITPOINTS_PER_THREAD]; MaterialTable materialTable; PawnTable pawnTable; - int idx; + size_t idx; int maxPly; Lock sleepLock; WaitCondition sleepCond; @@ -107,18 +104,15 @@ public: /// All the access to shared thread data is done through this class. class ThreadPool { - /* As long as the single ThreadPool object is defined as a global we don't - need to explicitly initialize to zero its data members because variables with - static storage duration are automatically set to zero before enter main() - */ + public: - void init(); // No c'tor becuase Threads is global and we need engine initialized + void init(); // No c'tor, Threads object is global and engine shall be fully initialized ~ThreadPool(); Thread& operator[](int id) { return *threads[id]; } bool use_sleeping_threads() const { return useSleepingThreads; } int min_split_depth() const { return minimumSplitDepth; } - int size() const { return (int)threads.size(); } + size_t size() const { return threads.size(); } Thread* main_thread() { return threads[0]; } void wake_up() const;