Skip to content

Commit e3393df

Browse files
committed
Introduce default parameters for some appropriate functions and merge some signatures
1 parent aa0c53c commit e3393df

File tree

16 files changed

+73
-207
lines changed

16 files changed

+73
-207
lines changed

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/command/selfplay.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,8 @@ int MainCmds::selfplay(const vector<string>& args) {
212212

213213
//Note that this inputsVersion passed here is NOT necessarily the same as the one used in the neural net self play, it
214214
//simply controls the input feature version for the written data
215-
TrainingDataWriter* tdataWriter = new TrainingDataWriter(
216-
tdataOutputDir, inputsVersion, maxRowsPerTrainFile, firstFileRandMinProp, dataBoardLen, dataBoardLen, Global::uint64ToHexString(rand.nextUInt64()));
215+
auto tdataWriter = new TrainingDataWriter(
216+
tdataOutputDir, nullptr, inputsVersion, maxRowsPerTrainFile, firstFileRandMinProp, dataBoardLen, dataBoardLen, Global::uint64ToHexString(rand.nextUInt64()));
217217
ofstream* sgfOut = NULL;
218218
if(sgfOutputDir.length() > 0) {
219219
sgfOut = new ofstream();

cpp/core/config_parser.cpp

Lines changed: 15 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#include "../core/fileutils.h"
44

5-
#include <cmath>
65
#include <fstream>
76
#include <sstream>
87

@@ -113,8 +112,6 @@ void ConfigParser::processIncludedFile(const std::string &fname) {
113112
baseDirs.pop_back();
114113
}
115114

116-
117-
118115
bool ConfigParser::parseKeyValue(const std::string& trimmedLine, std::string& key, std::string& value) {
119116
// Parse trimmed line, taking into account comments and quoting.
120117
key.clear();
@@ -598,14 +595,7 @@ enabled_t ConfigParser::getEnabled(const string& key) {
598595
return x;
599596
}
600597

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) {
598+
int ConfigParser::getInt(const string& key, const int min, const int max) {
609599
assert(min <= max);
610600
string value = getString(key);
611601
int x;
@@ -615,19 +605,8 @@ int ConfigParser::getInt(const string& key, int min, int max) {
615605
throw IOError("Key '" + key + "' must be in the range " + Global::intToString(min) + " to " + Global::intToString(max) + " in config file " + fileName);
616606
return x;
617607
}
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) {
608+
609+
vector<int> ConfigParser::getInts(const string& key, const int min, const int max) {
631610
vector<string> values = getStrings(key);
632611
vector<int> ret;
633612
for(size_t i = 0; i<values.size(); i++) {
@@ -670,15 +649,7 @@ vector<std::pair<int,int>> ConfigParser::getNonNegativeIntDashedPairs(const stri
670649
return ret;
671650
}
672651

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) {
652+
int64_t ConfigParser::getInt64(const string& key, const int64_t min, const int64_t max) {
682653
assert(min <= max);
683654
string value = getString(key);
684655
int64_t x;
@@ -688,19 +659,8 @@ int64_t ConfigParser::getInt64(const string& key, int64_t min, int64_t max) {
688659
throw IOError("Key '" + key + "' must be in the range " + Global::int64ToString(min) + " to " + Global::int64ToString(max) + " in config file " + fileName);
689660
return x;
690661
}
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) {
662+
663+
vector<int64_t> ConfigParser::getInt64s(const string& key, const int64_t min, const int64_t max) {
704664
vector<string> values = getStrings(key);
705665
vector<int64_t> ret;
706666
for(size_t i = 0; i<values.size(); i++) {
@@ -715,15 +675,7 @@ vector<int64_t> ConfigParser::getInt64s(const string& key, int64_t min, int64_t
715675
return ret;
716676
}
717677

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) {
678+
uint64_t ConfigParser::getUInt64(const string& key, const uint64_t min, const uint64_t max) {
727679
assert(min <= max);
728680
string value = getString(key);
729681
uint64_t x;
@@ -733,19 +685,8 @@ uint64_t ConfigParser::getUInt64(const string& key, uint64_t min, uint64_t max)
733685
throw IOError("Key '" + key + "' must be in the range " + Global::uint64ToString(min) + " to " + Global::uint64ToString(max) + " in config file " + fileName);
734686
return x;
735687
}
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) {
688+
689+
vector<uint64_t> ConfigParser::getUInt64s(const string& key, const uint64_t min, const uint64_t max) {
749690
vector<string> values = getStrings(key);
750691
vector<uint64_t> ret;
751692
for(size_t i = 0; i<values.size(); i++) {
@@ -760,15 +701,7 @@ vector<uint64_t> ConfigParser::getUInt64s(const string& key, uint64_t min, uint6
760701
return ret;
761702
}
762703

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) {
704+
float ConfigParser::getFloat(const string& key, const float min, const float max) {
772705
assert(min <= max);
773706
string value = getString(key);
774707
float x;
@@ -780,19 +713,8 @@ float ConfigParser::getFloat(const string& key, float min, float max) {
780713
throw IOError("Key '" + key + "' must be in the range " + Global::floatToString(min) + " to " + Global::floatToString(max) + " in config file " + fileName);
781714
return x;
782715
}
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) {
716+
717+
vector<float> ConfigParser::getFloats(const string& key, const float min, const float max) {
796718
vector<string> values = getStrings(key);
797719
vector<float> ret;
798720
for(size_t i = 0; i<values.size(); i++) {
@@ -809,15 +731,7 @@ vector<float> ConfigParser::getFloats(const string& key, float min, float max) {
809731
return ret;
810732
}
811733

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) {
734+
double ConfigParser::getDouble(const string& key, const double min, const double max) {
821735
assert(min <= max);
822736
string value = getString(key);
823737
double x;
@@ -829,19 +743,8 @@ double ConfigParser::getDouble(const string& key, double min, double max) {
829743
throw IOError("Key '" + key + "' must be in the range " + Global::doubleToString(min) + " to " + Global::doubleToString(max) + " in config file " + fileName);
830744
return x;
831745
}
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) {
746+
747+
vector<double> ConfigParser::getDoubles(const string& key, const double min, const double max) {
845748
vector<string> values = getStrings(key);
846749
vector<double> ret;
847750
for(size_t i = 0; i<values.size(); i++) {

cpp/core/config_parser.h

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,34 +60,24 @@ class ConfigParser {
6060
std::string getString(const std::string& key);
6161
bool getBool(const std::string& key);
6262
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);
6863

6964
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);
65+
int getInt(const std::string& key, int min = std::numeric_limits<int>::min(), int max = std::numeric_limits<int>::max());
66+
int64_t getInt64(const std::string& key, int64_t min = std::numeric_limits<int64_t>::min(), int64_t max = std::numeric_limits<int64_t>::max());
67+
uint64_t getUInt64(const std::string& key, uint64_t min = std::numeric_limits<uint64_t>::min(), uint64_t max = std::numeric_limits<uint64_t>::max());
68+
float getFloat(const std::string& key, float min = std::numeric_limits<float>::min(), float max = std::numeric_limits<float>::max());
69+
double getDouble(const std::string& key, double min = std::numeric_limits<double>::min(), double max = std::numeric_limits<double>::max());
7570

7671
std::vector<std::string> getStrings(const std::string& key);
7772
std::vector<std::string> getStringsNonEmptyTrim(const std::string& key);
7873
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);
8474

8575
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);
76+
std::vector<int> getInts(const std::string& key, int min = std::numeric_limits<int>::min(), int max = std::numeric_limits<int>::max());
77+
std::vector<int64_t> getInt64s(const std::string& key, int64_t min = std::numeric_limits<int64_t>::min(), int64_t max = std::numeric_limits<int64_t>::max());
78+
std::vector<uint64_t> getUInt64s(const std::string& key, uint64_t min = std::numeric_limits<uint64_t>::min(), uint64_t max = std::numeric_limits<uint64_t>::max());
79+
std::vector<float> getFloats(const std::string& key, float min = std::numeric_limits<float>::min(), float max = std::numeric_limits<float>::max());
80+
std::vector<double> getDoubles(const std::string& key, double min = std::numeric_limits<double>::min(), double max = std::numeric_limits<double>::max());
9181

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

cpp/dataio/trainingwrite.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -954,15 +954,15 @@ void TrainingWriteBuffers::writeToTextOstream(ostream& out) {
954954

955955
//-------------------------------------------------------------------------------------
956956

957-
TrainingDataWriter::TrainingDataWriter(const string& outDir, int iVersion, int maxRowsPerFile, double firstFileMinRandProp, int dataXLen, int dataYLen, const string& randSeed)
958-
: TrainingDataWriter(outDir,NULL,iVersion,maxRowsPerFile,firstFileMinRandProp,dataXLen,dataYLen,1,randSeed)
959-
{}
960-
TrainingDataWriter::TrainingDataWriter(ostream* dbgOut, int iVersion, int maxRowsPerFile, double firstFileMinRandProp, int dataXLen, int dataYLen, int onlyEvery, const string& randSeed)
961-
: TrainingDataWriter(string(),dbgOut,iVersion,maxRowsPerFile,firstFileMinRandProp,dataXLen,dataYLen,onlyEvery,randSeed)
962-
{}
963-
964-
TrainingDataWriter::TrainingDataWriter(const string& outDir, ostream* dbgOut, int iVersion, int maxRowsPerFile, double firstFileMinRandProp, int dataXLen, int dataYLen, int onlyEvery, const string& randSeed)
965-
:outputDir(outDir),inputsVersion(iVersion),rand(randSeed),writeBuffers(NULL),debugOut(dbgOut),debugOnlyWriteEvery(onlyEvery),rowCount(0)
957+
TrainingDataWriter::TrainingDataWriter(const string& outputDir, ostream* debugOut,
958+
const int inputsVersion,
959+
const int maxRowsPerFile,
960+
const double firstFileMinRandProp,
961+
const int dataXLen,
962+
const int dataYLen,
963+
const string& randSeed,
964+
const int onlyWriteEvery)
965+
:outputDir(outputDir),inputsVersion(inputsVersion),rand(randSeed),writeBuffers(nullptr),debugOut(debugOut),debugOnlyWriteEvery(onlyWriteEvery),rowCount(0)
966966
{
967967
int numBinaryChannels;
968968
int numGlobalChannels;

cpp/dataio/trainingwrite.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,7 @@ struct TrainingWriteBuffers {
311311

312312
class TrainingDataWriter {
313313
public:
314-
TrainingDataWriter(const std::string& outputDir, int inputsVersion, int maxRowsPerFile, double firstFileMinRandProp, int dataXLen, int dataYLen, const std::string& randSeed);
315-
TrainingDataWriter(std::ostream* debugOut, int inputsVersion, int maxRowsPerFile, double firstFileMinRandProp, int dataXLen, int dataYLen, int onlyWriteEvery, const std::string& randSeed);
316-
TrainingDataWriter(const std::string& outputDir, std::ostream* debugOut, int inputsVersion, int maxRowsPerFile, double firstFileMinRandProp, int dataXLen, int dataYLen, int onlyWriteEvery, const std::string& randSeed);
314+
TrainingDataWriter(const std::string& outputDir, std::ostream* debugOut, int inputsVersion, int maxRowsPerFile, double firstFileMinRandProp, int dataXLen, int dataYLen, const std::string& randSeed, int onlyWriteEvery = 1);
317315
~TrainingDataWriter();
318316

319317
void writeGame(const FinishedGameData& data);

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.

0 commit comments

Comments
 (0)