This commit is contained in:
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_;
|
||||
};
|
||||
Reference in New Issue
Block a user