Path: blob/master/src/sage/structure/list_clone_demo.pyx
8814 views
r"""1Elements, Array and Lists With Clone Protocol, demonstration classes23This module demonstrate the usage of the various classes defined in4:mod:`~sage.structure.list_clone`5"""67#*****************************************************************************8# Copyright (C) 2011 Florent Hivert <[email protected]>9#10# Distributed under the terms of the GNU General Public License (GPL)11# http://www.gnu.org/licenses/12#*****************************************************************************1314from sage.categories.sets_cat import Sets15from sage.structure.unique_representation import UniqueRepresentation16from sage.structure.list_clone cimport (17ClonableArray, ClonableList, NormalizedClonableList, ClonableIntArray )18from sage.structure.parent import Parent1920cdef class IncreasingArray(ClonableArray):21"""22A small extension class for testing23:class:`~sage.structure.list_clone.ClonableArray`.2425TESTS::2627sage: from sage.structure.list_clone_demo import IncreasingArrays28sage: TestSuite(IncreasingArrays()([1,2,3])).run()29sage: TestSuite(IncreasingArrays()([])).run()30"""3132cpdef check(self):33"""34Check that ``self`` is increasing.3536EXAMPLES::3738sage: from sage.structure.list_clone_demo import IncreasingArrays39sage: IncreasingArrays()([1,2,3]) # indirect doctest40[1, 2, 3]41sage: IncreasingArrays()([3,2,1]) # indirect doctest42Traceback (most recent call last):43...44ValueError: array is not increasing45"""46cdef int i47for i in range(len(self)-1):48if self._getitem(i) > self._getitem(i+1):49raise ValueError, "array is not increasing"505152class IncreasingArrays(UniqueRepresentation, Parent):53"""54A small (incomplete) parent for testing55:class:`~sage.structure.list_clone.ClonableArray`5657TESTS::5859sage: from sage.structure.list_clone_demo import IncreasingArrays60sage: IncreasingArrays().element_class61<type 'sage.structure.list_clone_demo.IncreasingArray'>62"""6364def __init__(self):65"""66TESTS::6768sage: from sage.structure.list_clone_demo import IncreasingArrays69sage: IncreasingArrays()70<class 'sage.structure.list_clone_demo.IncreasingArrays_with_category'>71sage: IncreasingArrays() == IncreasingArrays()72True73"""74Parent.__init__(self, category = Sets())7576def _element_constructor_(self, *args, **keywords):77"""78TESTS::7980sage: from sage.structure.list_clone_demo import IncreasingArrays81sage: IncreasingArrays()([1]) # indirect doctest82[1]83"""84return self.element_class(self, *args, **keywords)8586Element = IncreasingArray87888990class IncreasingLists(IncreasingArrays):91"""92A small (incomplete) parent for testing93:class:`~sage.structure.list_clone.ClonableList`9495TESTS::9697sage: from sage.structure.list_clone_demo import IncreasingLists98sage: IncreasingLists().element_class99<type 'sage.structure.list_clone_demo.IncreasingList'>100"""101Element = IncreasingList102103cdef class IncreasingList(ClonableList):104"""105A small extension class for testing106:class:`~sage.structure.list_clone.ClonableList`107108TESTS::109110sage: from sage.structure.list_clone_demo import IncreasingLists111sage: TestSuite(IncreasingLists()([1,2,3])).run()112sage: TestSuite(IncreasingLists()([])).run()113"""114115cpdef check(self):116"""117Check that ``self`` is increasing118119EXAMPLES::120121sage: from sage.structure.list_clone_demo import IncreasingLists122sage: IncreasingLists()([1,2,3]) # indirect doctest123[1, 2, 3]124sage: IncreasingLists()([3,2,1]) # indirect doctest125Traceback (most recent call last):126...127ValueError: array is not increasing128"""129cdef int i130for i in range(len(self)-1):131if self._getitem(i) >= self._getitem(i+1):132raise ValueError , "array is not increasing"133134135136cdef class IncreasingIntArray(ClonableIntArray):137"""138A small extension class for testing139:class:`~sage.structure.list_clone.ClonableIntArray`.140141TESTS::142143sage: from sage.structure.list_clone_demo import IncreasingIntArrays144sage: TestSuite(IncreasingIntArrays()([1,2,3])).run()145sage: TestSuite(IncreasingIntArrays()([])).run()146"""147148cpdef check(self):149"""150Check that ``self`` is increasing.151152EXAMPLES::153154sage: from sage.structure.list_clone_demo import IncreasingIntArrays155sage: IncreasingIntArrays()([1,2,3]) # indirect doctest156[1, 2, 3]157sage: IncreasingIntArrays()([3,2,1]) # indirect doctest158Traceback (most recent call last):159...160ValueError: array is not increasing161"""162cdef int i163if not self:164return165for i in range(len(self)-1):166if self._getitem(i) >= self._getitem(i+1):167raise ValueError, "array is not increasing"168169class IncreasingIntArrays(IncreasingArrays):170"""171A small (incomplete) parent for testing172:class:`~sage.structure.list_clone.ClonableIntArray`173174TESTS::175176sage: from sage.structure.list_clone_demo import IncreasingIntArrays177sage: IncreasingIntArrays().element_class178<type 'sage.structure.list_clone_demo.IncreasingIntArray'>179"""180Element = IncreasingIntArray181182183184cdef class SortedList(NormalizedClonableList):185"""186A small extension class for testing187:class:`~sage.structure.list_clone.NormalizedClonableList`.188189TESTS::190191sage: from sage.structure.list_clone_demo import IncreasingIntArrays192sage: TestSuite(IncreasingIntArrays()([1,2,3])).run()193sage: TestSuite(IncreasingIntArrays()([])).run()194"""195cpdef normalize(self):196"""197Normalize ``self``198199Sort the list stored in ``self``.200201EXAMPLES::202203sage: from sage.structure.list_clone_demo import SortedList, SortedLists204sage: l = SortedList(SortedLists(), [3,1,2], False, False)205sage: l # indirect doctest206[1, 2, 3]207sage: l[1] = 5; l208[1, 5, 3]209sage: l.normalize(); l210[1, 3, 5]211"""212self._require_mutable()213self._get_list().sort()214215cpdef check(self):216"""217Check that ``self`` is strictly increasing218219EXAMPLES::220221sage: from sage.structure.list_clone_demo import SortedList, SortedLists222sage: SortedLists()([1,2,3]) # indirect doctest223[1, 2, 3]224sage: SortedLists()([3,2,2]) # indirect doctest225Traceback (most recent call last):226...227ValueError: list is not strictly increasing228"""229for i in range(len(self)-1):230if self._getitem(i) >= self._getitem(i+1):231raise ValueError, "list is not strictly increasing"232233class SortedLists(IncreasingLists):234"""235A small (incomplete) parent for testing236:class:`~sage.structure.list_clone.NormalizedClonableList`237238TESTS::239240sage: from sage.structure.list_clone_demo import SortedList, SortedLists241sage: SL = SortedLists()242sage: SL([3,1,2])243[1, 2, 3]244"""245Element = SortedList246247248