static void require_valid_table(const mesh& Mesh, const string_t& Name, const table& Table) { if(Name == "constant" && Table.column_count() && Table.row_count() != 1) throw std::runtime_error("'constant' table must have length 1."); for(mesh::table_t::const_iterator array_iterator = Table.begin(); array_iterator != Table.end(); ++array_iterator) { const array* const current_array = array_iterator->second.get(); if(!current_array) throw std::runtime_error("NULL table array."); const array* const first_array = Table.begin()->second.get(); if(current_array->size() != first_array->size()) throw std::runtime_error("Array length mismatch for table [" + Name + "]"); if(current_array->get_metadata_value(metadata::key::domain()) == metadata::value::point_indices_domain()) { if(!Mesh.points) throw std::runtime_error("Mesh missing points array."); if(!Mesh.point_selection) throw std::runtime_error("Mesh missing point selections array."); require_valid_points(Mesh); const mesh::indices_t* const indices = dynamic_cast<const mesh::indices_t*>(current_array); if(!indices) throw std::runtime_error("Point indices array must be an index type."); const mesh::indices_t::const_iterator max = std::max_element(indices->begin(), indices->end()); if(max != indices->end() && *max >= Mesh.points->size()) throw std::runtime_error("Point indices array out-of-bounds."); } } }
void insert(table &fTable, char c) { bool inserted = false; for(table::iterator it = fTable.begin(); it != fTable.end(); ++it) { if (it->first == c) { ++(it->second); inserted = true; break; } } if (!inserted) { pair<char, int> ins(c, 1); fTable.push_back(ins); } }
//! Constructor for a distribution in the log space. multivariate_categorical_distribution(const table<T>& lp, log_tag) : psum_(lp.shape()) { std::transform(lp.begin(), lp.end(), psum_.begin(), exponent<T>()); std::partial_sum(psum_.begin(), psum_.end(), psum_.begin()); }
int main() { int P, i, j, prev, next; char X; string right; bool complete = false; // read in the CFG ifstream fin("CFG.txt"); fin >> P; for(i=0; i<P; i++) { fin >> X >> right; // if the first symbol on the right side of a production is a terminal, // include it in the FIRST set of the variable on the left side of the production. if(islower(right[0])) FIRST[X - 'A'] |= 1 << (right[0] - 'a'); else if(productions.find(X) != productions.end()) productions[X].push_back(right); else { vector <string> vec; vec.push_back(right); productions[X] = vec; } } fin.close(); // iterate until no more elements can be added to FIRST set of any variable while(!complete) { complete = true; for(table::iterator it=productions.begin(); it != productions.end(); it++) { prev = next = FIRST[it->first - 'A']; for(vector<string>::iterator jt=(it->second).begin(); jt != (it->second).end(); jt++) { for(i=0; i<jt->length(); i++) { X = (*jt)[i]; if(islower(X)) { next |= 1 << (X - 'a'); break; } if((FIRST[X - 'A'] & (1 << 'e' - 'a')) == 0) { next |= FIRST[X - 'A']; break; } else next |= ~(~FIRST[X - 'A'] | (1 << 'e' - 'a')); } if(i == jt->length()) next |= (1 << 'e' - 'a'); } if(prev != next) { FIRST[it->first - 'A'] = next; complete = false; } } } // print out the FIRST set of each variable for(i=0; i<26; i++) if(FIRST[i]) { printf("FIRST(%c) = { ", i + 'A'); for(j=0; j<26; j++) if(FIRST[i] & (1 << j)) printf("%c ", j + 'a'); printf("}\n"); } return 0; }
void sortByFreq(table &v) { sort(v.begin(), v.end(), comp); }
void printfTable(table t) { for (table::iterator it = t.begin(); it != t.end(); ++it) { cout<< it->first << " => " << it->second << endl; } }
//! Returns true if the two tables have the same shape and elements bool operator==(const table& x) const { return shape_ == x.shape_ && std::equal(begin(), end(), x.begin()); }
/** * Copy constructor. Copies the shape and elements of a table to this one. */ table(const table& x) : shape_(x.shape_), offset_(x.offset_) { data_.reset(new T[size()]); std::copy(x.begin(), x.end(), data()); }