Class: Tb::Search::State

Inherits:
Object
  • Object
show all
Defined in:
lib/tb/search.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, val, tail = nil) ⇒ State

Returns a new instance of State.



482
483
484
485
486
# File 'lib/tb/search.rb', line 482

def initialize(key, val, tail=nil)
  @key = key
  @val = val
  @tail = tail
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



487
488
489
# File 'lib/tb/search.rb', line 487

def key
  @key
end

#tailObject (readonly)

Returns the value of attribute tail.



487
488
489
# File 'lib/tb/search.rb', line 487

def tail
  @tail
end

#valObject (readonly)

Returns the value of attribute val.



487
488
489
# File 'lib/tb/search.rb', line 487

def val
  @val
end

Class Method Details

.make(hash) ⇒ Object



478
479
480
# File 'lib/tb/search.rb', line 478

def self.make(hash)
  Tb::Search::EmptyState.merge(hash)
end

Instance Method Details

#==(other) ⇒ Object



512
513
514
515
# File 'lib/tb/search.rb', line 512

def ==(other)
  other.kind_of?(Tb::Search::State) &&
  self.to_h == other.to_h
end

#[](k) ⇒ Object



532
533
534
# File 'lib/tb/search.rb', line 532

def [](k)
  fetch(k, nil)
end

#eachObject



493
494
495
496
497
498
499
500
# File 'lib/tb/search.rb', line 493

def each
  pairs = self
  while !pairs.empty?
    yield [pairs.key, pairs.val]
    pairs = pairs.tail
  end
  nil
end

#empty?Boolean

Returns:

  • (Boolean)


489
490
491
# File 'lib/tb/search.rb', line 489

def empty?
  false
end

#fetch(k, *rest) ⇒ Object



517
518
519
520
521
522
523
524
525
526
527
528
529
530
# File 'lib/tb/search.rb', line 517

def fetch(k, *rest)
  pairs = self
  while !pairs.empty?
    return pairs.val if k == pairs.key
    pairs = pairs.tail
  end
  if block_given?
    yield k
  elsif !rest.empty?
    return rest[0]
  else
    raise KeyError, "key not found: #{k}"
  end
end

#inspectObject



594
595
596
597
598
599
600
601
602
603
# File 'lib/tb/search.rb', line 594

def inspect
  pairs = self
  str = ''
  while !pairs.empty?
    str << pairs.key.inspect << "=>" << pairs.val.inspect << ", "
    pairs = pairs.tail
  end
  str.sub!(/, \z/, '')
  "\#<#{self.class}: #{str}>"
end

#keysObject



540
541
542
543
544
545
546
547
548
# File 'lib/tb/search.rb', line 540

def keys
  result = []
  pairs = self
  while !pairs.empty?
    result << pairs.key
    pairs = pairs.tail
  end
  result
end

#merge(h) ⇒ Object



550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
# File 'lib/tb/search.rb', line 550

def merge(h)
  return self if h.empty?
  n = 0
  pairs = self
  ary = []
  result = self
  needs_copy = 0
  while !pairs.empty?
    if h.has_key? pairs.key
      needs_copy = ary.length
      result = pairs.tail
      n += 1
      break if n == h.size
    else
      ary << pairs
    end
    pairs = pairs.tail
  end
  (needs_copy-1).downto(0) {|i|
    pairs = ary[i]
    result = Tb::Search::State.new(pairs.key, pairs.val, result)
  }
  h.reverse_each {|k, v|
    result = Tb::Search::State.new(k, v, result)
  }
  result
end

#rejectObject



578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
# File 'lib/tb/search.rb', line 578

def reject
  ary = []
  pairs = self
  while !pairs.empty?
    unless yield pairs.key, pairs.val
      ary << pairs
    end
    pairs = pairs.tail
  end
  result = Tb::Search::EmptyState
  ary.reverse_each {|pairs2|
    result = Tb::Search::State.new(pairs2.key, pairs2.val, result)
  }
  result
end

#to_hObject



502
503
504
505
506
507
508
509
510
# File 'lib/tb/search.rb', line 502

def to_h
  res = {}
  each {|k, v|
    if !res.has_key? k
      res[k] = v
    end
  }
  res
end

#values_at(*ks) ⇒ Object



536
537
538
# File 'lib/tb/search.rb', line 536

def values_at(*ks)
  ks.map {|k| self[k] }
end