loon
High-performance header-only C++ library for low-latency applications
Loading...
Searching...
No Matches
redis_list.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include <algorithm>
7#include <deque>
8#include <optional>
9#include <vector>
10
11namespace loon {
12
27template <typename T>
28class RedisList {
29 public:
31 explicit RedisList() {}
32
34 ~RedisList() = default;
35
37 RedisList(const RedisList& other) = default;
38
40 RedisList(RedisList&& other) noexcept = default;
41
44 explicit RedisList(std::vector<T>&& other)
45 : impl(std::make_move_iterator(other.begin()), std::make_move_iterator(other.end())) {}
46
48 RedisList& operator=(const RedisList& other) = default;
49
51 RedisList& operator=(RedisList&& other) noexcept = default;
52
56 size_t lpush(const T& value) {
57 impl.push_front(value);
58 return impl.size();
59 }
60
64 size_t lpush(T&& value) {
65 impl.push_front(std::move(value));
66 return impl.size();
67 }
68
72 size_t rpush(const T& value) {
73 impl.push_back(value);
74 return impl.size();
75 }
76
80 size_t rpush(T&& value) {
81 impl.push_back(std::move(value));
82 return impl.size();
83 }
84
87 std::optional<T> lpop() {
88 if (empty()) {
89 return std::nullopt;
90 }
91 const auto result = std::move(impl.front());
92 impl.pop_front();
93 return result;
94 }
95
99 std::vector<T> lpop(size_t count) {
100 count = std::min(count, impl.size());
101 std::vector<T> result(std::make_move_iterator(impl.begin()),
102 std::make_move_iterator(impl.begin() + count));
103 impl.erase(impl.begin(), impl.begin() + count);
104 return result;
105 }
106
109 std::optional<T> rpop() {
110 if (empty()) {
111 return std::nullopt;
112 }
113 const auto result = std::move(impl.back());
114 impl.pop_back();
115 return result;
116 }
117
121 std::vector<T> rpop(size_t count) {
122 count = std::min(count, impl.size());
123 std::vector<T> result(std::make_move_iterator(impl.end() - count),
124 std::make_move_iterator(impl.end()));
125 impl.erase(impl.end() - count, impl.end());
126 return result;
127 }
128
143 std::vector<T> lrange(int start, int stop) const {
144 start = start < 0 ? size() + start : start;
145 stop = stop < 0 ? size() + stop : stop;
146
147 // Check if range is valid
148 if (start > stop || start >= (int)size() || stop < 0) {
149 return {};
150 }
151 // Clamp to list bounds
152 start = std::max(0, start);
153 stop = std::min(stop, (int)size() - 1);
154
155 std::vector<T> result(impl.begin() + start, impl.begin() + stop + 1);
156 return result;
157 }
158
161 size_t llen() const { return size(); }
162
165 size_t size() const { return impl.size(); }
166
169 bool empty() const { return impl.empty(); }
170
171 private:
172 std::deque<T> impl;
173};
174
175} // namespace loon
A Redis-compatible list supporting operations from both ends.
size_t lpush(const T &value)
Pushes a value to the front of the list (left push).
RedisList(const RedisList &other)=default
Copy constructor.
size_t llen() const
Returns the length of the list (Redis LLEN command).
std::vector< T > lpop(size_t count)
Removes and returns up to count elements from the front.
std::optional< T > rpop()
Removes and returns the last element (right pop).
size_t rpush(T &&value)
Pushes a value to the back of the list (right push).
bool empty() const
Checks if the list is empty.
~RedisList()=default
Default destructor.
RedisList & operator=(const RedisList &other)=default
Copy assignment operator.
std::optional< T > lpop()
Removes and returns the first element (left pop).
size_t lpush(T &&value)
Pushes a value to the front of the list (left push).
RedisList & operator=(RedisList &&other) noexcept=default
Move assignment operator.
std::vector< T > lrange(int start, int stop) const
Returns a range of elements without removing them.
std::vector< T > rpop(size_t count)
Removes and returns up to count elements from the back.
size_t size() const
Returns the number of elements in the list.
RedisList(RedisList &&other) noexcept=default
Move constructor.
RedisList(std::vector< T > &&other)
Constructs a RedisList from a vector by moving its elements.
size_t rpush(const T &value)
Pushes a value to the back of the list (right push).
RedisList()
Constructs an empty RedisList.