//' Ranks elements with column of a matrix, assuming a one-sided market. //' //' Returns the rank of each element with each column of a matrix. So, if row 34 //' is the highest number for column 3, then the first row of column 3 will be //' 34 -- unless it is column 34, in which case it will be 35, to adjust for the //' fact that this is a single-sided market. //' //' @param u A matrix with agents' cardinal preferences. Column i is agent i's //' preferences. //' @return a matrix with the agents' ordinal preferences // [[Rcpp::export]] umat sortIndexOneSided(const mat& u) { uword N = u.n_rows; uword M = u.n_cols; umat sortedIdx(N,M); for(uword jX=0; jX<M; jX++) { sortedIdx.col(jX) = sort_index(u.col(jX), "descend"); } for (uword iX=0; iX<M; iX++) { for (uword iY=0; iY<N; iY++) { if (sortedIdx(iY, iX) >= iX) { ++sortedIdx(iY, iX); } } } return sortedIdx; }
//' Sort indices of a matrix within a column //' //' Within each column of a matrix, this function returns the indices of each //' element in descending order //' //' @param u is the input matrix with cardinal preferences //' @return a matrix with sorted indices (the agents' ordinal preferences) // [[Rcpp::export]] umat sortIndex(const mat& u) { int N = u.n_rows; int M = u.n_cols; umat sortedIdx(N,M); for(int jX=0; jX<M; jX++) { sortedIdx.col(jX) = sort_index(u.col(jX), "descend"); } return sortedIdx; }
void VAttachmentList::sortItems() { const QVector<VAttachment> &attas = m_file->getAttachments(); if (attas.size() < 2) { return; } VSortDialog dialog(tr("Sort Attachments"), tr("Sort attachments of note <span style=\"%1\">%2</span> " "in the configuration file.") .arg(g_config->c_dataTextStyle) .arg(m_file->getName()), g_mainWin); QTreeWidget *tree = dialog.getTreeWidget(); tree->clear(); tree->setColumnCount(1); QStringList headers(tr("Name")); tree->setHeaderLabels(headers); for (int i = 0; i < attas.size(); ++i) { QTreeWidgetItem *item = new QTreeWidgetItem(tree, QStringList(attas[i].m_name)); item->setData(0, Qt::UserRole, i); } dialog.treeUpdated(); if (dialog.exec()) { QVector<QVariant> data = dialog.getSortedData(); Q_ASSERT(data.size() == attas.size()); QVector<int> sortedIdx(data.size(), -1); for (int i = 0; i < data.size(); ++i) { sortedIdx[i] = data[i].toInt(); } if (!m_file->sortAttachments(sortedIdx)) { VUtils::showMessage(QMessageBox::Warning, tr("Warning"), tr("Fail to sort attachments of note <span style=\"%1\">%2</span>.") .arg(g_config->c_dataTextStyle) .arg(m_file->getName()), "", QMessageBox::Ok, QMessageBox::Ok, this); } } }
void VDirectoryTree::sortItems(VDirectory *p_dir) { if (!p_dir) { return; } const QVector<VDirectory *> &dirs = p_dir->getSubDirs(); if (dirs.size() < 2) { return; } bool isNotebook = p_dir->parent() == NULL; VSortDialog dialog(tr("Sort Folders"), tr("Sort folders in %1 <span style=\"%2\">%3</span> " "in the configuration file.") .arg(isNotebook ? tr("notebook") : tr("folder")) .arg(g_config->c_dataTextStyle) .arg(isNotebook ? p_dir->getNotebook()->getName() : p_dir->getName()), this); QTreeWidget *tree = dialog.getTreeWidget(); tree->clear(); tree->setColumnCount(2); QStringList headers; headers << tr("Name") << tr("Created Time"); tree->setHeaderLabels(headers); for (int i = 0; i < dirs.size(); ++i) { const VDirectory *dir = dirs[i]; Q_ASSERT(dir->isOpened()); QString createdTime = VUtils::displayDateTime(dir->getCreatedTimeUtc().toLocalTime()); QStringList cols; cols << dir->getName() << createdTime; QTreeWidgetItem *item = new QTreeWidgetItem(tree, cols); item->setData(0, Qt::UserRole, i); } dialog.treeUpdated(); if (dialog.exec()) { QVector<QVariant> data = dialog.getSortedData(); Q_ASSERT(data.size() == dirs.size()); QVector<int> sortedIdx(data.size(), -1); for (int i = 0; i < data.size(); ++i) { sortedIdx[i] = data[i].toInt(); } qDebug() << "sort dirs" << sortedIdx; if (!p_dir->sortSubDirectories(sortedIdx)) { VUtils::showMessage(QMessageBox::Warning, tr("Warning"), tr("Fail to sort folders in %1 <span style=\"%2\">%3</span>.") .arg(isNotebook ? tr("notebook") : tr("folder")) .arg(g_config->c_dataTextStyle) .arg(isNotebook ? p_dir->getNotebook()->getName() : p_dir->getName()), "", QMessageBox::Ok, QMessageBox::Ok, this); } updateItemDirectChildren(findVDirectory(p_dir)); } }