factbase 0.19.5 → 0.19.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dd4224329381f390e0d8262e6868f1edf6b4f0ae07c972bf0d22fdc7581b0ea9
4
- data.tar.gz: 92d79cb596b6b9dc647555c60b0ce7ddc30d270b7b7bdccf1cd8e0a1db4190ce
3
+ metadata.gz: 99e8fdb60e5fb1e8083b76db845cfad8a7b6be35700c543819ce631f8ac3ca19
4
+ data.tar.gz: 1fd106429c8a3aa39e56683060b5e61395e705abe336290da63fb1a35e748ec5
5
5
  SHA512:
6
- metadata.gz: 7a2a9b52b2576c634702c057b2399dc985c6582c7f371c4feae37c8d794f68a3c71ac4467aed8d05becb00e8b63a5fa8cef2b20882455f2ecc6199d215888ce6
7
- data.tar.gz: 88c27d05a03f5bf0270ad713ab76a3f592ecb4128f5d16e284fbf3f910f7e443f961c184b5f658647b4caafa4ccac83c2fcdd5c1578c2d6f1d7ac1e6c12dae46
6
+ metadata.gz: e03d29fb285a301e48723690f3bdad751a0bc229bd559d901e22fdb196a4e263d5b75bbc6fef2da5480f07f1a4d22d510e630f7f7a7af8988d2b981d6a4b0b10
7
+ data.tar.gz: c41be62abd514f4ea0fde5e47fba206f27a8bd89a82a17e03c55a3908e5d9c98e909eca6bb571e183f8c2f11421617f0a20b31db132c1107a91eea0fb12cb0c1
@@ -4,6 +4,11 @@
4
4
  # SPDX-License-Identifier: MIT
5
5
 
6
6
  # Indexed term 'exists'.
7
+ # The @idx[key] structure:
8
+ # {
9
+ # count: Integer (number of facts already processed),
10
+ # facts: Array (facts found),
11
+ # }
7
12
  class Factbase::IndexedExists
8
13
  def initialize(term, idx)
9
14
  @term = term
@@ -11,25 +16,21 @@ class Factbase::IndexedExists
11
16
  end
12
17
 
13
18
  def predict(maps, _fb, _params)
14
- return nil if @idx.nil?
15
- key = [maps.object_id, @term.operands.first, @term.op]
19
+ operand = @term.operands.first.to_s
20
+ key = [maps.object_id, operand, @term.op]
21
+ @idx[key] = { facts: [], count: 0 } if @idx[key].nil?
16
22
  entry = @idx[key]
17
- maps_array = maps.to_a
18
- if entry.nil?
19
- entry = { facts: [], indexed_count: 0 }
20
- @idx[key] = entry
21
- end
22
- if entry[:indexed_count] < maps_array.size
23
- prop = @term.operands.first.to_s
24
- maps_array[entry[:indexed_count]..].each do |m|
25
- entry[:facts] << m unless m[prop].nil?
26
- end
27
- entry[:indexed_count] = maps_array.size
28
- end
29
- if maps.respond_to?(:ensure_copied!)
30
- maps & entry[:facts]
31
- else
32
- (maps & []) | entry[:facts]
23
+ feed(maps.to_a, entry, operand)
24
+ return maps.repack(entry[:facts]) if maps.respond_to?(:repack)
25
+ entry[:facts]
26
+ end
27
+
28
+ private
29
+
30
+ def feed(facts, entry, operand)
31
+ facts[entry[:count]..].each do |m|
32
+ entry[:facts] << m unless m[operand].nil?
33
33
  end
34
+ entry[:count] = facts.size
34
35
  end
35
36
  end
@@ -14,6 +14,7 @@ class Factbase::LazyTaped
14
14
  @copied = false
15
15
  @maps = nil
16
16
  @pairs = nil
17
+ @inverted_pairs = nil
17
18
  @inserted = []
18
19
  @deleted = []
19
20
  @added = []
@@ -93,6 +94,12 @@ class Factbase::LazyTaped
93
94
  (@maps || @origin).to_a
94
95
  end
95
96
 
97
+ def repack(other)
98
+ ensure_copied!
99
+ copied = other.map { |o| @inverted_pairs[o] || o }
100
+ Factbase::Taped.new(copied, inserted: @inserted, deleted: @deleted, added: @added)
101
+ end
102
+
96
103
  def &(other)
97
104
  if other == [] || (@maps || @origin).empty?
98
105
  return Factbase::Taped.new([], inserted: @inserted, deleted: @deleted, added: @added)
@@ -111,10 +118,12 @@ class Factbase::LazyTaped
111
118
  def ensure_copied!
112
119
  return if @copied
113
120
  @pairs = {}.compare_by_identity
121
+ @inverted_pairs = {}.compare_by_identity
114
122
  @maps =
115
123
  @origin.map do |m|
116
124
  n = m.transform_values(&:dup)
117
125
  @pairs[n] = m
126
+ @inverted_pairs[m] = n
118
127
  n
119
128
  end
120
129
  @copied = true
@@ -68,6 +68,10 @@ class Factbase::Taped
68
68
  @origin.to_a
69
69
  end
70
70
 
71
+ def repack(other)
72
+ Factbase::Taped.new(other, inserted: @inserted, deleted: @deleted, added: @added)
73
+ end
74
+
71
75
  def &(other)
72
76
  if other == [] || @origin.empty?
73
77
  return Factbase::Taped.new([], inserted: @inserted, deleted: @deleted, added: @added)
data/lib/factbase/term.rb CHANGED
@@ -176,7 +176,7 @@ class Factbase::Term < Factbase::TermBase
176
176
  # Try to predict which facts from the provided list
177
177
  # should be evaluated. If no prediction can be made,
178
178
  # the same list is returned.
179
- # @param [Array<Hash>] maps Records to iterate, maybe
179
+ # @param [Array<Hash>, Factbase::Taped, Factbase::LazyTaped] maps Records to iterate, maybe
180
180
  # @param [Hash] params Params to use (keys must be strings, not symbols, with values as arrays)
181
181
  # @return [Array<Hash>] Records to iterate
182
182
  def predict(maps, fb, params)
@@ -9,5 +9,5 @@
9
9
  # License:: MIT
10
10
  class Factbase
11
11
  # Current version of the gem (changed by .rultor.yml on every release)
12
- VERSION = '0.19.5' unless const_defined?(:VERSION)
12
+ VERSION = '0.19.6' unless const_defined?(:VERSION)
13
13
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: factbase
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.19.5
4
+ version: 0.19.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko