Class: NRSER::Labs::Index

Inherits:
Object show all
Defined in:
lib/nrser/labs/index.rb

Overview

A very basic index data structure that

Instance Method Summary collapse

Constructor Details

#initialize(entries = nil, sort: false, &indexer) ⇒ Index

Instantiate a new ‘NRSER::Index`.



41
42
43
44
45
46
# File 'lib/nrser/labs/index.rb', line 41

def initialize entries = nil, sort: false, &indexer
  @indexer = indexer
  @hash = Hash.new { |hash, key| hash[key] = Set.new }
  
  add( *entries ) if entries
end

Instance Method Details

#[](key) ⇒ Object



67
68
69
# File 'lib/nrser/labs/index.rb', line 67

def [] key
  @hash[key]
end

#add(*entries) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/nrser/labs/index.rb', line 72

def add *entries
  entries.each do |entry|
    @hash[key_for( entry )].add entry
  end
  
  self
end

#key_for(entry) ⇒ Object

Instance Methods



52
53
54
# File 'lib/nrser/labs/index.rb', line 52

def key_for entry
  @indexer.call entry
end

#keysObject



57
58
59
# File 'lib/nrser/labs/index.rb', line 57

def keys
  Set.new @hash.keys
end

#remove(*entries) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/nrser/labs/index.rb', line 81

def remove *entries
  entries.each do |entry|
    @hash[key_for( entry )].remove entry
  end
  
  self
end

#valuesObject



62
63
64
# File 'lib/nrser/labs/index.rb', line 62

def values
  @hash.values.reduce :+
end