Skip to content

Commit

Permalink
Improve functions UpdateRow
Browse files Browse the repository at this point in the history
  • Loading branch information
morzhovets committed May 14, 2024
1 parent 9b38f69 commit 764535d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
12 changes: 8 additions & 4 deletions include/momo/DataTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,8 @@ class DataTable
}

template<typename Item>
RowReference UpdateRow(ConstRowReference rowRef, const Column<Item>& column, Item&& newItem)
RowReference UpdateRow(ConstRowReference rowRef, const Column<Item>& column,
internal::Identity<Item>&& newItem)
{
TryResult res = pvTryUpdateRow(rowRef, column, std::move(newItem));
if (res.uniqueHashIndex != UniqueHashIndex::empty)
Expand All @@ -617,7 +618,8 @@ class DataTable
}

template<typename Item>
RowReference UpdateRow(ConstRowReference rowRef, const Column<Item>& column, const Item& newItem)
RowReference UpdateRow(ConstRowReference rowRef, const Column<Item>& column,
const internal::Identity<Item>& newItem)
{
TryResult res = pvTryUpdateRow(rowRef, column, newItem);
if (res.uniqueHashIndex != UniqueHashIndex::empty)
Expand All @@ -641,13 +643,15 @@ class DataTable
}

template<typename Item>
TryResult TryUpdateRow(ConstRowReference rowRef, const Column<Item>& column, Item&& newItem)
TryResult TryUpdateRow(ConstRowReference rowRef, const Column<Item>& column,
internal::Identity<Item>&& newItem)
{
return pvTryUpdateRow(rowRef, column, std::move(newItem));
}

template<typename Item>
TryResult TryUpdateRow(ConstRowReference rowRef, const Column<Item>& column, const Item& newItem)
TryResult TryUpdateRow(ConstRowReference rowRef, const Column<Item>& column,
const internal::Identity<Item>& newItem)
{
return pvTryUpdateRow(rowRef, column, newItem);
}
Expand Down
13 changes: 7 additions & 6 deletions test/sources/SimpleDataSampler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,23 @@ namespace sample_data1
table.AddUniqueHashIndex(strCol, intCol);

table.AddRow(strCol = "b", intCol = 1, dblCol = 0.5);
table.AddRow(intCol = 2, dblCol = 0.5); // strCol = ""
table.AddRow(intCol = 2, dblCol = 2.5); // strCol = ""

table.TryAddRow(intCol = 2); // not added because of unique index

std::cout << table.GetCount() << std::endl; // 2

// table[0][dblCol] = 1.5;
table.UpdateRow(table[0], dblCol, 1.5);
// table[0][dblCol] returns `const double&`
std::cout << table[0][dblCol] << std::endl; // 0.5

// table[1][strCol] = "a";
table.UpdateRow(table[1], strCol, std::string("a"));
// after adding the row can be modified by function `UpdateRow`
table.UpdateRow(table[0], dblCol, 1.5);
table.UpdateRow(table[1], strCol, "a");

for (auto row : table)
std::cout << row[intCol] << " " << row[dblCol] << " " << row[strCol] << std::endl;
// 1 1.5 b
// 2 0.5 a
// 2 2.5 a

{
// select by condition
Expand Down
2 changes: 1 addition & 1 deletion test/sources/SimpleDataTester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ class SimpleDataTester
}

for (size_t i = 0; i < count; i += 2)
table.UpdateRow(table[i], strCol, std::string("0"));
table.UpdateRow(table[i], strCol, "0");

for (size_t i = 1; i < count; i += 2)
{
Expand Down

0 comments on commit 764535d

Please sign in to comment.