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

public initialize(list_pointer = nil)

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)

[View source]


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

clib, outlen_op, pointer, pointer_or_raise

Public Class Method Details

free

public free(list_pointer)

Frees (closes) the given ‘native’ (FFI) list (memory pointer)

[View source]


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

def self.free (list_pointer)

  CabinetLib.tclistdel(list_pointer)
end

release

public release(list_pointer)

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

[View source]


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

<<

public <<(s)

Inserts an element in the list (note that the lib will raise an ArgumentError if s is not a String)

[View source]


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

[]

public [](i, count = nil)

The equivalent of Ruby Array#[]

[View source]


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

[]=

public []=(a, b, c = nil)

The put operation.

[View source]


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

public clear

Empties the list.

[View source]


356
357
358
359
# File 'lib/rufus/tokyo/cabinet/util.rb', line 356

def clear

  clib.tclistclear(@pointer)
end

delete_at

public delete_at(i)

Removes the value at a given index and returns the value (returns nil if no value available)

[View source]


310
311
312
313
# File 'lib/rufus/tokyo/cabinet/util.rb', line 310

def delete_at (i)

  outlen_op(:tclistremove, i)
end

delete_if

public delete_if
[View source]


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

def delete_if
  # TODO
end

each

public each

The classical each.

[View source]


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

public free

Also known as: close destroy

Closes (frees) this list

[View source]


377
378
379
380
381
# File 'lib/rufus/tokyo/cabinet/util.rb', line 377

def free

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

pop

public pop

Pops the last element in the list

[View source]


253
254
255
256
# File 'lib/rufus/tokyo/cabinet/util.rb', line 253

def pop

  outlen_op(:tclistpop)
end

push

public push(*args)

Pushes an argument or a list of arguments to this list

[View source]


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

public release

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

[View source]


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

public shift

Removes and returns the first element in a list

[View source]


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

public size

Also known as: length

Returns the size of this Tokyo Cabinet list

[View source]


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

def size

  clib.tclistnum(@pointer)
end

slice

public slice
[View source]


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

def slice
  # TODO
end

slice!

public slice!
[View source]


322
323
324
# File 'lib/rufus/tokyo/cabinet/util.rb', line 322

def slice!
  # TODO
end

to_a

public to_a

Turns this Tokyo Cabinet list into a Ruby array

[View source]


370
371
372
373
# File 'lib/rufus/tokyo/cabinet/util.rb', line 370

def to_a

  self.collect { |e| e }
end

unshift

public unshift(s)

Inserts a string at the beginning of the list

[View source]


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
Generated on Friday, September 18 2009 at 10:35:44 AM by YARD 0.2.3.5 (ruby-1.8.7).