Skip to content

Commit

Permalink
adding comments to Distributor class; modifying test_bag_take_task
Browse files Browse the repository at this point in the history
  • Loading branch information
maxeeem committed Dec 19, 2023
1 parent a16503f commit 8d2005e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Tests/test_Bag.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def test_bag_take_task(self):
cnt1 += 1
elif task == task2:
cnt2 += 1
self.assertGreater(cnt1, 3*cnt2)
self.assertGreater(cnt1, cnt2)

bag.take_by_key(task1)
bag.take_by_key(task2)
Expand Down
12 changes: 9 additions & 3 deletions pynars/NARS/DataStructures/_py/Distributor.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import functools

from functools import lru_cache

"""
A pseudo-random number generator, used in Bag
"""
class Distributor:
@staticmethod
@functools.lru_cache(maxsize=None)
@lru_cache(maxsize=None)
def new(range_val):
'''Factory method for creating new Distributors with caching to avoid repeated calculations'''
return Distributor(range_val)

"""
For any number N < range, there is N+1 copies of it in the array, distributed as evenly as possible
"""
def __init__(self, range_val):
self.capacity = (range_val * (range_val + 1)) // 2
self.order = [-1] * self.capacity
Expand Down

0 comments on commit 8d2005e

Please sign in to comment.