Skip to content

Commit ede45a2

Browse files
committed
Changed from try/catch to map.count for getting default value
1 parent ea9fe7b commit ede45a2

10 files changed

Lines changed: 47 additions & 83 deletions

libhdt/include/HDTSpecification.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ class HDTSpecification
7272
*/
7373
const std::string& get(const std::string& key);
7474

75+
76+
/**
77+
* Get the value of a property, or "" if the key is not found.
78+
*/
79+
const std::string& getOrEmpty(const std::string& key);
80+
81+
7582
/** Set the value of a property */
7683
void set(const std::string& key, const std::string& value);
7784
};

libhdt/src/dictionary/FourSectionDictionary.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,7 @@ FourSectionDictionary::FourSectionDictionary(HDTSpecification & spec) : blocksiz
5656
objects = new csd::CSD_PFC();
5757
shared = new csd::CSD_PFC();
5858

59-
string blockSizeStr = "";
60-
try{
61-
blockSizeStr = spec.get("dict.block.size");
62-
}catch(exception& e){}
59+
string blockSizeStr = spec.getOrEmpty("dict.block.size");
6360

6461
if(!blockSizeStr.empty() && (blockSizeStr.find_first_not_of("0123456789") == string::npos))
6562
{

libhdt/src/dictionary/KyotoDictionary.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,7 @@ KyotoDictionary::KyotoDictionary() {
5050
}
5151

5252
KyotoDictionary::KyotoDictionary(HDTSpecification &specification) : spec(specification) {
53-
string map = "";
54-
try{
55-
map = spec.get("dictionary.mapping");
56-
}catch(exception& e){}
53+
string map = spec.getOrEmpty("dictionary.mapping");
5754
if(map=="mapping1") {
5855
this->mapping = MAPPING1;
5956
} else {

libhdt/src/dictionary/LiteralDictionary.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,8 @@ LiteralDictionary::LiteralDictionary(HDTSpecification & spec) : blocksize(8) {
5757
objectsLiterals = new csd::CSD_FMIndex();
5858
shared = new csd::CSD_PFC();
5959

60-
string blockSizeStr = "";
61-
try{
62-
blockSizeStr = spec.get("dict.block.size");
63-
}catch(exception& e){}
60+
string blockSizeStr = spec.getOrEmpty("dict.block.size");
61+
6462
if (blockSizeStr != "") {
6563
blocksize = atoi(blockSizeStr.c_str());
6664
}

libhdt/src/dictionary/PlainDictionary.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,7 @@ PlainDictionary::PlainDictionary() {
5555
}
5656

5757
PlainDictionary::PlainDictionary(HDTSpecification &specification) : spec(specification) {
58-
string map ="";
59-
try{
60-
map = spec.get("dictionary.mapping");
61-
}catch(exception& e){}
58+
string map =spec.getOrEmpty("dictionary.mapping");
6259
if(map=="mapping1") {
6360
this->mapping = MAPPING1;
6461
} else {

libhdt/src/hdt/BasicHDT.cpp

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,8 @@ void BasicHDT::createComponents() {
9696

9797
// DICTIONARY
9898

99-
std::string dictType = "";
100-
try{
101-
dictType = spec.get("dictionary.type");
102-
}
103-
catch (std::exception& e){
104-
}
99+
std::string dictType = spec.getOrEmpty("dictionary.type");
100+
105101

106102
if(dictType==HDTVocabulary::DICTIONARY_TYPE_FOUR) {
107103
dictionary = new FourSectionDictionary(spec);
@@ -118,11 +114,8 @@ void BasicHDT::createComponents() {
118114
}
119115

120116
// TRIPLES
121-
std::string triplesType = "";
122-
try{
123-
triplesType = spec.get("triples.type");
124-
}catch (std::exception& e) {
125-
}
117+
std::string triplesType = spec.getOrEmpty("triples.type");
118+
126119
if(triplesType==HDTVocabulary::TRIPLES_TYPE_BITMAP) {
127120
triples = new BitmapTriples(spec);
128121
} else if(triplesType==HDTVocabulary::TRIPLES_TYPE_PLAIN) {
@@ -299,11 +292,8 @@ void BasicHDT::loadTriples(const char* fileName, const char* baseUri, RDFNotatio
299292
triplesList->stopProcessing(&iListener);
300293

301294
// SORT & Duplicates
302-
string ord = "";
303-
try{
304-
ord = spec.get("triplesOrder");
305-
}catch (std::exception& e){
306-
}
295+
string ord = spec.getOrEmpty("triplesOrder");
296+
307297
TripleComponentOrder order = parseOrder(
308298
ord.c_str());
309299
if (order == Unknown) {
@@ -581,12 +571,8 @@ void BasicHDT::loadTriplesFromHDTs(const char** fileNames, size_t numFiles, cons
581571
triplesList->stopProcessing(&iListener);
582572

583573
// SORT & Duplicates
584-
string ord = "";
585-
try{
586-
ord = spec.get("triplesOrder");
587-
}
588-
catch (std::exception& e){
589-
}
574+
string ord = spec.getOrEmpty("triplesOrder");
575+
590576
TripleComponentOrder order = parseOrder(ord.c_str());
591577
if (order == Unknown) {
592578
order = SPO;

libhdt/src/hdt/HDTSpecification.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ const std::string& HDTSpecification::get(const std::string& key) {
6363
return map.at(key);
6464
}
6565

66+
const std::string emptyString = "";
67+
68+
const std::string& HDTSpecification::getOrEmpty(const std::string& key) {
69+
if(map.count(key) == 0) return emptyString;
70+
return map.at(key);
71+
}
72+
6673
void HDTSpecification::set(const std::string& key, const std::string& value) {
6774
map[key] = value;
6875
}

libhdt/src/triples/BitmapTriples.cpp

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,9 @@ namespace hdt {
4444
#define CHECK_BITMAPTRIPLES_INITIALIZED if(bitmapY==NULL || bitmapZ==NULL){ throw std::runtime_error("Accessing uninitialized BitmapTriples"); }
4545

4646
BitmapTriples::BitmapTriples() : order(SPO) {
47-
string typey="";
48-
string typez="";
49-
try{
50-
typey = spec.get("stream.y");
51-
typez = spec.get("stream.z");
52-
}catch (std::exception& e){}
47+
string typey=spec.getOrEmpty("stream.y");
48+
string typez=spec.getOrEmpty("stream.z");
49+
5350
arrayY = IntSequence::getArray(typey);
5451
arrayZ = IntSequence::getArray(typez);
5552
arrayIndex = NULL;
@@ -61,20 +58,14 @@ BitmapTriples::BitmapTriples() : order(SPO) {
6158
}
6259

6360
BitmapTriples::BitmapTriples(HDTSpecification &specification) : spec(specification) {
64-
std::string orderStr = "";
65-
try{
66-
orderStr = spec.get("triplesOrder");
67-
}catch (std::exception& e){}
61+
std::string orderStr = spec.getOrEmpty("triplesOrder");
6862

6963
order= parseOrder(orderStr.c_str());
7064
if(order==Unknown)
7165
order = SPO;
72-
string typey="";
73-
string typez="";
74-
try{
75-
typey = spec.get("stream.y");
76-
typez = spec.get("stream.z");
77-
}catch (std::exception& e){}
66+
string typey= spec.getOrEmpty("stream.y");
67+
string typez= spec.getOrEmpty("stream.z");
68+
7869
arrayY = IntSequence::getArray(typey);
7970
arrayZ = IntSequence::getArray(typez);
8071
arrayIndex = NULL;
@@ -852,11 +843,7 @@ size_t BitmapTriples::loadIndex(unsigned char *ptr, unsigned char *ptrMax, Progr
852843
}
853844

854845
size_t numTriples = controlInformation.getUint("numTriples");
855-
std::string typeIndex ="";
856-
try{
857-
typeIndex = controlInformation.get("stream.index");
858-
}
859-
catch(exception &e){}
846+
std::string typeIndex = spec.getOrEmpty("stream.index");
860847

861848
if(this->getNumberOfElements()!=numTriples) {
862849
// FIXME: Force index regeneration instead of error.

libhdt/src/triples/PlainTriples.cpp

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,36 +37,26 @@
3737
namespace hdt {
3838

3939
PlainTriples::PlainTriples() : order(SPO) {
40-
string typex="";
41-
string typey="";
42-
string typez="";
43-
try{
44-
typex = spec.get("stream.x");
45-
typey = spec.get("stream.y");
46-
typez = spec.get("stream.z");
47-
}catch (std::exception& e){}
40+
string typex= spec.getOrEmpty("stream.x");
41+
string typey= spec.getOrEmpty("stream.y");
42+
string typez= spec.getOrEmpty("stream.z");
43+
4844
streamX = IntSequence::getArray(typex);
4945
streamY = IntSequence::getArray(typey);
5046
streamZ = IntSequence::getArray(typez);
5147
}
5248

5349
PlainTriples::PlainTriples(HDTSpecification &specification) : spec(specification) {
54-
std::string orderStr = "";
55-
try{
56-
orderStr= spec.get("triplesOrder");
57-
}catch(exception& e){}
50+
std::string orderStr = spec.getOrEmpty("triplesOrder");
51+
5852
order = parseOrder(orderStr.c_str());
5953
if(order==Unknown) {
6054
order = SPO;
6155
}
62-
string typex="";
63-
string typey="";
64-
string typez="";
65-
try{
66-
typex = spec.get("stream.x");
67-
typey = spec.get("stream.y");
68-
typez = spec.get("stream.z");
69-
}catch (std::exception& e){}
56+
string typex= spec.getOrEmpty("stream.x");
57+
string typey= spec.getOrEmpty("stream.y");
58+
string typez= spec.getOrEmpty("stream.z");
59+
7060
streamX = IntSequence::getArray(typex);
7161
streamY = IntSequence::getArray(typey);
7262
streamZ = IntSequence::getArray(typez);

libhdt/src/triples/TriplesKyoto.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,8 @@ int32_t TriplesKyoto::compare (const char *akbuf, size_t aksiz, const char *bkbu
6565

6666
TriplesKyoto::TriplesKyoto(HDTSpecification &specification) : spec(specification) {
6767
unlink("triples.kct");
68-
string ord = "";
69-
try{
70-
ord = spec.get("triplesOrder");
71-
}catch(exception& e){}
68+
string ord = spec.getOrEmpty("triplesOrder");
69+
7270
order = parseOrder(ord.c_str());
7371
if(order==Unknown){
7472
order = SPO;

0 commit comments

Comments
 (0)