This commit is contained in:
14
rediska/data-structures/AbstractDataStructure.hpp
Normal file
14
rediska/data-structures/AbstractDataStructure.hpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <expected>
|
||||
#include <optional>
|
||||
#include "rediska/common/enums.hpp"
|
||||
#include "rediska/data-structures/enums.hpp"
|
||||
#include "rediska/data-structures/types.hpp"
|
||||
|
||||
class AbstractDataStructure {
|
||||
public:
|
||||
virtual ~AbstractDataStructure() = default;
|
||||
|
||||
virtual std::expected<std::optional<DSValue>, DSReturnCode> handle(OperationId op, DSValue data) = 0;
|
||||
};
|
||||
7
rediska/data-structures/CMakeLists.txt
Normal file
7
rediska/data-structures/CMakeLists.txt
Normal file
@@ -0,0 +1,7 @@
|
||||
add_library(data-structures STATIC
|
||||
impl/ListDataStructure.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(data-structures PUBLIC
|
||||
common
|
||||
)
|
||||
0
rediska/data-structures/README.md
Normal file
0
rediska/data-structures/README.md
Normal file
3
rediska/data-structures/enums.hpp
Normal file
3
rediska/data-structures/enums.hpp
Normal file
@@ -0,0 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
enum class DSReturnCode { OK, INCOMPATIBLE_OPERATION, EMPTY, OUT_OF_RANGE, NOT_FOUND };
|
||||
56
rediska/data-structures/impl/ListDataStructure.cpp
Normal file
56
rediska/data-structures/impl/ListDataStructure.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
#include <list>
|
||||
#include "rediska/data-structures/AbstractDataStructure.hpp"
|
||||
#include "rediska/data-structures/enums.hpp"
|
||||
#include "rediska/data-structures/types.hpp"
|
||||
|
||||
class ListDataStructure : public AbstractDataStructure {
|
||||
public:
|
||||
~ListDataStructure() = default;
|
||||
|
||||
using arguments = struct {};
|
||||
|
||||
std::expected<std::optional<DSValue>, DSReturnCode> handle(OperationId op, DSValue data) override {
|
||||
switch (op) {
|
||||
case OperationId::GET: {
|
||||
// TODO: Index
|
||||
// if (index < 0 || index >= list_.size()) {
|
||||
// return std::unexpected(DSReturnCode::OUT_OF_RANGE);
|
||||
// }
|
||||
return std::make_optional(*list_.begin());
|
||||
}
|
||||
case OperationId::SET: {
|
||||
// Replace entire list with a single value if provided
|
||||
list_.clear();
|
||||
list_.push_back(data);
|
||||
return std::nullopt;
|
||||
}
|
||||
case OperationId::LIST_PUSH_BACK: {
|
||||
list_.push_back(data);
|
||||
return std::nullopt;
|
||||
}
|
||||
case OperationId::LIST_POP_BACK: {
|
||||
if (list_.empty()) {
|
||||
return std::unexpected(DSReturnCode::EMPTY);
|
||||
}
|
||||
DSValue value = list_.back();
|
||||
list_.pop_back();
|
||||
return std::make_optional(value);
|
||||
}
|
||||
case OperationId::LIST_INSERT: {
|
||||
// TODO: if index
|
||||
list_.insert(list_.begin(), data);
|
||||
return std::nullopt;
|
||||
}
|
||||
case OperationId::LIST_ERASE: {
|
||||
// TODO: if index
|
||||
list_.erase(list_.begin());
|
||||
return std::nullopt;
|
||||
}
|
||||
default:
|
||||
return std::unexpected(DSReturnCode::INCOMPATIBLE_OPERATION);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::list<DSValue> list_;
|
||||
};
|
||||
15
rediska/data-structures/impl/ListDataStructure.hpp
Normal file
15
rediska/data-structures/impl/ListDataStructure.hpp
Normal file
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include <list>
|
||||
#include "rediska/data-structures/AbstractDataStructure.hpp"
|
||||
#include "rediska/data-structures/types.hpp"
|
||||
|
||||
class ListDataStructure : public AbstractDataStructure {
|
||||
public:
|
||||
~ListDataStructure() = default;
|
||||
|
||||
std::expected<std::optional<DSValue>, DSReturnCode> handle(OperationId op, DSValue data) override;
|
||||
|
||||
private:
|
||||
std::list<DSValue> list_;
|
||||
};
|
||||
7
rediska/data-structures/types.hpp
Normal file
7
rediska/data-structures/types.hpp
Normal file
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
|
||||
using DSValue = std::variant<bool, int64_t, double, std::string>;
|
||||
Reference in New Issue
Block a user