Class: Rufus::Tokyo::List
Included Modules
Enumerable, Rufus::Tokyo::ListMapMixin
A Tokyo Cabinet in-memory (tcutil.h) list
http://tokyocabinet.sourceforge.net/spex-en.html#tcutilapi
Constructor Summary
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 |
Public Visibility
Public Class Method Summary
| free(list_pointer) |
Frees (closes) the given ‘native’ (FFI) list (memory pointer). |
|---|---|
| release(list_pointer) |
Turns a list pointer into a Ruby Array instance (and makes sure to release the pointer. |
Public Instance Method Summary
| #<<(s) |
Inserts an element in the list (note that the lib will raise an ArgumentError if s is not a String). |
|---|---|
| #[](i, count = nil) |
The equivalent of Ruby Array#[]. |
| #[]=(a, b, c = nil) |
The put operation. |
| #clear |
Empties the list. |
| #delete_at(i) |
Removes the value at a given index and returns the value (returns nil if no value available). |
| #delete_if | |
| #each |
The classical each. |
| #free #close #destroy |
Closes (frees) this list. |
| #pop |
Pops the last element in the list. |
| #push(*args) |
Pushes an argument or a list of arguments to this list. |
| #release |
Closes (frees memory from it) this list and returns the ruby version of it. |
| #shift |
Removes and returns the first element in a list. |
| #size #length |
Returns the size of this Tokyo Cabinet list. |
| #slice | |
| #slice! | |
| #to_a |
Turns this Tokyo Cabinet list into a Ruby array. |
| #unshift(s) |
Inserts a string at the beginning of the list. |
Public Instance Methods Included from Rufus::Tokyo::ListMapMixin
Public Class Method Details
free
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 |
release
Turns a list pointer into a Ruby Array instance (and makes sure to release the pointer
406 407 408 409 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 406 def self.release (list_pointer) Rufus::Tokyo::List.new(list_pointer).release end |
Public Instance Method Details
<<
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 |
[]
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 |
[]=
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
Empties the list.
356 357 358 359 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 356 def clear clib.tclistclear(@pointer) end |
delete_at
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
315 316 317 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 315 def delete_if # TODO end |
each
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
Also known as: close destroy
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
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
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
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
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
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
319 320 321 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 319 def slice # TODO end |
slice!
322 323 324 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 322 def slice! # TODO end |
to_a
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 |
unshift
Inserts a string at the beginning of the list
268 269 270 271 272 273 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 268 def unshift (s) clib.tclistunshift(@pointer, s, Rufus::Tokyo.blen(s)) self end |