Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
mapping argument for .copy()
Browse files Browse the repository at this point in the history
  • Loading branch information
dkrenn committed Aug 28, 2015
1 parent d647d95 commit e64cbd8
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions src/sage/data_structures/mutable_poset.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ def eq(left, right):
__eq__ = eq


def _copy_all_linked_(self, memo, poset):
def _copy_all_linked_(self, memo, poset, mapping):
r"""
Helper function for :meth:`MutablePoset.copy`.
Expand All @@ -619,6 +619,8 @@ def _copy_all_linked_(self, memo, poset):
- ``poset`` -- the poset to which the newly created shells belongs.
- ``mapping`` -- a function which is applied on each of the elements.
OUTPUT:
A new shell.
Expand All @@ -629,7 +631,7 @@ def _copy_all_linked_(self, memo, poset):
sage: P = MP()
sage: Q = MP()
sage: memo = {}
sage: z = P.null._copy_all_linked_(memo, Q)
sage: z = P.null._copy_all_linked_(memo, Q, lambda e: e)
sage: z.poset is Q
True
sage: oo = z.successors().pop()
Expand All @@ -641,12 +643,13 @@ def _copy_all_linked_(self, memo, poset):
except KeyError:
pass

new = self.__class__(poset, self.element)
new = self.__class__(poset, mapping(self.element)
if self.element is not None else None)
memo[id(self)] = new

for reverse in (False, True):
for e in self.successors(reverse):
new.successors(reverse).add(e._copy_all_linked_(memo, poset))
new.successors(reverse).add(e._copy_all_linked_(memo, poset, mapping))

return new

Expand Down Expand Up @@ -1231,7 +1234,7 @@ def __init__(self, data=None, key=None, merge=None, can_merge=None):
if is_MutablePoset(data):
if key is not None:
raise TypeError('Cannot use key when data is a poset.')
self._copy_shells_(data)
self._copy_shells_(data, lambda e: e)

else:
self.clear()
Expand Down Expand Up @@ -1435,7 +1438,7 @@ def get_key(self, element):
return self._key_(element)


def _copy_shells_(self, other):
def _copy_shells_(self, other, mapping):
r"""
Helper function for copying shells.
Expand All @@ -1444,6 +1447,8 @@ def _copy_shells_(self, other):
- ``other`` -- the mutable poset from which the shells
should be copied this poset.
- ``mapping`` -- a function which is applied on each of the elements.
OUTPUT:
Nothing.
Expand All @@ -1461,7 +1466,7 @@ def _copy_shells_(self, other):
sage: P.add(T((4, 4)))
sage: P.add(T((1, 2)))
sage: Q = MP()
sage: Q._copy_shells_(P)
sage: Q._copy_shells_(P, lambda e: e)
sage: P.repr_full() == Q.repr_full()
True
"""
Expand All @@ -1470,20 +1475,20 @@ def _copy_shells_(self, other):
self._merge_ = copy(other._merge_)
self._can_merge_ = copy(other._can_merge_)
memo = {}
self._null_ = other._null_._copy_all_linked_(memo, self)
self._null_ = other._null_._copy_all_linked_(memo, self, mapping)
self._oo_ = memo[id(other._oo_)]
self._shells_ = dict((f.key, f) for f in
iter(memo[id(e)]
for e in other._shells_.itervalues()))


def copy(self):
def copy(self, mapping=None):
r"""
Creates a shallow copy.
INPUT:
Nothing.
- ``mapping`` -- a function which is applied on each of the elements.
OUTPUT:
Expand All @@ -1505,8 +1510,10 @@ def copy(self):
sage: P.repr_full() == Q.repr_full()
True
"""
if mapping is None:
mapping = lambda element: element
new = self.__class__()
new._copy_shells_(self)
new._copy_shells_(self, mapping)
return new


Expand Down

0 comments on commit e64cbd8

Please sign in to comment.