void LedFrame::setLedColor(int row, int col, const FullNColor &ledColor) { if (frame_matrix_.size() == rowCount() && row >= 0 && row < rowCount()) { if (frame_matrix_[row].size() == colCount() && col >= 0 && col < colCount()) { frame_matrix_[row][col] = ledColor; } } }
void LedFrame::clear() { frame_delay_ = 0; for (int i = 0; i < rowCount(); ++i) { for (int j = 0; j < colCount(); ++j) { Q_ASSERT(frame_matrix_.size() == rowCount()); Q_ASSERT(frame_matrix_[i].size() == colCount()); frame_matrix_[i][j] = FullNColor(); } } }
FullNColor LedFrame::getLedColor(int row, int col) const { FullNColor res; if (frame_matrix_.size() == rowCount() && row >= 0 && row < rowCount()) { if (frame_matrix_[row].size() == colCount() && col >= 0 && col < colCount()) { res = frame_matrix_[row][col]; } } return res; }
LedFrame::LedFrame() : frame_delay_(0) { frame_matrix_.clear(); for (int i = 0; i < rowCount(); ++i) frame_matrix_.append(QVector<FullNColor>(colCount())); clear(); }
bool LedFrame::fromBinaryFrame(const QByteArray &bin) { bool res = false; if (bin.size() == binFrameSize()) { Q_ASSERT(bin.size() == 32); Q_ASSERT(rowCount() == 10); Q_ASSERT(colCount() == 3); QByteArray data = bin; for (int i = 0; i < rowCount(); ++i) { for (int j = 0; j < colCount(); ++j) { quint8 byteColor = 0; if (data.size() > 0) { byteColor = data[0]; data.remove(0, 1); } frame_matrix_[i][j] = LedColorFromByte(byteColor); } } Q_ASSERT(data.size() == 2); frame_delay_ = 0; if (data.size() == 2) frame_delay_ = Common::unserializeUINT16(data); res = true; } return res; }
int Solution::maximalSquare(vector<vector<char>>& matrix) { int nRow = matrix.size(); if (nRow == 0) return 0; int nCol = matrix[0].size(); vector<vector<int>> colCount(nRow, vector<int>(nCol, 0)); for (int i = 0; i < nRow; ++i) { int cur = 0; for (int j = 0; j < nCol; ++j) { if (matrix[i][j] == '1') { ++cur; colCount[i][j] = cur; } else cur = 0; } } int square = 0; for (int i = 0; i < nCol; ++i) { int j = 0; while (j < nRow) { if (colCount[j][i] <= square) ++j; else { int count = 1; int k = j + 1; while (k < nRow && colCount[k][i] > square) { ++count; ++k; } if (count > square) ++square; j = k; } } } return square * square; }
int main(int argc,char * argv[]) { int row=rowCount(argv[1]); int col=colCount(argv[1]); printf("%d %d\n",row,col); int speed; FILE * action=fopen(argv[2],"r"); char * dir=(char*)malloc(sizeof(char)*100); char ** arr; arr=init(row,col); scanVal(argv[1],arr,row,col); while(!(feof(action))) { fscanf(action,"%s",dir); if(feof(action)) break; speed=nextMove(arr,row,col,dir); } printVal(arr,row,col); return 1; }
QByteArray LedFrame::toBinaryFrame() const { QByteArray bin; for (int i = 0; i < rowCount(); ++i) { for (int j = 0; j < colCount(); ++j) { bin.append(LedColorToByte(frame_matrix_[i][j])); } } Q_ASSERT(bin.size() == 30); bin.append(Common::serializeUINT16(frame_delay_)); Q_ASSERT(bin.size() == 32); Q_ASSERT(bin.size() == binFrameSize()); return bin; }
static int compute_hypergraph_metrics(const Epetra_BlockMap &rowmap, const Epetra_BlockMap &colmap, int numGlobalColumns, Isorropia::Epetra::CostDescriber &costs, double &myGoalWeight, double &balance, double &cutn, double &cutl) // output { const Epetra_Comm &comm = rowmap.Comm(); #ifdef HAVE_MPI const Epetra_MpiComm* mpiComm = dynamic_cast<const Epetra_MpiComm*>(&comm); MPI_Comm mcomm = mpiComm->Comm(); #endif int nProcs = comm.NumProc(); int myProc = comm.MyPID(); double min, avg; std::map<int, float> vertexWeights; std::map<int, std::map<int, float > > graphEdgeWeights; std::map<int, float> hyperEdgeWeights; costs.getCosts(vertexWeights, // vertex global ID -> weight graphEdgeWeights, // vertex global ID -> map from neighbor global ID to edge weight hyperEdgeWeights); // hyperedge global ID -> weight Epetra_Vector vwgt(rowmap); int numVWgts = vertexWeights.size(); if (numVWgts > 0){ double *wvals = new double [numVWgts]; int *gids = new int [numVWgts]; std::map<int, float>::iterator vnext = vertexWeights.begin(); int i=0; while (vnext != vertexWeights.end()){ wvals[i] = vnext->second; gids[i] = vnext->first; vnext++; i++; } vwgt.ReplaceGlobalValues(i, wvals, gids); delete [] wvals; delete [] gids; } else{ vwgt.PutScalar(1.0); // default to unit weights } compute_balance(vwgt, myGoalWeight, min, balance, avg); if (balance < 0){ return 1; } /* Compute cutl and cutn. */ int totalHEWeights = 0; int numHEWeights = hyperEdgeWeights.size(); comm.SumAll(&numHEWeights, &totalHEWeights, 1); if ((totalHEWeights > 0) && (totalHEWeights < numGlobalColumns)){ if (myProc == 0) std::cerr << "Must supply either no h.e. weights or else supply at least one for each column" << std::endl; return -1; } std::map<int, float>::iterator heWgtIter; // Create a set containing all the columns in my rows. We assume all // the rows are in the same partition. int numMyCols = colmap.NumMyElements(); std::set<int> colGIDS; std::set<int>::iterator gidIter; for (int j=0; j<numMyCols; j++){ colGIDS.insert(colmap.GID(j)); } /* Divide columns among processes, then each process computes its * assigned columns' cutl and cutn. * TODO - numGlobalColumns can be less than nprocs * Fix this when a process is assigned no columns. TODO */ int ncols = numGlobalColumns / nProcs; int leftover = numGlobalColumns - (nProcs * ncols); std::vector<int> colCount(nProcs, 0); for (int i=0; i<nProcs; i++){ colCount[i] = ncols; if (i < leftover) colCount[i]++; } int *colTotals = NULL; double *colWeights = NULL; if (colCount[myProc] > 0){ colTotals = new int [colCount[myProc]]; if (totalHEWeights > 0){ colWeights = new double [colCount[myProc]]; } } int *colLocal= new int [ncols + 1]; double *localWeights = NULL; if (totalHEWeights > 0){ localWeights = new double [ncols + 1]; } int base = colmap.IndexBase(); int colStart = base; for (int i=0; i<nProcs; i++){ // All processes send info to the process reponsible // for the next group of columns int ncols = colCount[i]; int colEnd = colStart + ncols; for (int j=colStart,k=0; j < colEnd; j++,k++){ gidIter = colGIDS.find(j); if (gidIter != colGIDS.end()){ colLocal[k] = 1; // column j has rows in my partition } else{ colLocal[k] = 0; } if (totalHEWeights > 0){ std::map<int, float>::iterator heWgtIter = hyperEdgeWeights.find(j); if (heWgtIter != hyperEdgeWeights.end()){ // I have the edge weight for column j localWeights[k] = heWgtIter->second; } else{ localWeights[k] = 0.0; } } } #ifdef HAVE_MPI int rc = MPI_Reduce(colLocal, colTotals, ncols, MPI_INT, MPI_SUM, i, mcomm); if (totalHEWeights > 0){ rc = MPI_Reduce(localWeights, colWeights, ncols, MPI_DOUBLE, MPI_SUM, i, mcomm); } // TODO handle possible MPI error #else memcpy(colTotals, colLocal, ncols * sizeof(int)); if (totalHEWeights > 0){ memcpy(colWeights, localWeights, ncols * sizeof(double)); } #endif colStart = colEnd; } delete [] colLocal; if (localWeights) delete [] localWeights; double localCutN=0; double localCutL=0; double ewgt = 1.0; for (int j=0; j<colCount[myProc]; j++){ if (totalHEWeights > 0){ ewgt = colWeights[j]; } if (colTotals[j] > 1){ localCutL += (colTotals[j] - 1) * ewgt; // # of cuts in columns/edges localCutN += ewgt; // # of cut columns/edges } } if (colTotals) delete [] colTotals; if (colWeights) delete [] colWeights; comm.SumAll(&localCutN, &cutn, 1); comm.SumAll(&localCutL, &cutl, 1); return 0; }