Skip to content

Commit 3ef68bf

Browse files
committed
Pull in some defaults from #1136
1 parent 938da7e commit 3ef68bf

10 files changed

Lines changed: 131 additions & 152 deletions

File tree

cpp/command/genbook.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,8 +525,8 @@ int MainCmds::genbook(const vector<string>& args) {
525525
if(!bonusInitialBoard.isEqualForTesting(book->getInitialHist().getRecentBoard(0), false, false))
526526
throw StringError(
527527
"Book initial board and initial board in bonus sgf file do not match\n" +
528-
Board::toStringSimple(book->getInitialHist().getRecentBoard(0),'\n') + "\n" +
529-
Board::toStringSimple(bonusInitialBoard,'\n')
528+
Board::toStringSimple(book->getInitialHist().getRecentBoard(0)) + "\n" +
529+
Board::toStringSimple(bonusInitialBoard)
530530
);
531531
if(bonusInitialPla != book->initialPla)
532532
throw StringError(

cpp/core/config_parser.cpp

Lines changed: 15 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,6 @@ void ConfigParser::processIncludedFile(const std::string &fname) {
113113
baseDirs.pop_back();
114114
}
115115

116-
117-
118116
bool ConfigParser::parseKeyValue(const std::string& trimmedLine, std::string& key, std::string& value) {
119117
// Parse trimmed line, taking into account comments and quoting.
120118
key.clear();
@@ -598,14 +596,7 @@ enabled_t ConfigParser::getEnabled(const string& key) {
598596
return x;
599597
}
600598

601-
int ConfigParser::getInt(const string& key) {
602-
string value = getString(key);
603-
int x;
604-
if(!Global::tryStringToInt(value,x))
605-
throw IOError("Could not parse '" + value + "' as int for key '" + key + "' in config file " + fileName);
606-
return x;
607-
}
608-
int ConfigParser::getInt(const string& key, int min, int max) {
599+
int ConfigParser::getInt(const string& key, const int min, const int max) {
609600
assert(min <= max);
610601
string value = getString(key);
611602
int x;
@@ -615,19 +606,8 @@ int ConfigParser::getInt(const string& key, int min, int max) {
615606
throw IOError("Key '" + key + "' must be in the range " + Global::intToString(min) + " to " + Global::intToString(max) + " in config file " + fileName);
616607
return x;
617608
}
618-
vector<int> ConfigParser::getInts(const string& key) {
619-
vector<string> values = getStrings(key);
620-
vector<int> ret;
621-
for(size_t i = 0; i<values.size(); i++) {
622-
const string& value = values[i];
623-
int x;
624-
if(!Global::tryStringToInt(value,x))
625-
throw IOError("Could not parse '" + value + "' as int for key '" + key + "' in config file " + fileName);
626-
ret.push_back(x);
627-
}
628-
return ret;
629-
}
630-
vector<int> ConfigParser::getInts(const string& key, int min, int max) {
609+
610+
vector<int> ConfigParser::getInts(const string& key, const int min, const int max) {
631611
vector<string> values = getStrings(key);
632612
vector<int> ret;
633613
for(size_t i = 0; i<values.size(); i++) {
@@ -670,15 +650,7 @@ vector<std::pair<int,int>> ConfigParser::getNonNegativeIntDashedPairs(const stri
670650
return ret;
671651
}
672652

673-
674-
int64_t ConfigParser::getInt64(const string& key) {
675-
string value = getString(key);
676-
int64_t x;
677-
if(!Global::tryStringToInt64(value,x))
678-
throw IOError("Could not parse '" + value + "' as int64_t for key '" + key + "' in config file " + fileName);
679-
return x;
680-
}
681-
int64_t ConfigParser::getInt64(const string& key, int64_t min, int64_t max) {
653+
int64_t ConfigParser::getInt64(const string& key, const int64_t min, const int64_t max) {
682654
assert(min <= max);
683655
string value = getString(key);
684656
int64_t x;
@@ -688,19 +660,8 @@ int64_t ConfigParser::getInt64(const string& key, int64_t min, int64_t max) {
688660
throw IOError("Key '" + key + "' must be in the range " + Global::int64ToString(min) + " to " + Global::int64ToString(max) + " in config file " + fileName);
689661
return x;
690662
}
691-
vector<int64_t> ConfigParser::getInt64s(const string& key) {
692-
vector<string> values = getStrings(key);
693-
vector<int64_t> ret;
694-
for(size_t i = 0; i<values.size(); i++) {
695-
const string& value = values[i];
696-
int64_t x;
697-
if(!Global::tryStringToInt64(value,x))
698-
throw IOError("Could not parse '" + value + "' as int64_t for key '" + key + "' in config file " + fileName);
699-
ret.push_back(x);
700-
}
701-
return ret;
702-
}
703-
vector<int64_t> ConfigParser::getInt64s(const string& key, int64_t min, int64_t max) {
663+
664+
vector<int64_t> ConfigParser::getInt64s(const string& key, const int64_t min, const int64_t max) {
704665
vector<string> values = getStrings(key);
705666
vector<int64_t> ret;
706667
for(size_t i = 0; i<values.size(); i++) {
@@ -715,15 +676,7 @@ vector<int64_t> ConfigParser::getInt64s(const string& key, int64_t min, int64_t
715676
return ret;
716677
}
717678

718-
719-
uint64_t ConfigParser::getUInt64(const string& key) {
720-
string value = getString(key);
721-
uint64_t x;
722-
if(!Global::tryStringToUInt64(value,x))
723-
throw IOError("Could not parse '" + value + "' as uint64_t for key '" + key + "' in config file " + fileName);
724-
return x;
725-
}
726-
uint64_t ConfigParser::getUInt64(const string& key, uint64_t min, uint64_t max) {
679+
uint64_t ConfigParser::getUInt64(const string& key, const uint64_t min, const uint64_t max) {
727680
assert(min <= max);
728681
string value = getString(key);
729682
uint64_t x;
@@ -733,19 +686,8 @@ uint64_t ConfigParser::getUInt64(const string& key, uint64_t min, uint64_t max)
733686
throw IOError("Key '" + key + "' must be in the range " + Global::uint64ToString(min) + " to " + Global::uint64ToString(max) + " in config file " + fileName);
734687
return x;
735688
}
736-
vector<uint64_t> ConfigParser::getUInt64s(const string& key) {
737-
vector<string> values = getStrings(key);
738-
vector<uint64_t> ret;
739-
for(size_t i = 0; i<values.size(); i++) {
740-
const string& value = values[i];
741-
uint64_t x;
742-
if(!Global::tryStringToUInt64(value,x))
743-
throw IOError("Could not parse '" + value + "' as uint64_t for key '" + key + "' in config file " + fileName);
744-
ret.push_back(x);
745-
}
746-
return ret;
747-
}
748-
vector<uint64_t> ConfigParser::getUInt64s(const string& key, uint64_t min, uint64_t max) {
689+
690+
vector<uint64_t> ConfigParser::getUInt64s(const string& key, const uint64_t min, const uint64_t max) {
749691
vector<string> values = getStrings(key);
750692
vector<uint64_t> ret;
751693
for(size_t i = 0; i<values.size(); i++) {
@@ -760,15 +702,7 @@ vector<uint64_t> ConfigParser::getUInt64s(const string& key, uint64_t min, uint6
760702
return ret;
761703
}
762704

763-
764-
float ConfigParser::getFloat(const string& key) {
765-
string value = getString(key);
766-
float x;
767-
if(!Global::tryStringToFloat(value,x))
768-
throw IOError("Could not parse '" + value + "' as float for key '" + key + "' in config file " + fileName);
769-
return x;
770-
}
771-
float ConfigParser::getFloat(const string& key, float min, float max) {
705+
float ConfigParser::getFloat(const string& key, const float min, const float max) {
772706
assert(min <= max);
773707
string value = getString(key);
774708
float x;
@@ -780,19 +714,8 @@ float ConfigParser::getFloat(const string& key, float min, float max) {
780714
throw IOError("Key '" + key + "' must be in the range " + Global::floatToString(min) + " to " + Global::floatToString(max) + " in config file " + fileName);
781715
return x;
782716
}
783-
vector<float> ConfigParser::getFloats(const string& key) {
784-
vector<string> values = getStrings(key);
785-
vector<float> ret;
786-
for(size_t i = 0; i<values.size(); i++) {
787-
const string& value = values[i];
788-
float x;
789-
if(!Global::tryStringToFloat(value,x))
790-
throw IOError("Could not parse '" + value + "' as float for key '" + key + "' in config file " + fileName);
791-
ret.push_back(x);
792-
}
793-
return ret;
794-
}
795-
vector<float> ConfigParser::getFloats(const string& key, float min, float max) {
717+
718+
vector<float> ConfigParser::getFloats(const string& key, const float min, const float max) {
796719
vector<string> values = getStrings(key);
797720
vector<float> ret;
798721
for(size_t i = 0; i<values.size(); i++) {
@@ -809,15 +732,7 @@ vector<float> ConfigParser::getFloats(const string& key, float min, float max) {
809732
return ret;
810733
}
811734

812-
813-
double ConfigParser::getDouble(const string& key) {
814-
string value = getString(key);
815-
double x;
816-
if(!Global::tryStringToDouble(value,x))
817-
throw IOError("Could not parse '" + value + "' as double for key '" + key + "' in config file " + fileName);
818-
return x;
819-
}
820-
double ConfigParser::getDouble(const string& key, double min, double max) {
735+
double ConfigParser::getDouble(const string& key, const double min, const double max) {
821736
assert(min <= max);
822737
string value = getString(key);
823738
double x;
@@ -829,19 +744,8 @@ double ConfigParser::getDouble(const string& key, double min, double max) {
829744
throw IOError("Key '" + key + "' must be in the range " + Global::doubleToString(min) + " to " + Global::doubleToString(max) + " in config file " + fileName);
830745
return x;
831746
}
832-
vector<double> ConfigParser::getDoubles(const string& key) {
833-
vector<string> values = getStrings(key);
834-
vector<double> ret;
835-
for(size_t i = 0; i<values.size(); i++) {
836-
const string& value = values[i];
837-
double x;
838-
if(!Global::tryStringToDouble(value,x))
839-
throw IOError("Could not parse '" + value + "' as double for key '" + key + "' in config file " + fileName);
840-
ret.push_back(x);
841-
}
842-
return ret;
843-
}
844-
vector<double> ConfigParser::getDoubles(const string& key, double min, double max) {
747+
748+
vector<double> ConfigParser::getDoubles(const string& key, const double min, const double max) {
845749
vector<string> values = getStrings(key);
846750
vector<double> ret;
847751
for(size_t i = 0; i<values.size(); i++) {

cpp/core/config_parser.h

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef CORE_CONFIG_PARSER_H_
22
#define CORE_CONFIG_PARSER_H_
33

4+
#include <limits>
45
#include <mutex>
56

67
#include "../core/global.h"
@@ -60,34 +61,24 @@ class ConfigParser {
6061
std::string getString(const std::string& key);
6162
bool getBool(const std::string& key);
6263
enabled_t getEnabled(const std::string& key);
63-
int getInt(const std::string& key);
64-
int64_t getInt64(const std::string& key);
65-
uint64_t getUInt64(const std::string& key);
66-
float getFloat(const std::string& key);
67-
double getDouble(const std::string& key);
6864

6965
std::string getString(const std::string& key, const std::set<std::string>& possibles);
70-
int getInt(const std::string& key, int min, int max);
71-
int64_t getInt64(const std::string& key, int64_t min, int64_t max);
72-
uint64_t getUInt64(const std::string& key, uint64_t min, uint64_t max);
73-
float getFloat(const std::string& key, float min, float max);
74-
double getDouble(const std::string& key, double min, double max);
66+
int getInt(const std::string& key, int min = std::numeric_limits<int>::lowest(), int max = std::numeric_limits<int>::max());
67+
int64_t getInt64(const std::string& key, int64_t min = std::numeric_limits<int64_t>::lowest(), int64_t max = std::numeric_limits<int64_t>::max());
68+
uint64_t getUInt64(const std::string& key, uint64_t min = std::numeric_limits<uint64_t>::lowest(), uint64_t max = std::numeric_limits<uint64_t>::max());
69+
float getFloat(const std::string& key, float min = std::numeric_limits<float>::lowest(), float max = std::numeric_limits<float>::max());
70+
double getDouble(const std::string& key, double min = std::numeric_limits<double>::lowest(), double max = std::numeric_limits<double>::max());
7571

7672
std::vector<std::string> getStrings(const std::string& key);
7773
std::vector<std::string> getStringsNonEmptyTrim(const std::string& key);
7874
std::vector<bool> getBools(const std::string& key);
79-
std::vector<int> getInts(const std::string& key);
80-
std::vector<int64_t> getInt64s(const std::string& key);
81-
std::vector<uint64_t> getUInt64s(const std::string& key);
82-
std::vector<float> getFloats(const std::string& key);
83-
std::vector<double> getDoubles(const std::string& key);
8475

8576
std::vector<std::string> getStrings(const std::string& key, const std::set<std::string>& possibles);
86-
std::vector<int> getInts(const std::string& key, int min, int max);
87-
std::vector<int64_t> getInt64s(const std::string& key, int64_t min, int64_t max);
88-
std::vector<uint64_t> getUInt64s(const std::string& key, uint64_t min, uint64_t max);
89-
std::vector<float> getFloats(const std::string& key, float min, float max);
90-
std::vector<double> getDoubles(const std::string& key, double min, double max);
77+
std::vector<int> getInts(const std::string& key, int min = std::numeric_limits<int>::lowest(), int max = std::numeric_limits<int>::max());
78+
std::vector<int64_t> getInt64s(const std::string& key, int64_t min = std::numeric_limits<int64_t>::lowest(), int64_t max = std::numeric_limits<int64_t>::max());
79+
std::vector<uint64_t> getUInt64s(const std::string& key, uint64_t min = std::numeric_limits<uint64_t>::lowest(), uint64_t max = std::numeric_limits<uint64_t>::max());
80+
std::vector<float> getFloats(const std::string& key, float min = std::numeric_limits<float>::lowest(), float max = std::numeric_limits<float>::max());
81+
std::vector<double> getDoubles(const std::string& key, double min = std::numeric_limits<double>::lowest(), double max = std::numeric_limits<double>::max());
9182

9283
std::vector<std::pair<int,int>> getNonNegativeIntDashedPairs(const std::string& key, int min, int max);
9384

cpp/game/board.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2688,10 +2688,6 @@ string Board::toStringSimple(const Board& board, char lineDelimiter) {
26882688
return s;
26892689
}
26902690

2691-
Board Board::parseBoard(int xSize, int ySize, const string& s) {
2692-
return parseBoard(xSize,ySize,s,'\n');
2693-
}
2694-
26952691
Board Board::parseBoard(int xSize, int ySize, const string& s, char lineDelimiter) {
26962692
Board board(xSize,ySize);
26972693
vector<string> lines = Global::split(Global::trim(s),lineDelimiter);

cpp/game/board.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,12 +297,11 @@ struct Board
297297
void checkConsistency() const;
298298
//For the moment, only used in testing since it does extra consistency checks.
299299
//If we need a version to be used in "prod", we could make an efficient version maybe as operator==.
300-
bool isEqualForTesting(const Board& other, bool checkNumCaptures, bool checkSimpleKo) const;
300+
bool isEqualForTesting(const Board& other, bool checkNumCaptures = true, bool checkSimpleKo = true) const;
301301

302-
static Board parseBoard(int xSize, int ySize, const std::string& s);
303-
static Board parseBoard(int xSize, int ySize, const std::string& s, char lineDelimiter);
302+
static Board parseBoard(int xSize, int ySize, const std::string& s, char lineDelimiter = '\n');
304303
static void printBoard(std::ostream& out, const Board& board, Loc markLoc, const std::vector<Move>* hist);
305-
static std::string toStringSimple(const Board& board, char lineDelimiter);
304+
static std::string toStringSimple(const Board& board, char lineDelimiter = '\n');
306305
static nlohmann::json toJson(const Board& board);
307306
static Board ofJson(const nlohmann::json& data);
308307

cpp/game/boardhistory.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -914,10 +914,6 @@ bool BoardHistory::makeBoardMoveTolerant(Board& board, Loc moveLoc, Player moveP
914914
return true;
915915
}
916916

917-
void BoardHistory::makeBoardMoveAssumeLegal(Board& board, Loc moveLoc, Player movePla, const KoHashTable* rootKoHashTable) {
918-
makeBoardMoveAssumeLegal(board,moveLoc,movePla,rootKoHashTable,false);
919-
}
920-
921917
void BoardHistory::makeBoardMoveAssumeLegal(Board& board, Loc moveLoc, Player movePla, const KoHashTable* rootKoHashTable, bool preventEncore) {
922918
Hash128 posHashBeforeMove = board.pos_hash;
923919

cpp/game/boardhistory.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,7 @@ struct BoardHistory {
158158
//even if the move violates superko or encore ko recapture prohibitions, or is past when the game is ended.
159159
//This allows for robustness when this code is being used for analysis or with external data sources.
160160
//preventEncore artifically prevents any move from entering or advancing the encore phase when using territory scoring.
161-
void makeBoardMoveAssumeLegal(Board& board, Loc moveLoc, Player movePla, const KoHashTable* rootKoHashTable);
162-
void makeBoardMoveAssumeLegal(Board& board, Loc moveLoc, Player movePla, const KoHashTable* rootKoHashTable, bool preventEncore);
161+
void makeBoardMoveAssumeLegal(Board& board, Loc moveLoc, Player movePla, const KoHashTable* rootKoHashTable, bool preventEncore = false);
163162
//Make a move with legality checking, but be mostly tolerant and allow moves that can still be handled but that may not technically
164163
//be legal. This is intended for reading moves from SGFs and such where maybe we're getting moves that were played in a different
165164
//ruleset than ours. Returns true if successful, false if was illegal even unter tolerant rules.

cpp/tests/testboardbasic.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2507,9 +2507,9 @@ oxxxxx.xo
25072507
//if(rep < 100)
25082508
// hist.printDebugInfo(cout,board);
25092509

2510-
testAssert(boardCopy.isEqualForTesting(board, true, true));
2511-
testAssert(boardCopy.isEqualForTesting(histCopy.getRecentBoard(0), true, true));
2512-
testAssert(histCopy.getRecentBoard(0).isEqualForTesting(hist.getRecentBoard(0), true, true));
2510+
testAssert(boardCopy.isEqualForTesting(board));
2511+
testAssert(boardCopy.isEqualForTesting(histCopy.getRecentBoard(0)));
2512+
testAssert(histCopy.getRecentBoard(0).isEqualForTesting(hist.getRecentBoard(0)));
25132513
testAssert(BoardHistory::getSituationRulesAndKoHash(boardCopy,histCopy,pla,drawEquivalentWinsForWhite) == hist.getSituationRulesAndKoHash(board,hist,pla,drawEquivalentWinsForWhite));
25142514
testAssert(histCopy.currentSelfKomi(P_BLACK, drawEquivalentWinsForWhite) == hist.currentSelfKomi(P_BLACK, drawEquivalentWinsForWhite));
25152515
testAssert(histCopy.currentSelfKomi(P_WHITE, drawEquivalentWinsForWhite) == hist.currentSelfKomi(P_WHITE, drawEquivalentWinsForWhite));

0 commit comments

Comments
 (0)