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
-
#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)
224 225 226 227 228 229 230 231 232 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 224 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)
390 391 392 393 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 390 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)
237 238 239 240 241 242 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 237 def << (s) clib.tclistpush(@pointer, s, Rufus::Tokyo.blen(s)) self end |
#[](i, count = nil) ⇒ Object
The equivalent of Ruby Array#[]
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 339 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 { |ii| outlen_op(:tclistval, ii) } range.first == range.last ? r.first : r end |
#[]=(a, b, c = nil) ⇒ Object
The put operation.
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 306 307 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 279 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.
358 359 360 361 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 358 def clear clib.tclistclear(@pointer) end |
#close ⇒ Object
Closes (frees) this list
385 386 387 388 389 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 385 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)
312 313 314 315 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 312 def delete_at (i) outlen_op(:tclistremove, i) end |
#delete_if ⇒ Object
317 318 319 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 317 def delete_if # TODO end |
#destroy ⇒ Object
Closes (frees) this list
386 387 388 389 390 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 386 def free self.class.free(@pointer) @pointer = nil end |
#each ⇒ Object
The classical each.
365 366 367 368 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 365 def each (0..self.size - 1).each { |i| yield self[i] } end |
#free ⇒ Object
Closes (frees) this list
379 380 381 382 383 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 379 def free self.class.free(@pointer) @pointer = nil end |
#pop ⇒ Object
Pops the last element in the list
255 256 257 258 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 255 def pop outlen_op(:tclistpop) end |
#push(*args) ⇒ Object
Pushes an argument or a list of arguments to this list
246 247 248 249 250 251 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 246 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
398 399 400 401 402 403 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 398 def release a = self.to_a self.close a end |
#shift ⇒ Object
Removes and returns the first element in a list
262 263 264 265 266 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 262 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
330 331 332 333 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 330 def size clib.tclistnum(@pointer) end |
#slice ⇒ Object
321 322 323 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 321 def slice # TODO end |
#slice! ⇒ Object
324 325 326 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 324 def slice! # TODO end |
#to_a ⇒ Object
Turns this Tokyo Cabinet list into a Ruby array
372 373 374 375 |
# File 'lib/rufus/tokyo/cabinet/util.rb', line 372 def to_a self.collect { |e| e } end |