Class: Rufus::Tokyo::List
- Inherits:
-
Object
- Object
- Rufus::Tokyo::List
- Includes:
- Enumerable, ListMapMixin
- Defined in:
- lib/rufus/tokyo/cabinet/util.rb
Overview
A Tokyo Cabinet in-memory (tcutil.h) list
Class Method Summary collapse
-
.free(list_pointer) ⇒ Object
Frees (closes) the given ‘native’ (FFI) list (memory pointer).
-
.release(list_pointer) ⇒ Object
Turns a list pointer into a Ruby Array instance (and makes sure to release the pointer.
Instance Method Summary collapse
-
#<<(s) ⇒ Object
Inserts an element in the list (note that the lib will raise an ArgumentError if s is not a String).
-
#[](i, count = nil) ⇒ Object
The equivalent of Ruby Array#[].
-
#[]=(a, b, c = nil) ⇒ Object
The put operation.
-
#clear ⇒ Object
Empties the list.
-
#close ⇒ Object
Closes (frees) this list.
-
#delete_at(i) ⇒ Object
Removes the value at a given index and returns the value (returns nil if no value available).
- #delete_if ⇒ Object
-
#destroy ⇒ Object
Closes (frees) this list.
-
#each ⇒ Object
The classical each.
-
#free ⇒ Object
Closes (frees) this list.
-
#initialize(list_pointer = nil) ⇒ List
constructor
Creates a new Tokyo Cabinet list.
-
#pop ⇒ Object
Pops the last element in the list.
-
#push(*args) ⇒ Object
Pushes an argument or a list of arguments to this list.
-
#release ⇒ Object
Closes (frees memory from it) this list and returns the ruby version of it.
-
#shift ⇒ Object
Removes and returns the first element in a list.
-
#size ⇒ Object
(also: #length)
Returns the size of this Tokyo Cabinet list.
- #slice ⇒ Object
-
#slice! ⇒ Object
TODO.
-
#to_a ⇒ Object
Turns this Tokyo Cabinet list into a Ruby array.
-
#unshift(s) ⇒ Object
Inserts a string at the beginning of the list.
Methods included from ListMapMixin
#clib, #outlen_op, #pointer, #pointer_or_raise
Constructor Details
#initialize(list_pointer = nil) ⇒ List
Creates a new Tokyo Cabinet list.
(by passing a list pointer, one can wrap an existing list pointer into a handy instance of this class)
222 223 224 225 226 227 228 229 230 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 222 def initialize (list_pointer=nil) if list_pointer.is_a?(FFI::Pointer) @pointer = list_pointer else @pointer = clib.tclistnew list_pointer.each { |e| self << e } if list_pointer end end |
Class Method Details
.free(list_pointer) ⇒ Object
Frees (closes) the given ‘native’ (FFI) list (memory pointer)
388 389 390 391 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 388 def self.free (list_pointer) CabinetLib.tclistdel(list_pointer) end |
Instance Method Details
#<<(s) ⇒ Object
Inserts an element in the list (note that the lib will raise an ArgumentError if s is not a String)
235 236 237 238 239 240 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 235 def << (s) clib.tclistpush(@pointer, s, Rufus::Tokyo.blen(s)) self end |
#[](i, count = nil) ⇒ Object
The equivalent of Ruby Array#[]
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 337 def [] (i, count=nil) return nil if (count != nil) && count < 1 len = self.size range = if count.nil? i.is_a?(Range) ? i : [i] else (i..i + count - 1) end r = norm(range).collect { |i| outlen_op(:tclistval, i) } range.first == range.last ? r.first : r end |
#[]=(a, b, c = nil) ⇒ Object
The put operation.
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 277 def []= (a, b, c=nil) i, s = c.nil? ? [ a, b ] : [ [a, b], c ] range = if i.is_a?(Range) i elsif i.is_a?(Array) start, count = i (start..start + count - 1) else [ i ] end range = norm(range) values = s.is_a?(Array) ? s : [ s ] # not "values = Array(s)" range.each_with_index do |offset, index| val = values[index] if val clib.tclistover(@pointer, offset, val, Rufus::Tokyo.blen(val)) else outlen_op(:tclistremove, values.size) end end self end |
#clear ⇒ Object
Empties the list.
356 357 358 359 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 356 def clear clib.tclistclear(@pointer) end |
#close ⇒ Object
Closes (frees) this list
383 384 385 386 387 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 383 def free self.class.free(@pointer) @pointer = nil end |
#delete_at(i) ⇒ Object
Removes the value at a given index and returns the value (returns nil if no value available)
310 311 312 313 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 310 def delete_at (i) outlen_op(:tclistremove, i) end |
#delete_if ⇒ Object
315 316 317 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 315 def delete_if # TODO end |
#destroy ⇒ Object
Closes (frees) this list
384 385 386 387 388 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 384 def free self.class.free(@pointer) @pointer = nil end |
#each ⇒ Object
The classical each.
363 364 365 366 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 363 def each (0..self.size - 1).each { |i| yield self[i] } end |
#free ⇒ Object
Closes (frees) this list
377 378 379 380 381 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 377 def free self.class.free(@pointer) @pointer = nil end |
#pop ⇒ Object
Pops the last element in the list
253 254 255 256 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 253 def pop outlen_op(:tclistpop) end |
#push(*args) ⇒ Object
Pushes an argument or a list of arguments to this list
244 245 246 247 248 249 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 244 def push (*args) args.each { |a| self << a } self end |
#release ⇒ Object
Closes (frees memory from it) this list and returns the ruby version of it
396 397 398 399 400 401 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 396 def release a = self.to_a self.close a end |
#shift ⇒ Object
Removes and returns the first element in a list
260 261 262 263 264 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 260 def shift #clib.tclistshift2(@pointer) rescue nil outlen_op(:tclistshift) end |
#size ⇒ Object Also known as: length
Returns the size of this Tokyo Cabinet list
328 329 330 331 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 328 def size clib.tclistnum(@pointer) end |
#slice ⇒ Object
319 320 321 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 319 def slice # TODO end |
#slice! ⇒ Object
TODO
322 323 324 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 322 def slice! # TODO end |
#to_a ⇒ Object
Turns this Tokyo Cabinet list into a Ruby array
370 371 372 373 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 370 def to_a self.collect { |e| e } end |