Class: Rufus::Tokyo::List

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

Overview

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

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

Instance Method Summary collapse

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)



162
163
164
# File 'lib/rufus/tokyo/cabinet/util.rb', line 162

def initialize (list_pointer = nil)
  @list = list_pointer || lib.tclistnew
end

Instance Method Details

#<<(s) ⇒ Object



166
167
168
169
170
# File 'lib/rufus/tokyo/cabinet/util.rb', line 166

def << (s)
  #raise 'cannot insert nils into Tokyo Cabinet lists' unless s
  lib.tclistpush2(@list, s)
  self
end

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

The equivalent of Ruby Array#[]



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/rufus/tokyo/cabinet/util.rb', line 262

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| lib.tclistval2(@list, i) rescue nil }

  range.first == range.last ? r.first : r
end

#[]=(a, b, c = nil) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/rufus/tokyo/cabinet/util.rb', line 201

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
      lib.tclistover2(@list, offset, val)
    else
      lib.tclistremove2(@list, values.size)
    end
  end

  self
end

#clearObject



279
280
281
# File 'lib/rufus/tokyo/cabinet/util.rb', line 279

def clear
  lib.tclistclear(@list)
end

#closeObject Also known as: free, destroy

Closes (frees) this list



297
298
299
300
# File 'lib/rufus/tokyo/cabinet/util.rb', line 297

def close
  lib.tclistdel(@list)
  @list = nil
end

#delete_at(i) ⇒ Object

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



235
236
237
# File 'lib/rufus/tokyo/cabinet/util.rb', line 235

def delete_at (i)
  lib.tclistremove2(@list, i)
end

#delete_ifObject



239
240
241
# File 'lib/rufus/tokyo/cabinet/util.rb', line 239

def delete_if
  # TODO
end

#eachObject



283
284
285
# File 'lib/rufus/tokyo/cabinet/util.rb', line 283

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

#popObject

Pops the last element in the list



182
183
184
# File 'lib/rufus/tokyo/cabinet/util.rb', line 182

def pop
  lib.tclistpop2(@list) rescue nil
end

#push(*args) ⇒ Object

Pushes an argument or a list of arguments to this list



175
176
177
# File 'lib/rufus/tokyo/cabinet/util.rb', line 175

def push (*args)
  args.each { |a| self << a }
end

#shiftObject

Removes and returns the first element in a list



189
190
191
# File 'lib/rufus/tokyo/cabinet/util.rb', line 189

def shift
  lib.tclistshift2(@list) rescue nil
end

#sizeObject Also known as: length

Returns the size of this Tokyo Cabinet list



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

def size
  lib.tclistnum(@list)
end

#sliceObject



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

def slice
  # TODO
end

#slice!Object

TODO



246
247
248
# File 'lib/rufus/tokyo/cabinet/util.rb', line 246

def slice!
  # TODO
end

#to_aObject

Turns this Tokyo Cabinet list into a Ruby array



290
291
292
# File 'lib/rufus/tokyo/cabinet/util.rb', line 290

def to_a
  self.collect { |e| e }
end

#unshift(s) ⇒ Object

Inserts a string at the beginning of the list



196
197
198
199
# File 'lib/rufus/tokyo/cabinet/util.rb', line 196

def unshift (s)
  lib.tclistunshift2(@list, s)
  self
end