-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from bowen-xu/bug/83-distributor
add OpenNARS-style Distributor in Bag
- Loading branch information
Showing
7 changed files
with
58 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from functools import lru_cache | ||
|
||
""" | ||
A pseudo-random number generator, used in Bag | ||
""" | ||
class Distributor: | ||
@staticmethod | ||
@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 | ||
|
||
index = 0 | ||
for rank in range(range_val, 0, -1): | ||
for time in range(rank): | ||
index = ((self.capacity // rank) + index) % self.capacity | ||
while self.order[index] >= 0: | ||
index = (index + 1) % self.capacity | ||
self.order[index] = rank - 1 | ||
|
||
def pick(self, index): | ||
return self.order[index] | ||
|
||
def next(self, index): | ||
return (index + 1) % self.capacity |