class Redstruct::SortedSet::Slice

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.

Attributes

exclusive[R]

@return [Boolean] if true, assumes the range bounds are exclusive

key[R]

@return [String] the key for the underlying sorted set

lex[R]

@return [Boolean] if true, then assumes the slice is lexicographically sorted

lower[R]

@return [String, Float] the lower bound of the slice

upper[R]

@return [String, Float] the upper bound of the slice

Public Class Methods

new(set, lower: nil, upper: nil, lex: false, exclusive: false) click to toggle source

@param [String, Float] lower lower bound for the slice operation @param [String, Float] upper upper bound for the slice operation @param [Boolean] lex if true, uses lexicographic operations @param [Boolean] exclusive if true, assumes bounds are exclusive

Calls superclass method Redstruct::Factory::Object::new
# 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

Public Instance Methods

inspectable_attributes() click to toggle source
# File lib/redstruct/sorted_set/slice.rb, line 90
def inspectable_attributes
  { lower: @lower, upper: @upper, lex: @lex, exclusive: @exclusive, key: @key }
end
remove() click to toggle source

@return [Integer] the number of elements removed

# 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() click to toggle source

@return [Array<String>] returns an array of values reversed

# 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() click to toggle source

@return [Integer] number of elements in the slice

# 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() click to toggle source

@return [Array<String>] returns an array of values for the given bounds

# 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() click to toggle source

TODO: consider using SortedSet, some other data structure, or nothing @return [::Set] an unordered set representation of the slice

# File lib/redstruct/sorted_set/slice.rb, line 86
def to_set
  ::Set.new(to_a)
end

Private Instance Methods

parse_bound(bound) click to toggle source

( is exclusive

# File lib/redstruct/sorted_set/slice.rb, line 106
def parse_bound(bound)
  case bound
  when -Float::INFINITY then '-inf'
  when Float::INFINITY then '+inf'
  when String then prefix(bound, exclusion: '(')
  else prefix(bound.to_f, exclusion: '(')
  end
end
parse_lex_bound(bound) click to toggle source

( is exclusive, [ is inclusive

# File lib/redstruct/sorted_set/slice.rb, line 97
def parse_lex_bound(bound)
  case bound
  when -Float::INFINITY then '-'
  when Float::INFINITY then '+'
  else prefix(bound, inclusion: '[', exclusion: '(')
  end
end
prefix(value, inclusion: '', exclusion: '') click to toggle source
# File lib/redstruct/sorted_set/slice.rb, line 115
def prefix(value, inclusion: '', exclusion: '')
  prefix = @exclusive ? exclusion : inclusion
  prefixed = value
  prefixed = "#{prefix}#{value}" unless prefix.empty? || prefixed.to_s.start_with?(prefix)

  return prefixed
end