forked from rperlste/UniversalCompiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrammarSet.cpp
78 lines (63 loc) · 1.64 KB
/
GrammarSet.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "GrammarSet.h"
// Return true if change occurred, false if none occurred
bool GrammarSet::add( const Symbol& grammar ){
return grammarSet.insert( grammar ).second;
}
// Return true if change occurred, false if none occurred
bool GrammarSet::add( GrammarSet grammarSetIn ){
bool changes = false;
for( int i = 0; i < grammarSetIn.size(); i++ ){
if( this->grammarSet.insert( (*grammarSetIn[i]) ).second )
changes = true;
}
return changes;
}
// Return true if change occurred, false if none occurred
bool GrammarSet::remove( const Symbol& grammar ){
if( grammarSet.find( grammar ) != grammarSet.end() ){
grammarSet.erase( grammarSet.find( grammar ) );
return true;
}
else
return false;
}
std::set<Symbol>* GrammarSet::getGrammarSet(){
return &grammarSet;
}
const int GrammarSet::size(){
return grammarSet.size();
}
bool GrammarSet::contains( const Symbol& grammar ){
if( grammarSet.find( grammar ) != grammarSet.end() )
return true;
else
return false;
}
const int GrammarSet::getIndex( const Symbol& symbol ){
int index = 0;
for( std::set<Symbol>::iterator it = grammarSet.begin();
it != grammarSet.end();
it++ ){
if( (*it) == symbol )
return index;
index++;
}
throw GrammarException( "Value not found => " + symbol );
}
const Symbol* GrammarSet::getSymbol( const int& index ){
if( index >= size() )
throw IndexOutOfBounds();
else{
int count = 0;
for( std::set<Symbol>::iterator itSet = grammarSet.begin();
itSet != grammarSet.end();
itSet++ ){
if( count == index )
return &(*itSet);
count++;
}
}
}
const Symbol* GrammarSet::operator [] ( const int& index ){
return getSymbol( index );
}