Class: Rufus::Tokyo::List

Inherits:
Object
  • Object
show all
Includes:
Enumerable, ListMapMixin
Defined in:
lib/rufus/tokyo/cabinet/util.rb

Overview

A Tokyo Cabinet in-memory (tcutil.h) list

tokyocabinet.sourceforge.net/spex-en.html#tcutilapi

Class Method Summary collapse

Instance Method Summary collapse

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)



227
228
229
230
231
232
233
234
235
# File 'lib/rufus/tokyo/cabinet/util.rb', line 227

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)



393
394
395
396
397
398
# File 'lib/rufus/tokyo/cabinet/util.rb', line 393

def self.free (list_pointer)

  return if list_pointer.address.zero?

  CabinetLib.tclistdel(list_pointer)
end

.release(list_pointer) ⇒ Object

Turns a list pointer into a Ruby Array instance (and makes sure to release the pointer



413
414
415
416
# File 'lib/rufus/tokyo/cabinet/util.rb', line 413

def self.release (list_pointer)

  Rufus::Tokyo::List.new(list_pointer).release
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)



240
241
242
243
244
245
# File 'lib/rufus/tokyo/cabinet/util.rb', line 240

def << (s)

  clib.tclistpush(@pointer, s, Rufus::Tokyo.blen(s))

  self
end

#[](i, count = nil) ⇒ Object

The equivalent of Ruby Array#[]



342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/rufus/tokyo/cabinet/util.rb', line 342

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.



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
308
309
310
# File 'lib/rufus/tokyo/cabinet/util.rb', line 282

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

#clearObject

Empties the list.



361
362
363
364
# File 'lib/rufus/tokyo/cabinet/util.rb', line 361

def clear

  clib.tclistclear(@pointer)
end

#closeObject

Closes (frees) this list



388
389
390
391
392
# File 'lib/rufus/tokyo/cabinet/util.rb', line 388

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)



315
316
317
318
# File 'lib/rufus/tokyo/cabinet/util.rb', line 315

def delete_at (i)

  outlen_op(:tclistremove, i)
end

#delete_ifObject



320
321
322
# File 'lib/rufus/tokyo/cabinet/util.rb', line 320

def delete_if
  # TODO
end

#destroyObject

Closes (frees) this list



389
390
391
392
393
# File 'lib/rufus/tokyo/cabinet/util.rb', line 389

def free

  self.class.free(@pointer)
  @pointer = nil
end

#eachObject

The classical each.



368
369
370
371
# File 'lib/rufus/tokyo/cabinet/util.rb', line 368

def each

  (0..self.size - 1).each { |i| yield self[i] }
end

#freeObject

Closes (frees) this list



382
383
384
385
386
# File 'lib/rufus/tokyo/cabinet/util.rb', line 382

def free

  self.class.free(@pointer)
  @pointer = nil
end

#popObject

Pops the last element in the list



258
259
260
261
# File 'lib/rufus/tokyo/cabinet/util.rb', line 258

def pop

  outlen_op(:tclistpop)
end

#push(*args) ⇒ Object

Pushes an argument or a list of arguments to this list



249
250
251
252
253
254
# File 'lib/rufus/tokyo/cabinet/util.rb', line 249

def push (*args)

  args.each { |a| self << a }

  self
end

#releaseObject

Closes (frees memory from it) this list and returns the ruby version of it



403
404
405
406
407
408
# File 'lib/rufus/tokyo/cabinet/util.rb', line 403

def release

  a = self.to_a
  self.close
  a
end

#shiftObject

Removes and returns the first element in a list



265
266
267
268
269
# File 'lib/rufus/tokyo/cabinet/util.rb', line 265

def shift

  #clib.tclistshift2(@pointer) rescue nil
  outlen_op(:tclistshift)
end

#sizeObject Also known as: length

Returns the size of this Tokyo Cabinet list



333
334
335
336
# File 'lib/rufus/tokyo/cabinet/util.rb', line 333

def size

  clib.tclistnum(@pointer)
end

#sliceObject



324
325
326
# File 'lib/rufus/tokyo/cabinet/util.rb', line 324

def slice
  # TODO
end

#slice!Object

TODO



327
328
329
# File 'lib/rufus/tokyo/cabinet/util.rb', line 327

def slice!
  # TODO
end

#to_aObject

Turns this Tokyo Cabinet list into a Ruby array



375
376
377
378
# File 'lib/rufus/tokyo/cabinet/util.rb', line 375

def to_a

  @pointer.address.zero? ? nil : self.collect { |e| e }
end

#unshift(s) ⇒ Object

Inserts a string at the beginning of the list



273
274
275
276
277
278
# File 'lib/rufus/tokyo/cabinet/util.rb', line 273

def unshift (s)

  clib.tclistunshift(@pointer, s, Rufus::Tokyo.blen(s))

  self
end