Skip to content

Commit c3b961b

Browse files
committed
-
1 parent dc1dba6 commit c3b961b

File tree

2 files changed

+62
-11
lines changed

2 files changed

+62
-11
lines changed

source_py3/python_toolbox/combi/perming/perm_space.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -577,21 +577,20 @@ def __getitem__(self, i):
577577
assert not self.is_dapplied and not self.is_degreed and \
578578
not self.is_sliced
579579
available_values = list(self.sequence)
580-
reserved_values = list(self.fixed_map.values())
580+
reserved_values = nifty_collections.Tally(self.fixed_map.values())
581581
wip_perm_sequence_dict = dict(self.fixed_map)
582582
wip_i = i
583583
shit_set = set()
584584
for j in range(self.n_elements):
585585
if j in self.fixed_map:
586586
available_values.remove(self.fixed_map[j])
587-
reserved_values.remove(self.fixed_map[j])
587+
reserved_values[self.fixed_map[j]] -= 1
588588
continue
589-
unused_values = nifty_collections.OrderedSet((
590-
value for value in available_values if not
591-
((value in reserved_values and available_values.count(value)
592-
== reserved_values.count(value)) or value in shit_set)
593-
594-
))
589+
unused_values = [
590+
item for item in
591+
nifty_collections.OrderedTally(available_values) -
592+
reserved_values if item not in shit_set
593+
]
595594
for unused_value in unused_values:
596595
wip_perm_sequence_dict[j] = unused_value
597596

source_py3/python_toolbox/nifty_collections/tallying.py

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,56 @@ def __repr__(self):
295295
'[%s]' % ', '.join('%s' % (item,) for item in self.items())
296296
)
297297

298+
299+
class _OrderedDictDelegator(collections.MutableMapping):
300+
301+
# Start by filling-out the abstract methods
302+
def __init__(self, dict=None, **kwargs):
303+
self.data = OrderedDict()
304+
if dict is not None:
305+
self.update(dict)
306+
if len(kwargs):
307+
self.update(kwargs)
308+
def __len__(self): return len(self.data)
309+
def __getitem__(self, key):
310+
if key in self.data:
311+
return self.data[key]
312+
if hasattr(self.__class__, "__missing__"):
313+
return self.__class__.__missing__(self, key)
314+
raise KeyError(key)
315+
def __setitem__(self, key, item): self.data[key] = item
316+
def __delitem__(self, key): del self.data[key]
317+
def __iter__(self):
318+
return iter(self.data)
319+
320+
# Modify __contains__ to work correctly when __missing__ is present
321+
def __contains__(self, key):
322+
return key in self.data
323+
324+
# Now, add the methods in dicts but not in MutableMapping
325+
def __repr__(self): return repr(self.data)
326+
def copy(self):
327+
if self.__class__ is _OrderedDictDelegator:
328+
return _OrderedDictDelegator(self.data.copy())
329+
import copy
330+
data = self.data
331+
try:
332+
self.data = OrderedDict
333+
c = copy.copy(self)
334+
finally:
335+
self.data = data
336+
c.update(self)
337+
return c
338+
@classmethod
339+
def fromkeys(cls, iterable, value=None):
340+
d = cls()
341+
for key in iterable:
342+
d[key] = value
343+
return d
344+
345+
298346

299-
class Tally(_TallyMixin, FrozenDict):
347+
class Tally(_TallyMixin, collections.UserDict):
300348
'''
301349
blocktododoc
302350
An immutable tally.
@@ -315,8 +363,10 @@ class that has an identity crisis and doesn't know whether it's a
315363
counter or a dict.
316364
317365
'''
366+
_dict = property(lambda self: self.data)
367+
318368

319-
class OrderedTally(_TallyMixin, FrozenDict):
369+
class OrderedTally(_OrderedTallyMixin, _TallyMixin, _OrderedDictDelegator):
320370
'''
321371
blocktododoc
322372
An immutable, ordered tally.
@@ -336,7 +386,9 @@ class that has an identity crisis and doesn't know whether it's a
336386
337387
- It has an order to its elements, like `collections.OrderedDict`.
338388
339-
'''
389+
'''
390+
_dict = property(lambda self: self.data)
391+
340392

341393
class FrozenTally(_BaseTallyMixin, FrozenDict):
342394
'''

0 commit comments

Comments
 (0)