Insert Rows and Columns - A Thank you!!!
jtaylorme opened this issue · comments
Assuming you are interested in adding them, here is code for inserting rows and columns. Guess you will want to tweak things a bit as well as making sure I didn't mess up something. Tried to keep things in line with your other code. Check on the cCount thing I did in the row insert. Probably a better way since you will know what you are doing.
Thanks again.
jim
/**
* @brief Insert row by index. Inserts at postion so new row will preceed specified row.
* @param pRowIdx zero-based row index.
*/
void InsertRow(const size_t pRowIdx, const string defValue = "", const int cCount = 0)
{
const ssize_t rowIdx = pRowIdx + (mLabelParams.mColumnNameIdx + 1);
vector<string> insVector(GetColumnCount()+cCount, defValue);
mData.insert(mData.begin() + rowIdx, insVector);
}
/**
* @brief Insert row by name. Insert at postion so new row will preceed specified row.
* @param pRowName row label name.
*/
void InsertRow(const std::string& pRowName, const std::string newRowName = "", const string defValue = "")
{
ssize_t rowIdx = GetRowIdx(pRowName);
if (rowIdx < 0)
{
throw std::out_of_range("row not found: " + pRowName);
}
InsertRow(rowIdx, defValue, 1);
SetRowName(rowIdx, newRowName);
}
/**
* @brief Insert column by index. Inserts at specified position so will preceed column at current position.
* @param pColumnIdx zero-based column index.
*/
void InsertColumn(const size_t pColumnIdx, const string defValue = "")
{
const ssize_t columnIdx = pColumnIdx + (mLabelParams.mRowNameIdx + 1);
for (auto itRow = mData.begin(); itRow != mData.end(); ++itRow)
{
itRow->insert(itRow->begin() + columnIdx, defValue);
}
}
/**
* @brief Insert column by name. Inserts at specified position so will preceed column at current position.
* @param pColumnName column label name.
*/
void InsertColumn(const std::string& pColumnName, const string newColName = "", const string defValue = "")
{
ssize_t columnIdx = GetColumnIdx(pColumnName);
if (columnIdx < 0)
{
throw std::out_of_range("column not found: " + pColumnName);
}
InsertColumn(columnIdx, defValue);
SetColumnName(columnIdx,newColName);
}
Thanks a lot! I will take a look and see how to integrate this. It makes sense to support inserting columns/rows.
Hi again @jtaylorme - thanks again for the contributions.
I made a bit of adjustments for consistency with the other functions in rapidcsv, and also added some tests.
It was committed above.
Most Excellent. Thanks again.
Jim