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



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

def clear
  Native.expanse_set_clear(@ptr)
  self
end

#count_range(lo, hi) ⇒ Object



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

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



191
192
193
194
195
196
197
198
# File 'lib/expanse.rb', line 191

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



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

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



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

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

#next(key) ⇒ Object



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

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



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

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



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

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

#select(k) ⇒ Object



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

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