cathy 0.1.0 → 0.2.0
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 +4 -4
- data/.standard.yml +1 -1
- data/CHANGELOG.md +14 -1
- data/Gemfile +1 -1
- data/README.md +2 -2
- data/cathy.gemspec +2 -3
- data/examples/chatty_cathy.rb +3 -1
- data/lib/cathy/version.rb +1 -1
- data/lib/cathy.rb +28 -25
- data/sig/cathy.rbs +48 -2
- metadata +6 -8
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 5a5f23ec188f22e8c5a5fee0febd524baea72aabfe506d45833afb869c632a50
|
|
4
|
+
data.tar.gz: 3e74d451692de3704b0eb6797e80c7393ce8ac6523b1ad48cf49d5e770c6f7f8
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6ab5b417e971aea741b335b2c77ba7aaf3b4a5f44c85f71fa9689b5b92838a3b599d2bfb1e0af1e1abc30dfde09b121d07dca9f09f1b90719fa7a5b6d52b8dee
|
|
7
|
+
data.tar.gz: 925d036f54e2da5f854655f6a0a51bdf7c0a6f09d4f7cf0c3f6495ade0e43288eb969a3da0619809ec6c1b76650e10fdc8095af1a8248fafcadb3ebde35a57a0
|
data/.standard.yml
CHANGED
data/CHANGELOG.md
CHANGED
|
@@ -1,4 +1,17 @@
|
|
|
1
|
-
## [
|
|
1
|
+
## [0.2.0] - 2026-11-28
|
|
2
|
+
|
|
3
|
+
### Changed
|
|
4
|
+
|
|
5
|
+
- `Cathy#add` now locates existing items in O(1) via an internal hash index instead of a linear scan, making repeated adds of known items dramatically faster (roughly 30x in benchmarks with thousands of distinct items)
|
|
6
|
+
- `Cathy#pick` uses an iterative tree walk and no longer allocates temporary arrays when memoizing child lookups
|
|
7
|
+
- Minimum supported Ruby version is now 3.1
|
|
8
|
+
- Items are now identified by hash semantics (`eql?`/`hash`), consistent with the existing behavior of `Cathy#counts`
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- GitHub Actions CI running the test suite across supported Ruby versions and Standard linting
|
|
13
|
+
- Expanded test coverage: empty distributions, single-item distributions, on-the-fly updates, non-symbol items (including `nil`), and continued adds after deserialization
|
|
14
|
+
- RBS type signatures for the public API
|
|
2
15
|
|
|
3
16
|
## [0.1.0] - 2022-10-01
|
|
4
17
|
|
data/Gemfile
CHANGED
data/README.md
CHANGED
|
@@ -69,7 +69,7 @@ This is basically a deserialization method.
|
|
|
69
69
|
|
|
70
70
|
## Contributing
|
|
71
71
|
|
|
72
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
|
72
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/dfhoughton/cathy. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/dfhoughton/cathy/blob/main/CODE_OF_CONDUCT.md).
|
|
73
73
|
|
|
74
74
|
## License
|
|
75
75
|
|
|
@@ -77,7 +77,7 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
|
77
77
|
|
|
78
78
|
## Code of Conduct
|
|
79
79
|
|
|
80
|
-
Everyone interacting in the Cathy project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/
|
|
80
|
+
Everyone interacting in the Cathy project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/dfhoughton/cathy/blob/main/CODE_OF_CONDUCT.md).
|
|
81
81
|
|
|
82
82
|
## Efficiency
|
|
83
83
|
|
data/cathy.gemspec
CHANGED
|
@@ -18,11 +18,12 @@ Gem::Specification.new do |spec|
|
|
|
18
18
|
DESCRIPTION
|
|
19
19
|
spec.homepage = "https://github.com/dfhoughton/cathy"
|
|
20
20
|
spec.license = "MIT"
|
|
21
|
-
spec.required_ruby_version = ">=
|
|
21
|
+
spec.required_ruby_version = ">= 3.1.0"
|
|
22
22
|
|
|
23
23
|
spec.metadata["homepage_uri"] = spec.homepage
|
|
24
24
|
spec.metadata["source_code_uri"] = "https://github.com/dfhoughton/cathy"
|
|
25
25
|
spec.metadata["changelog_uri"] = "https://github.com/dfhoughton/cathy/blob/main/CHANGELOG.md"
|
|
26
|
+
spec.metadata["bug_tracker_uri"] = "https://github.com/dfhoughton/cathy/issues"
|
|
26
27
|
|
|
27
28
|
# Specify which files should be added to the gem when it is released.
|
|
28
29
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
@@ -31,8 +32,6 @@ Gem::Specification.new do |spec|
|
|
|
31
32
|
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
|
|
32
33
|
end
|
|
33
34
|
end
|
|
34
|
-
spec.bindir = "exe"
|
|
35
|
-
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
|
36
35
|
spec.require_paths = ["lib"]
|
|
37
36
|
|
|
38
37
|
# Uncomment to register a new dependency of your gem
|
data/examples/chatty_cathy.rb
CHANGED
|
@@ -7,7 +7,7 @@ require "cathy"
|
|
|
7
7
|
class ChattyCathy
|
|
8
8
|
# the easy way to begin a conversation
|
|
9
9
|
def self.chat!
|
|
10
|
-
new.chat
|
|
10
|
+
new.send(:chat)
|
|
11
11
|
end
|
|
12
12
|
|
|
13
13
|
private
|
|
@@ -81,3 +81,5 @@ class ChattyCathy
|
|
|
81
81
|
puts "Cathy: It's been good chatting with you!"
|
|
82
82
|
end
|
|
83
83
|
end
|
|
84
|
+
|
|
85
|
+
ChattyCathy.chat! if __FILE__ == $PROGRAM_NAME
|
data/lib/cathy/version.rb
CHANGED
data/lib/cathy.rb
CHANGED
|
@@ -7,11 +7,11 @@ require_relative "cathy/version"
|
|
|
7
7
|
# added to it. Then you can pick one of the things added to it with a probability proportional to the number
|
|
8
8
|
# of times it has been added.
|
|
9
9
|
class Cathy
|
|
10
|
-
|
|
11
10
|
##
|
|
12
11
|
# Create a new distribution. In its initial state, it will always pick nil.
|
|
13
12
|
def initialize
|
|
14
13
|
@heap = []
|
|
14
|
+
@items = {} # item => index in @heap, so adds of known items are O(1) to locate
|
|
15
15
|
@total = 0
|
|
16
16
|
end
|
|
17
17
|
|
|
@@ -20,15 +20,14 @@ class Cathy
|
|
|
20
20
|
def pick
|
|
21
21
|
return if @heap.empty?
|
|
22
22
|
|
|
23
|
-
|
|
24
|
-
@heap[0].find(amount)
|
|
23
|
+
@heap[0].find(rand(0...@total))
|
|
25
24
|
end
|
|
26
25
|
|
|
27
26
|
##
|
|
28
27
|
# Increment the frequency of this item by one
|
|
29
28
|
def add(item)
|
|
30
29
|
@total += 1
|
|
31
|
-
if (i =
|
|
30
|
+
if (i = @items[item])
|
|
32
31
|
child = @heap[i]
|
|
33
32
|
child.increment!
|
|
34
33
|
loop do
|
|
@@ -48,8 +47,9 @@ class Cathy
|
|
|
48
47
|
else
|
|
49
48
|
# new items require minimal work
|
|
50
49
|
i = @heap.length
|
|
51
|
-
child = Item.new(item, @heap, i)
|
|
50
|
+
child = Item.new(item, @heap, @items, i)
|
|
52
51
|
@heap << child
|
|
52
|
+
@items[item] = i
|
|
53
53
|
child.clear_parents!
|
|
54
54
|
end
|
|
55
55
|
end
|
|
@@ -64,9 +64,11 @@ class Cathy
|
|
|
64
64
|
# Generates a distribution from a map from items to frequencies.
|
|
65
65
|
def self.from_counts(counts)
|
|
66
66
|
heap = []
|
|
67
|
+
items = {}
|
|
67
68
|
counts.each_with_index do |(item, count), i|
|
|
68
|
-
c = Item.new(item, heap, i, count)
|
|
69
|
+
c = Item.new(item, heap, items, i, count)
|
|
69
70
|
heap << c
|
|
71
|
+
items[item] = i
|
|
70
72
|
loop do
|
|
71
73
|
break if c.index.zero?
|
|
72
74
|
|
|
@@ -83,26 +85,18 @@ class Cathy
|
|
|
83
85
|
total = counts.values.sum
|
|
84
86
|
new.tap do |obj|
|
|
85
87
|
obj.instance_variable_set :@heap, heap
|
|
88
|
+
obj.instance_variable_set :@items, items
|
|
86
89
|
obj.instance_variable_set :@total, total
|
|
87
90
|
end
|
|
88
91
|
end
|
|
89
92
|
|
|
90
|
-
private
|
|
91
|
-
|
|
92
|
-
# because the heap tends to be sorted from most common to least, a linear
|
|
93
|
-
# search is reasonably efficient
|
|
94
|
-
#
|
|
95
|
-
# this returns the index of an item in the heap
|
|
96
|
-
def index(item)
|
|
97
|
-
@heap.index { |c| c.item == item }
|
|
98
|
-
end
|
|
99
|
-
|
|
100
93
|
# a container for things added
|
|
101
94
|
#
|
|
102
95
|
# items memoize useful counts
|
|
103
96
|
class Item
|
|
104
|
-
def initialize(item, heap, index, count = 1)
|
|
97
|
+
def initialize(item, heap, items, index, count = 1)
|
|
105
98
|
@heap = heap
|
|
99
|
+
@items = items
|
|
106
100
|
@index = index
|
|
107
101
|
@count = count
|
|
108
102
|
@item = item
|
|
@@ -110,12 +104,20 @@ class Cathy
|
|
|
110
104
|
|
|
111
105
|
# find an item in this subtree given an "amount"
|
|
112
106
|
def find(amount)
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
107
|
+
node = self
|
|
108
|
+
loop do
|
|
109
|
+
if amount < node.left_count
|
|
110
|
+
node = node.left
|
|
111
|
+
elsif amount < node.non_right
|
|
112
|
+
return node.item
|
|
113
|
+
else
|
|
114
|
+
amount -= node.non_right
|
|
115
|
+
if (right = node.right)
|
|
116
|
+
node = right
|
|
117
|
+
else
|
|
118
|
+
return node.item
|
|
119
|
+
end
|
|
120
|
+
end
|
|
119
121
|
end
|
|
120
122
|
end
|
|
121
123
|
|
|
@@ -126,11 +128,11 @@ class Cathy
|
|
|
126
128
|
end
|
|
127
129
|
|
|
128
130
|
def left
|
|
129
|
-
|
|
131
|
+
@left ||= heap[index * 2 + 1]
|
|
130
132
|
end
|
|
131
133
|
|
|
132
134
|
def right
|
|
133
|
-
|
|
135
|
+
@right ||= heap[index * 2 + 2]
|
|
134
136
|
end
|
|
135
137
|
|
|
136
138
|
def sum
|
|
@@ -153,6 +155,7 @@ class Cathy
|
|
|
153
155
|
def move!(new_index)
|
|
154
156
|
@index = new_index
|
|
155
157
|
heap[new_index] = self
|
|
158
|
+
@items[@item] = new_index
|
|
156
159
|
clear!
|
|
157
160
|
end
|
|
158
161
|
|
data/sig/cathy.rbs
CHANGED
|
@@ -1,4 +1,50 @@
|
|
|
1
|
-
|
|
1
|
+
class Cathy
|
|
2
2
|
VERSION: String
|
|
3
|
-
|
|
3
|
+
|
|
4
|
+
def initialize: () -> void
|
|
5
|
+
|
|
6
|
+
# Draw a sample from the distribution, or nil if the distribution is empty.
|
|
7
|
+
def pick: () -> untyped
|
|
8
|
+
|
|
9
|
+
# Increment the frequency of `item` by one.
|
|
10
|
+
def add: (untyped item) -> void
|
|
11
|
+
|
|
12
|
+
# A map from the items added to their frequencies.
|
|
13
|
+
def counts: () -> Hash[untyped, Integer]
|
|
14
|
+
|
|
15
|
+
# Build a distribution from a map from items to their frequencies.
|
|
16
|
+
def self.from_counts: (Hash[untyped, Integer] counts) -> Cathy
|
|
17
|
+
|
|
18
|
+
class Item
|
|
19
|
+
attr_reader item: untyped
|
|
20
|
+
attr_reader count: Integer
|
|
21
|
+
attr_reader heap: Array[Item]
|
|
22
|
+
attr_reader index: Integer
|
|
23
|
+
|
|
24
|
+
def initialize: (untyped item, Array[Item] heap, Hash[untyped, Integer] items, Integer index, ?Integer count) -> void
|
|
25
|
+
|
|
26
|
+
def find: (Integer amount) -> untyped
|
|
27
|
+
|
|
28
|
+
def parent: () -> Item?
|
|
29
|
+
|
|
30
|
+
def left: () -> Item?
|
|
31
|
+
|
|
32
|
+
def right: () -> Item?
|
|
33
|
+
|
|
34
|
+
def sum: () -> Integer
|
|
35
|
+
|
|
36
|
+
def left_count: () -> Integer
|
|
37
|
+
|
|
38
|
+
def right_count: () -> Integer
|
|
39
|
+
|
|
40
|
+
def non_right: () -> Integer
|
|
41
|
+
|
|
42
|
+
def move!: (Integer new_index) -> void
|
|
43
|
+
|
|
44
|
+
def increment!: () -> void
|
|
45
|
+
|
|
46
|
+
def clear!: () -> void
|
|
47
|
+
|
|
48
|
+
def clear_parents!: () -> void
|
|
49
|
+
end
|
|
4
50
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: cathy
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- David F. Houghton
|
|
8
|
-
|
|
9
|
-
bindir: exe
|
|
8
|
+
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 2026-11-28 00:00:00.000000000 Z
|
|
12
11
|
dependencies: []
|
|
13
12
|
description: Cathy lets you model an updatable weighted probability distribution which
|
|
14
13
|
you can draw from. For example, suppose you want to randomly pull cards from a deck
|
|
@@ -41,7 +40,7 @@ metadata:
|
|
|
41
40
|
homepage_uri: https://github.com/dfhoughton/cathy
|
|
42
41
|
source_code_uri: https://github.com/dfhoughton/cathy
|
|
43
42
|
changelog_uri: https://github.com/dfhoughton/cathy/blob/main/CHANGELOG.md
|
|
44
|
-
|
|
43
|
+
bug_tracker_uri: https://github.com/dfhoughton/cathy/issues
|
|
45
44
|
rdoc_options: []
|
|
46
45
|
require_paths:
|
|
47
46
|
- lib
|
|
@@ -49,15 +48,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
49
48
|
requirements:
|
|
50
49
|
- - ">="
|
|
51
50
|
- !ruby/object:Gem::Version
|
|
52
|
-
version:
|
|
51
|
+
version: 3.1.0
|
|
53
52
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
54
53
|
requirements:
|
|
55
54
|
- - ">="
|
|
56
55
|
- !ruby/object:Gem::Version
|
|
57
56
|
version: '0'
|
|
58
57
|
requirements: []
|
|
59
|
-
rubygems_version: 3.
|
|
60
|
-
signing_key:
|
|
58
|
+
rubygems_version: 3.6.2
|
|
61
59
|
specification_version: 4
|
|
62
60
|
summary: an updatable weighted probability distribution model
|
|
63
61
|
test_files: []
|