loon
High-performance header-only C++ library for low-latency applications
Loading...
Searching...
No Matches
loon::RedisList< T > Class Template Reference

A Redis-compatible list supporting operations from both ends. More...

#include <redis_list.hpp>

Public Member Functions

 RedisList ()
 Constructs an empty RedisList.
 
 ~RedisList ()=default
 Default destructor.
 
 RedisList (const RedisList &other)=default
 Copy constructor.
 
 RedisList (RedisList &&other) noexcept=default
 Move constructor.
 
 RedisList (std::vector< T > &&other)
 Constructs a RedisList from a vector by moving its elements.
 
RedisListoperator= (const RedisList &other)=default
 Copy assignment operator.
 
RedisListoperator= (RedisList &&other) noexcept=default
 Move assignment operator.
 
size_t lpush (const T &value)
 Pushes a value to the front of the list (left push).
 
size_t lpush (T &&value)
 Pushes a value to the front of the list (left push).
 
size_t rpush (const T &value)
 Pushes a value to the back of the list (right push).
 
size_t rpush (T &&value)
 Pushes a value to the back of the list (right push).
 
std::optional< T > lpop ()
 Removes and returns the first element (left pop).
 
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).
 
std::vector< T > rpop (size_t count)
 Removes and returns up to count elements from the back.
 
std::vector< T > lrange (int start, int stop) const
 Returns a range of elements without removing them.
 
size_t llen () const
 Returns the length of the list (Redis LLEN command).
 
size_t size () const
 Returns the number of elements in the list.
 
bool empty () const
 Checks if the list is empty.
 

Detailed Description

template<typename T>
class loon::RedisList< T >

A Redis-compatible list supporting operations from both ends.

RedisList provides a double-ended queue with an API modeled after Redis list commands. It supports efficient O(1) push/pop operations at both ends and O(n) range queries.

Template Parameters
TElement type stored in the list.
list.rpush("hello");
list.rpush("world");
auto val = list.lpop(); // returns "hello"
A Redis-compatible list supporting operations from both ends.
std::optional< T > lpop()
Removes and returns the first element (left pop).
size_t rpush(const T &value)
Pushes a value to the back of the list (right push).

Definition at line 28 of file redis_list.hpp.

Constructor & Destructor Documentation

◆ RedisList()

template<typename T >
loon::RedisList< T >::RedisList ( std::vector< T > &&  other)
inlineexplicit

Constructs a RedisList from a vector by moving its elements.

Parameters
otherVector to move elements from.

Definition at line 44 of file redis_list.hpp.

Member Function Documentation

◆ empty()

template<typename T >
bool loon::RedisList< T >::empty ( ) const
inline

Checks if the list is empty.

Returns
true if the list contains no elements, false otherwise.

Definition at line 169 of file redis_list.hpp.

Referenced by loon::RedisList< T >::lpop(), and loon::RedisList< T >::rpop().

◆ llen()

template<typename T >
size_t loon::RedisList< T >::llen ( ) const
inline

Returns the length of the list (Redis LLEN command).

Returns
The number of elements in the list.

Definition at line 161 of file redis_list.hpp.

References loon::RedisList< T >::size().

◆ lpop() [1/2]

template<typename T >
std::optional< T > loon::RedisList< T >::lpop ( )
inline

Removes and returns the first element (left pop).

Returns
The removed element, or std::nullopt if the list is empty.

Definition at line 87 of file redis_list.hpp.

References loon::RedisList< T >::empty().

◆ lpop() [2/2]

template<typename T >
std::vector< T > loon::RedisList< T >::lpop ( size_t  count)
inline

Removes and returns up to count elements from the front.

Parameters
countMaximum number of elements to remove.
Returns
Vector of removed elements (may be smaller than count).

Definition at line 99 of file redis_list.hpp.

◆ lpush() [1/2]

template<typename T >
size_t loon::RedisList< T >::lpush ( const T &  value)
inline

Pushes a value to the front of the list (left push).

Parameters
valueThe value to push (copied).
Returns
The new length of the list.

Definition at line 56 of file redis_list.hpp.

◆ lpush() [2/2]

template<typename T >
size_t loon::RedisList< T >::lpush ( T &&  value)
inline

Pushes a value to the front of the list (left push).

Parameters
valueThe value to push (moved).
Returns
The new length of the list.

Definition at line 64 of file redis_list.hpp.

◆ lrange()

template<typename T >
std::vector< T > loon::RedisList< T >::lrange ( int  start,
int  stop 
) const
inline

Returns a range of elements without removing them.

Supports negative indices: -1 is the last element, -2 is second to last, etc. The range is inclusive on both ends (unlike typical C++ iterators).

Parameters
startStarting index (inclusive, can be negative).
stopEnding index (inclusive, can be negative).
Returns
Vector of elements in the specified range, empty if invalid range.
list.lrange(0, -1); // Returns all elements
list.lrange(0, 2); // Returns first 3 elements
list.lrange(-3, -1); // Returns last 3 elements

Definition at line 143 of file redis_list.hpp.

References loon::RedisList< T >::size().

◆ rpop() [1/2]

template<typename T >
std::optional< T > loon::RedisList< T >::rpop ( )
inline

Removes and returns the last element (right pop).

Returns
The removed element, or std::nullopt if the list is empty.

Definition at line 109 of file redis_list.hpp.

References loon::RedisList< T >::empty().

◆ rpop() [2/2]

template<typename T >
std::vector< T > loon::RedisList< T >::rpop ( size_t  count)
inline

Removes and returns up to count elements from the back.

Parameters
countMaximum number of elements to remove.
Returns
Vector of removed elements (may be smaller than count).

Definition at line 121 of file redis_list.hpp.

◆ rpush() [1/2]

template<typename T >
size_t loon::RedisList< T >::rpush ( const T &  value)
inline

Pushes a value to the back of the list (right push).

Parameters
valueThe value to push (copied).
Returns
The new length of the list.

Definition at line 72 of file redis_list.hpp.

◆ rpush() [2/2]

template<typename T >
size_t loon::RedisList< T >::rpush ( T &&  value)
inline

Pushes a value to the back of the list (right push).

Parameters
valueThe value to push (moved).
Returns
The new length of the list.

Definition at line 80 of file redis_list.hpp.

◆ size()

template<typename T >
size_t loon::RedisList< T >::size ( ) const
inline

Returns the number of elements in the list.

Returns
The number of elements in the list.

Definition at line 165 of file redis_list.hpp.

Referenced by loon::RedisList< T >::llen(), and loon::RedisList< T >::lrange().


The documentation for this class was generated from the following file: