Ticket #11407: trac_11407-list_clone_improve-fh.patch
| File trac_11407-list_clone_improve-fh.patch, 50.5 KB (added by hivert, 14 months ago) |
|---|
-
doc/en/reference/structure.rst
# HG changeset patch # User Florent Hivert <Florent.Hivert@univ-rouen.fr> # Date 1332431268 -3600 # Node ID 2a373127be0b76d2b15df8953698233920c549cb # Parent e4143acca6f448dfdc2e519730f6f04328044b35 #11407: Add normalization to clonable lists diff --git a/doc/en/reference/structure.rst b/doc/en/reference/structure.rst
a b Basic Structures 13 13 sage/structure/unique_representation 14 14 sage/structure/dynamic_class 15 15 sage/structure/list_clone 16 sage/structure/list_clone_demo 16 17 sage/structure/mutability 17 18 sage/structure/sequence 18 19 sage/structure/element_wrapper -
module_list.py
diff --git a/module_list.py b/module_list.py
a b ext_modules = [ 189 189 190 190 Extension('sage.structure.list_clone', 191 191 sources=['sage/structure/list_clone.pyx']), 192 Extension('sage.structure.list_clone_demo', 193 sources=['sage/structure/list_clone_demo.pyx']), 192 194 Extension('sage.structure.list_clone_timings_cy', 193 195 sources=['sage/structure/list_clone_timings_cy.pyx']), 194 196 -
sage/structure/all.py
diff --git a/sage/structure/all.py b/sage/structure/all.py
a b from formal_sum import FormalSums, Form 49 49 from mutability import Mutability 50 50 51 51 from element_wrapper import ElementWrapper 52 53 from list_clone import (ClonableElement, ClonableArray, ClonableIntArray) -
sage/structure/list_clone.pxd
diff --git a/sage/structure/list_clone.pxd b/sage/structure/list_clone.pxd
a b cdef class ClonableList(ClonableArray): 44 44 cpdef pop(self, int index=*) 45 45 cpdef remove(self, el) 46 46 47 cdef class NormalizedClonableList(ClonableList): 48 cpdef inline bint __exit__(self, typ, value, tracback) except -2 49 cpdef normalize(self) 50 47 51 cdef class ClonableIntArray(ClonableElement): 48 52 cdef int _len 49 53 cdef int* _list -
sage/structure/list_clone.pyx
diff --git a/sage/structure/list_clone.pyx b/sage/structure/list_clone.pyx
a b 1 """1 r""" 2 2 Elements, Array and Lists With Clone Protocol 3 3 4 4 This module defines several classes which are subclasses of … … and its subclasses: 27 27 28 28 - :class:`ClonableArray` for arrays (lists of fixed length) of objects; 29 29 - :class:`ClonableList` for (resizable) lists of objects; 30 - :class:`NormalizedClonableList` for lists of objects with a normalization method; 30 31 - :class:`ClonableIntArray` for arrays of int. 31 32 32 The following parents demonstrate how to use them: 33 .. seealso:: The following parents from :mod:`sage.structure.list_clone_demo` 34 demonstrate how to use them: 33 35 34 - ``IncreasingArrays()`` (see :class:`IncreasingArray` and the parent class 35 :class:`IncreasingArrays`) 36 - ``IncreasingLists()`` (see :class:`IncreasingList` and the parent class 37 :class:`IncreasingLists`) 38 - ``IncreasingIntArrays()`` (see :class:`IncreasingIntArray` and the parent class 39 :class:`IncreasingIntArrays`) 36 - ``IncreasingArrays()`` (see 37 :class:`~sage.structure.list_clone_demo.IncreasingArray` 38 and the parent class 39 :class:`~sage.structure.list_clone_demo.IncreasingArrays`) 40 - ``IncreasingLists()`` (see 41 :class:`~sage.structure.list_clone_demo.IncreasingList` 42 and the parent class 43 :class:`~sage.structure.list_clone_demo.IncreasingLists`) 44 - ``SortedLists()`` (see 45 :class:`~sage.structure.list_clone_demo.SortedList` 46 and the parent class 47 :class:`~sage.structure.list_clone_demo.SortedLists`) 48 - ``IncreasingIntArrays()`` (see 49 :class:`~sage.structure.list_clone_demo.IncreasingIntArray` 50 and the parent class 51 :class:`~sage.structure.list_clone_demo.IncreasingIntArrays`) 40 52 41 53 EXAMPLES: 42 54 43 We now demonstrate how :class:`IncreasingArray` works, creating an instance 44 ``el`` through its parent ``IncreasingArrays()``:: 55 We now demonstrate how 56 :class:`~sage.structure.list_clone_demo.IncreasingArray` works, creating an 57 instance ``el`` through its parent ``IncreasingArrays()``:: 45 58 46 sage: from sage.structure.list_clone import IncreasingArrays59 sage: from sage.structure.list_clone_demo import IncreasingArrays 47 60 sage: P = IncreasingArrays() 48 61 sage: P([1, 4 ,8]) 49 62 [1, 4, 8] … … from sage.structure.parent cimport Paren 143 156 ### Basic clone elements ### 144 157 ############################################################################ 145 158 cdef class ClonableElement(Element): 146 """159 r""" 147 160 Abstract class for elements with clone protocol 148 161 149 162 This class is a subclass of :class:`Element<sage.structure.element.Element>` … … cdef class ClonableElement(Element): 203 216 :class:`ClonableElement`. We implement a class or pairs `(x,y)` 204 217 such that `x < y`:: 205 218 219 sage: from sage.structure.list_clone import ClonableElement 206 220 sage: class IntPair(ClonableElement): 207 221 ... def __init__(self, parent, x, y): 208 222 ... ClonableElement.__init__(self, parent=parent) … … cdef class ClonableElement(Element): 267 281 """ 268 282 TESTS:: 269 283 270 sage: from sage.structure.list_clone import IncreasingArrays284 sage: from sage.structure.list_clone_demo import IncreasingArrays 271 285 sage: el = IncreasingArrays()([1,2,3]) # indirect doctest 272 286 sage: el.is_immutable() 273 287 True … … cdef class ClonableElement(Element): 284 298 285 299 TESTS:: 286 300 287 sage: from sage.structure.list_clone import IncreasingArrays301 sage: from sage.structure.list_clone_demo import IncreasingArrays 288 302 sage: el = IncreasingArrays()([1,2,3]) 289 303 sage: el._require_mutable() 290 304 Traceback (most recent call last): … … cdef class ClonableElement(Element): 303 317 304 318 EXAMPLES:: 305 319 306 sage: from sage.structure.list_clone import IncreasingArrays320 sage: from sage.structure.list_clone_demo import IncreasingArrays 307 321 sage: el = IncreasingArrays()([1,2,3]) 308 322 sage: el.is_mutable() 309 323 False … … cdef class ClonableElement(Element): 324 338 325 339 EXAMPLES:: 326 340 327 sage: from sage.structure.list_clone import IncreasingArrays341 sage: from sage.structure.list_clone_demo import IncreasingArrays 328 342 sage: el = IncreasingArrays()([1,2,3]) 329 343 sage: el.is_immutable() 330 344 True … … cdef class ClonableElement(Element): 342 356 343 357 EXAMPLES:: 344 358 345 sage: from sage.structure.list_clone import IncreasingArrays359 sage: from sage.structure.list_clone_demo import IncreasingArrays 346 360 sage: el = IncreasingArrays()([1,2,3]) 347 361 sage: el1 = copy(el); el1.is_mutable() 348 362 True … … cdef class ClonableElement(Element): 359 373 """ 360 374 Makes ``self`` mutable, so it can be changed. 361 375 362 This function is for debugging only, you are not supposed to use it. 376 .. warning:: for internal use only. Casual users should make a copy 377 using either the :meth:`__copy__` method or the :meth:`clone` 378 protocol. Use only if you really know what you are doing. You 379 should in particular make sure that you are the only owner of 380 your object. 363 381 364 382 TESTS:: 365 383 366 sage: from sage.structure.list_clone import IncreasingArrays384 sage: from sage.structure.list_clone_demo import IncreasingArrays 367 385 sage: el = IncreasingArrays()([1,2,3]) 368 386 sage: el._set_mutable(); el.is_mutable() 369 387 True 388 sage: hash(el) 389 Traceback (most recent call last): 390 ... 391 ValueError: cannot hash a mutable object. 370 392 """ 393 self._hash = 0 371 394 self._is_immutable = False 372 395 373 396 def __hash__(self): … … cdef class ClonableElement(Element): 376 399 377 400 TESTS:: 378 401 379 sage: from sage.structure.list_clone import IncreasingArrays402 sage: from sage.structure.list_clone_demo import IncreasingArrays 380 403 sage: el = IncreasingArrays()([1,2,3]) 381 404 sage: hash(el) # random 382 405 -309690657 … … cdef class ClonableElement(Element): 403 426 404 427 EXAMPLES:: 405 428 406 sage: from sage.structure.list_clone import IncreasingArrays429 sage: from sage.structure.list_clone_demo import IncreasingArrays 407 430 sage: el = IncreasingArrays()([1,2,3]) 408 431 sage: with el.clone() as el1: 409 432 ... el1[2] = 5 … … cdef class ClonableElement(Element): 421 444 422 445 TESTS:: 423 446 424 sage: from sage.structure.list_clone import IncreasingArrays447 sage: from sage.structure.list_clone_demo import IncreasingArrays 425 448 sage: el = IncreasingArrays()([1,2,3]) 426 449 sage: el.clone().__enter__() 427 450 [1, 2, 3] … … cdef class ClonableElement(Element): 438 461 439 462 TESTS:: 440 463 441 sage: from sage.structure.list_clone import IncreasingArrays464 sage: from sage.structure.list_clone_demo import IncreasingArrays 442 465 sage: el = IncreasingArrays()([1,2,3]) 443 466 sage: el1 = el.clone().__enter__() 444 467 sage: el1.__exit__(None, None, None) … … cdef class ClonableArray(ClonableElement 471 494 :class:`Element<sage.structure.element.Element>` behave as arrays 472 495 (i.e. lists of fixed length) and implement the clone protocol. See 473 496 :class:`ClonableElement` for details about clone protocol. 497 498 INPUT: 499 500 - ``parent`` -- a :class:`Parent<sage.structure.parent.Parent>` 501 - ``lst`` -- a list 502 - ``check`` -- a boolean specifying if the invariant must be checked 503 using method :meth:`check`. 504 - ``immutable`` -- a boolean telling wether the created element is 505 immutable (defaults to ``True``) 506 507 .. seealso:: :class:`~sage.structure.list_clone_demo.IncreasingArray` for 508 an example of usage. 509 510 EXAMPLES:: 511 512 sage: from sage.structure.list_clone_demo import IncreasingArrays 513 sage: IA = IncreasingArrays() 514 sage: ia1 = IA([1, 4, 6]); ia1 515 [1, 4, 6] 516 sage: with ia1.clone() as ia2: 517 ... ia2[1] = 5 518 sage: ia2 519 [1, 5, 6] 520 sage: with ia1.clone() as ia2: 521 ... ia2[1] = 7 522 Traceback (most recent call last): 523 ... 524 AssertionError: array is not increasing 474 525 """ 475 def __init__(self, Parent parent, lst, check = True ):526 def __init__(self, Parent parent, lst, check = True, immutable = True): 476 527 """ 477 528 Initialize ``self`` 478 529 479 INPUT:480 481 - ``parent`` -- a :class:`Parent<sage.structure.parent.Parent>`482 - ``lst`` -- a list483 - ``check`` -- a boolean specifying if the invariant must be checked484 using method :meth:`check`.485 486 530 TESTS:: 487 531 488 sage: from sage.structure.list_clone import IncreasingArrays532 sage: from sage.structure.list_clone_demo import IncreasingArrays 489 533 sage: IncreasingArrays()([1,2,3]) 490 534 [1, 2, 3] 491 535 … … cdef class ClonableArray(ClonableElement 506 550 """ 507 551 self._parent = parent 508 552 self._list = list(lst) 509 self._is_immutable = True553 self._is_immutable = immutable 510 554 if check: 511 555 self.check() 512 556 … … cdef class ClonableArray(ClonableElement 514 558 """ 515 559 TESTS:: 516 560 517 sage: from sage.structure.list_clone import IncreasingArrays561 sage: from sage.structure.list_clone_demo import IncreasingArrays 518 562 sage: IncreasingArrays()([1,2,3]) 519 563 [1, 2, 3] 520 564 """ … … cdef class ClonableArray(ClonableElement 526 570 527 571 EXAMPLES:: 528 572 529 sage: from sage.structure.list_clone import IncreasingArrays573 sage: from sage.structure.list_clone_demo import IncreasingArrays 530 574 sage: IncreasingArrays()([1,2,3]).__nonzero__() 531 575 True 532 576 sage: IncreasingArrays()([]).__nonzero__() … … cdef class ClonableArray(ClonableElement 538 582 """ 539 583 Returns the list embedded in ``self``. 540 584 541 Don't use ! For internal purpose only. 585 .. warning:: No copy is performed. As a consequence, modifying the 586 returned list is not allowed. 542 587 543 588 TESTS:: 544 589 545 sage: from sage.structure.list_clone import IncreasingArrays590 sage: from sage.structure.list_clone_demo import IncreasingArrays 546 591 sage: el = IncreasingArrays()([1,2,3]) 547 592 sage: el._get_list() 548 593 [1, 2, 3] … … cdef class ClonableArray(ClonableElement 553 598 """ 554 599 Set the list embedded in ``self``. 555 600 556 Don't use ! For internal purpose only. 601 .. warning:: No copy is performed. Modifying the list after calling 602 ``_set_list`` on it is not allowed. 557 603 558 604 TESTS:: 559 605 560 sage: from sage.structure.list_clone import IncreasingArrays606 sage: from sage.structure.list_clone_demo import IncreasingArrays 561 607 sage: el = IncreasingArrays()([1,2,3]) 562 608 sage: el._set_list([1,4,5]) 563 609 sage: el … … cdef class ClonableArray(ClonableElement 571 617 572 618 EXAMPLES:: 573 619 574 sage: from sage.structure.list_clone import IncreasingArrays620 sage: from sage.structure.list_clone_demo import IncreasingArrays 575 621 sage: len(IncreasingArrays()([1,2,3])) 576 622 3 577 623 """ … … cdef class ClonableArray(ClonableElement 585 631 586 632 EXAMPLES:: 587 633 588 sage: from sage.structure.list_clone import IncreasingArrays634 sage: from sage.structure.list_clone_demo import IncreasingArrays 589 635 sage: IncreasingArrays()([1,2,3])[1] 590 636 2 591 637 sage: IncreasingArrays()([1,2,3])[7] … … cdef class ClonableArray(ClonableElement 613 659 614 660 EXAMPLES:: 615 661 616 sage: from sage.structure.list_clone import IncreasingArrays662 sage: from sage.structure.list_clone_demo import IncreasingArrays 617 663 sage: el = IncreasingArrays()([1,2,4,10]) 618 664 sage: elc = copy(el) 619 665 sage: elc[1] = 3; elc … … cdef class ClonableArray(ClonableElement 639 685 640 686 EXAMPLES:: 641 687 642 sage: from sage.structure.list_clone import IncreasingArrays688 sage: from sage.structure.list_clone_demo import IncreasingArrays 643 689 sage: IncreasingArrays()([1,2,3])._getitem(1) 644 690 2 645 691 sage: IncreasingArrays()([1,2,3])._getitem(5) … … cdef class ClonableArray(ClonableElement 658 704 659 705 EXAMPLES:: 660 706 661 sage: from sage.structure.list_clone import IncreasingArrays707 sage: from sage.structure.list_clone_demo import IncreasingArrays 662 708 sage: el = IncreasingArrays()([1,2,4]) 663 709 sage: elc = copy(el) 664 710 sage: elc._setitem(1, 3); elc … … cdef class ClonableArray(ClonableElement 681 727 682 728 EXAMPLES:: 683 729 684 sage: from sage.structure.list_clone import IncreasingArrays730 sage: from sage.structure.list_clone_demo import IncreasingArrays 685 731 sage: el = IncreasingArrays()([1,2,4]) 686 732 sage: list(iter(el)) 687 733 [1, 2, 4] … … cdef class ClonableArray(ClonableElement 703 749 """ 704 750 EXAMPLES:: 705 751 706 sage: from sage.structure.list_clone import IncreasingArrays752 sage: from sage.structure.list_clone_demo import IncreasingArrays 707 753 sage: c = IncreasingArrays()([1,2,4]) 708 754 sage: 1 in c 709 755 True … … cdef class ClonableArray(ClonableElement 718 764 719 765 EXAMPLES:: 720 766 721 sage: from sage.structure.list_clone import IncreasingArrays767 sage: from sage.structure.list_clone_demo import IncreasingArrays 722 768 sage: c = IncreasingArrays()([1,2,4]) 723 769 sage: c.index(1) 724 770 0 … … cdef class ClonableArray(ClonableElement 742 788 743 789 EXAMPLES:: 744 790 745 sage: from sage.structure.list_clone import IncreasingArrays791 sage: from sage.structure.list_clone_demo import IncreasingArrays 746 792 sage: c = IncreasingArrays()([1,2,2,4]) 747 793 sage: c.count(1) 748 794 1 … … cdef class ClonableArray(ClonableElement 761 807 762 808 TESTS:: 763 809 764 sage: from sage.structure.list_clone import IncreasingArrays810 sage: from sage.structure.list_clone_demo import IncreasingArrays 765 811 sage: el = IncreasingArrays()([1,2,3]) 766 812 sage: hash(el) # random 767 813 -309690657 … … cdef class ClonableArray(ClonableElement 781 827 """ 782 828 TESTS:: 783 829 784 sage: from sage.structure.list_clone import IncreasingArrays830 sage: from sage.structure.list_clone_demo import IncreasingArrays 785 831 sage: el = IncreasingArrays()([1,2,4]) 786 832 sage: elc = copy(el) 787 833 sage: elc == el # indirect doctest … … cdef class ClonableArray(ClonableElement 794 840 """ 795 841 TEST:: 796 842 797 sage: from sage.structure.list_clone import IncreasingArrays843 sage: from sage.structure.list_clone_demo import IncreasingArrays 798 844 sage: el1 = IncreasingArrays()([1,2,4]) 799 845 sage: el2 = IncreasingArrays()([1,2,3]) 800 846 sage: el1 == el1, el2 == el2, el1 == el2 # indirect doctest … … cdef class ClonableArray(ClonableElement 811 857 812 858 TESTS:: 813 859 814 sage: from sage.structure.list_clone import IncreasingArrays860 sage: from sage.structure.list_clone_demo import IncreasingArrays 815 861 sage: el = IncreasingArrays()([1,2,4]) 816 862 sage: elc = copy(el) 817 863 sage: el[:] == elc[:] … … cdef class ClonableArray(ClonableElement 858 904 859 905 EXAMPLES:: 860 906 907 sage: from sage.structure.list_clone import ClonableArray 861 908 sage: ClonableArray(Parent(), [1,2,3]) # indirect doctest 862 909 Traceback (most recent call last): 863 910 ... 864 911 AssertionError: This should never be called, please overload 865 sage: from sage.structure.list_clone import IncreasingArrays912 sage: from sage.structure.list_clone_demo import IncreasingArrays 866 913 sage: el = IncreasingArrays()([1,2,4]) # indirect doctest 867 914 """ 868 915 assert False, "This should never be called, please overload" … … cdef class ClonableArray(ClonableElement 873 920 874 921 TESTS:: 875 922 876 sage: from sage.structure.list_clone import IncreasingArrays923 sage: from sage.structure.list_clone_demo import IncreasingArrays 877 924 sage: el = IncreasingArrays()([1,2,3]) 878 925 sage: el._hash_() # random 879 926 -309711137 … … cdef class ClonableArray(ClonableElement 888 935 """ 889 936 TESTS:: 890 937 891 sage: from sage.structure.list_clone import IncreasingArrays938 sage: from sage.structure.list_clone_demo import IncreasingArrays 892 939 sage: el = IncreasingArrays()([1,2,4]) 893 940 sage: loads(dumps(el)) 894 941 [1, 2, 4] 895 942 sage: t = el.__reduce__(); t 896 (<built-in function _make_array_clone>, (<type 'sage.structure.list_clone .IncreasingArray'>, <class 'sage.structure.list_clone.IncreasingArrays_with_category'>, [1, 2, 4], True, True, None))943 (<built-in function _make_array_clone>, (<type 'sage.structure.list_clone_demo.IncreasingArray'>, <class 'sage.structure.list_clone_demo.IncreasingArrays_with_category'>, [1, 2, 4], True, True, None)) 897 944 sage: t[0](*t[1]) 898 945 [1, 2, 4] 899 946 """ … … def _make_array_clone(clas, parent, list 914 961 915 962 TESTS:: 916 963 917 sage: from sage.structure.list_clone import _make_array_clone, IncreasingArrays 964 sage: from sage.structure.list_clone import _make_array_clone 965 sage: from sage.structure.list_clone_demo import IncreasingArrays 918 966 sage: ILs = IncreasingArrays() 919 967 sage: el = _make_array_clone(ILs.element_class, ILs, [1,2,3], True, True, None) 920 968 sage: el … … def _make_array_clone(clas, parent, list 945 993 return res 946 994 947 995 948 949 #####################################################################950 ###### TESTS Classes ######951 #####################################################################952 ##### Cython version #####953 cdef class IncreasingArray(ClonableArray):954 """955 A small extension class for testing :class:`ClonableArray`.956 957 TESTS::958 959 sage: from sage.structure.list_clone import IncreasingArrays960 sage: TestSuite(IncreasingArrays()([1,2,3])).run()961 sage: TestSuite(IncreasingArrays()([])).run()962 """963 964 cpdef check(self):965 """966 Check that ``self`` is increasing.967 968 EXAMPLES::969 970 sage: from sage.structure.list_clone import IncreasingArrays971 sage: IncreasingArrays()([1,2,3]) # indirect doctest972 [1, 2, 3]973 sage: IncreasingArrays()([3,2,1]) # indirect doctest974 Traceback (most recent call last):975 ...976 AssertionError: array is not increasing977 """978 cdef int i979 for i in range(len(self)-1):980 assert self._getitem(i) <= self._getitem(i+1), "array is not increasing"981 982 983 ##### Parents #####984 from sage.categories.sets_cat import Sets985 from sage.structure.unique_representation import UniqueRepresentation986 class IncreasingArrays(UniqueRepresentation, Parent):987 """988 A small (incomplete) parent for testing :class:`ClonableArray`989 990 TESTS::991 992 sage: from sage.structure.list_clone import IncreasingArrays993 sage: IncreasingArrays().element_class994 <type 'sage.structure.list_clone.IncreasingArray'>995 """996 997 def __init__(self):998 """999 TESTS::1000 1001 sage: from sage.structure.list_clone import IncreasingArrays1002 sage: IncreasingArrays()1003 <class 'sage.structure.list_clone.IncreasingArrays_with_category'>1004 sage: IncreasingArrays() == IncreasingArrays()1005 True1006 """1007 Parent.__init__(self, category = Sets())1008 1009 def _element_constructor_(self, *args, **keywords):1010 """1011 TESTS::1012 1013 sage: from sage.structure.list_clone import IncreasingArrays1014 sage: IncreasingArrays()([1]) # indirect doctest1015 [1]1016 """1017 return self.element_class(self, *args, **keywords)1018 1019 Element = IncreasingArray1020 1021 1022 996 ############################################################################ 1023 997 ### Clonable (Resizable) Lists ### 1024 998 ############################################################################ … … cdef class ClonableList(ClonableArray): 1030 1004 :class:`Element<sage.structure.element.Element>` behave as lists and 1031 1005 implement the clone protocol. See :class:`ClonableElement` for details 1032 1006 about clone protocol. 1007 1008 .. seealso:: :class:`~sage.structure.list_clone_demo.IncreasingList` for 1009 an example of usage. 1033 1010 """ 1034 1011 cpdef append(self, el): 1035 1012 """ … … cdef class ClonableList(ClonableArray): 1039 1016 1040 1017 EXAMPLES:: 1041 1018 1042 sage: from sage.structure.list_clone import IncreasingLists1019 sage: from sage.structure.list_clone_demo import IncreasingLists 1043 1020 sage: el = IncreasingLists()([1]) 1044 1021 sage: el.append(3) 1045 1022 Traceback (most recent call last): … … cdef class ClonableList(ClonableArray): 1068 1045 1069 1046 EXAMPLES:: 1070 1047 1071 sage: from sage.structure.list_clone import IncreasingLists1048 sage: from sage.structure.list_clone_demo import IncreasingLists 1072 1049 sage: el = IncreasingLists()([1, 4, 5, 8, 9]) 1073 1050 sage: el.extend((10,11)) 1074 1051 Traceback (most recent call last): … … cdef class ClonableList(ClonableArray): 1106 1083 1107 1084 EXAMPLES:: 1108 1085 1109 sage: from sage.structure.list_clone import IncreasingLists1086 sage: from sage.structure.list_clone_demo import IncreasingLists 1110 1087 sage: el = IncreasingLists()([1, 4, 5, 8, 9]) 1111 1088 sage: el.insert(3, 6) 1112 1089 Traceback (most recent call last): … … cdef class ClonableList(ClonableArray): 1133 1110 1134 1111 EXAMPLES:: 1135 1112 1136 sage: from sage.structure.list_clone import IncreasingLists1113 sage: from sage.structure.list_clone_demo import IncreasingLists 1137 1114 sage: el = IncreasingLists()([1, 4, 5, 8, 9]) 1138 1115 sage: el.pop() 1139 1116 Traceback (most recent call last): … … cdef class ClonableList(ClonableArray): 1161 1138 1162 1139 EXAMPLES:: 1163 1140 1164 sage: from sage.structure.list_clone import IncreasingLists1141 sage: from sage.structure.list_clone_demo import IncreasingLists 1165 1142 sage: el = IncreasingLists()([1, 4, 5, 8, 9]) 1166 1143 sage: el.remove(4) 1167 1144 Traceback (most recent call last): … … cdef class ClonableList(ClonableArray): 1188 1165 1189 1166 EXAMPLES:: 1190 1167 1191 sage: from sage.structure.list_clone import IncreasingLists1168 sage: from sage.structure.list_clone_demo import IncreasingLists 1192 1169 sage: el = IncreasingLists()([1,2,4,10,15,17]) 1193 1170 sage: el[1] = 3 1194 1171 Traceback (most recent call last): … … cdef class ClonableList(ClonableArray): 1216 1193 1217 1194 EXAMPLES:: 1218 1195 1219 sage: from sage.structure.list_clone import IncreasingLists1196 sage: from sage.structure.list_clone_demo import IncreasingLists 1220 1197 sage: el = IncreasingLists()([1, 4, 5, 8, 9]) 1221 1198 sage: del el[3] 1222 1199 Traceback (most recent call last): … … cdef class ClonableList(ClonableArray): 1235 1212 del self._list[key] 1236 1213 1237 1214 1238 class IncreasingLists(IncreasingArrays):1239 """1240 A small (incomplete) parent for testing :class:`ClonableArray`1241 1242 TESTS::1243 1244 sage: from sage.structure.list_clone import IncreasingLists1245 sage: IncreasingLists().element_class1246 <type 'sage.structure.list_clone.IncreasingList'>1247 """1248 Element = IncreasingList1249 1250 cdef class IncreasingList(ClonableList):1251 """1252 A small extension class for testing :class:`ClonableList`1253 1254 TESTS::1255 1256 sage: from sage.structure.list_clone import IncreasingLists1257 sage: TestSuite(IncreasingLists()([1,2,3])).run()1258 sage: TestSuite(IncreasingLists()([])).run()1259 """1260 1261 cpdef check(self):1262 """1263 Check that ``self`` is increasing1264 1265 EXAMPLES::1266 1267 sage: from sage.structure.list_clone import IncreasingLists1268 sage: IncreasingLists()([1,2,3]) # indirect doctest1269 [1, 2, 3]1270 sage: IncreasingLists()([3,2,1]) # indirect doctest1271 Traceback (most recent call last):1272 ...1273 AssertionError: array is not increasing1274 """1275 cdef int i1276 for i in range(len(self)-1):1277 assert self._getitem(i) < self._getitem(i+1), "array is not increasing"1278 1279 1280 1281 1282 1215 ############################################################################ 1283 1216 ### Clonable Arrays of int ## 1284 1217 ############################################################################ … … cdef class ClonableIntArray(ClonableElem 1290 1223 :class:`Element<sage.structure.element.Element>` behave as list of int and 1291 1224 implement the clone protocol. See :class:`ClonableElement` for details 1292 1225 about clone protocol. 1226 1227 1228 INPUT: 1229 1230 - ``parent`` -- a :class:`Parent<sage.structure.parent.Parent>` 1231 - ``lst`` -- a list 1232 - ``check`` -- a boolean specifying if the invariant must be checked 1233 using method :meth:`check` 1234 - ``immutable`` -- a boolean telling wether the created element is 1235 immutable (defaults to ``True``) 1236 1237 .. seealso:: :class:`~sage.structure.list_clone_demo.IncreasingIntArray` 1238 for an example of usage. 1293 1239 """ 1294 1240 def __cinit__(self): 1295 1241 self._len = -1 1296 1242 self._list = NULL 1297 1243 1298 def __init__(self, Parent parent, lst, check = True ):1244 def __init__(self, Parent parent, lst, check = True, immutable = True): 1299 1245 """ 1300 1246 Initialize ``self`` 1301 1247 1302 INPUT:1303 1304 - ``parent`` -- a :class:`Parent<sage.structure.parent.Parent>`1305 - ``lst`` -- a list1306 - ``check`` -- a boolean specifying if the invariant must be checked1307 using method :meth:`check`.1308 1309 1248 TESTS:: 1310 1249 1311 sage: from sage.structure.list_clone import IncreasingIntArrays1250 sage: from sage.structure.list_clone_demo import IncreasingIntArrays 1312 1251 sage: IncreasingIntArrays()([1,2,3]) 1313 1252 [1, 2, 3] 1314 1253 sage: IncreasingIntArrays()((1,2,3)) … … cdef class ClonableIntArray(ClonableElem 1330 1269 sage: list(iter(IncreasingIntArrays()([]))) 1331 1270 [] 1332 1271 1333 1334 1272 You are not supposed to do the following (giving a wrong list and 1335 1273 desactivating checks):: 1336 1274 … … cdef class ClonableIntArray(ClonableElem 1345 1283 for i from 0 <= i < self._len: 1346 1284 self._list[i] = lst[i] 1347 1285 1348 self._is_immutable = True1286 self._is_immutable = immutable 1349 1287 if check: 1350 1288 self.check() 1351 1289 … … cdef class ClonableIntArray(ClonableElem 1359 1297 1360 1298 EXAMPLES:: 1361 1299 1362 sage: from sage.structure.list_clone import IncreasingIntArrays1300 sage: from sage.structure.list_clone_demo import IncreasingIntArrays 1363 1301 sage: el = IncreasingIntArrays()([], check=False) 1364 1302 sage: el._alloc_(3) 1365 1303 sage: el._setitem(0, 1); el._setitem(1, 5); el._setitem(2, 8) … … cdef class ClonableIntArray(ClonableElem 1394 1332 """ 1395 1333 TESTS:: 1396 1334 1397 sage: from sage.structure.list_clone import IncreasingIntArrays1335 sage: from sage.structure.list_clone_demo import IncreasingIntArrays 1398 1336 sage: IncreasingIntArrays()([1,2,3]) 1399 1337 [1, 2, 3] 1400 1338 """ … … cdef class ClonableIntArray(ClonableElem 1404 1342 """ 1405 1343 EXAMPLES:: 1406 1344 1407 sage: from sage.structure.list_clone import IncreasingIntArrays1345 sage: from sage.structure.list_clone_demo import IncreasingIntArrays 1408 1346 sage: IncreasingIntArrays()([1,2,3]).__nonzero__() 1409 1347 True 1410 1348 sage: IncreasingIntArrays()([]).__nonzero__() … … cdef class ClonableIntArray(ClonableElem 1418 1356 1419 1357 EXAMPLES:: 1420 1358 1421 sage: from sage.structure.list_clone import IncreasingIntArrays1359 sage: from sage.structure.list_clone_demo import IncreasingIntArrays 1422 1360 sage: len(IncreasingIntArrays()([1,2,3])) 1423 1361 3 1424 1362 """ … … cdef class ClonableIntArray(ClonableElem 1430 1368 1431 1369 EXAMPLE:: 1432 1370 1433 sage: from sage.structure.list_clone import IncreasingIntArrays1371 sage: from sage.structure.list_clone_demo import IncreasingIntArrays 1434 1372 sage: I = IncreasingIntArrays()(range(5)) 1435 1373 sage: I == range(5) 1436 1374 False … … cdef class ClonableIntArray(ClonableElem 1445 1383 1446 1384 EXAMPLE:: 1447 1385 1448 sage: from sage.structure.list_clone import IncreasingIntArrays1386 sage: from sage.structure.list_clone_demo import IncreasingIntArrays 1449 1387 sage: I = IncreasingIntArrays()(range(5)) 1450 1388 sage: I == range(5) 1451 1389 False … … cdef class ClonableIntArray(ClonableElem 1470 1408 1471 1409 EXAMPLES:: 1472 1410 1473 sage: from sage.structure.list_clone import IncreasingIntArrays1411 sage: from sage.structure.list_clone_demo import IncreasingIntArrays 1474 1412 sage: el = IncreasingIntArrays()([1,2,3]) 1475 1413 sage: el[1] 1476 1414 2 … … cdef class ClonableIntArray(ClonableElem 1523 1461 1524 1462 EXAMPLES:: 1525 1463 1526 sage: from sage.structure.list_clone import IncreasingIntArrays1464 sage: from sage.structure.list_clone_demo import IncreasingIntArrays 1527 1465 sage: el = IncreasingIntArrays()([1,2,4]) 1528 1466 sage: elc = copy(el) 1529 1467 sage: elc[1] = 3; elc … … cdef class ClonableIntArray(ClonableElem 1548 1486 1549 1487 EXAMPLES:: 1550 1488 1551 sage: from sage.structure.list_clone import IncreasingIntArrays1489 sage: from sage.structure.list_clone_demo import IncreasingIntArrays 1552 1490 sage: IncreasingIntArrays()([1,2,3])._getitem(1) 1553 1491 2 1554 1492 """ … … cdef class ClonableIntArray(ClonableElem 1566 1504 1567 1505 EXAMPLES:: 1568 1506 1569 sage: from sage.structure.list_clone import IncreasingIntArrays1507 sage: from sage.structure.list_clone_demo import IncreasingIntArrays 1570 1508 sage: el = IncreasingIntArrays()([1,2,4]) 1571 1509 sage: elc = copy(el) 1572 1510 sage: elc._setitem(1, 3); elc … … cdef class ClonableIntArray(ClonableElem 1586 1524 """ 1587 1525 EXAMPLES:: 1588 1526 1589 sage: from sage.structure.list_clone import IncreasingIntArrays1527 sage: from sage.structure.list_clone_demo import IncreasingIntArrays 1590 1528 sage: c = IncreasingIntArrays()([1,2,4]) 1591 1529 sage: 1 in c 1592 1530 True … … cdef class ClonableIntArray(ClonableElem 1603 1541 """ 1604 1542 EXAMPLES:: 1605 1543 1606 sage: from sage.structure.list_clone import IncreasingIntArrays1544 sage: from sage.structure.list_clone_demo import IncreasingIntArrays 1607 1545 sage: c = IncreasingIntArrays()([1,2,4]) 1608 1546 sage: c.index(1) 1609 1547 0 … … cdef class ClonableIntArray(ClonableElem 1629 1567 1630 1568 TESTS:: 1631 1569 1632 sage: from sage.structure.list_clone import IncreasingIntArrays1570 sage: from sage.structure.list_clone_demo import IncreasingIntArrays 1633 1571 sage: el = IncreasingIntArrays()([1,2,3]) 1634 1572 sage: hash(el) # random 1635 1573 -309690657 … … cdef class ClonableIntArray(ClonableElem 1649 1587 """ 1650 1588 TESTS:: 1651 1589 1652 sage: from sage.structure.list_clone import IncreasingIntArrays1590 sage: from sage.structure.list_clone_demo import IncreasingIntArrays 1653 1591 sage: el = IncreasingIntArrays()([1,2,4]) 1654 1592 sage: elc = copy(el) 1655 1593 sage: elc == el # indirect doctest … … cdef class ClonableIntArray(ClonableElem 1662 1600 """ 1663 1601 TEST:: 1664 1602 1665 sage: from sage.structure.list_clone import IncreasingIntArrays1603 sage: from sage.structure.list_clone_demo import IncreasingIntArrays 1666 1604 sage: el1 = IncreasingIntArrays()([1,2,4]) 1667 1605 sage: el2 = IncreasingIntArrays()([1,2,3]) 1668 1606 sage: el1 == el1, el2 == el2, el1 == el2 # indirect doctest … … cdef class ClonableIntArray(ClonableElem 1672 1610 """ 1673 1611 cdef int i, minlen, reslen 1674 1612 cdef ClonableIntArray rgt = <ClonableIntArray>right 1613 if left is right: 1614 return 0 1675 1615 if left._list is NULL: 1676 1616 if rgt._list is NULL: 1677 1617 return 0 … … cdef class ClonableIntArray(ClonableElem 1702 1642 1703 1643 TESTS:: 1704 1644 1705 sage: from sage.structure.list_clone import IncreasingIntArrays1645 sage: from sage.structure.list_clone_demo import IncreasingIntArrays 1706 1646 sage: el = IncreasingIntArrays()([1,2,4]) 1707 1647 sage: elc = copy(el) 1708 1648 sage: el[:] == elc[:] … … cdef class ClonableIntArray(ClonableElem 1751 1691 1752 1692 EXAMPLES:: 1753 1693 1694 sage: from sage.structure.list_clone import ClonableArray 1754 1695 sage: ClonableArray(Parent(), [1,2,3]) # indirect doctest 1755 1696 Traceback (most recent call last): 1756 1697 ... 1757 1698 AssertionError: This should never be called, please overload 1758 sage: from sage.structure.list_clone import IncreasingIntArrays1699 sage: from sage.structure.list_clone_demo import IncreasingIntArrays 1759 1700 sage: el = IncreasingIntArrays()([1,2,4]) # indirect doctest 1760 1701 """ 1761 1702 assert False, "This should never be called, please overload" … … cdef class ClonableIntArray(ClonableElem 1766 1707 1767 1708 TESTS:: 1768 1709 1769 sage: from sage.structure.list_clone import IncreasingIntArrays1710 sage: from sage.structure.list_clone_demo import IncreasingIntArrays 1770 1711 sage: el = IncreasingIntArrays()([1,2,3]) 1771 1712 sage: el._hash_() # random 1772 1713 -309711137 … … cdef class ClonableIntArray(ClonableElem 1784 1725 """ 1785 1726 TESTS:: 1786 1727 1787 sage: from sage.structure.list_clone import IncreasingIntArrays1728 sage: from sage.structure.list_clone_demo import IncreasingIntArrays 1788 1729 sage: el = IncreasingIntArrays()([1,2,4]) 1789 1730 sage: loads(dumps(el)) 1790 1731 [1, 2, 4] 1791 1732 sage: t = el.__reduce__(); t 1792 (<built-in function _make_int_array_clone>, (<type 'sage.structure.list_clone .IncreasingIntArray'>, <class 'sage.structure.list_clone.IncreasingIntArrays_with_category'>, [1, 2, 4], True, True, None))1733 (<built-in function _make_int_array_clone>, (<type 'sage.structure.list_clone_demo.IncreasingIntArray'>, <class 'sage.structure.list_clone_demo.IncreasingIntArrays_with_category'>, [1, 2, 4], True, True, None)) 1793 1734 sage: t[0](*t[1]) 1794 1735 [1, 2, 4] 1795 1736 """ … … def _make_int_array_clone(clas, parent, 1810 1751 1811 1752 TESTS:: 1812 1753 1813 sage: from sage.structure.list_clone import _make_int_array_clone, IncreasingIntArrays 1754 sage: from sage.structure.list_clone import _make_int_array_clone 1755 sage: from sage.structure.list_clone_demo import IncreasingIntArrays 1814 1756 sage: ILs = IncreasingIntArrays() 1815 1757 sage: el = _make_int_array_clone(ILs.element_class, ILs, [1,2,3], True, True, None) 1816 1758 sage: el … … def _make_int_array_clone(clas, parent, 1832 1774 """ 1833 1775 cdef ClonableIntArray res 1834 1776 res = <ClonableIntArray> PY_NEW(clas) 1835 ClonableIntArray.__init__(res, parent, lst, needs_check) 1836 res._is_immutable = is_immutable 1777 ClonableIntArray.__init__(res, parent, lst, needs_check, is_immutable) 1837 1778 if dic is not None: 1838 1779 res.__dict__ = dic 1839 1780 return res 1840 1781 1841 1782 1842 cdef class IncreasingIntArray(ClonableIntArray): 1783 1784 cdef class NormalizedClonableList(ClonableList): 1843 1785 """ 1844 A small extension class for testing :class:`ClonableIntArray`.1786 List with clone protocol and normal form 1845 1787 1846 TESTS:: 1788 This is a subclass of :class:`ClonableList` which call a method 1789 :meth:`normalize` at creation and after any modification of its instance. 1847 1790 1848 sage: from sage.structure.list_clone import IncreasingIntArrays 1849 sage: TestSuite(IncreasingIntArrays()([1,2,3])).run() 1850 sage: TestSuite(IncreasingIntArrays()([])).run() 1791 .. seealso:: :class:`~sage.structure.list_clone_demo.SortedList` for an 1792 example of usage. 1793 1794 EXAMPLES: 1795 1796 We construct a sorted list through its parent:: 1797 1798 sage: from sage.structure.list_clone_demo import SortedLists 1799 sage: SL = SortedLists() 1800 sage: sl1 = SL([4,2,6,1]); sl1 1801 [1, 2, 4, 6] 1802 1803 Normalization is also performed atfer modification:: 1804 1805 sage: with sl1.clone() as sl2: 1806 ... sl2[1] = 12 1807 sage: sl2 1808 [1, 4, 6, 12] 1851 1809 """ 1852 1810 1853 cpdef check(self): 1811 def __init__(self, Parent parent, lst, check = True, immutable = True): 1812 r""" 1813 TESTS:: 1814 1815 sage: from sage.structure.list_clone_demo import SortedList, SortedLists 1816 sage: SortedList(SortedLists(), [2,3,1]) 1817 [1, 2, 3] 1854 1818 """ 1855 Check that ``self`` is increasing. 1819 ClonableList.__init__(self, parent, lst, False, False) 1820 self.normalize() 1821 self._is_immutable = immutable 1822 if check: 1823 self.check() 1824 1825 cpdef bint __exit__(self, typ, value, tracback): 1826 r""" 1827 TESTS:: 1828 1829 sage: from sage.structure.list_clone_demo import SortedList, SortedLists 1830 sage: l = SortedList(SortedLists(), [2,3,1], immutable=False); l 1831 [1, 2, 3] 1832 sage: l[1] = 5; l 1833 [1, 5, 3] 1834 sage: l.__exit__(None, None, None) 1835 False 1836 sage: l 1837 [1, 3, 5] 1838 """ 1839 self.normalize() 1840 ClonableList.__exit__(self, typ, value, tracback) 1841 1842 cpdef normalize(self): 1843 """ 1844 Normalize ``self`` 1845 1846 This is an abstract method. Subclasses are supposed to overload 1847 :meth:`normalize`. The call ``self.normalize()`` is supposed to 1848 1849 - call ``self._require_mutable()`` to check that ``self`` is in a 1850 proper mutable state 1851 - modify ``self`` to put it in a normal form 1856 1852 1857 1853 EXAMPLES:: 1858 1854 1859 sage: from sage.structure.list_clone import IncreasingIntArrays 1860 sage: IncreasingIntArrays()([1,2,3]) # indirect doctest 1861 [1, 2, 3] 1862 sage: IncreasingIntArrays()([3,2,1]) # indirect doctest 1855 sage: from sage.structure.list_clone_demo import SortedList, SortedLists 1856 sage: l = SortedList(SortedLists(), [2,3,2], False, False) 1857 sage: l 1858 [2, 2, 3] 1859 sage: l.check() 1863 1860 Traceback (most recent call last): 1864 1861 ... 1865 AssertionError: array is notincreasing1862 AssertionError: list is not strictly increasing 1866 1863 """ 1867 cdef int i 1868 if not self: 1869 return 1870 for i in range(len(self)-1): 1871 assert self._getitem(i) < self._getitem(i+1), "array is not increasing" 1864 assert False, "This should never be called, please overload" 1872 1865 1873 class IncreasingIntArrays(IncreasingArrays):1874 """1875 A small (incomplete) parent for testing :class:`ClonableIntArray`1876 1877 TESTS::1878 1879 sage: from sage.structure.list_clone import IncreasingIntArrays1880 sage: IncreasingIntArrays().element_class1881 <type 'sage.structure.list_clone.IncreasingIntArray'>1882 """1883 Element = IncreasingIntArray -
new file sage/structure/list_clone_demo.pyx
diff --git a/sage/structure/list_clone_demo.pyx b/sage/structure/list_clone_demo.pyx new file mode 100644
- + 1 r""" 2 Elements, Array and Lists With Clone Protocol, demonstration classes 3 4 This module demonstrate the usage of the various classes defined in 5 :mod:`~sage.structure.list_clone` 6 """ 7 8 #***************************************************************************** 9 # Copyright (C) 2011 Florent Hivert <Florent.Hivert@univ-rouen.fr> 10 # 11 # Distributed under the terms of the GNU General Public License (GPL) 12 # http://www.gnu.org/licenses/ 13 #***************************************************************************** 14 15 from sage.categories.sets_cat import Sets 16 from sage.structure.unique_representation import UniqueRepresentation 17 from sage.structure.list_clone cimport ( 18 ClonableArray, ClonableList, NormalizedClonableList, ClonableIntArray ) 19 from sage.structure.parent import Parent 20 21 cdef class IncreasingArray(ClonableArray): 22 """ 23 A small extension class for testing 24 :class:`~sage.structure.list_clone.ClonableArray`. 25 26 TESTS:: 27 28 sage: from sage.structure.list_clone_demo import IncreasingArrays 29 sage: TestSuite(IncreasingArrays()([1,2,3])).run() 30 sage: TestSuite(IncreasingArrays()([])).run() 31 """ 32 33 cpdef check(self): 34 """ 35 Check that ``self`` is increasing. 36 37 EXAMPLES:: 38 39 sage: from sage.structure.list_clone_demo import IncreasingArrays 40 sage: IncreasingArrays()([1,2,3]) # indirect doctest 41 [1, 2, 3] 42 sage: IncreasingArrays()([3,2,1]) # indirect doctest 43 Traceback (most recent call last): 44 ... 45 AssertionError: array is not increasing 46 """ 47 cdef int i 48 for i in range(len(self)-1): 49 assert self._getitem(i) <= self._getitem(i+1), "array is not increasing" 50 51 52 class IncreasingArrays(UniqueRepresentation, Parent): 53 """ 54 A small (incomplete) parent for testing 55 :class:`~sage.structure.list_clone.ClonableArray` 56 57 TESTS:: 58 59 sage: from sage.structure.list_clone_demo import IncreasingArrays 60 sage: IncreasingArrays().element_class 61 <type 'sage.structure.list_clone_demo.IncreasingArray'> 62 """ 63 64 def __init__(self): 65 """ 66 TESTS:: 67 68 sage: from sage.structure.list_clone_demo import IncreasingArrays 69 sage: IncreasingArrays() 70 <class 'sage.structure.list_clone_demo.IncreasingArrays_with_category'> 71 sage: IncreasingArrays() == IncreasingArrays() 72 True 73 """ 74 Parent.__init__(self, category = Sets()) 75 76 def _element_constructor_(self, *args, **keywords): 77 """ 78 TESTS:: 79 80 sage: from sage.structure.list_clone_demo import IncreasingArrays 81 sage: IncreasingArrays()([1]) # indirect doctest 82 [1] 83 """ 84 return self.element_class(self, *args, **keywords) 85 86 Element = IncreasingArray 87 88 89 90 class IncreasingLists(IncreasingArrays): 91 """ 92 A small (incomplete) parent for testing 93 :class:`~sage.structure.list_clone.ClonableList` 94 95 TESTS:: 96 97 sage: from sage.structure.list_clone_demo import IncreasingLists 98 sage: IncreasingLists().element_class 99 <type 'sage.structure.list_clone_demo.IncreasingList'> 100 """ 101 Element = IncreasingList 102 103 cdef class IncreasingList(ClonableList): 104 """ 105 A small extension class for testing 106 :class:`~sage.structure.list_clone.ClonableList` 107 108 TESTS:: 109 110 sage: from sage.structure.list_clone_demo import IncreasingLists 111 sage: TestSuite(IncreasingLists()([1,2,3])).run() 112 sage: TestSuite(IncreasingLists()([])).run() 113 """ 114 115 cpdef check(self): 116 """ 117 Check that ``self`` is increasing 118 119 EXAMPLES:: 120 121 sage: from sage.structure.list_clone_demo import IncreasingLists 122 sage: IncreasingLists()([1,2,3]) # indirect doctest 123 [1, 2, 3] 124 sage: IncreasingLists()([3,2,1]) # indirect doctest 125 Traceback (most recent call last): 126 ... 127 AssertionError: array is not increasing 128 """ 129 cdef int i 130 for i in range(len(self)-1): 131 assert self._getitem(i) < self._getitem(i+1), "array is not increasing" 132 133 134 135 cdef class IncreasingIntArray(ClonableIntArray): 136 """ 137 A small extension class for testing 138 :class:`~sage.structure.list_clone.ClonableIntArray`. 139 140 TESTS:: 141 142 sage: from sage.structure.list_clone_demo import IncreasingIntArrays 143 sage: TestSuite(IncreasingIntArrays()([1,2,3])).run() 144 sage: TestSuite(IncreasingIntArrays()([])).run() 145 """ 146 147 cpdef check(self): 148 """ 149 Check that ``self`` is increasing. 150 151 EXAMPLES:: 152 153 sage: from sage.structure.list_clone_demo import IncreasingIntArrays 154 sage: IncreasingIntArrays()([1,2,3]) # indirect doctest 155 [1, 2, 3] 156 sage: IncreasingIntArrays()([3,2,1]) # indirect doctest 157 Traceback (most recent call last): 158 ... 159 AssertionError: array is not increasing 160 """ 161 cdef int i 162 if not self: 163 return 164 for i in range(len(self)-1): 165 assert self._getitem(i) < self._getitem(i+1), "array is not increasing" 166 167 class IncreasingIntArrays(IncreasingArrays): 168 """ 169 A small (incomplete) parent for testing 170 :class:`~sage.structure.list_clone.ClonableIntArray` 171 172 TESTS:: 173 174 sage: from sage.structure.list_clone_demo import IncreasingIntArrays 175 sage: IncreasingIntArrays().element_class 176 <type 'sage.structure.list_clone_demo.IncreasingIntArray'> 177 """ 178 Element = IncreasingIntArray 179 180 181 182 cdef class SortedList(NormalizedClonableList): 183 """ 184 A small extension class for testing 185 :class:`~sage.structure.list_clone.NormalizedClonableList`. 186 187 TESTS:: 188 189 sage: from sage.structure.list_clone_demo import IncreasingIntArrays 190 sage: TestSuite(IncreasingIntArrays()([1,2,3])).run() 191 sage: TestSuite(IncreasingIntArrays()([])).run() 192 """ 193 cpdef normalize(self): 194 """ 195 Normalize ``self`` 196 197 Sort the list stored in ``self``. 198 199 EXAMPLES:: 200 201 sage: from sage.structure.list_clone_demo import SortedList, SortedLists 202 sage: l = SortedList(SortedLists(), [3,1,2], False, False) 203 sage: l # indirect doctest 204 [1, 2, 3] 205 sage: l[1] = 5; l 206 [1, 5, 3] 207 sage: l.normalize(); l 208 [1, 3, 5] 209 """ 210 self._require_mutable() 211 self._get_list().sort() 212 213 cpdef check(self): 214 """ 215 Check that ``self`` is strictly increasing 216 217 EXAMPLES:: 218 219 sage: from sage.structure.list_clone_demo import SortedList, SortedLists 220 sage: SortedLists()([1,2,3]) # indirect doctest 221 [1, 2, 3] 222 sage: SortedLists()([3,2,2]) # indirect doctest 223 Traceback (most recent call last): 224 ... 225 AssertionError: list is not strictly increasing 226 """ 227 for i in range(len(self)-1): 228 assert self._getitem(i) != self._getitem(i+1), "list is not strictly increasing" 229 230 class SortedLists(IncreasingLists): 231 """ 232 A small (incomplete) parent for testing 233 :class:`~sage.structure.list_clone.NormalizedClonableList` 234 235 TESTS:: 236 237 sage: from sage.structure.list_clone_demo import SortedList, SortedLists 238 sage: SL = SortedLists() 239 sage: SL([3,1,2]) 240 [1, 2, 3] 241 """ 242 Element = SortedList -
sage/structure/list_clone_timings.py
diff --git a/sage/structure/list_clone_timings.py b/sage/structure/list_clone_timings.py
a b Various timings using a Python class:: 80 80 #***************************************************************************** 81 81 82 82 83 from sage.structure.list_clone import ClonableArray, IncreasingArrays 83 from sage.structure.list_clone import ClonableArray 84 from sage.structure.list_clone_demo import IncreasingArrays 84 85 from sage.structure.list_clone_timings_cy import * 85 86 86 87 class IncreasingArraysPy(IncreasingArrays): … … class IncreasingArraysPy(IncreasingArray 116 117 ##################################################################### 117 118 ###### Timings functions ###### 118 119 ##################################################################### 119 from list_clone import *120 from list_clone_timings_cy import *121 122 120 def add1_internal(bla): 123 121 """ 124 122 TESTS::
