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.



517
518
519
520
# File 'lib/drip.rb', line 517

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

Instance Method Details

#fetch(key) ⇒ Object



522
523
524
# File 'lib/drip.rb', line 522

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

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



574
575
576
# File 'lib/drip.rb', line 574

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

#head_tag(n, tag) ⇒ Object



565
566
567
568
569
570
571
572
# File 'lib/drip.rb', line 565

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)


538
539
540
541
542
543
544
545
546
547
548
549
550
# File 'lib/drip.rb', line 538

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



591
592
593
594
# File 'lib/drip.rb', line 591

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

#older(key, tag = nil) ⇒ Object



584
585
586
587
588
589
# File 'lib/drip.rb', line 584

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



578
579
580
581
582
# File 'lib/drip.rb', line 578

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



526
527
528
# File 'lib/drip.rb', line 526

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

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



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

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

#tag_next(tag) ⇒ Object



552
553
554
555
556
# File 'lib/drip.rb', line 552

def tag_next(tag)
  lower = lower_boundary(@tag, [tag, INF])
  return nil unless @tag[lower]
  @tag[lower][0][0]
end

#tag_prev(tag) ⇒ Object



558
559
560
561
562
563
# File 'lib/drip.rb', line 558

def tag_prev(tag)
  upper = upper_boundary(@tag, [tag, 0]) - 1
  return nil if upper < 0
  return nil unless @tag[upper]
  @tag[upper][0][0]
end