[kutil] Add sorted insert to kutil::vector

Added a sorted insert to vector, as well as cleaning up and extending
the removal functions.
This commit is contained in:
2020-09-07 16:59:21 -07:00
parent 8490472581
commit 71cd7af17b
5 changed files with 102 additions and 15 deletions

View File

@@ -0,0 +1,13 @@
#pragma once
struct unsortableT {
int value;
};
struct sortableT {
int value;
int compare(const sortableT *other) const {
return value - other->value;
}
};

View File

@@ -5,22 +5,12 @@
#include <vector>
#include "kutil/linked_list.h"
#include "catch.hpp"
#include "container_helpers.h"
using namespace kutil;
const int test_list_size = 100;
struct unsortableT {
int value;
};
struct sortableT {
int value;
int compare(const sortableT *other) {
return value - other->value;
}
};
template <typename T>
class ListVectorCompare :
public Catch::MatcherBase<std::vector<list_node<T>>>

25
src/tests/vector.cpp Normal file
View File

@@ -0,0 +1,25 @@
#include <vector>
#include "kutil/vector.h"
#include "catch.hpp"
#include "container_helpers.h"
TEST_CASE( "sorted vector tests", "[containers] [vector]" )
{
using clock = std::chrono::system_clock;
unsigned seed = clock::now().time_since_epoch().count();
std::default_random_engine rng(seed);
std::uniform_int_distribution<int> distrib(0,10000);
kutil::vector<sortableT> v;
int sizes[] = {1, 2, 3, 5, 100};
for (int s : sizes) {
for (int i = 0; i < s; ++i) {
sortableT t { distrib(rng) };
v.sorted_insert(t);
}
for (int i = 1; i < s; ++i)
CHECK( v[i].value >= v[i-1].value );
}
}