Class: Expanse::Set

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/expanse.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSet

Returns a new instance of Set.



108
109
110
111
# File 'lib/expanse.rb', line 108

def initialize
  @ptr = Native.expanse_set_new
  ObjectSpace.define_finalizer(self, self.class.finalize(@ptr))
end

Class Method Details

.finalize(ptr) ⇒ Object



113
114
115
# File 'lib/expanse.rb', line 113

def self.finalize(ptr)
  proc { Native.expanse_set_free(ptr) if ptr && !ptr.null? }
end

Instance Method Details

#add(key) ⇒ Object Also known as: <<



117
118
119
# File 'lib/expanse.rb', line 117

def add(key)
  Native.expanse_set_insert(@ptr, key) != 0
end

#clearObject



149
150
151
152
# File 'lib/expanse.rb', line 149

def clear
  Native.expanse_set_clear(@ptr)
  self
end

#count_range(lo, hi) ⇒ Object



193
194
195
# File 'lib/expanse.rb', line 193

def count_range(lo, hi)
  Native.expanse_set_count_range(@ptr, lo, hi)
end

#delete(key) ⇒ Object Also known as: remove



122
123
124
# File 'lib/expanse.rb', line 122

def delete(key)
  Native.expanse_set_remove(@ptr, key) != 0
end

#eachObject



197
198
199
200
201
202
203
204
# File 'lib/expanse.rb', line 197

def each
  return enum_for(:each) unless block_given?
  cur = first
  while cur
    yield cur
    cur = self.next(cur)
  end
end

#empty?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/expanse.rb', line 139

def empty?
  size.zero?
end

#firstObject



154
155
156
157
158
159
# File 'lib/expanse.rb', line 154

def first
  buf = Fiddle::Pointer.malloc(8)
  if Native.expanse_set_first(@ptr, buf) != 0
    buf.to_str(8).unpack1("Q<")
  end
end

#include?(key) ⇒ Boolean Also known as: key?, member?

Returns:

  • (Boolean)


127
128
129
# File 'lib/expanse.rb', line 127

def include?(key)
  Native.expanse_set_contains(@ptr, key) != 0
end

#lastObject



161
162
163
164
165
166
# File 'lib/expanse.rb', line 161

def last
  buf = Fiddle::Pointer.malloc(8)
  if Native.expanse_set_last(@ptr, buf) != 0
    buf.to_str(8).unpack1("Q<")
  end
end

#mem_usedObject

Bytes of native trie memory currently held by this set (arena accounting from expanse_set_mem_used; excludes the Ruby wrapper object itself).



145
146
147
# File 'lib/expanse.rb', line 145

def mem_used
  Native.expanse_set_mem_used(@ptr)
end

#next(key) ⇒ Object



168
169
170
171
172
173
# File 'lib/expanse.rb', line 168

def next(key)
  buf = Fiddle::Pointer.malloc(8)
  if Native.expanse_set_next_after(@ptr, key, buf) != 0
    buf.to_str(8).unpack1("Q<")
  end
end

#prev(key) ⇒ Object



175
176
177
178
179
180
# File 'lib/expanse.rb', line 175

def prev(key)
  buf = Fiddle::Pointer.malloc(8)
  if Native.expanse_set_prev_before(@ptr, key, buf) != 0
    buf.to_str(8).unpack1("Q<")
  end
end

#rank(key) ⇒ Object



182
183
184
# File 'lib/expanse.rb', line 182

def rank(key)
  Native.expanse_set_count_below(@ptr, key)
end

#select(k) ⇒ Object



186
187
188
189
190
191
# File 'lib/expanse.rb', line 186

def select(k)
  buf = Fiddle::Pointer.malloc(8)
  if Native.expanse_set_by_count(@ptr, k, buf) != 0
    buf.to_str(8).unpack1("Q<")
  end
end

#sizeObject Also known as: length, count



133
134
135
# File 'lib/expanse.rb', line 133

def size
  Native.expanse_set_len(@ptr)
end