[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:
@@ -318,6 +318,7 @@ modules:
|
|||||||
- src/tests/logger.cpp
|
- src/tests/logger.cpp
|
||||||
- src/tests/heap_allocator.cpp
|
- src/tests/heap_allocator.cpp
|
||||||
- src/tests/main.cpp
|
- src/tests/main.cpp
|
||||||
|
- src/tests/vector.cpp
|
||||||
overlays:
|
overlays:
|
||||||
- url: https://f000.backblazeb2.com/file/jsix-os/sysroot-llvm8-20190706.tar.bz2
|
- url: https://f000.backblazeb2.com/file/jsix-os/sysroot-llvm8-20190706.tar.bz2
|
||||||
path: sysroot
|
path: sysroot
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
/// Definition of a simple dynamic vector collection for use in kernel space
|
/// Definition of a simple dynamic vector collection for use in kernel space
|
||||||
|
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
#include "kutil/assert.h"
|
||||||
#include "kutil/memory.h"
|
#include "kutil/memory.h"
|
||||||
|
|
||||||
namespace kutil {
|
namespace kutil {
|
||||||
@@ -104,13 +105,66 @@ public:
|
|||||||
return m_elements[m_size++];
|
return m_elements[m_size++];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Insert an item into the array at the given index
|
||||||
|
void insert(size_t i, const T& item)
|
||||||
|
{
|
||||||
|
if (i >= count()) {
|
||||||
|
append(item);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ensure_capacity(m_size + 1);
|
||||||
|
for (size_t j = m_size; j > i; --j)
|
||||||
|
m_elements[j] = m_elements[j-1];
|
||||||
|
m_size += 1;
|
||||||
|
|
||||||
|
m_elements[i] = item;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Insert an item into the list in a sorted position. Depends on T
|
||||||
|
/// having a method `int compare(const T *other)`.
|
||||||
|
void sorted_insert(const T& item)
|
||||||
|
{
|
||||||
|
size_t start = 0;
|
||||||
|
size_t end = m_size;
|
||||||
|
while (end > start) {
|
||||||
|
size_t m = start + (end - start) / 2;
|
||||||
|
int c = item.compare(&m_elements[m]);
|
||||||
|
if (c < 0) end = m;
|
||||||
|
else start = m + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
insert(start, item);
|
||||||
|
}
|
||||||
|
|
||||||
/// Remove an item from the end of the array.
|
/// Remove an item from the end of the array.
|
||||||
void remove()
|
void remove()
|
||||||
{
|
{
|
||||||
|
kassert(m_size, "Called remove() on an empty array");
|
||||||
|
|
||||||
m_size -= 1;
|
m_size -= 1;
|
||||||
m_elements[m_size].~T();
|
m_elements[m_size].~T();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Remove an item from the front of the array, preserving order.
|
||||||
|
void remove_front()
|
||||||
|
{
|
||||||
|
kassert(m_size, "Called remove_front() on an empty array");
|
||||||
|
remove_at(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Remove the item at the given index from the array, not
|
||||||
|
/// order-preserving.
|
||||||
|
void remove_at(size_t i)
|
||||||
|
{
|
||||||
|
if (i >= count()) return;
|
||||||
|
|
||||||
|
m_elements[i].~T();
|
||||||
|
for (; i < m_size - 1; ++i)
|
||||||
|
m_elements[i] = m_elements[i+1];
|
||||||
|
m_size -= 1;
|
||||||
|
}
|
||||||
|
|
||||||
/// Remove the first occurance of an item from the array, not
|
/// Remove the first occurance of an item from the array, not
|
||||||
/// order-preserving. Does nothing if the item is not in the array.
|
/// order-preserving. Does nothing if the item is not in the array.
|
||||||
void remove_swap(const T &item)
|
void remove_swap(const T &item)
|
||||||
@@ -128,14 +182,18 @@ public:
|
|||||||
void remove_swap_at(size_t i)
|
void remove_swap_at(size_t i)
|
||||||
{
|
{
|
||||||
if (i >= count()) return;
|
if (i >= count()) return;
|
||||||
|
|
||||||
|
m_elements[i].~T();
|
||||||
if (i < m_size - 1)
|
if (i < m_size - 1)
|
||||||
m_elements[i] = m_elements[m_size - 1];
|
m_elements[i] = m_elements[m_size - 1];
|
||||||
remove();
|
m_size -= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Remove an item from the end of the array and return it.
|
/// Remove an item from the end of the array and return it.
|
||||||
T pop()
|
T pop()
|
||||||
{
|
{
|
||||||
|
kassert(m_size, "Called pop() on an empty array");
|
||||||
|
|
||||||
T temp = m_elements[m_size - 1];
|
T temp = m_elements[m_size - 1];
|
||||||
remove();
|
remove();
|
||||||
return temp;
|
return temp;
|
||||||
@@ -144,10 +202,10 @@ public:
|
|||||||
/// Remove an item from the beginning of the array and return it.
|
/// Remove an item from the beginning of the array and return it.
|
||||||
T pop_front()
|
T pop_front()
|
||||||
{
|
{
|
||||||
|
kassert(m_size, "Called pop_front() on an empty array");
|
||||||
|
|
||||||
T temp = m_elements[0];
|
T temp = m_elements[0];
|
||||||
for (size_t i = 1; i < m_size; ++i)
|
remove_front();
|
||||||
m_elements[i-1] = m_elements[i];
|
|
||||||
remove();
|
|
||||||
return temp;
|
return temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
13
src/tests/container_helpers.h
Normal file
13
src/tests/container_helpers.h
Normal 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;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
@@ -5,22 +5,12 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include "kutil/linked_list.h"
|
#include "kutil/linked_list.h"
|
||||||
#include "catch.hpp"
|
#include "catch.hpp"
|
||||||
|
#include "container_helpers.h"
|
||||||
|
|
||||||
using namespace kutil;
|
using namespace kutil;
|
||||||
|
|
||||||
const int test_list_size = 100;
|
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>
|
template <typename T>
|
||||||
class ListVectorCompare :
|
class ListVectorCompare :
|
||||||
public Catch::MatcherBase<std::vector<list_node<T>>>
|
public Catch::MatcherBase<std::vector<list_node<T>>>
|
||||||
|
|||||||
25
src/tests/vector.cpp
Normal file
25
src/tests/vector.cpp
Normal 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 );
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user