void SipPresenceMonitor::publishContent(UtlString& contact, SipPresenceEvent* presenceEvent) { bool contentChanged; // Loop through all the resource lists UtlHashMapIterator iterator(mMonitoredLists); UtlString* listUri; SipResourceList* list; Resource* resource; UtlString id, state; while (listUri = dynamic_cast <UtlString *> (iterator())) { contentChanged = false; list = dynamic_cast <SipResourceList *> (mMonitoredLists.findValue(listUri)); OsSysLog::add(FAC_SIP, PRI_DEBUG, "SipPresenceMonitor::publishContent listUri %s list %p", listUri->data(), list); // Search for the contact in this list resource = list->getResource(contact); if (resource) { resource->getInstance(id, state); if (presenceEvent->isEmpty()) { resource->setInstance(id, STATE_TERMINATED); } else { UtlString id; NetMd5Codec::encode(contact, id); Tuple* tuple = presenceEvent->getTuple(id); UtlString status; tuple->getStatus(status); if (status.compareTo(STATUS_CLOSE) == 0) { resource->setInstance(id, STATE_TERMINATED); } else { resource->setInstance(id, STATE_ACTIVE); } } list->buildBody(); contentChanged = true; } if (contentChanged) { int numOldContents; HttpBody* oldContent[1]; // Publish the content to the subscribe server if (!mSipPublishContentMgr.publish(listUri->data(), PRESENCE_EVENT_TYPE, DIALOG_EVENT_TYPE, 1, (HttpBody**)&list, 1, numOldContents, oldContent)) { UtlString presenceContent; int length; list->getBytes(&presenceContent, &length); OsSysLog::add(FAC_SIP, PRI_ERR, "SipPresenceMonitor::publishContent PresenceEvent %s\n was not successfully published to the subscribe server", presenceContent.data()); } } } }
void test_fields_prim() { Tuple* tuple = new_tuple(); TS_ASSERT_EQUALS(Fixnum::from(3), as<Fixnum>(tuple->fields_prim(state))); }
ModEvent CPTupleVarImp::assign(Space& home, const Tuple& t) { TupleSet s(t.arity()); s.add(t); return exclude(home,s.complement()); }
void test_copy_from_to_empty_this() { Tuple* tuple = new_tuple(); Tuple* dest = Tuple::create(state, 0); dest->copy_from(state, tuple, Fixnum::from(0), Fixnum::from(0), Fixnum::from(0)); }
void test_at_prim() { Tuple* tuple = new_tuple(); TS_ASSERT_EQUALS(Fixnum::from(4), as<Fixnum>(tuple->at_prim(state, Fixnum::from(1)))); TS_ASSERT_THROWS_ASSERT(tuple->at_prim(state, Fixnum::from(4)), const RubyException &e, TS_ASSERT(Exception::object_bounds_exceeded_error_p(state, e.exception))); }
inline bool operator==(const Tuple &l, const tOther &r) { return l.equal(r); }
// There is no subtlety here: mov just sets velocity directly void SimpleDynamics::mov(Tuple v) { flo x = v[0].asNumber(); flo y = v[1].asNumber(); flo z = v.size() > 2 ? v[2].asNumber() : 0; device->body->set_velocity(x,y,z); }
bool Tuple_Comparison::operator()(Tuple& tuple1, Tuple& tuple2) { if(tuple1.isNull() && tuple2.isNull()) return false; if((!tuple1.isNull()) && tuple2.isNull()) return true; if(tuple1.isNull() && (!tuple2.isNull())) return false; vector<string>::iterator start = field_names.begin(); vector<string>::iterator stop = field_names.end(); vector<string>::iterator index; Schema schema_tuple1 = tuple1.getSchema(); Schema schema_tuple2 = tuple2.getSchema(); // cout<< "comparing "<<tuple1<<" "<<tuple2<<endl; if(schema_tuple1 != schema_tuple2) throw string("Error!! Tuple comparison failed - Tuple_Comparison::operator()"); for(index = start; index!=stop; index++) { string column_name = *index; FIELD_TYPE field_type = schema_tuple1.getFieldType(column_name); if(field_type == INT) { int value1, value2; value1 = tuple1.getField(column_name).integer; value2 = tuple2.getField(column_name).integer; if(value1 != value2) return value1<value2; } else //FIELD_TYPE == STR20 { string value1, value2; value1 = *(tuple1.getField(column_name).str); value2 = *(tuple2.getField(column_name).str); if(value1.compare(value2) < 0) return true; else if(value1.compare(value2) > 0) return false; } } return false; }
void SimulatorAtom::retrieve(const Query& query, Answer& answer) throw (PluginError){ RegistryPtr reg = query.interpretation->getRegistry(); const Tuple& params = query.input; // get ASP filename std::string programpath = reg->terms.getByID(params[0]).getUnquotedString(); // if we access this file for the first time, parse the content if (programs.find(programpath) == programs.end()){ DBGLOG(DBG, "Parsing simulation program"); InputProviderPtr ip(new InputProvider()); ip->addFileInput(programpath); Logger::Levels l = Logger::Instance().getPrintLevels(); // workaround: verbose causes the parse call below to fail (registry pointer is 0) Logger::Instance().setPrintLevels(0); programs[programpath].changeRegistry(reg); ModuleHexParser hp; hp.parse(ip, programs[programpath]); Logger::Instance().setPrintLevels(l); } ProgramCtx& pc = programs[programpath]; // construct edb DBGLOG(DBG, "Constructing EDB"); InterpretationPtr edb = InterpretationPtr(new Interpretation(*pc.edb)); // go through all input atoms DBGLOG(DBG, "Rewriting input"); for(Interpretation::Storage::enumerator it = query.interpretation->getStorage().first(); it != query.interpretation->getStorage().end(); ++it){ ID ogid(ID::MAINKIND_ATOM | ID::SUBKIND_ATOM_ORDINARYG, *it); const OrdinaryAtom& ogatom = reg->ogatoms.getByID(ogid); // check if the predicate matches any of the input parameters to simulator atom bool found = false; for (int inp = 1; inp < params.size(); ++inp){ if (ogatom.tuple[0] == params[inp]){ // replace the predicate by "in[inp]" std::stringstream inPredStr; inPredStr << "in" << inp; Term inPredTerm(ID::MAINKIND_TERM | ID::SUBKIND_TERM_CONSTANT, inPredStr.str()); ID inPredID = reg->storeTerm(inPredTerm); OrdinaryAtom oareplace = ogatom; oareplace.tuple[0] = inPredID; // get ID of replaced atom ID oareplaceID = reg->storeOrdinaryGAtom(oareplace); // set this atom in the input interpretation edb->getStorage().set_bit(oareplaceID.address); found = true; break; } } assert(found); } DBGLOG(DBG, "Grounding simulation program"); OrdinaryASPProgram program(pc.registry(), pc.idb, edb); InternalGrounderPtr ig = InternalGrounderPtr(new InternalGrounder(pc, program)); OrdinaryASPProgram gprogram = ig->getGroundProgram(); DBGLOG(DBG, "Evaluating simulation program"); GenuineSolverPtr igas = GenuineSolver::getInstance(pc, gprogram); InterpretationPtr as = igas->getNextModel(); if (as != InterpretationPtr()){ // extract parameters from all atoms over predicate "out" DBGLOG(DBG, "Rewrting output"); Term outPredTerm(ID::MAINKIND_TERM | ID::SUBKIND_TERM_CONSTANT, "out"); ID outPredID = reg->storeTerm(outPredTerm); for(Interpretation::Storage::enumerator it = as->getStorage().first(); it != as->getStorage().end(); ++it){ ID ogid(ID::MAINKIND_ATOM | ID::SUBKIND_ATOM_ORDINARYG, *it); const OrdinaryAtom& ogatom = reg->ogatoms.getByID(ogid); if (ogatom.tuple[0] == outPredID){ Tuple t; for (int ot = 1; ot < ogatom.tuple.size(); ++ot){ t.push_back(ogatom.tuple[ot]); } answer.get().push_back(t); } } } }
GenericRelation *Relation::In( ListExpr typeInfo, ListExpr value, int errorPos, ListExpr& errorInfo, bool& correct, bool tupleBuf /*=false*/) { ListExpr tuplelist, TupleTypeInfo, first; GenericRelation* rel; Tuple* tupleaddr; int tupleno, count; bool tupleCorrect; correct = true; count = 0; if (tupleBuf) rel = new TupleBuffer; else rel = new Relation( typeInfo ); tuplelist = value; TupleTypeInfo = nl->TwoElemList(nl->Second(typeInfo), nl->IntAtom(nl->ListLength(nl->Second(nl->Second(typeInfo))))); tupleno = 0; if (nl->IsAtom(value)) { correct = false; errorInfo = nl->Append(errorInfo, nl->ThreeElemList( nl->IntAtom(70), nl->SymbolAtom(Relation::BasicType()), tuplelist)); return rel; } else { // increase tupleno while (!nl->IsEmpty(tuplelist)) { first = nl->First(tuplelist); tuplelist = nl->Rest(tuplelist); tupleno++; tupleaddr = Tuple::In(TupleTypeInfo, first, tupleno, errorInfo, tupleCorrect); if (tupleCorrect) { rel->AppendTuple(tupleaddr); tupleaddr->DeleteIfAllowed(); count++; } else { correct = false; } } if (!correct) { errorInfo = nl->Append(errorInfo, nl->TwoElemList( nl->IntAtom(72), nl->SymbolAtom(Relation::BasicType()))); delete rel; return 0; } else return rel; } }
int main(int argc, char* argv[]) { std::string file_name; if (argc < 2) { std::cerr << "please specify the file name" << std::endl; return 1; } file_name = argv[1]; std::ifstream infile; #ifdef _MSC_VER // handling file names with non-ascii characters wchar_t *wcstring = new wchar_t[file_name.size()+1]; setlocale(LC_ALL, ".OCP"); mbstowcs(wcstring, file_name.c_str(), file_name.size()+1); infile.open(wcstring); delete[] wcstring; setlocale(LC_ALL, ""); #else // _MSC_VER infile.open(file_name.c_str()); #endif // _MSC_VER if (!infile.is_open()) { std::cerr << "cannot open the input file" << std::endl; return 1; } SUTModel sut_model; Assembler assembler; try { ct::lexer lexer(&infile); assembler.setErrLogger(boost::shared_ptr<ErrLogger>(new ErrLogger_Cerr())); yy::ct_parser parser(lexer, sut_model.param_specs_, sut_model.strengths_, sut_model.seeds_, sut_model.constraints_, assembler); parser.parse(); } catch (std::runtime_error e) { std::cerr << e.what() << std::endl; } catch (...) { std::cerr << "unhandled exception when parsing input file" << std::endl; std::cerr << "exiting" << std::endl; return 1; } if (assembler.numErrs() > 0) { std::cerr << assembler.numErrs() << " errors in the input file, exiting" << std::endl; return 2; } std::cout << "successfully parsed the input file" << std::endl; std::cout << "# parameters: " << sut_model.param_specs_.size() << std::endl; std::cout << "# strengths: " << sut_model.strengths_.size() << std::endl; std::cout << "# seeds: " << sut_model.seeds_.size() << std::endl; std::cout << "# constraints: " << sut_model.constraints_.size() << std::endl; std::vector<RawStrength> raw_strengths; TuplePool tuple_pool; for (std::size_t i = 0; i < sut_model.strengths_.size(); ++i) { attach_2_raw_strength(sut_model.strengths_[i], raw_strengths); } for (std::size_t i = 0; i < raw_strengths.size(); ++i) { Tuple tuple; for (std::size_t j = 0; j < raw_strengths[i].size(); ++j) { tuple.push_back(PVPair(raw_strengths[i][j], 0)); } if (tuple.size() <= 0) { continue; } do { tuple_pool.insert(tuple); } while (tuple.to_the_next_tuple(sut_model.param_specs_)); } std::cout << "# target combinations: " << tuple_pool.size() << std::endl; TuplePool forbidden_tuple_pool; for (std::size_t i = 0; i < sut_model.constraints_.size(); ++i) { std::set<std::size_t> rel_pids; sut_model.constraints_[i]->touch_pids(sut_model.param_specs_, rel_pids); Tuple tuple; for (std::set<std::size_t>::const_iterator iter = rel_pids.begin(); iter != rel_pids.end(); iter++) { tuple.push_back(PVPair(*iter, 0)); } do { EvalType_Bool result = sut_model.constraints_[i]->Evaluate(sut_model.param_specs_, tuple); if (!result.is_valid_ || !result.value_) { forbidden_tuple_pool.insert(tuple); } } while (tuple.to_the_next_tuple_with_ivld(sut_model.param_specs_)); } std::cout << "# forbidden combinations: " << forbidden_tuple_pool.size() << std::endl; return 0; }
Tuple *Tuple::In( ListExpr typeInfo, ListExpr value, int errorPos, ListExpr& errorInfo, bool& correct ) { int attrno, algebraId, typeId, noOfAttrs; Word attr; bool valueCorrect; ListExpr first, firstvalue, valuelist, attrlist; attrno = 0; noOfAttrs = 0; Tuple *t = new Tuple( nl->First( typeInfo ) ); attrlist = nl->Second(nl->First(typeInfo)); valuelist = value; correct = true; if (nl->IsAtom(valuelist)) { correct = false; cout << "Error in reading tuple: an atom instead of a list " << "of values." << endl; cout << "Tuple no." << errorPos << endl; cout << "The tuple is: " << endl; nl->WriteListExpr(value); errorInfo = nl->Append(errorInfo, nl->FourElemList( nl->IntAtom(71), nl->SymbolAtom(Tuple::BasicType()), nl->IntAtom(1), nl->IntAtom(errorPos))); delete t; return 0; } else { while (!nl->IsEmpty(attrlist)) { first = nl->First(attrlist); attrlist = nl->Rest(attrlist); algebraId = t->GetTupleType()->GetAttributeType( attrno ).algId; typeId = t->GetTupleType()->GetAttributeType( attrno ).typeId; attrno++; if (nl->IsEmpty(valuelist)) { correct = false; cout << "Error in reading tuple: list of values is empty." << endl; cout << "Tuple no." << errorPos << endl; cout << "The tuple is: " << endl; nl->WriteListExpr(value); errorInfo = nl->Append(errorInfo, nl->FourElemList( nl->IntAtom(71), nl->SymbolAtom(Tuple::BasicType()), nl->IntAtom(2), nl->IntAtom(errorPos))); delete t; return 0; } else { firstvalue = nl->First(valuelist); valuelist = nl->Rest(valuelist); attr = (am->InObj(algebraId, typeId)) (nl->First(nl->Rest(first)), firstvalue, attrno, errorInfo, valueCorrect); if (valueCorrect) { correct = true; t->PutAttribute(attrno - 1, (Attribute *)attr.addr); noOfAttrs++; } else { correct = false; cout << "Error in reading tuple: " << "wrong attribute value representation." << endl; cout << "Tuple no." << errorPos << endl; cout << "The tuple is: " << endl; nl->WriteListExpr(value); cout << endl << "The attribute is: " << endl; nl->WriteListExpr(firstvalue); cout << endl; errorInfo = nl->Append(errorInfo, nl->FiveElemList( nl->IntAtom(71), nl->SymbolAtom(Tuple::BasicType()), nl->IntAtom(3), nl->IntAtom(errorPos), nl->IntAtom(attrno))); delete t; return 0; } } } if (!nl->IsEmpty(valuelist)) { correct = false; cout << "Error in reading tuple: " << "too many attribute values." << endl; cout << "Tuple no." << errorPos << endl; cout << "The tuple is: " << endl; nl->WriteListExpr(value); errorInfo = nl->Append(errorInfo, nl->FourElemList( nl->IntAtom(71), nl->SymbolAtom(Tuple::BasicType()), nl->IntAtom(4), nl->IntAtom(errorPos))); delete t; return 0; } } return t; }
Relation* Insert(vector<string> &words, string &line, SchemaManager &schema_manager, MainMemory &mem){ Relation* relation_ptr = schema_manager.getRelation(words[2]); vector<string>::iterator it = find(words.begin(), words.end(), "SELECT"); // no select if (it == words.end()){ // get insert vals vector<string> content = splitBy(line, "()"); vector<string> fields = splitBy(content[1], ", "); vector<string> vals = splitBy(content[3], ","); //preProcess(vector<string>(1, words[2]), fields, schema_manager); preProcess(vector<string>(1, words[2]), vals, schema_manager); assert(fields.size() == vals.size()); Tuple tuple = relation_ptr->createTuple(); // standard insert doesn't have table names vector<string> col_names = nakedFieldNames(relation_ptr); // comparing for (int i = 0; i < fields.size(); i++){ for (int j = 0; j < col_names.size(); j++){ // this is a match if (fields[i] == col_names[j]){ if (tuple.getSchema().getFieldType(j) == INT){ tuple.setField(j, atoi(vals[i].c_str())); } else{ tuple.setField(j, vals[i]); } break; } } } appendTupleToRelation(relation_ptr, mem, tuple); } // with SELECT else{ vector<string> SFW(it, words.end()); Relation* new_relation = Select(SFW, schema_manager, mem); assert(new_relation); vector<string> new_field_names = nakedFieldNames(new_relation); vector<string> field_names = nakedFieldNames(relation_ptr); // mapping: index of new_field_names to field_names vector<int> mapping(new_field_names.size(), -1); for (int i = 0; i < new_field_names.size(); i++){ for (int j = 0; j < field_names.size(); j++){ if (new_field_names[i] == field_names[j]){ mapping[i] = j; break; } } } int new_field_size = new_relation->getSchema().getNumOfFields(); // warning: new_relation and relation_ptr might be the same! // get all tuples from the new_relation in one run vector<Tuple> new_tuples; for (int i = 0; i < new_relation->getNumOfBlocks(); i++){ assert(!free_blocks.empty()); int memory_block_index = free_blocks.front(); free_blocks.pop(); // read the relation block by block new_relation->getBlock(i, memory_block_index); Block* block_ptr = mem.getBlock(memory_block_index); assert(block_ptr); vector<Tuple> block_tuples = block_ptr->getTuples(); new_tuples.insert(new_tuples.end(), block_tuples.begin(), block_tuples.end()); if(new_tuples.empty()){ cerr<<"Warning: Insert from SFW, No tuples in the current mem block!"<<endl; } free_blocks.push(memory_block_index); } for (int j = 0; j < new_tuples.size(); j++){ Tuple tuple = relation_ptr->createTuple(); for (int k = 0; k < new_field_size; k++){ if (mapping[k] != -1){ int idx = mapping[k]; assert(idx < relation_ptr->getSchema().getNumOfFields() && idx >= 0); if (tuple.getSchema().getFieldType(idx) == INT){ int val = new_tuples[j].getField(k).integer; tuple.setField(field_names[idx], val); } else{ string *str = new_tuples[j].getField(k).str; tuple.setField(field_names[idx], *str); } } } appendTupleToRelation(relation_ptr, mem, tuple); } cout<<*relation_ptr<<endl; } return relation_ptr; }
Relation Relation::select(vector<Token>& inputTokens) { Relation newRelation = (*this); set<Tuple>* newTuples = new set<Tuple>(); map<Token, vector<int> > myMap; for(int i = 0; i < inputTokens.size(); i++) { if(inputTokens[i].getTokenType() == ID) { bool inserted = false; for(map<Token, vector<int> >::iterator it = myMap.begin(); it != myMap.end(); it++) { if(it->first.getTokensValue() == inputTokens[i].getTokensValue()) { it->second.push_back(i); inserted = true; } } if(!inserted) { vector<int> newVec; newVec.push_back(i); myMap.insert(pair<Token, vector<int> >(inputTokens[i], newVec) ); } } } //Goes through all Tuples in THIS relation, checks if all query strings match, and adds matching tuples to the returned relation for(set<Tuple>::iterator it = tuples->begin(); it != tuples->end(); it++) //for all tuples in THIS { bool isTrue = true; Tuple tempTuple = (*it); Tuple thisTuple = tempTuple; for(int j = 0; j < thisTuple.getPairVectorSize(); j++) //for all pairs in this tuple { for(int i = 0; i < inputTokens.size(); i++) //for all parameters in the query { //CHECKS FOR TUPLES WITH CORRECT STRING COMBINATIONS if(inputTokens[i].getTokenType() == STRING && j == i) //if this parameter is the placeholder string { if(inputTokens[i].getTokensValue() != thisTuple.getTokenFromPairAt(j).getTokensValue()) //if the values don't match { isTrue = false; //do not add to the new relation } } //CHECK FOR TUPLES WITH DUPLICATE VALUES FOR DUPLICATE IDs IN QUERY else if(inputTokens[i].getTokenType() == ID) { for(map<Token, vector<int> >::iterator mit = myMap.begin(); mit != myMap.end(); mit++) { for(int k = 0; k < thisTuple.getPairVectorSize(); k++) { for(int h = 0; h < mit->second.size(); h++) { if(k == mit->second[h]) { for(int n = 0; n < mit->second.size(); n++) { if(thisTuple.getPairs()[k].second.getTokensValue() != thisTuple.getPairs()[mit->second[n]].second.getTokensValue()) { isTrue = false; } } } } } } } } } if(isTrue) //otherwise add the tuple to the new relation { newTuples->insert(thisTuple); } } set<Tuple>* deleteSet = newRelation.getTuples(); newRelation.setTuples(newTuples); delete deleteSet; return newRelation; }
Object* get_local(int pos) { if(isolated_) { return heap_locals_->at(pos); } return locals_[pos]; }
int CompiledMethod::start_line(STATE) { if(lines_->nil_p()) return -1; if(lines_->num_fields() < 1) return -1; Tuple* top = as<Tuple>(lines_->at(state, 0)); return as<Fixnum>(top->at(state, 2))->to_native(); }
inline bool operator!=(const Tuple &l, const Tuple &r) { return !l.equal(r); }
KPoint::KPoint(const Tuple& other) : Tuple2D(other.at(0), other.at(1)) { // Nothing; }
inline bool operator!=(const tOther &r, const Tuple &l) { return !l.equal(r); }
bool testTuple( Teuchos::FancyOStream &out ) { using Teuchos::Tuple; using Teuchos::tuple; using Teuchos::ArrayView; using Teuchos::arrayView; using Teuchos::arrayViewFromVector; using Teuchos::outArg; using Teuchos::NullIteratorTraits; using Teuchos::TypeNameTraits; using Teuchos::getConst; using Teuchos::as; typedef typename ArrayView<T>::size_type size_type; bool success = true; out << "\n***" << "\n*** Testing "<<TypeNameTraits<Tuple<T,N> >::name()<<" of size = "<<N << "\n***\n"; Teuchos::OSTab tab(out); // out << "\nA) Initial setup testing ...\n\n"; // Tuple<T,N> t; TEST_EQUALITY_CONST(t.size(),N); for( int i = 0; i < N; ++i ) t[i] = i; // tests non-const operator[](i) { out << "\nTest that t[i] == i ... "; const ArrayView<const T> cav2 = t; bool local_success = true; for( int i = 0; i < N; ++i ) { TEST_ARRAY_ELE_EQUALITY( cav2, i, as<T>(i) ); } if (local_success) out << "passed\n"; else success = false; } { const int n = 1; out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n"; Tuple<T,n> tn = tuple<T>(0); TEST_EQUALITY_CONST(tn.size(),n); out << "Test that tn[i] == i ... "; bool local_success = true; for( int i = 0; i < n; ++i ) { TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) ); } if (local_success) out << "passed\n"; else success = false; } { const int n = 2; out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n"; Tuple<T,n> tn = tuple<T>(0,1); TEST_EQUALITY_CONST(tn.size(),n); out << "Test that tn[i] == i ... "; bool local_success = true; for( int i = 0; i < n; ++i ) { TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) ); } if (local_success) out << "passed\n"; else success = false; } { const int n = 3; out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n"; Tuple<T,n> tn = tuple<T>(0,1,2); TEST_EQUALITY_CONST(tn.size(),n); out << "Test that tn[i] == i ... "; bool local_success = true; for( int i = 0; i < n; ++i ) { TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) ); } if (local_success) out << "passed\n"; else success = false; } { const int n = 4; out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n"; Tuple<T,n> tn = tuple<T>(0,1,2,3); TEST_EQUALITY_CONST(tn.size(),n); out << "Test that tn[i] == i ... "; bool local_success = true; for( int i = 0; i < n; ++i ) { TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) ); } if (local_success) out << "passed\n"; else success = false; } { const int n = 5; out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n"; Tuple<T,n> tn = tuple<T>(0,1,2,3,4); TEST_EQUALITY_CONST(tn.size(),n); out << "Test that tn[i] == i ... "; bool local_success = true; for( int i = 0; i < n; ++i ) { TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) ); } if (local_success) out << "passed\n"; else success = false; } { const int n = 6; out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n"; Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5); TEST_EQUALITY_CONST(tn.size(),n); out << "Test that tn[i] == i ... "; bool local_success = true; for( int i = 0; i < n; ++i ) { TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) ); } if (local_success) out << "passed\n"; else success = false; } { const int n = 7; out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n"; Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6); TEST_EQUALITY_CONST(tn.size(),n); out << "Test that tn[i] == i ... "; bool local_success = true; for( int i = 0; i < n; ++i ) { TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) ); } if (local_success) out << "passed\n"; else success = false; } { const int n = 8; out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n"; Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7); TEST_EQUALITY_CONST(tn.size(),n); out << "Test that tn[i] == i ... "; bool local_success = true; for( int i = 0; i < n; ++i ) { TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) ); } if (local_success) out << "passed\n"; else success = false; } { const int n = 9; out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n"; Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8); TEST_EQUALITY_CONST(tn.size(),n); out << "Test that tn[i] == i ... "; bool local_success = true; for( int i = 0; i < n; ++i ) { TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) ); } if (local_success) out << "passed\n"; else success = false; } { const int n = 10; out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n"; Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8,9); TEST_EQUALITY_CONST(tn.size(),n); out << "Test that tn[i] == i ... "; bool local_success = true; for( int i = 0; i < n; ++i ) { TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) ); } if (local_success) out << "passed\n"; else success = false; } { const int n = 11; out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n"; Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8,9,10); TEST_EQUALITY_CONST(tn.size(),n); out << "Test that tn[i] == i ... "; bool local_success = true; for( int i = 0; i < n; ++i ) { TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) ); } if (local_success) out << "passed\n"; else success = false; } { const int n = 12; out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n"; Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8,9,10,11); TEST_EQUALITY_CONST(tn.size(),n); out << "Test that tn[i] == i ... "; bool local_success = true; for( int i = 0; i < n; ++i ) { TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) ); } if (local_success) out << "passed\n"; else success = false; } { const int n = 13; out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n"; Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8,9,10,11,12); TEST_EQUALITY_CONST(tn.size(),n); out << "Test that tn[i] == i ... "; bool local_success = true; for( int i = 0; i < n; ++i ) { TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) ); } if (local_success) out << "passed\n"; else success = false; } { const int n = 14; out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n"; Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8,9,10,11,12,13); TEST_EQUALITY_CONST(tn.size(),n); out << "Test that tn[i] == i ... "; bool local_success = true; for( int i = 0; i < n; ++i ) { TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) ); } if (local_success) out << "passed\n"; else success = false; } { const int n = 15; out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n"; Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14); TEST_EQUALITY_CONST(tn.size(),n); out << "Test that tn[i] == i ... "; bool local_success = true; for( int i = 0; i < n; ++i ) { TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) ); } if (local_success) out << "passed\n"; else success = false; } { out << "\nTest constructing Array<const T> from Tuple<T,N> ...\n"; const ArrayView<const T> av2 = t; TEST_COMPARE_ARRAYS( av2, t ); } // ToDo: Add more tests! return success; }
/** Same as aref(state, key). */ Object* LookupTable::fetch(STATE, Object* key) { Tuple* entry = find_entry(state, key); if(entry) return entry->at(state, 1); return Qnil; }
Relation *slowCrossJoin(Relation *rptr1, Relation *rptr2, string r3, vector<string> fields1, vector<string> fields2, vector<string> op) { Schema s1, s2; vector <string> fields_r1, fields_r2, fields_r3; vector <enum FIELD_TYPE> types_r3; //Relation *rptr1, *rptr2; vector<string> values_r3; int num_blocks_r1, num_blocks_r2, max_blocks; num_blocks_r1 = rptr1->getNumOfBlocks(); num_blocks_r2 = rptr2->getNumOfBlocks(); max_blocks = mem.getMemorySize(); s1 = rptr1->getSchema(); s2 = rptr2->getSchema(); fields_r1 = s1.getFieldNames(); fields_r2 = s2.getFieldNames(); if(disp) { cout<<"#"<<rptr1->getRelationName()<<" ="<<num_blocks_r1<<endl; cout<<"#"<<rptr2->getRelationName()<<" ="<<num_blocks_r2<<endl; } for(int i=0; i<fields_r1.size(); i++) { fields_r3.push_back(rptr1->getRelationName() + "." + fields_r1[i]); types_r3.push_back(s1.getFieldType(fields_r1[i])); } for(int i=0; i<fields_r2.size(); i++) { fields_r3.push_back(rptr2->getRelationName() + "." + fields_r2[i]); types_r3.push_back(s2.getFieldType(fields_r2[i])); } Schema s3(fields_r3,types_r3); //string r3 = rptr1->getRelationName() + "." + rptr2->getRelationName() + ".CrossJoin"; Relation *rptr3 = schema_manager.createRelation(r3, s3); if(disp) { cout<<"New Relation: "<<r3<<endl; cout<<s3<<endl; } for(int id = 0; id<num_blocks_r2; id += (max_blocks - 2)) { //Get tuples of smaller relation into Main Memory, assuming all can fit in int num_blocks_to_read = ((num_blocks_r2 - id) < (max_blocks - 2))?(num_blocks_r2 - id):(max_blocks-2); rptr2->getBlocks(id, 0, num_blocks_to_read); vector<Tuple> tuples_r3, tuples_r2 = mem.getTuples(0, num_blocks_to_read); //Read one block of relation into Main memory and combine each tuple in the block //with all the tuples in the other Block *block_r1 = mem.getBlock(max_blocks - 2); Block *block_r3 = mem.getBlock(max_blocks - 1); block_r3->clear(); for(int i=0; i<num_blocks_r1; i++) { block_r1->clear(); rptr1->getBlock(i, max_blocks - 2); vector<Tuple> tuples_r1 = block_r1->getTuples(); for(int j =0; j < tuples_r1.size(); j++) { //Check for holes if(tuples_r1[j].isNull()) continue; for(int k=0; k < tuples_r2.size(); k++) { //Check for holes if(tuples_r2[k].isNull()) continue; Tuple tuple = rptr3->createTuple(); if(isJoinTuple(tuples_r1[j], tuples_r2[k], fields1, fields2, op)) { for(int l =0; l < fields_r1.size(); l++) { if(s3.getFieldType(rptr1->getRelationName() + "." + fields_r1[l]) == INT) tuple.setField(rptr1->getRelationName() + "." + fields_r1[l],tuples_r1[j].getField(fields_r1[l]).integer); else tuple.setField(rptr1->getRelationName() + "." + fields_r1[l],*(tuples_r1[j].getField(fields_r1[l]).str)); } for(int l =0; l < fields_r2.size(); l++) { if(s3.getFieldType(rptr2->getRelationName() + "." + fields_r2[l]) == INT) tuple.setField(rptr2->getRelationName() + "." + fields_r2[l],tuples_r2[k].getField(fields_r2[l]).integer); else tuple.setField(rptr2->getRelationName() + "." + fields_r2[l],*(tuples_r2[k].getField(fields_r2[l]).str)); } if(disp) cout<<"New Tuple:"<<tuple<<endl; tuples_r3.push_back(tuple); block_r3->appendTuple(tuple); //If main memory block is full, write it to disk and clear that block if(tuples_r3.size() == s3.getTuplesPerBlock()) { rptr3->setBlock(rptr3->getNumOfBlocks(),max_blocks - 1); tuples_r3.clear(); block_r3->clear(); } } } } } //For the last tuple which might not be full, need to write that to disk too if(tuples_r3.size() !=0) rptr3->setBlock(rptr3->getNumOfBlocks(),max_blocks - 1); if(disp) { cout<<*rptr3<<endl; } } return rptr3; }
void test_at() { Tuple* tuple = new_tuple(); TS_ASSERT_EQUALS(Fixnum::from(4), as<Fixnum>(tuple->at(state, 1))); TS_ASSERT_EQUALS(Qnil, tuple->at(state, 4)); }
Relation *onePassNaturalJoin(Relation *rptr1, Relation *rptr2, string r3, vector<string> fields1, vector<string> fields2, vector<string> op, string r1_name, string r2_name) { Schema s1, s2; bool flag = 1; vector <string> fields_r1, fields_r2, fields_r3; vector <enum FIELD_TYPE> types_r3; //Relation *rptr1, *rptr2; vector<string> values_r3; int num_blocks_r1, num_blocks_r2; num_blocks_r1 = rptr1->getNumOfBlocks(); num_blocks_r2 = rptr2->getNumOfBlocks(); s1 = rptr1->getSchema(); s2 = rptr2->getSchema(); fields_r1 = s1.getFieldNames(); fields_r2 = s2.getFieldNames(); for(int i=0; i<fields_r1.size(); i++){ fields_r3.push_back(r1_name + "." + fields_r1[i]); types_r3.push_back(s1.getFieldType(fields_r1[i])); } //Find common attributes of both relations for(int i=0; i<fields_r2.size(); i++) { flag = 1; for(int k=0; k<op.size(); k++) { if(op[k] == "=" && fields_r2[i] == fields2[k] ) { flag = 0; break; } } if(flag) { fields_r3.push_back(r2_name + "." + fields_r2[i]); types_r3.push_back(s2.getFieldType(fields_r2[i])); } } Schema s3(fields_r3,types_r3); //string r3 = rptr1->getRelationName() + "." + rptr2->getRelationName() + ".NaturalJoin"; Relation *rptr3 = schema_manager.createRelation(r3, s3); if(disp) { displayRelationInfo(rptr3); cout<<s3<<endl; } //Get tuples of smaller relation into Main Memory, assuming all can fit in if( num_blocks_r1 > num_blocks_r2 ) { rptr2->getBlocks(0, 0, num_blocks_r2); vector<Tuple> tuples_r3, tuples_r2 = mem.getTuples(0, num_blocks_r2); //Read one block of relation into Main memory and combine each tuple in the block //with all the tuples in the other Block *block_r1 = mem.getBlock(num_blocks_r2); Block *block_r3 = mem.getBlock(num_blocks_r2 + 1); block_r3->clear(); for(int i=0; i<num_blocks_r1; i++) { block_r1->clear(); rptr1->getBlock(i, num_blocks_r2); vector<Tuple> tuples_r1 = block_r1->getTuples(); for(int j =0; j < tuples_r1.size(); j++) { //Check for holes if(tuples_r1[j].isNull()) continue; for(int k=0; k < tuples_r2.size(); k++) { //Check for holes if(tuples_r2[k].isNull()) continue; Tuple tuple = rptr3->createTuple(); if(isJoinTuple(tuples_r1[j], tuples_r2[k], fields1, fields2, op)) { for(int l =0; l < fields_r1.size(); l++) { if(s3.getFieldType(r1_name + "." + fields_r1[l]) == INT) tuple.setField(r1_name + "." + fields_r1[l],tuples_r1[j].getField(fields_r1[l]).integer); else tuple.setField(r1_name + "." + fields_r1[l],*(tuples_r1[j].getField(fields_r1[l]).str)); } for(int l =0; l < fields_r2.size(); l++) { flag = 1; for(int m = 0; m < op.size(); m++) { if(op[m] == "=" && fields_r2[l].compare(fields2[m]) == 0) { flag = 0; break; } } if(flag) { if(s3.getFieldType(r2_name + "." + fields_r2[l]) == INT) tuple.setField(r2_name + "." + fields_r2[l],tuples_r2[k].getField(fields_r2[l]).integer); else tuple.setField(r2_name + "." + fields_r2[l],*(tuples_r2[k].getField(fields_r2[l]).str)); } } tuples_r3.push_back(tuple); block_r3->appendTuple(tuple); //If main memory block is full, write it to disk and clear that block if(tuples_r3.size() == s3.getTuplesPerBlock()) { rptr3->setBlock(rptr3->getNumOfBlocks(),num_blocks_r2 + 1); tuples_r3.clear(); block_r3->clear(); } } } } } //For the last tuple which might not be full, need to write that to disk too if(tuples_r3.size() !=0) rptr3->setBlock(rptr3->getNumOfBlocks(),num_blocks_r2 + 1); if(disp) { cout<<*rptr3<<endl; } } else { rptr1->getBlocks(0, 0, num_blocks_r1); vector<Tuple> tuples_r3, tuples_r1 = mem.getTuples(0, num_blocks_r1); //Read one block of rptr1 into Main memory and combine each tuple in the block //with all the tuples in rptr2 Block *block_r2 = mem.getBlock(num_blocks_r1); Block *block_r3 = mem.getBlock(num_blocks_r1 + 1); block_r3->clear(); for(int i=0; i<num_blocks_r2; i++) { block_r2->clear(); rptr2->getBlock(i, num_blocks_r1); vector<Tuple> tuples_r2 = block_r2->getTuples(); for(int j =0; j < tuples_r2.size(); j++) { //Check for holes if(tuples_r2[j].isNull()) continue; for(int k=0; k < tuples_r1.size(); k++) { //Check for holes if(tuples_r1[k].isNull()) continue; Tuple tuple = rptr3->createTuple(); if(isJoinTuple(tuples_r1[k], tuples_r2[j], fields1, fields2, op)) { for(int l =0; l < fields_r1.size(); l++) { if(s3.getFieldType(r1_name + "." + fields_r1[l]) == INT) tuple.setField(r1_name + "." + fields_r1[l],tuples_r1[k].getField(fields_r1[l]).integer); else tuple.setField(r1_name + "." + fields_r1[l],*(tuples_r1[k].getField(fields_r1[l]).str)); } for(int l =0; l < fields_r2.size(); l++) { flag = 1; for(int m = 0; m < op.size(); m++) { if(op[m] == "=" && fields_r2[l].compare(fields2[m]) == 0) { flag = 0; break; } } if(flag) { if(s3.getFieldType(r2_name + "." + fields_r2[l]) == INT) tuple.setField(r2_name + "." + fields_r2[l],tuples_r2[j].getField(fields_r2[l]).integer); else tuple.setField(r2_name + "." + fields_r2[l],*(tuples_r2[j].getField(fields_r2[l]).str)); } } tuples_r3.push_back(tuple); block_r3->appendTuple(tuple); //If main memory block is full, write it to disk and clear that block if(tuples_r3.size() == s3.getTuplesPerBlock()) { rptr3->setBlock(rptr3->getNumOfBlocks(),num_blocks_r1 + 1); tuples_r3.clear(); block_r3->clear(); } } } } } //For the last tuple which might not be full, need to write that to disk too if(tuples_r3.size() !=0) rptr3->setBlock(rptr3->getNumOfBlocks(),num_blocks_r1 + 1); if(disp) { cout<<*rptr3<<endl; } } return rptr3; }
void test_put_prim() { Tuple* tuple = new_tuple(); tuple->put_prim(state, Fixnum::from(1), Fixnum::from(22)); TS_ASSERT_EQUALS(Fixnum::from(22), as<Fixnum>(tuple->at_prim(state, Fixnum::from(1)))); }
Relation *twoPassNaturalJoin(Relation *rptr1, Relation *rptr2, string r3, vector<string> fields1, vector<string> fields2, vector<string> op) { int i1,j1,k,l,m,c1,c2; i1=j1=k=l=m=c1=c2 = 0; Schema s1, s2; bool flag = 1; vector <string> fields_r1, fields_r2, fields_r3; vector<enum FIELD_TYPE> field_type1, field_type2, types_r3; Block *block_ptr1, *block_ptr2; //Relation *rptr1, *rptr2; vector<string> values_r3; int num_blocks_r1, num_blocks_r2, max_blocks; num_blocks_r1 = rptr1->getNumOfBlocks(); num_blocks_r2 = rptr2->getNumOfBlocks(); max_blocks = mem.getMemorySize(); s1 = rptr1->getSchema(); s2 = rptr2->getSchema(); fields_r1 = s1.getFieldNames(); fields_r2 = s2.getFieldNames(); for(int i=0; i< fields1.size(); i++) field_type1.push_back(s1.getFieldType(fields1[i])); //Create sorted sublists of size <M sortRelation(rptr1, s1, fields1, field_type1); for(int i=0; i< fields2.size(); i++) field_type2.push_back(s2.getFieldType(fields2[i])); //Create sorted sublists of size <M sortRelation(rptr2, s2, fields2, field_type2); for(int i=0; i<fields_r1.size(); i++){ fields_r3.push_back(rptr1->getRelationName() + "." + fields_r1[i]); types_r3.push_back(s1.getFieldType(fields_r1[i])); } //Find common attributes of both relations for(int i=0; i<fields_r2.size(); i++) { flag = 1; for(int k=0; k<op.size(); k++) { if(op[k] == "=" && fields_r2[i] == fields2[k] ) { flag = 0; break; } } if(flag) { fields_r3.push_back(rptr2->getRelationName() + "." + fields_r2[i]); types_r3.push_back(s2.getFieldType(fields_r2[i])); } } Schema s3(fields_r3,types_r3); //string r3 = rptr1->getRelationName() + "." + rptr2->getRelationName() + ".NaturalJoin"; Relation *rptr3 = schema_manager.createRelation(r3, s3); if(disp) { //displayRelationInfo(rptr3); //cout<<s3<<endl; cout<<*rptr1<<endl; cout<<*rptr2<<endl; } int num_sublists_r1 = num_blocks_r1/(max_blocks - 1)+((num_blocks_r1%(max_blocks - 1) > 0)?1:0); int num_sublists_r2 = num_blocks_r2/(max_blocks - 1)+((num_blocks_r2%(max_blocks - 1) > 0)?1:0); vector<int> block_used_r1, block_used_r2, disk_block_index_r1, disk_block_index_r2; if(disp) { cout<<"number of sublists in r1: "<<num_sublists_r1<<endl; cout<<"number of sublists in r2: "<<num_sublists_r2<<endl; } //Get one block from each sublist into main memory for(k = 0; k < num_sublists_r1; k++) { if(disp) { cout<<"k: "<<k<<endl; cout<<"disk block index: "<<k*(max_blocks-1)<<endl; } block_ptr1 = mem.getBlock(k); rptr1->getBlock(k*(max_blocks-1), k); disk_block_index_r1.push_back(1); block_used_r1.push_back(block_ptr1->getNumTuples()); //block_ptr1->clear(); } for(l = 0; l < num_sublists_r2; l++) { if(disp) { cout<<"l: "<<l<<endl; cout<<"disk block index: "<<l*(max_blocks - 1)<<endl; } block_ptr2 = mem.getBlock(l+num_sublists_r1); rptr2->getBlock(l*(max_blocks-1), l + num_sublists_r1); disk_block_index_r2.push_back(1); block_used_r2.push_back(block_ptr2->getNumTuples()); //block_ptr2->clear(); } if(disp) { cout<<"Block Used 1: "<<block_used_r1.size()<<endl; cout<<"Block Used 2: "<<block_used_r2.size()<<endl; } vector<Tuple> tuples_r3; //Read one block of relation into Main memory and combine each tuple in the block //with all the tuples in the other //Block *block_r1 = mem.getBlock(max_blocks - 2); Block *block_r3 = mem.getBlock(max_blocks - 1); block_r3->clear(); i1 = j1 = 1; bool done = false; int block_id, tuple_offset; while(!done) { vector<Tuple> tuples_r1 = mem.getTuples(0, num_sublists_r1); vector<Tuple> tuples_r2 = mem.getTuples(num_sublists_r1, num_sublists_r2); Tuple tuple = rptr3->createTuple(); int rel_no = 0; int id = findSmallest(tuples_r1, tuples_r2, fields1, fields2, rel_no); if(disp) cout<<"id: "<<id<<"rel: "<<rel_no<<endl; if(rel_no == 1) { if(disp) cout<<"Smallest Tuple: "<<tuples_r1[id]<<endl; for(m = 0; m < tuples_r2.size(); m++) { if(isJoinTuple(tuples_r1[id], tuples_r2[m], fields1, fields2, op)) { if(disp) { cout<<"\n********************\nJoining:"<<endl; cout<<tuples_r1[id]<<endl; cout<<tuples_r2[m]<<endl; } for(int l =0; l < fields_r1.size(); l++) { if(s3.getFieldType(rptr1->getRelationName() + "." + fields_r1[l]) == INT) tuple.setField(rptr1->getRelationName() + "." + fields_r1[l],tuples_r1[id].getField(fields_r1[l]).integer); else tuple.setField(rptr1->getRelationName() + "." + fields_r1[l],*(tuples_r1[id].getField(fields_r1[l]).str)); } for(int l =0; l < fields_r2.size(); l++) { int flag = 1; for(int n = 0; n < op.size(); n++) { if(op[n] == "=" && fields_r2[l].compare(fields2[n]) == 0) { flag = 0; break; } } if(flag) { if(s3.getFieldType(rptr2->getRelationName() + "." + fields_r2[l]) == INT) tuple.setField(rptr2->getRelationName() + "." + fields_r2[l],tuples_r2[m].getField(fields_r2[l]).integer); else tuple.setField(rptr2->getRelationName() + "." + fields_r2[l],*(tuples_r2[m].getField(fields_r2[l]).str)); } } if(disp) cout<<"New Tuple:"<<tuple<<endl; tuples_r3.push_back(tuple); block_r3->appendTuple(tuple); //If main memory block is full, write it to disk and clear that block if(tuples_r3.size() == s3.getTuplesPerBlock()) { rptr3->setBlock(rptr3->getNumOfBlocks(),max_blocks - 1); tuples_r3.clear(); block_r3->clear(); } } } //block_ptr1->clear(); if(s1.getTuplesPerBlock() == 1) { block_id = id; tuple_offset = 0; } else { block_id = id/num_sublists_r1; tuple_offset = id%num_sublists_r1; } block_used_r1[block_id]--; block_ptr1 = mem.getBlock(block_id); block_ptr1->nullTuple(tuple_offset); //tuples_r1[id].null(); //If a particular block of tuples has been used in the main memory, replenish from disk if(block_used_r1[block_id] == 0) { if(disp) cout<<"Block list consumed: "<<block_id<<endl; //If we have used up all the blocks in this sublist if(exhaustedAllSublists(block_used_r1)) done = true; else { cout<<disk_block_index_r1[block_id]<<endl; if(disk_block_index_r1[block_id] == ((max_blocks-1 < num_blocks_r1) ? (max_blocks - 1) : num_blocks_r1)) { continue; } else { block_ptr1->clear(); block_ptr1 = mem.getBlock(block_id); rptr1->getBlock(disk_block_index_r1[block_id] + (block_id)*(max_blocks-1), block_id); block_used_r1[block_id] = block_ptr1->getNumTuples(); ++disk_block_index_r1[block_id]; } } } } else if(rel_no == 2) { if(disp) cout<<"Smallest Tuple: "<<tuples_r2[id]<<endl; for(m = 0; m < tuples_r1.size(); m++) { if(isJoinTuple(tuples_r1[m], tuples_r2[id], fields1, fields2, op)) { if(disp) { cout<<"\n********************\nJoining:"<<endl; cout<<tuples_r1[m]<<endl; cout<<tuples_r2[id]<<endl; } for(int l =0; l < fields_r1.size(); l++) { if(s3.getFieldType(rptr1->getRelationName() + "." + fields_r1[l]) == INT) tuple.setField(rptr1->getRelationName() + "." + fields_r1[l],tuples_r1[m].getField(fields_r1[l]).integer); else tuple.setField(rptr1->getRelationName() + "." + fields_r1[l],*(tuples_r1[m].getField(fields_r1[l]).str)); } for(int l =0; l < fields_r2.size(); l++) { int flag = 1; for(int n = 0; n < op.size(); n++) { if(op[n] == "=" && fields_r2[l].compare(fields2[n]) == 0) { flag = 0; break; } } if(flag) { if(s3.getFieldType(rptr2->getRelationName() + "." + fields_r2[l]) == INT) tuple.setField(rptr2->getRelationName() + "." + fields_r2[l],tuples_r2[id].getField(fields_r2[l]).integer); else tuple.setField(rptr2->getRelationName() + "." + fields_r2[l],*(tuples_r2[id].getField(fields_r2[l]).str)); } } if(disp) cout<<"New Tuple:"<<tuple<<endl; tuples_r3.push_back(tuple); block_r3->appendTuple(tuple); //If main memory block is full, write it to disk and clear that block if(tuples_r3.size() == s3.getTuplesPerBlock()) { rptr3->setBlock(rptr3->getNumOfBlocks(),max_blocks - 1); tuples_r3.clear(); block_r3->clear(); } } } //block_ptr2->clear(); if(s2.getTuplesPerBlock() == 1) { block_id = id; tuple_offset = 0; } else { block_id = id/num_sublists_r2; tuple_offset = id%num_sublists_r2; } block_used_r2[block_id]--; block_ptr2 = mem.getBlock(num_sublists_r1 + block_id); block_ptr2->nullTuple(tuple_offset); //If a particular block of tuples has been used in the main memory, replenish from disk if(block_used_r2[block_id] == 0) { if(disp) cout<<"Block list consumed: "<<block_id<<endl; //If we have used up all the blocks in this sublist if(exhaustedAllSublists(block_used_r2)) done = true; else { cout<<disk_block_index_r2[block_id]<<endl; if(disk_block_index_r2[block_id] == ((max_blocks-1<< num_blocks_r2) ? (max_blocks - 1) : num_blocks_r2)) { continue; } else { block_ptr2->clear(); block_ptr2 = mem.getBlock(num_sublists_r1 + block_id); rptr2->getBlock(disk_block_index_r2[block_id] + (block_id)*(max_blocks-1), num_sublists_r1 + block_id); block_used_r2[block_id] = block_ptr2->getNumTuples(); ++disk_block_index_r2[block_id]; } } } } if(disp) { cout<<"#r3: "<<rptr3->getNumOfTuples()<<endl; //cout<<*rptr3<<endl; //cin.get(); } } //For the last tuple which might not be full, need to write that to disk too if(tuples_r3.size() !=0) rptr3->setBlock(rptr3->getNumOfBlocks(),max_blocks - 1); if(disp) { cout<<*rptr3<<endl; } return rptr3; }
bool Adapt::adapt(Tuple<RefinementSelectors::Selector *> refinement_selectors, double thr, int strat, int regularize, double to_be_processed) { error_if(!have_errors, "element errors have to be calculated first, call Adapt::calc_err_est()."); error_if(refinement_selectors == Tuple<RefinementSelectors::Selector *>(), "selector not provided"); if (spaces.size() != refinement_selectors.size()) error("Wrong number of refinement selectors."); TimePeriod cpu_time; //get meshes int max_id = -1; Mesh* meshes[H2D_MAX_COMPONENTS]; for (int j = 0; j < this->num; j++) { meshes[j] = this->spaces[j]->get_mesh(); rsln[j]->set_quad_2d(&g_quad_2d_std); rsln[j]->enable_transform(false); if (meshes[j]->get_max_element_id() > max_id) max_id = meshes[j]->get_max_element_id(); } //reset element refinement info AUTOLA2_OR(int, idx, max_id + 1, this->num + 1); for(int j = 0; j < max_id; j++) for(int l = 0; l < this->num; l++) idx[j][l] = -1; // element not refined double err0_squared = 1000.0; double processed_error_squared = 0.0; vector<ElementToRefine> elem_inx_to_proc; //list of indices of elements that are going to be processed elem_inx_to_proc.reserve(num_act_elems); //adaptivity loop double error_squared_threshod = -1; //an error threshold that breaks the adaptivity loop in a case of strategy 1 int num_exam_elem = 0; //a number of examined elements int num_ignored_elem = 0; //a number of ignored elements int num_not_changed = 0; //a number of element that were not changed int num_priority_elem = 0; //a number of elements that were processed using priority queue bool first_regular_element = true; //true if first regular element was not processed yet int inx_regular_element = 0; while (inx_regular_element < num_act_elems || !priority_queue.empty()) { int id, comp, inx_element; //get element identification if (priority_queue.empty()) { id = regular_queue[inx_regular_element].id; comp = regular_queue[inx_regular_element].comp; inx_element = inx_regular_element; inx_regular_element++; } else { id = priority_queue.front().id; comp = priority_queue.front().comp; inx_element = -1; priority_queue.pop(); num_priority_elem++; } num_exam_elem++; //get info linked with the element double err_squared = errors[comp][id]; Mesh* mesh = meshes[comp]; Element* e = mesh->get_element(id); if (!should_ignore_element(inx_element, mesh, e)) { //check if adaptivity loop should end if (inx_element >= 0) { //prepare error threshold for strategy 1 if (first_regular_element) { error_squared_threshod = thr * err_squared; first_regular_element = false; } // first refinement strategy: // refine elements until prescribed amount of error is processed // if more elements have similar error refine all to keep the mesh symmetric if ((strat == 0) && (processed_error_squared > sqrt(thr) * errors_squared_sum) && fabs((err_squared - err0_squared)/err0_squared) > 1e-3) break; // second refinement strategy: // refine all elements whose error is bigger than some portion of maximal error if ((strat == 1) && (err_squared < error_squared_threshod)) break; if ((strat == 2) && (err_squared < thr)) break; if ((strat == 3) && ( (err_squared < error_squared_threshod) || ( processed_error_squared > 1.5 * to_be_processed )) ) break; } // get refinement suggestion ElementToRefine elem_ref(id, comp); int current = this->spaces[comp]->get_element_order(id); bool refined = refinement_selectors[comp]->select_refinement(e, current, rsln[comp], elem_ref); //add to a list of elements that are going to be refined if (can_refine_element(mesh, e, refined, elem_ref) ) { idx[id][comp] = (int)elem_inx_to_proc.size(); elem_inx_to_proc.push_back(elem_ref); err0_squared = err_squared; processed_error_squared += err_squared; } else { debug_log("Element (id:%d, comp:%d) not changed", e->id, comp); num_not_changed++; } } else { num_ignored_elem++; } } verbose("Examined elements: %d", num_exam_elem); verbose(" Elements taken from priority queue: %d", num_priority_elem); verbose(" Ignored elements: %d", num_ignored_elem); verbose(" Not changed elements: %d", num_not_changed); verbose(" Elements to process: %d", elem_inx_to_proc.size()); bool done = false; if (num_exam_elem == 0) done = true; else if (elem_inx_to_proc.empty()) { warn("None of the elements selected for refinement could be refined. Adaptivity step not successful, returning 'true'."); done = true; } //fix refinement if multimesh is used fix_shared_mesh_refinements(meshes, elem_inx_to_proc, idx, refinement_selectors); //apply refinements apply_refinements(elem_inx_to_proc); // in singlemesh case, impose same orders across meshes homogenize_shared_mesh_orders(meshes); // mesh regularization if (regularize >= 0) { if (regularize == 0) { regularize = 1; warn("Total mesh regularization is not supported in adaptivity. 1-irregular mesh is used instead."); } for (int i = 0; i < this->num; i++) { int* parents; parents = meshes[i]->regularize(regularize); this->spaces[i]->distribute_orders(meshes[i], parents); delete [] parents; } } for (int j = 0; j < this->num; j++) rsln[j]->enable_transform(true); verbose("Refined elements: %d", elem_inx_to_proc.size()); report_time("Refined elements in: %g s", cpu_time.tick().last()); //store for the user to retrieve last_refinements.swap(elem_inx_to_proc); have_errors = false; if (strat == 2 && done == true) have_errors = true; // space without changes // since space changed, assign dofs: Space::assign_dofs(this->spaces); return done; }
Object* get_local(STATE, int pos) { if(isolated_) { return heap_locals_->at(state, pos); } return locals_[pos]; }
ModEvent CPTupleVarImp::exclude(Space& home, const Tuple& t) { TupleSet s(t.arity()); s.add(t); return exclude(home,s); }
static void calc_pressure_func(int n, Tuple<scalar*> scalars, scalar* result) { for (int i = 0; i < n; i++) result[i] = num_flux.R/num_flux.c_v * (scalars.at(3)[i] - (scalars.at(1)[i]*scalars.at(1)[i] + scalars.at(2)[i]*scalars.at(2)[i])/(2*scalars.at(0)[i])); };