Class: Drip::ImmutableDrip

Inherits:
Object
  • Object
show all
Includes:
ArrayBsearch
Defined in:
lib/drip.rb,
lib/drip.rb

Defined Under Namespace

Classes: Generator

Instance Method Summary collapse

Methods included from ArrayBsearch

lower_boundary, upper_boundary

Constructor Details

#initialize(pool = [], tag = []) ⇒ ImmutableDrip

Returns a new instance of ImmutableDrip.



495
496
497
498
# File 'lib/drip.rb', line 495

def initialize(pool=[], tag=[])
  @pool = Drip::SortedArray.new(pool)
  @tag = tag
end

Instance Method Details

#fetch(key) ⇒ Object



500
501
502
# File 'lib/drip.rb', line 500

def fetch(key)
  @pool.fetch(key)
end

#head(n = 1, tag = nil) ⇒ Object



539
540
541
# File 'lib/drip.rb', line 539

def head(n=1, tag=nil)
  tag ? head_tag(n, tag) : @pool.head(n)
end

#head_tag(n, tag) ⇒ Object



530
531
532
533
534
535
536
537
# File 'lib/drip.rb', line 530

def head_tag(n, tag)
  lower = lower_boundary(@tag, [tag, 0])
  upper = upper_boundary(@tag, [tag, INF])
  lower = [lower, upper - n].max
  @tag[lower ... upper].collect {|kv|
    [kv[0][1], *fetch(kv[0][1])]
  }
end

#latest?(key, tag) ⇒ Boolean

Returns:

  • (Boolean)


516
517
518
519
520
521
522
523
524
525
526
527
528
# File 'lib/drip.rb', line 516

def latest?(key, tag)
  return (key == 0) if @pool.empty?
  return @pool.latest?(key) unless tag

  lower = lower_boundary(@tag, [tag, key])
  upper = upper_boundary(@tag, [tag, INF])
  
  if key == 0
    return lower == upper
  else
    return lower == upper - 1
  end
end

#newer(key, tag = nil) ⇒ Object



556
557
558
559
# File 'lib/drip.rb', line 556

def newer(key, tag=nil)
  return read(key, 1)[0] unless tag
  read_tag(key, tag, 1)[0]
end

#older(key, tag = nil) ⇒ Object



549
550
551
552
553
554
# File 'lib/drip.rb', line 549

def older(key, tag=nil)
  return nil if @pool.empty?
  key = @pool.last_key + 1 unless key
  return older_tag(key, tag) if tag
  @pool.older(key)
end

#older_tag(key, tag) ⇒ Object



543
544
545
546
547
# File 'lib/drip.rb', line 543

def older_tag(key, tag)
  idx = upper_boundary(@tag, [tag, key-1])
  k, v = @tag[idx - 1]
  k && k[0] == tag ? [k[1], *fetch(k[1])] : nil
end

#read(key, n = 1) ⇒ Object



504
505
506
# File 'lib/drip.rb', line 504

def read(key, n=1)
  @pool.read(key, n)
end

#read_tag(key, tag, n = 1) ⇒ Object



508
509
510
511
512
513
514
# File 'lib/drip.rb', line 508

def read_tag(key, tag, n=1)
  idx = lower_boundary(@tag, [tag, key + 1])
  return [] unless idx
  @tag[idx, n].find_all {|kv| kv[0][0] == tag}.collect {|kv| 
    [kv[0][1], *fetch(kv[0][1])]
  }
end