Class: Redstruct::SortedSet::Slice
- Inherits:
-
Factory::Object
- Object
- Factory::Object
- Redstruct::SortedSet::Slice
- Defined in:
- lib/redstruct/sorted_set/slice.rb
Overview
Utility class to allow operations on portions of the set only TODO: Support #length property (using LIMIT offset count) of the different range commands, so a slice could be defined as starting at offset X and having length Y, instead of just starting at X and finishing at Y.
Instance Attribute Summary collapse
-
#exclusive ⇒ Boolean
readonly
If true, assumes the range bounds are exclusive.
-
#key ⇒ String
readonly
The key for the underlying sorted set.
-
#lex ⇒ Boolean
readonly
If true, then assumes the slice is lexicographically sorted.
-
#lower ⇒ String, Float
readonly
The lower bound of the slice.
-
#upper ⇒ String, Float
readonly
The upper bound of the slice.
Attributes inherited from Factory::Object
Instance Method Summary collapse
-
#initialize(set, lower: nil, upper: nil, lex: false, exclusive: false) ⇒ Slice
constructor
A new instance of Slice.
- #inspectable_attributes ⇒ Object
-
#remove ⇒ Integer
The number of elements removed.
-
#reverse ⇒ Array<String>
Returns an array of values reversed.
-
#size ⇒ Integer
Number of elements in the slice.
-
#to_a ⇒ Array<String>
Returns an array of values for the given bounds.
-
#to_set ⇒ ::Set
TODO: consider using SortedSet, some other data structure, or nothing.
Methods inherited from Factory::Object
Methods included from Utils::Inspectable
Constructor Details
#initialize(set, lower: nil, upper: nil, lex: false, exclusive: false) ⇒ Slice
Returns a new instance of Slice.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/redstruct/sorted_set/slice.rb', line 29 def initialize(set, lower: nil, upper: nil, lex: false, exclusive: false) super(factory: set.factory) @key = set.key @lex = lex @exclusive = exclusive lower ||= -Float::INFINITY upper ||= Float::INFINITY if @lex @lower = parse_lex_bound(lower) @upper = parse_lex_bound(upper) else @lower = parse_bound(lower) @upper = parse_bound(upper) end end |
Instance Attribute Details
#exclusive ⇒ Boolean (readonly)
Returns if true, assumes the range bounds are exclusive.
23 24 25 |
# File 'lib/redstruct/sorted_set/slice.rb', line 23 def exclusive @exclusive end |
#key ⇒ String (readonly)
Returns the key for the underlying sorted set.
11 12 13 |
# File 'lib/redstruct/sorted_set/slice.rb', line 11 def key @key end |
#lex ⇒ Boolean (readonly)
Returns if true, then assumes the slice is lexicographically sorted.
20 21 22 |
# File 'lib/redstruct/sorted_set/slice.rb', line 20 def lex @lex end |
#lower ⇒ String, Float (readonly)
Returns the lower bound of the slice.
14 15 16 |
# File 'lib/redstruct/sorted_set/slice.rb', line 14 def lower @lower end |
#upper ⇒ String, Float (readonly)
Returns the upper bound of the slice.
17 18 19 |
# File 'lib/redstruct/sorted_set/slice.rb', line 17 def upper @upper end |
Instance Method Details
#inspectable_attributes ⇒ Object
90 91 92 |
# File 'lib/redstruct/sorted_set/slice.rb', line 90 def inspectable_attributes { lower: @lower, upper: @upper, lex: @lex, exclusive: @exclusive, key: @key } end |
#remove ⇒ Integer
Returns the number of elements removed.
67 68 69 70 71 72 73 |
# File 'lib/redstruct/sorted_set/slice.rb', line 67 def remove if @lex self.connection.zremrangebylex(@key, @lower, @upper) else self.connection.zremrangebyscore(@key, @lower, @upper) end end |
#reverse ⇒ Array<String>
Returns an array of values reversed
58 59 60 61 62 63 64 |
# File 'lib/redstruct/sorted_set/slice.rb', line 58 def reverse if @lex self.connection.zrevrangebylex(@key, @lower, @upper) else self.connection.zrevrangebyscore(@key, @lower, @upper) end end |
#size ⇒ Integer
Returns number of elements in the slice.
76 77 78 79 80 81 82 |
# File 'lib/redstruct/sorted_set/slice.rb', line 76 def size if @lex self.connection.zlexcount(@key, @lower, @upper) else self.connection.zcount(@key, @lower, @upper) end end |
#to_a ⇒ Array<String>
Returns an array of values for the given bounds
49 50 51 52 53 54 55 |
# File 'lib/redstruct/sorted_set/slice.rb', line 49 def to_a if @lex self.connection.zrangebylex(@key, @lower, @upper) else self.connection.zrangebyscore(@key, @lower, @upper) end end |
#to_set ⇒ ::Set
TODO: consider using SortedSet, some other data structure, or nothing
86 87 88 |
# File 'lib/redstruct/sorted_set/slice.rb', line 86 def to_set ::Set.new(to_a) end |