substring and hide to_string for non-unit-testing.

This commit is contained in:
Michael Niksa 2021-01-15 15:30:19 -08:00
parent 69538c4ab3
commit 9b8c9d4f51
2 changed files with 76 additions and 2 deletions

View file

@ -394,6 +394,13 @@ namespace til // Terminal Implementation Library. Also: "Today I Learned"
boost::container::small_vector<std::pair<T, S>, 1> _list;
S _size;
rle(boost::container::small_vector<std::pair<T, S>, 1> list, S size) :
_list(list),
_size(size)
{
}
public:
//using iterator = details::rle_iterator<typename decltype(_list)::iterator>;
using const_iterator = details::rle_const_iterator<typename decltype(_list)::const_iterator>;
@ -406,6 +413,8 @@ namespace til // Terminal Implementation Library. Also: "Today I Learned"
fill(value);
}
// Returns the total length of all runs as encoded.
S size() const noexcept
{
@ -426,6 +435,23 @@ namespace til // Terminal Implementation Library. Also: "Today I Learned"
return _at(position, applies)->first;
}
[[nodiscard]] rle<T, S> substr(const S offset = 0, const S count = std::numeric_limits<S>::max()) const
{
// TODO: validate params
const S startIndex = offset;
const S endIndex = std::min(_size - offset, count) + offset - 1;
S startApplies, endApplies;
const auto firstRun{ _at(startIndex, startApplies) };
const auto lastRun{ _at(endIndex, endApplies) };
decltype(_list) substring{ firstRun, lastRun + 1};
substring.front().second = startApplies;
substring.back().second = substring.back().second - endApplies + 1;
return til::rle<T, S>(substring, endIndex - startIndex + 1);
}
// Replaces every value seen in the run with a new one
// Does not change the length or position of the values.
void replace(const T oldValue, const T newValue)
@ -579,6 +605,7 @@ namespace til // Terminal Implementation Library. Also: "Today I Learned"
return rend();
}
#ifdef UNIT_TESTING
std::wstring to_string() const
{
std::wstringstream wss;
@ -594,6 +621,7 @@ namespace til // Terminal Implementation Library. Also: "Today I Learned"
return wss.str();
}
#endif
protected:
// TODO: get Dustin help to not duplicate this for constness.
@ -673,8 +701,6 @@ namespace til // Terminal Implementation Library. Also: "Today I Learned"
applies = totalLength - position;
FAIL_FAST_IF(!(applies > 0)); // An attribute applies for >0 characters
// MSFT: 17130145 - will restore this and add a better assert to catch the real issue.
//FAIL_FAST_IF(!(attrApplies <= _size)); // An attribute applies for a maximum of the total length available to us
return runPos;
}

View file

@ -91,6 +91,54 @@ class RunLengthEncodingTests
VERIFY_ARE_EQUAL(appliesExpected, applies);
}
TEST_METHOD(Substr)
{
til::rle<int> rle(10, 10);
rle.insert(3, 0, 4);
rle.insert(7, 4, 2);
rle.insert(11, 6, 3);
rle.insert(4, 9, 1);
// 3 3 3 3 7 7 11 11 11 4
Log::Comment(L"1.) Nothing substring should match original.");
{
til::rle<int> expected(10, 10);
expected = rle;
// 3 3 3 3 7 7 11 11 11 4
const auto actual = rle.substr();
VERIFY_ARE_EQUAL(expected, actual);
}
Log::Comment(L"2.) Offset substring to implicit end.");
{
til::rle<int> expected(7, 10);
expected.insert(3, 0, 1);
expected.insert(7, 1, 2);
expected.insert(11, 3, 3);
expected.insert(4, 6, 1);
// 3 7 7 11 11 11 4
const auto actual = rle.substr(3);
VERIFY_ARE_EQUAL(expected, actual);
}
Log::Comment(L"3.) Substring cutting out middle bit.");
{
til::rle<int> expected(4, 4);
expected.insert(7, 0, 1);
expected.insert(11, 1, 3);
// 7 11 11 11
const auto actual = rle.substr(5, 4);
VERIFY_ARE_EQUAL(expected, actual);
}
}
TEST_METHOD(Replace)
{
til::rle<int> actual(20, 10);