Skip to content

Commit

Permalink
allow slice deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
maxbachmann committed Aug 18, 2022
1 parent c00e8bb commit 1d63267
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 73 deletions.
90 changes: 53 additions & 37 deletions extras/rapidfuzz_amalgamated.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
// SPDX-License-Identifier: MIT
// RapidFuzz v1.0.2
// Generated: 2022-08-16 20:44:38.855246
// Generated: 2022-08-18 23:05:26.329830
// ----------------------------------------------------------
// This file is an amalgamation of multiple different files.
// You probably shouldn't edit it directly.
Expand Down Expand Up @@ -293,52 +293,63 @@ auto vector_slice(const Vec& vec, int start, int stop, int step) -> Vec
{
Vec new_vec;

if (step > 0) {
if (start < 0)
start = std::max<int>(start + static_cast<int>(vec.size()), 0);
else if (start > static_cast<int>(vec.size()))
start = static_cast<int>(vec.size());

if (stop < 0)
stop = std::max<int>(stop + static_cast<int>(vec.size()), 0);
else if (stop > static_cast<int>(vec.size()))
stop = static_cast<int>(vec.size());
if (step == 0)
throw std::invalid_argument("slice step cannot be zero");
if (step < 0)
throw std::invalid_argument("step sizes below 0 lead to an invalid order of editops");

if (start >= stop)
return new_vec;
if (start < 0)
start = std::max<int>(start + static_cast<int>(vec.size()), 0);
else if (start > static_cast<int>(vec.size()))
start = static_cast<int>(vec.size());

int count = (stop - 1 - start) / step + 1;
new_vec.reserve(static_cast<size_t>(count));
if (stop < 0)
stop = std::max<int>(stop + static_cast<int>(vec.size()), 0);
else if (stop > static_cast<int>(vec.size()))
stop = static_cast<int>(vec.size());

for (int i = start; i < stop; i += step)
new_vec.push_back(vec[static_cast<size_t>(i)]);
}
else if (step < 0) {
if (start < 0)
start = std::max<int>(start + static_cast<int>(vec.size()), -1);
else if (start >= static_cast<int>(vec.size()))
start = static_cast<int>(vec.size()) - 1;
if (start >= stop)
return new_vec;

if (stop < 0)
stop = std::max<int>(stop + static_cast<int>(vec.size()), -1);
else if (stop >= static_cast<int>(vec.size()))
stop = static_cast<int>(vec.size()) - 1;
int count = (stop - 1 - start) / step + 1;
new_vec.reserve(static_cast<size_t>(count));

if (start <= stop)
return new_vec;
for (int i = start; i < stop; i += step)
new_vec.push_back(vec[static_cast<size_t>(i)]);

int count = (stop + 1 - start) / step + 1;
new_vec.reserve(static_cast<size_t>(count));
return new_vec;
}

for (int i = start; i > stop; i += step)
new_vec.push_back(vec[static_cast<size_t>(i)]);
}
else {
template <typename Vec>
void vector_remove_slice(Vec& vec, int start, int stop, int step)
{
if (step == 0)
throw std::invalid_argument("slice step cannot be zero");
}
if (step < 0)
throw std::invalid_argument("step sizes below 0 lead to an invalid order of editops");

return new_vec;
if (start < 0)
start = std::max<int>(start + static_cast<int>(vec.size()), 0);
else if (start > static_cast<int>(vec.size()))
start = static_cast<int>(vec.size());

if (stop < 0)
stop = std::max<int>(stop + static_cast<int>(vec.size()), 0);
else if (stop > static_cast<int>(vec.size()))
stop = static_cast<int>(vec.size());

if (start >= stop)
return;

auto iter = vec.begin() + start;
for (int i = start; i < static_cast<int>(vec.size()); i++)
if (i >= stop || ((i - start) % step != 0))
*(iter++) = vec[static_cast<size_t>(i)];

vec.resize(static_cast<size_t>(std::distance(vec.begin(), iter)));
vec.shrink_to_fit();
}

} // namespace detail

class Opcodes;
Expand Down Expand Up @@ -423,6 +434,11 @@ class Editops : private std::vector<EditOp> {
return ed_slice;
}

void remove_slice(int start, int stop, int step = 1)
{
detail::vector_remove_slice(*this, start, stop, step);
}

Editops reverse() const
{
Editops reversed = *this;
Expand Down
88 changes: 52 additions & 36 deletions rapidfuzz/details/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,52 +112,63 @@ auto vector_slice(const Vec& vec, int start, int stop, int step) -> Vec
{
Vec new_vec;

if (step > 0) {
if (start < 0)
start = std::max<int>(start + static_cast<int>(vec.size()), 0);
else if (start > static_cast<int>(vec.size()))
start = static_cast<int>(vec.size());

if (stop < 0)
stop = std::max<int>(stop + static_cast<int>(vec.size()), 0);
else if (stop > static_cast<int>(vec.size()))
stop = static_cast<int>(vec.size());
if (step == 0)
throw std::invalid_argument("slice step cannot be zero");
if (step < 0)
throw std::invalid_argument("step sizes below 0 lead to an invalid order of editops");

if (start >= stop)
return new_vec;
if (start < 0)
start = std::max<int>(start + static_cast<int>(vec.size()), 0);
else if (start > static_cast<int>(vec.size()))
start = static_cast<int>(vec.size());

int count = (stop - 1 - start) / step + 1;
new_vec.reserve(static_cast<size_t>(count));
if (stop < 0)
stop = std::max<int>(stop + static_cast<int>(vec.size()), 0);
else if (stop > static_cast<int>(vec.size()))
stop = static_cast<int>(vec.size());

for (int i = start; i < stop; i += step)
new_vec.push_back(vec[static_cast<size_t>(i)]);
}
else if (step < 0) {
if (start < 0)
start = std::max<int>(start + static_cast<int>(vec.size()), -1);
else if (start >= static_cast<int>(vec.size()))
start = static_cast<int>(vec.size()) - 1;
if (start >= stop)
return new_vec;

if (stop < 0)
stop = std::max<int>(stop + static_cast<int>(vec.size()), -1);
else if (stop >= static_cast<int>(vec.size()))
stop = static_cast<int>(vec.size()) - 1;
int count = (stop - 1 - start) / step + 1;
new_vec.reserve(static_cast<size_t>(count));

if (start <= stop)
return new_vec;
for (int i = start; i < stop; i += step)
new_vec.push_back(vec[static_cast<size_t>(i)]);

int count = (stop + 1 - start) / step + 1;
new_vec.reserve(static_cast<size_t>(count));
return new_vec;
}

for (int i = start; i > stop; i += step)
new_vec.push_back(vec[static_cast<size_t>(i)]);
}
else {
template <typename Vec>
void vector_remove_slice(Vec& vec, int start, int stop, int step)
{
if (step == 0)
throw std::invalid_argument("slice step cannot be zero");
}
if (step < 0)
throw std::invalid_argument("step sizes below 0 lead to an invalid order of editops");

return new_vec;
if (start < 0)
start = std::max<int>(start + static_cast<int>(vec.size()), 0);
else if (start > static_cast<int>(vec.size()))
start = static_cast<int>(vec.size());

if (stop < 0)
stop = std::max<int>(stop + static_cast<int>(vec.size()), 0);
else if (stop > static_cast<int>(vec.size()))
stop = static_cast<int>(vec.size());

if (start >= stop)
return;

auto iter = vec.begin() + start;
for (int i = start; i < static_cast<int>(vec.size()); i++)
if (i >= stop || ((i - start) % step != 0))
*(iter++) = vec[static_cast<size_t>(i)];

vec.resize(static_cast<size_t>(std::distance(vec.begin(), iter)));
vec.shrink_to_fit();
}

} // namespace detail

class Opcodes;
Expand Down Expand Up @@ -242,6 +253,11 @@ class Editops : private std::vector<EditOp> {
return ed_slice;
}

void remove_slice(int start, int stop, int step = 1)
{
detail::vector_remove_slice(*this, start, stop, step);
}

Editops reverse() const
{
Editops reversed = *this;
Expand Down

0 comments on commit 1d63267

Please sign in to comment.