ErrorType HashTable::add(Info *info){ if (count == N) return FULLTABLE; if (!checkKey(info->getKey())) return WRONGKEY; int a0 = hashF(info->getKey()); int a = a0; int i = 0; int k = -1; bool stop = false; while (!stop && i<N){ switch (table[a].state){ case FREE: stop = true; break; case DELETED: if (k == -1) k = a; a = nextCell(a0,i); break; case FULL: if (table[a].info->getKey().compare(info->getKey()) == 0) return EXISTS; else a = nextCell(a0,i); } } if (k == -1) k = a; if (table[k].state == FULL) return FULLTABLE; table[k].state = FULL; table[k].info = info; count++; return NONE; }
int HashTable::indexOf(std::string key){ int a0 = hashF(key); int i = 0,a = a0; while (i<N){ switch (table[a].state){ case FREE: return -1; case DELETED: a = nextCell(a0,i); break; case FULL: if (table[a].info->getKey().compare(key) == 0) return a; else a = nextCell(a0,i); } } return -1; }
void KWQTableView::closeEditor(QWidget * editor, QAbstractItemDelegate::EndEditHint hint) { QTableView::closeEditor(editor, hint); if (hint == QAbstractItemDelegate::SubmitModelCache) { adjustRow(currentIndex().row()); nextCell(); } }
void OctreeGrid::assertIsValid() { // Check node adjacency relations for (int node1 = 0; node1 < (int) m_Nodes.size(); ++node1) { for (int axis = 0; axis < 3; ++axis) { int node0 = prevNode(node1, axis); int node2 = nextNode(node1, axis); if (m_Nodes[node1].position[axis] == 0) { oct_debug(node0 == -1); } else if (node0 != -1) { oct_debug(nextNode(node0, axis) == node1); } if (m_Nodes[node1].position[axis] == m_CellGridSize[axis]) { oct_debug(node2 == -1); } else if (node2 != -1) { oct_debug(prevNode(node2, axis) == node1); } } } // Check cell adjacency relations for (int cell1 = 0; cell1 < (int) m_Cells.size(); ++cell1) { for (int axis = 0; axis < 3; ++axis) { int cell0 = prevCell(cell1, axis); int cell2 = nextCell(cell1, axis); if (cellCornerPos(cell1, CORNER_X0_Y0_Z0)[axis] == 0) { oct_debug(cell0 == -1); } else { oct_debug(cell0 != -1); if (cellExtent(cell1) == cellExtent(cell0)) { oct_debug(nextCell(cell0, axis) == cell1); } } if (cellCornerPos(cell1, CORNER_X1_Y1_Z1)[axis] == m_CellGridSize[axis]) { oct_debug(cell2 == -1); } else { oct_debug(cell2 != -1); if (cellExtent(cell1) == cellExtent(cell2)) { oct_debug(prevCell(cell2, axis) == cell1); } } } } }
void KWQTableView::keyPressEvent(QKeyEvent * e) { if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) { e->accept(); if (state() != QAbstractItemView::EditingState) nextCell(); return; } QTableView::keyPressEvent(e); }
// Solution for the puzzle by Backtracking void solveSudoku(int row, int column) { if (row == boardSize) { // Means that all cells are filled solutionPrint(); } if (table[row][column] != 0) { // If a cell is not a zero, it means we have to move to the next cell nextCell(row, column); }else { for (int counter = 1; counter <= boardSize; counter++) { // Checks if numbers 1-9 can be put on a cell if ((rowCheck(row, counter) == true) && (columnCheck(column, counter) == true) && (boxCheck(row, column, counter) == true)) { // If looks promising table[row][column] = counter; // Make tentative assignment nextCell(row, column); } } table[row][column] = 0; // If doesn't lead to a solution, reset to 0. This triggers the backtracking // Note this backtracking step, a very important step** // We come at this position, this step, this line when we have already checked all possible values at // sudoku[i][j] and we couldn't find the solution // Put any value does not solves our board implies that we must have made wrong choice earlier // so we make this sudoku[i][j] again a vacant cell and try to correct our previous guesses/choices. } }
int DoubleDiagonalShoot::threeSquare(int squareNumberWidth, int squareNumberHeight) { return nextCell(squareNumberWidth, squareNumberHeight, 3); }
int DoubleDiagonalShoot::fourSquare(int squareNumberWidth, int squareNumberHeight) { return nextCell(squareNumberWidth, squareNumberHeight, 4); }
int DiagonalShoot::twoSquare(int squareNumberWidth, int squareNumberHeight) { return nextCell(squareNumberWidth, squareNumberHeight, 2); }
// Subdivide a cell and add the subcells to the octree int OctreeGrid::splitCell(int cellId, bool graded, bool paired) { oct_debug(cellId != -1); oct_debug(cellIsLeaf(cellId)); const Cell &cell = m_Cells[cellId]; // Retrieve cell corners const int v0 = cell.corner(CORNER_X0_Y0_Z0); const int v1 = cell.corner(CORNER_X1_Y0_Z0); const int v2 = cell.corner(CORNER_X1_Y1_Z0); const int v3 = cell.corner(CORNER_X0_Y1_Z0); const int v4 = cell.corner(CORNER_X0_Y0_Z1); const int v5 = cell.corner(CORNER_X1_Y0_Z1); const int v6 = cell.corner(CORNER_X1_Y1_Z1); const int v7 = cell.corner(CORNER_X0_Y1_Z1); // Start by splitting incident faces const int f0 = splitFace(v0, v3, v4, v7, X); const int f1 = splitFace(v1, v2, v5, v6, X); const int f2 = splitFace(v0, v1, v4, v5, Y); const int f3 = splitFace(v3, v2, v7, v6, Y); const int f4 = splitFace(v0, v1, v3, v2, Z); const int f5 = splitFace(v4, v5, v7, v6, Z); // Then connect the middle points of the faces createNodeLinks(f0, f1, X); createNodeLinks(f2, f3, Y); createNodeLinks(f4, f5, Z); const int c0 = splitEdge(f0, f1, X); updateNodeLinks(f2, f3, c0, Y); updateNodeLinks(f4, f5, c0, Z); // Retrieve midpoint of cell edges const int e01 = getMidEdgeNode(v0, v1, X); const int e32 = getMidEdgeNode(v3, v2, X); const int e45 = getMidEdgeNode(v4, v5, X); const int e76 = getMidEdgeNode(v7, v6, X); const int e03 = getMidEdgeNode(v0, v3, Y); const int e12 = getMidEdgeNode(v1, v2, Y); const int e47 = getMidEdgeNode(v4, v7, Y); const int e56 = getMidEdgeNode(v5, v6, Y); const int e04 = getMidEdgeNode(v0, v4, Z); const int e15 = getMidEdgeNode(v1, v5, Z); const int e26 = getMidEdgeNode(v2, v6, Z); const int e37 = getMidEdgeNode(v3, v7, Z); // Create a new cell for each subvolume const int offset = (int) m_Cells.size(); m_Cells.resize(m_Cells.size() + 8); m_Cells[offset+CORNER_X0_Y0_Z0].cornerNodeId = {{v0, e01, f4, e03, e04, f2, c0, f0}}; m_Cells[offset+CORNER_X1_Y0_Z0].cornerNodeId = {{e01, v1, e12, f4, f2, e15, f1, c0}}; m_Cells[offset+CORNER_X1_Y1_Z0].cornerNodeId = {{f4, e12, v2, e32, c0, f1, e26, f3}}; m_Cells[offset+CORNER_X0_Y1_Z0].cornerNodeId = {{e03, f4, e32, v3, f0, c0, f3, e37}}; m_Cells[offset+CORNER_X0_Y0_Z1].cornerNodeId = {{e04, f2, c0, f0, v4, e45, f5, e47}}; m_Cells[offset+CORNER_X1_Y0_Z1].cornerNodeId = {{f2, e15, f1, c0, e45, v5, e56, f5}}; m_Cells[offset+CORNER_X1_Y1_Z1].cornerNodeId = {{c0, f1, e26, f3, f5, e56, v6, e76}}; m_Cells[offset+CORNER_X0_Y1_Z1].cornerNodeId = {{f0, c0, f3, e37, e47, f5, e76, v7}}; // Update link to child cell m_Cells[cellId].firstChild = offset; // Update cell adjacency relations for (int axis = 0; axis < 3; ++axis) { updateSubcellLinks(cellId, axis); updateSubcellLinks(cellId, nextCell(cellId, axis), axis); updateSubcellLinks(prevCell(cellId, axis), cellId, axis); } // Ensure proper 2:1 grading if (graded) { makeCellGraded(cellId, paired); } // Ensure children are either all leaves, or all internal cells if (paired) { makeCellPaired(cellId, graded); if (cellId < m_NumRootCells) { // Special case for root cells: if one gets split, then we need to split all root cells for (int c = 0; c < m_NumRootCells; ++c) { if (cellIsLeaf(c)) { splitCell(c, graded, paired); } } } else { // Ensure sibling cells are also properly split int firstSibling = m_NumRootCells + 8 * ((cellId - m_NumRootCells) / 8); for (int c = firstSibling; c < firstSibling + 8; ++c) { if (cellIsLeaf(c)) { splitCell(c, graded, paired); } } } } return c0; }