TEST(ffi, opaque) { Set points; FieldRef<int> a = points.addField<int>("a"); FieldRef<int> b = points.addField<int>("b"); ElementRef p0 = points.add(); ElementRef p1 = points.add(); ElementRef p2 = points.add(); a(p0) = 1.0; a(p1) = 2.0; a(p2) = 3.0; b(p0) = 0.0; b(p1) = 0.0; b(p2) = 0.0; Function func = loadFunction(TEST_FILE_NAME, "main"); if (!func.defined()) FAIL(); func.bind("points", &points); func.runSafe(); ASSERT_EQ((int)a(p0), (int)b(p0)); ASSERT_EQ((int)a(p1), (int)b(p1)); ASSERT_EQ((int)a(p2), (int)b(p2)); }
/* * Recursive Computer Search * * Recursively searches the Boggle board to find all the * words that can be made on the board and haven't * been guessed correctly by the human player */ void recursiveComputer(int row, int col, string word, Grid<string>& board, Lexicon& dictionary, Set<string>& results, Grid<bool>& visitedCubes, int& computerScore, Set<string>& usedWords){ if(!board.inBounds(row, col)) return; if(dictionary.contains(word) && word.length() >= 4) { if (!results.contains(word) && !usedWords.contains(word)) { computerScore += word.length() - 3; results.add(word); } } if(!dictionary.containsPrefix(word)){ return; } word = word + board[row][col]; visitedCubes[row][col] = true; for(int i = -1; i <= 1; i++){ for(int j = -1; j <= 1; j++){ if(visitedCubes.inBounds(row + i, col + j) && !visitedCubes[row + i][col + j]){ recursiveComputer(row + i, col + j, word, board, dictionary, results, visitedCubes, computerScore, usedWords); } if(visitedCubes.inBounds(row + i, col + j) && dictionary.contains(word) && !results.contains(word) && word.length() >=4) { computerScore += word.length() - 3; results.add(word); } } } visitedCubes[row][col] = false; }
TEST(ffi, vector_add) { Set points; FieldRef<simit_float> a = points.addField<simit_float>("a"); FieldRef<simit_float> b = points.addField<simit_float>("b"); FieldRef<simit_float> c = points.addField<simit_float>("c"); ElementRef p0 = points.add(); a.set(p0, 42.0); b.set(p0, 24.0); c.set(p0, -1.0); ElementRef p1 = points.add(); a.set(p1, 20.0); b.set(p1, 14.0); c.set(p1, -1.0); ElementRef p2 = points.add(); a.set(p2, 12.0); b.set(p2, 21.0); c.set(p2, -1.0); Function func = loadFunction(TEST_FILE_NAME, "main"); if (!func.defined()) FAIL(); func.bind("points", &points); func.runSafe(); SIMIT_EXPECT_FLOAT_EQ(66.0, (int)c.get(p0)); SIMIT_EXPECT_FLOAT_EQ(34.0, (int)c.get(p1)); SIMIT_EXPECT_FLOAT_EQ(33.0, (int)c.get(p2)); }
TEST(System, gemv_blocked_scaled_diagonal) { Set points; FieldRef<simit_float,2> b = points.addField<simit_float,2>("b"); FieldRef<simit_float,2> c = points.addField<simit_float,2>("c"); FieldRef<simit_float,2> a = points.addField<simit_float,2>("a"); ElementRef p0 = points.add(); ElementRef p1 = points.add(); ElementRef p2 = points.add(); a.set(p0, {1.0, 2.0}); a.set(p1, {3.0, 4.0}); a.set(p2, {5.0, 6.0}); b.set(p0, {1.0, 1.0}); b.set(p1, {1.0, 1.0}); b.set(p2, {1.0, 1.0}); Function func = loadFunction(TEST_FILE_NAME, "main"); if (!func.defined()) FAIL(); func.bind("points", &points); func.runSafe(); ASSERT_EQ(30.0, c.get(p0)(0)); ASSERT_EQ(60.0, c.get(p0)(1)); ASSERT_EQ(210.0, c.get(p1)(0)); ASSERT_EQ(280.0, c.get(p1)(1)); ASSERT_EQ(550.0, c.get(p2)(0)); ASSERT_EQ(660.0, c.get(p2)(1)); }
TEST(ffi, matrix_neg) { Set V; FieldRef<simit_float> a = V.addField<simit_float>("a"); FieldRef<simit_float> b = V.addField<simit_float>("b"); ElementRef v0 = V.add(); ElementRef v1 = V.add(); ElementRef v2 = V.add(); b(v0) = 1.0; b(v1) = 2.0; b(v2) = 3.0; Set E(V,V); FieldRef<simit_float> e = E.addField<simit_float>("e"); ElementRef e0 = E.add(v0,v1); ElementRef e1 = E.add(v1,v2); e(e0) = 1.0; e(e1) = 2.0; // Compile program and bind arguments Function func = loadFunction(TEST_FILE_NAME, "main"); if (!func.defined()) FAIL(); func.bind("V", &V); func.bind("E", &E); func.runSafe(); // Check that inputs are preserved ASSERT_EQ(1.0, (double)b(v0)); ASSERT_EQ(2.0, (double)b(v1)); ASSERT_EQ(3.0, (double)b(v2)); // Check that outputs are correct ASSERT_EQ(-3.0, (double)a(v0)); ASSERT_EQ(-13.0, (double)a(v1)); ASSERT_EQ(-10.0, (double)a(v2)); }
void testAuthor() { String *lname, *fname; lname = new String("Erdos"); fname = new String("P."); Author *n = new Author(fname, lname); Set<Author> *targets = new Set<Author>(); Author *x, *k; for (int i = 0; i < MAX_AUTHORS; i++) { lname = new String(i); fname = new String(i); x = new Author(fname, lname); n->publicouCom(x); lname = new String(i); fname = new String(i); k = new Author(fname, lname); x->publicouCom(k); targets->add(k); } if (k != null) { targets->add(k); } printf("targets %d\n", targets->size()); getchar(); Author::erdosPtr->process(targets); }
int main() { Set a(63), c(63); Set d(65); a.print(cout); cout << endl; a.add(1); a.add(5); a.print(cout); cout << endl; Set b = a; b.add(3); if (b.has(3)) b.remove(3); if (b.has(5)) b.add(6); //b.has(-10); //b.add(64); //b.remove(64); b.print(cout); cout << endl; c.add(8); c.add(a); c.print(cout); cout << endl; //d.add(a); a.complement(); a.print(cout); cout << endl; cout << "Finished\n"; }
//creates the union set by adding numbers from both sets to the union Set Set::Union(Set set1, Set set2) { vector<int> tempSet1=set1.getSet(); vector<int> tempSet2=set2.getSet(); Set unionSet; for(auto number1 : tempSet1) { unionSet.add(number1); } for(auto number2 : tempSet2) { unionSet.add(number2); } vector<int>Union=unionSet.getSet(); cout << "The union of the two sets is:" << endl; for(auto integer : Union) { cout << integer << endl; } return unionSet; }
static void generateAllPossibleWords(const Grid<char> & boggleBoard, Set<string> & wordsSpottedSoFar, const Lexicon & english, int rowIndex, int colIndex, string buildingWord, Set<coord> wordPath){ buildingWord += boggleBoard[rowIndex][colIndex]; if (buildingWord.size() >= kMinGuessLength && english.contains(buildingWord) && !wordsSpottedSoFar.contains(buildingWord)) { wordsSpottedSoFar.add(buildingWord); recordWordForPlayer(buildingWord, COMPUTER); } if (!english.containsPrefix(buildingWord)) return; for (int i = -1; i <= 1; i++) { for (int j = -1; j <=1; j++){ if(isShiftValid(boggleBoard, rowIndex, colIndex, i, j)){ coord nextPos; nextPos.row = rowIndex + i; nextPos.col = colIndex + j; if (!wordPath.contains(nextPos)){ wordPath.add(nextPos); generateAllPossibleWords(boggleBoard, wordsSpottedSoFar, english, rowIndex + i, colIndex + j, buildingWord, wordPath); wordPath.remove(nextPos); } } } } }
TEST(System, gemv_input) { // Points Set points; FieldRef<simit_float> b = points.addField<simit_float>("b"); FieldRef<simit_float> c = points.addField<simit_float>("c"); ElementRef p0 = points.add(); ElementRef p1 = points.add(); ElementRef p2 = points.add(); b.set(p0, 1.0); b.set(p1, 2.0); b.set(p2, 3.0); // Taint c c.set(p0, 42.0); c.set(p2, 42.0); // Springs Set springs(points,points); FieldRef<simit_float> a = springs.addField<simit_float>("a"); ElementRef s0 = springs.add(p0,p1); ElementRef s1 = springs.add(p1,p2); a.set(s0, 1.0); a.set(s1, 2.0); // Compile program and bind arguments Function func = loadFunction(TEST_FILE_NAME, "main"); if (!func.defined()) FAIL(); simit::Tensor<simit_float, 2> cs; cs(0) = 1.0; cs(1) = 2.0; func.bind("c", &cs); func.bind("points", &points); func.bind("springs", &springs); func.runSafe(); // Check that inputs are preserved ASSERT_EQ(1.0, b.get(p0)); ASSERT_EQ(2.0, b.get(p1)); ASSERT_EQ(3.0, b.get(p2)); // Check that outputs are correct ASSERT_EQ(6.0, (simit_float)c.get(p0)); ASSERT_EQ(26.0, (simit_float)c.get(p1)); ASSERT_EQ(20.0, (simit_float)c.get(p2)); }
TEST(System, gemv_blocked_computed) { // Points Set points; FieldRef<simit_float,2> b = points.addField<simit_float,2>("b"); FieldRef<simit_float,2> c = points.addField<simit_float,2>("c"); FieldRef<simit_float,2> x = points.addField<simit_float,2>("x"); ElementRef p0 = points.add(); ElementRef p1 = points.add(); ElementRef p2 = points.add(); b.set(p0, {1.0, 2.0}); b.set(p1, {3.0, 4.0}); b.set(p2, {5.0, 6.0}); x.set(p0, {1.0, 2.0}); x.set(p1, {3.0, 4.0}); x.set(p2, {5.0, 6.0}); // Taint c c.set(p0, {42.0, 42.0}); c.set(p2, {42.0, 42.0}); // Springs Set springs(points,points); springs.add(p0,p1); springs.add(p1,p2); // Compile program and bind arguments Function func = loadFunction(TEST_FILE_NAME, "main"); if (!func.defined()) FAIL(); func.bind("points", &points); func.bind("springs", &springs); func.runSafe(); // Check that outputs are correct // TODO: add support for comparing a tensorref like so: b0 == {1.0, 2.0, 3.0} TensorRef<simit_float,2> c0 = c.get(p0); ASSERT_EQ(36.0, c0(0)); ASSERT_EQ(72.0, c0(1)); TensorRef<simit_float,2> c1 = c.get(p1); ASSERT_EQ(336.0, c1(0)); ASSERT_EQ(472.0, c1(1)); TensorRef<simit_float,2> c2 = c.get(p2); ASSERT_EQ(300.0, c2(0)); ASSERT_EQ(400.0, c2(1)); }
TEST(System, gemv_storage) { // This test tests whether we determine storage correctly for matrices // that do not come (directly) from maps // Points Set points; FieldRef<simit_float> b = points.addField<simit_float>("b"); FieldRef<simit_float> c = points.addField<simit_float>("c"); ElementRef p0 = points.add(); ElementRef p1 = points.add(); ElementRef p2 = points.add(); b.set(p0, 1.0); b.set(p1, 2.0); b.set(p2, 3.0); // Taint c c.set(p0, 42.0); c.set(p2, 42.0); // Springs Set springs(points,points); FieldRef<simit_float> a = springs.addField<simit_float>("a"); ElementRef s0 = springs.add(p0,p1); ElementRef s1 = springs.add(p1,p2); a.set(s0, 1.0); a.set(s1, 2.0); // Compile program and bind arguments Function func = loadFunction(TEST_FILE_NAME, "main"); if (!func.defined()) FAIL(); func.bind("points", &points); func.bind("springs", &springs); func.runSafe(); // Check that inputs are preserved ASSERT_EQ(1.0, b.get(p0)); ASSERT_EQ(2.0, b.get(p1)); ASSERT_EQ(3.0, b.get(p2)); // Check that outputs are correct ASSERT_EQ(6.0, c.get(p0)); ASSERT_EQ(26.0, c.get(p1)); ASSERT_EQ(20.0, c.get(p2)); }
TEST(ffi, gemv_blocked) { // Points Set points; FieldRef<simit_float,2> b = points.addField<simit_float,2>("b"); FieldRef<simit_float,2> c = points.addField<simit_float,2>("c"); ElementRef p0 = points.add(); ElementRef p1 = points.add(); ElementRef p2 = points.add(); b.set(p0, {1.0, 2.0}); b.set(p1, {3.0, 4.0}); b.set(p2, {5.0, 6.0}); // Taint c c.set(p0, {42.0, 42.0}); c.set(p2, {42.0, 42.0}); // Springs Set springs(points,points); FieldRef<simit_float,2,2> a = springs.addField<simit_float,2,2>("a"); ElementRef s0 = springs.add(p0,p1); ElementRef s1 = springs.add(p1,p2); a.set(s0, {1.0, 2.0, 3.0, 4.0}); a.set(s1, {5.0, 6.0, 7.0, 8.0}); // Compile program and bind arguments Function func = loadFunction(TEST_FILE_NAME, "main"); if (!func.defined()) FAIL(); func.bind("points", &points); func.bind("springs", &springs); func.runSafe(); // Check that outputs are correct // TODO: add support for comparing a tensorref like so: b0 == {1.0, 2.0, 3.0} TensorRef<simit_float,2> c0 = c.get(p0); SIMIT_EXPECT_FLOAT_EQ(16.0, c0(0)); SIMIT_EXPECT_FLOAT_EQ(36.0, c0(1)); TensorRef<simit_float,2> c1 = c.get(p1); SIMIT_EXPECT_FLOAT_EQ(116.0, c1(0)); SIMIT_EXPECT_FLOAT_EQ(172.0, c1(1)); TensorRef<simit_float,2> c2 = c.get(p2); SIMIT_EXPECT_FLOAT_EQ(100.0, c2(0)); SIMIT_EXPECT_FLOAT_EQ(136.0, c2(1)); }
//Beware - FindSolution works only for 4x4 game!!! void Fifteen::FindSolution() { CopyTGameArray(GameArray, GameStartArray); points = 0; Set* nothing; Set* closedSet = new Set(); Set* openSet = new Set(); Set* best = NULL; openSet->add(openSet, 0, GameArray, openSet, nothing); while (!openSet->empty()) { openSet->best(openSet, best); closedSet->add(closedSet, best->G, best->move, best->prev, best); openSet->remove(openSet, best->move); CopyTGameArray(GameArray, best->move); if(best->H < 1) { SetSolution(best); ShowSolution(); closedSet->DeleteAll(closedSet); openSet->DeleteAll(openSet); return; } int zeroX; int zeroY; FindZero(zeroX, zeroY, best->move); for (int i = 0; i < 4; i++) { int dx = (-1) * ((zeroX > 0) && (i == 0)) + ((zeroX < SIZE - 1) && (i == 1)); int dy = (-1) * ((zeroY > 0) && (i == 2)) + ((zeroY < SIZE - 1) && (i == 3)); TGameArray newArray; CopyTGameArray(newArray, best->move); newArray[zeroX][zeroY] = best->move[zeroX+dx][zeroY+dy]; newArray[zeroX+dx][zeroY+dy] = 0; if(!closedSet->isAlready(closedSet, newArray)) { openSet->add(openSet, best->G + 1, newArray, best, nothing); } } } }
bool solvePuzzle(Grid<MarbleType>& board, int marblesLeft, Set<uint32_t>& exploredBoards, Vector<Move>& moveHistory){ /* TODO: COMPLETE THIS */ if (exploredBoards.size()%10000 == 0){ cout << "Boards evaluated: " << exploredBoards.size() << "\tDepth: " << moveHistory.size() << endl; cout.flush(); } if(marblesLeft == 1) return true; if(exploredBoards.contains(compressMarbleBoard(board))) return false; exploredBoards.add(compressMarbleBoard(board)); Vector<Move> moves = findPossibleMoves(board); for(int i=0; i<moves.size(); ++i){ Move move = moves[i]; makeMove(move, board); moveHistory.add(move); if(solvePuzzle(board, marblesLeft-1,exploredBoards, moveHistory)) { return true; } else{ undoMove(move, board); moveHistory.remove(moveHistory.size()-1); } } return false; }
static std::string addAutograderButton(GWindow& gui, const std::string& text, const std::string& icon) { static Set<char> usedMnemonics; std::string html = "<html><center>" + stringReplace(text, "\n", "<br>") + "</center></html>"; GButton* button = new GButton(html); STATIC_VARIABLE(AUTOGRADER_BUTTONS).add(button); // set mnemonic shortcut char mnemonic = '\0'; for (int i = 0; i < (int) text.length(); i++) { if (isalpha(text[i]) && !usedMnemonics.contains(text[i])) { mnemonic = text[i]; break; } } if (mnemonic) { usedMnemonics.add(mnemonic); button->setMnemonic(mnemonic); button->setAccelerator("ctrl " + charToString(mnemonic)); } if (!icon.empty()) { button->setIcon(icon); button->setTextPosition(SwingConstants::SWING_CENTER, SwingConstants::SWING_BOTTOM); } gui.addToRegion(button, "SOUTH"); return html; }
link_all_modules() // link all listed modules together. { Ref<sdlModule> bmod; Set<Ref<sdlModule> > omods; W_COERCE(Shore::begin_transaction(3)); Ref<sdlDeclaration> lpt; for (lpt = g_module_list; lpt != NULL; lpt = lpt->next) { bmod = ((Ref<sdlModDecl> &)lpt)->dmodule; omods.add(bmod); } sdl_linking = 1; for(int i=0; i<omods.get_size(); i++) { bmod = omods.get_elt(i); bmod.update()->resolve_types(); if (sdl_errors) { cerr << sdl_errors << " found linking module " << bmod->name << endl; SH_DO(Shore::abort_transaction()); return sdl_errors; } } sdl_linking = 0; // this should be a readonly transacton, so never commit. SH_DO(SH_COMMIT_TRANSACTION); return 0; }
static void tryPlayerGuess(const Grid<char> & boggleBoard, Set<string> & playerAnswers, string playerGuess, const Lexicon & english) { if (playerGuess.size() < kMinGuessLength){ cout << endl << "Words need to be at least " + integerToString(kMinGuessLength) + " characters long" << endl; return; } if (!english.contains(playerGuess)){ cout << endl << "That word is not in the english language" << endl; return; } playerGuess = toUpperCase(playerGuess); if (playerAnswers.contains(playerGuess)){ cout << endl << "You have already guessed that word" << endl; return; } for(int i = 0; i < boggleBoard.numRows(); i++){ for(int j = 0; j < boggleBoard.numCols(); j++){ Set <coord> wordPath; coord pos; pos.row = i; pos.col = j; wordPath.add(pos); if(tryPlayerGuess(boggleBoard, playerAnswers, playerGuess, english, "", i, j, wordPath)){ pause(highlightPause); clearBoard(wordPath); recordWordForPlayer(playerGuess, HUMAN); return; } } } cout << endl << "That word is not on the board." << endl; }
Set<char> setFromString(string str) { Set<char> set; for (int i = 0; i < str.length(); i++) { set.add(str[i]); } return set; }
Set Set::Intersection(Set set1, Set set2) { vector<int> tempSet1=set1.getSet(); vector<int> tempSet2=set2.getSet(); Set intersectionSet; for(auto num : tempSet1) { for(auto number : tempSet2) { //if the number appears in both vectors (Sets), then we want to add it to the intersection if(num==number) { intersectionSet.add(num); } } } vector<int> Intersection=intersectionSet.getSet(); cout << "The intersection of the two sets is:" << endl; for(auto integer : Intersection) { cout << integer << endl; } return intersectionSet; }
bool recSolvable(int start, Vector<int> & squares, Set<int> &indexVisited) { if (start < 0 || start >= squares.size() || indexVisited.contains(start)) return false; if (start == (squares.size() - 1)) return true; indexVisited.add(start); return (recSolvable(start + squares[start], squares, indexVisited) ||recSolvable(start - squares[start], squares, indexVisited)); }
static void testLexiconSet() { string lexfile; lexfile = findOnPath(LEXICON_PATH, "EnglishWords.dat"); if (lexfile == "") return; lexfile = expandPathname(lexfile); reportMessage("Reading EnglishWords.dat"); declare(Lexicon lexicon(lexfile)); reportMessage("Lexicon complete"); Vector<string> words; Lexicon::iterator iter1 = lexicon.begin(); Lexicon::iterator end1 = lexicon.end(); reportMessage("Creating word vector"); while (iter1 != end1) { words.add(*iter1++); } reportMessage("Word vector complete"); shuffle(words); reportMessage("Word vector shuffled"); reportMessage("Creating word set"); Set<string> wordSet; for (int i = 0; i < words.size(); i++) { wordSet.add(words[i]); } reportMessage("Word set complete"); iter1 = lexicon.begin(); end1 = lexicon.end(); Set<string>::iterator iter2 = wordSet.begin(); Set<string>::iterator end2 = wordSet.end(); int lexSetMatches = 0; while (iter1 != end1 && iter2 != end2) { if (*iter1++ == *iter2++) lexSetMatches++; } test(lexSetMatches, 127145); }
void initMesh(int nc, int p){ graph.clear(); set.clear(); initial.clear(); constraint.clear(); numCir = nc; poly = p; Circle cir = CXY(1); for (int i = 0; i < numCir; ++i){ VT radius = 1 + 2.0 * (float)i/numCir; Circle tcir = cir.dilate( log( radius ) ); for (int j = 0; j < poly; ++j ){ VT theta = TWOPI * j / poly; theta += (rtheta * i * PI/poly); Point p = Round::pnt_cir(tcir, theta ); set.add( p ); } } bool bSwitch = false; for (int j = 0; j < poly; ++j){ int a = j; int b = ( j < poly - 1 ) ? j + 1 : 0; int c = j + poly; //cout << a << " " << b << " " << c << endl; initial.add( Rigid2( set[c], set[a], set[b], bSwitch ) ); bSwitch = !bSwitch; } bSwitch = false; for (int i = 1; i < numCir-1; ++i){ for (int j = 0; j < poly; ++j ){ int a = i * poly + j; int b = a + poly; int c = j < poly -1 ? a + 1 : i * poly; int d = j < poly -1 ? a - poly + 1 : (i-1)*poly; constraint.add( Rigid3( set[b], set[a], set[d], set[c], bSwitch ) ); bSwitch = !bSwitch; } } }
Set<double> Cylindre::ensIntersect(const Ray &rayon) const { Set<double> res; double ox = rayon[0][0]; double oy = rayon[0][1]; double oz = rayon[0][2]; double dx = rayon[1][0]; double dy = rayon[1][1]; double dz = rayon[1][2]; double _a = _rotation(0, 0)*_rotation(0, 0) + _rotation(0, 1)*_rotation(0, 1); double _b = _rotation(1, 0)*_rotation(1, 0) + _rotation(1, 1)*_rotation(1, 1); double _c = _rotation(2, 0)*_rotation(2, 0) + _rotation(2, 1)*_rotation(2, 1); double _d = 2*(_rotation(0, 0)*_rotation(1, 0) + _rotation(0, 1)*_rotation(1, 1)); double _e = 2*(_rotation(0, 0)*_rotation(2, 0) + _rotation(0, 1)*_rotation(2, 1)); double _f = 2*(_rotation(1, 0)*_rotation(2, 0) + _rotation(1, 1)*_rotation(2, 1)); double _g = 2*(_rotation(0, 0)*_translation[0] + _rotation(0, 1)*_translation[1]); double _h = 2*(_rotation(1, 0)*_translation[0] + _rotation(1, 1)*_translation[1]); double _i = 2*(_rotation(2, 0)*_translation[0] + _rotation(2, 0)*_translation[1]); double _j = _translation[0]*_translation[0] + _translation[1]*_translation[1] -(_rayon * _rayon); double alpha = _a*dx*dx + _b*dy*dy + _c*dz*dz + 2*_d*dx*dy + 2*_e*dx*dz + 2*_f*dy*dz; double beta = 2*_a*ox*dx + 2*_b*oy*dy + 2*_c*oz*dz + 2*_d*(ox*dy + oy*dx) + 2*_e*(ox*dz + oz*dx) + 2*_f*(oy*dz + oz*dy) + _g*dx + _h*dy + _i*dz; double gamma = _a*ox*ox + _b*oy*oy + _c*oz*oz + 2*_d*ox*oy + 2*_e*ox*oz + 2*_f*oy*oz + _g*ox + _h*oy + _i* oz + _j; double delta = beta * beta - 4 * alpha * gamma; delta = sqrt(delta); if (delta == 0) { res.add(-beta / (2 * alpha)); } else if (delta > 0) { res.add((-beta - delta) / (2 * alpha)); res.add((-beta + delta) / (2 * alpha)); } return res; }
void publicouCom(Author *other) { if (other == null || other == this) return; #ifdef DEBUG printf("inserting %d on %d\n", other->id, this->id); #endif publ->add(other); }
static Set<char> charSet(string str) { Set<char> set; int nChars = str.length(); for (int i = 0; i < nChars; i++) { set.add(str[i]); } return set; }
/* * Method: initializeClusters * Parameters: BasicGraph graph by reference * Map of clusters with keys as Vertices and * values as connecting endpoints in a set * - - - - - - - - - - - - - - - - - - - - - - - - - - * Returns by reference a map of clusters for the * graph passed by reference. Since this is called at * initialization, each Vertex is its own cluster * and its connected endpoints is just itself. */ void initializeClusters(BasicGraph& graph, Map<Vertex*, Set<Vertex*> >& clusters) { Set<Vertex*> graphVertices = graph.getVertexSet(); for(Vertex* vertex : graphVertices) { Set<Vertex*> otherVertices; //Does a cluster include itself in connecting Vertices? otherVertices.add(vertex); clusters[vertex] = otherVertices; } }
public List<String> findRepeatedDnaSequences(String s) { Set<Integer> firstTime = new HashSet<Integer>(); Set<Integer> secondTime = new HashSet<Integer>(); List<String> list = new ArrayList<String>(); char[] map = new char[26]; int len = s.length(); // Hashing function. We have only 4 letters which we can represent by 2 bits. map['A' - 'A'] = 0; // A = 00 map['C' - 'A'] = 1; // B = 01 map['G' - 'A'] = 2; // C = 10 map['T' - 'A'] = 3; // D = 11 for(int i=0; i<= len - 10; i++) { int sequence = 0; for(int j=i; j< i+10; j++) { // Shift existing sequence by two to make space for the new character coming sequence = sequence << 2; // Copy the character from the map and paste those two bits in the newly created space. Read bit wise OR. sequence = sequence | map[s.charAt(j) - 'A']; } // For this number to be added in the list, this should be the second time this number is appearing // For this if condition to be true, firstTime.add() should be false. // firstTime.add() will be false when there is already the same number present. // How it will behave? // First time - firstTime.add(sequence) will return T // !firstTime.add(sequence) will become F // secondTime.add(sequence) will NOT be executed // Second time addition: // First time - firstTime.add(sequence) will return F // !firstTime.add(sequence) will become T // secondTime.add(sequence) will be executed if(!firstTime.add(sequence) && secondTime.add(sequence)) { list.add(s.substring(i, i+10)); } } return list; }
int main() { //Populate dummy cities Set// Set<string> cities; cities.add("A"); cities.add("B"); cities.add("C"); cities.add("D"); cities.add("E"); cities.add("F"); cities.add("G"); //Define vectors of sets for both candidate and result locations/ Vector<Set<string> > locations(5); Vector<Set<string> > result; //Populate dummy candidate locations/ locations[0].add("A");locations[0].add("B");locations[0].add("C"); locations[1].add("A");locations[1].add("C");locations[1].add("D"); locations[2].add("B");locations[2].add("F"); locations[3].add("C");locations[3].add("E");locations[3].add("F"); locations[4].add("G"); while(true) { //Variable to store number of hospitals int numHos=0; cout<<"Select number of hospitals to build (min 1-max "<<locations.size()<<") [0 to exit]"<<endl; numHos=getInteger(); if (numHos<1||numHos>locations.size()) break; //Call recursive function/ bool possible=canOfferUniversalCoverage(cities,locations,numHos,result); //Display if possible to cover. There is no output of the hospitals in result for simplicity/ if(possible) {cout<<"It is possible to cover all locations with "<<numHos<<" Hospitals"<<endl;} else {cout<<"Imposible to cover all locations with "<< numHos<<" Hospitals"<<endl;} } return 0; }
void streamInsertionOthersTest() { Vector<int> v; v.add(14); v.add(42); cout << "Vector: " << v << endl; Stack<int> s; s.add(14); s.add(42); cout << "Stack: " << s << endl; Queue<int> q; q.add(14); q.add(42); cout << "Queue: " << q << endl; PriorityQueue<int> pq; pq.add(14, 1); pq.add(42, 2); cout << "PriorityQueue: " << pq << endl; Grid<int> grid; grid.resize(2, 2); grid.fill(14); cout << "Grid: " << grid << endl; Map<string, Vector<int>> map; map.add("corfu", v); cout << "Map<string, Vector>: " << map << endl; HashMap<Stack<int>, Vector<int>> hashmap; hashmap.add(s, v); cout << "Map<Stack, Vector>: " << hashmap << endl; Set<int> set; set.add(14); set.add(42); cout << "Set: " << set << endl; HashSet<Set<int>> hashset; hashset.add(set); cout << "HashSet<Set>: " << hashset << endl; Graph<DumbNode, DumbEdge> graph; graph.addNode("a"); graph.addNode("b"); graph.addNode("c"); graph.addNode("d"); graph.addNode("e"); graph.addArc("a", "b"); graph.addArc("a", "d"); graph.addArc("b", "c"); graph.addArc("b", "d"); graph.addArc("c", "b"); graph.addArc("c", "e"); cout << "Graph: " << graph << endl; }