void visitModules() { // Loop on all modules left to process // Hitting a cell adds to the appropriate level of this level-sorted list, // so since cells originally exist top->bottom we process in top->bottom order too. while (!m_todoModps.empty()) { LevelModMap::iterator it = m_todoModps.begin(); AstNodeModule* nodep = it->second; m_todoModps.erase(it); if (!nodep->user5SetOnce()) { // Process once; note clone() must clear so we do it again UINFO(4," MOD "<<nodep<<endl); nodep->iterateChildren(*this); // Note above iterate may add to m_todoModps // // Process interface cells, then non-interface which may ref an interface cell for (int nonIf=0; nonIf<2; ++nonIf) { for (CellList::iterator it=m_cellps.begin(); it!=m_cellps.end(); ++it) { AstCell* nodep = *it; if ((nonIf==0 && nodep->modp()->castIface()) || (nonIf==1 && !nodep->modp()->castIface())) { visitCell(nodep); } } } m_cellps.clear(); } } }
bool MainWindow::updateConnections() { bool newconnection[MasterBoardSize * MasterBoardSize]; for(int i = 0; i < MasterBoardSize * MasterBoardSize; i++) newconnection[i] = false; CellList list; if(!root->isRotated()) { newconnection[root->index()] = true; list.append(root); } while(!list.isEmpty()) { Cell* cell = list.first(); Cell* ucell = uCell(cell); Cell* rcell = rCell(cell); Cell* dcell = dCell(cell); Cell* lcell = lCell(cell); if((cell->dirs() & Cell::U) && ucell && (ucell->dirs() & Cell::D) && !newconnection[ucell->index()] && !ucell->isRotated()) { newconnection[ucell->index()] = true; list.append(ucell); } if((cell->dirs() & Cell::R) && rcell && (rcell->dirs() & Cell::L) && !newconnection[rcell->index()] && !rcell->isRotated()) { newconnection[rcell->index()] = true; list.append(rcell); } if((cell->dirs() & Cell::D) && dcell && (dcell->dirs() & Cell::U) && !newconnection[dcell->index()] && !dcell->isRotated()) { newconnection[dcell->index()] = true; list.append(dcell); } if((cell->dirs() & Cell::L) && lcell && (lcell->dirs() & Cell::R) && !newconnection[lcell->index()] && !lcell->isRotated()) { newconnection[lcell->index()] = true; list.append(lcell); } list.remove(list.begin()); } bool isnewconnection = false; for(int i = 0; i < MasterBoardSize * MasterBoardSize; i++) { if(!board[i]->isConnected() && newconnection[i]) isnewconnection = true; board[i]->setConnected(newconnection[i]); } return isnewconnection; }
/* * Build the PairList, i.e., populate it with atom pairs. */ void PairList::build(CellList& cellList, bool reverseUpdateFlag) { // Precondition assert(isAllocated()); Cell::NeighborArray neighbors; double dRSq, cutoffSq; const Cell* cellPtr; Atom* atom1Ptr; Atom* atom2Ptr; Mask* maskPtr; Vector dr; int na; // number of atoms in this cell int nn; // number of neighbors for a cell int atom2Id; // atom Id for second atom int i, j; bool foundNeighbor; // Set maximum squared-separation for pairs in Pairlist cutoffSq = cutoff_*cutoff_; // Initialize counters for primary atoms and neighbors atom1Ptrs_.clear(); atom2Ptrs_.clear(); first_.clear(); first_.append(0); // Find all neighbors (cell list) cellPtr = cellList.begin(); while (cellPtr) { cellPtr->getNeighbors(neighbors, reverseUpdateFlag); na = cellPtr->nAtom(); nn = neighbors.size(); // Loop over primary atoms (atom1) in primary cell for (i = 0; i < na; ++i) { atom1Ptr = neighbors[i]; maskPtr = &(atom1Ptr->mask()); foundNeighbor = false; // Loop over secondary atoms (atom2) in primary cell for (j = 0; j < na; ++j) { atom2Ptr = neighbors[j]; if (atom2Ptr > atom1Ptr) { atom2Id = atom2Ptr->id(); if (!maskPtr->isMasked(atom2Id)) { dr.subtract(atom2Ptr->position(), atom1Ptr->position()); dRSq = dr.square(); if (dRSq < cutoffSq) { // If first neighbor of atom1, add atom1 to atom1Ptrs_ if (!foundNeighbor) { atom1Ptrs_.append(atom1Ptr); foundNeighbor = true; } // Append 2nd atom to atom2Ptrs_[] atom2Ptrs_.append(atom2Ptr); } } } } // Atoms in neighboring cells for (j = na; j < nn; ++j) { atom2Ptr = neighbors[j]; atom2Id = atom2Ptr->id(); if (!maskPtr->isMasked(atom2Id)) { dr.subtract(atom2Ptr->position(), atom1Ptr->position()); dRSq = dr.square(); if (dRSq < cutoffSq) { // If first_ neighbor, record iAtomId in atom1Ptrs_ if (!foundNeighbor) { atom1Ptrs_.append(atom1Ptr); foundNeighbor = true; } // Append Id of 2nd atom in pair to atom2Ptrs_[] atom2Ptrs_.append(atom2Ptr); } } } // When finished with atom1, set next element of first_ array. if (foundNeighbor) { first_.append(atom2Ptrs_.size()); } } // Advance to next cell in a linked list cellPtr = cellPtr->nextCellPtr(); } // Postconditions if (atom1Ptrs_.size()) { if (first_.size() != atom1Ptrs_.size() + 1) { UTIL_THROW("Array size problem"); } if (first_[0] != 0) { UTIL_THROW("Incorrect first element of first_"); } if (first_[atom1Ptrs_.size()] != atom2Ptrs_.size()) { UTIL_THROW("Incorrect last element of first_"); } } // Increment buildCounter_= number of times the list has been built. ++buildCounter_; // Increment maxima if (atom1Ptrs_.size() > maxNAtomLocal_) { maxNAtomLocal_ = atom1Ptrs_.size(); } if (atom2Ptrs_.size() > maxNPairLocal_) { maxNPairLocal_ = atom2Ptrs_.size(); } }
void MainWindow::newGame(int sk) { if (sk==Settings::EnumSkill::Novice || sk==Settings::EnumSkill::Normal || sk==Settings::EnumSkill::Expert || sk==Settings::EnumSkill::Master) { Settings::setSkill(sk); } if(Settings::skill() == Settings::EnumSkill::Master) wrapped = true; else wrapped = false; KExtHighscore::setGameType(Settings::skill()); Settings::writeConfig(); m_clickcount = 0; QString clicks = i18n("Click: %1"); statusBar()->changeItem(clicks.arg(QString::number(m_clickcount)),1); KNotifyClient::event(winId(), "startsound", i18n("New Game")); for(int i = 0; i < MasterBoardSize * MasterBoardSize; i++) { board[i]->setDirs(Cell::None); board[i]->setConnected(false); board[i]->setRoot(false); board[i]->setLocked(false); } const int size = (Settings::skill() == Settings::EnumSkill::Novice) ? NoviceBoardSize : (Settings::skill() == Settings::EnumSkill::Normal) ? NormalBoardSize : (Settings::skill() == Settings::EnumSkill::Expert) ? ExpertBoardSize : MasterBoardSize; const int start = (MasterBoardSize - size) / 2; const int rootrow = rand() % size; const int rootcol = rand() % size; root = board[(start + rootrow) * MasterBoardSize + start + rootcol]; root->setConnected(true); root->setRoot(true); while(true) { for(int row = start; row < start + size; row++) for(int col = start; col < start + size; col++) board[row * MasterBoardSize + col]->setDirs(Cell::Free); CellList list; list.append(root); if(rand() % 2) addRandomDir(list); while(!list.isEmpty()) { if(rand() % 2) { addRandomDir(list); if(rand() % 2) addRandomDir(list); list.remove(list.begin()); } else { list.append(list.first()); list.remove(list.begin()); } } int cells = 0; for(int i = 0; i < MasterBoardSize * MasterBoardSize; i++) { Cell::Dirs d = board[i]->dirs(); if((d != Cell::Free) && (d != Cell::None)) cells++; } if(cells >= MinimumNumCells) break; } for(int i = 0; i < MasterBoardSize * MasterBoardSize; i++) board[i]->rotate((rand() % 4) * 90); updateConnections(); }
/* * Build the PairList, i.e., populate it with atom pairs. */ void PairList::build(CellList& cellList, bool reverseUpdateFlag) { // Precondition assert(isAllocated()); Cell::NeighborArray neighbors; double cutoffSq; const Cell* cellPtr; CellAtom* atom1Ptr; CellAtom* atom2Ptr; Mask* maskPtr; Vector dr; int na; // number of atoms in this cell int nn; // number of neighbors for a cell int i, j; bool hasNeighbor; // Set maximum squared-separation for pairs in Pairlist cutoffSq = cutoff_*cutoff_; // Initialize counters for primary atoms and neighbors atom1Ptrs_.clear(); atom2Ptrs_.clear(); first_.clear(); first_.append(0); // Copy positions and ids into cell list cellList.update(); // Find all neighbors (cell list) cellPtr = cellList.begin(); while (cellPtr) { na = cellPtr->nAtom(); // # of atoms in cell if (na) { cellPtr->getNeighbors(neighbors, reverseUpdateFlag); nn = neighbors.size(); // Loop over primary atoms (atom1) in primary cell for (i = 0; i < na; ++i) { atom1Ptr = neighbors[i]; maskPtr = atom1Ptr->maskPtr(); // Loop over secondary atoms hasNeighbor = false; for (j = i + 1; j < nn; ++j) { atom2Ptr = neighbors[j]; dr.subtract(atom2Ptr->position(), atom1Ptr->position()); if (dr.square() < cutoffSq && !maskPtr->isMasked(atom2Ptr->id())) { atom2Ptrs_.append(atom2Ptr->ptr()); hasNeighbor = true; } } // Complete processing of atom1. if (hasNeighbor) { atom1Ptrs_.append(atom1Ptr->ptr()); first_.append(atom2Ptrs_.size()); } } // for ia } // if (na) // Advance to next cell in a linked list cellPtr = cellPtr->nextCellPtr(); } // Postconditions if (atom1Ptrs_.size()) { if (first_.size() != atom1Ptrs_.size() + 1) { UTIL_THROW("Array size problem"); } if (first_[0] != 0) { UTIL_THROW("Incorrect first element of first_"); } if (first_[atom1Ptrs_.size()] != atom2Ptrs_.size()) { UTIL_THROW("Incorrect last element of first_"); } } // Increment buildCounter_= number of times the list has been built. ++buildCounter_; // Increment maxima if (atom1Ptrs_.size() > maxNAtomLocal_) { maxNAtomLocal_ = atom1Ptrs_.size(); } if (atom2Ptrs_.size() > maxNPairLocal_) { maxNPairLocal_ = atom2Ptrs_.size(); } }