Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/sage/structure/list_clone_demo.pyx
8814 views
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 <[email protected]>
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
ValueError: array is not increasing
46
"""
47
cdef int i
48
for i in range(len(self)-1):
49
if self._getitem(i) > self._getitem(i+1):
50
raise ValueError, "array is not increasing"
51
52
53
class IncreasingArrays(UniqueRepresentation, Parent):
54
"""
55
A small (incomplete) parent for testing
56
:class:`~sage.structure.list_clone.ClonableArray`
57
58
TESTS::
59
60
sage: from sage.structure.list_clone_demo import IncreasingArrays
61
sage: IncreasingArrays().element_class
62
<type 'sage.structure.list_clone_demo.IncreasingArray'>
63
"""
64
65
def __init__(self):
66
"""
67
TESTS::
68
69
sage: from sage.structure.list_clone_demo import IncreasingArrays
70
sage: IncreasingArrays()
71
<class 'sage.structure.list_clone_demo.IncreasingArrays_with_category'>
72
sage: IncreasingArrays() == IncreasingArrays()
73
True
74
"""
75
Parent.__init__(self, category = Sets())
76
77
def _element_constructor_(self, *args, **keywords):
78
"""
79
TESTS::
80
81
sage: from sage.structure.list_clone_demo import IncreasingArrays
82
sage: IncreasingArrays()([1]) # indirect doctest
83
[1]
84
"""
85
return self.element_class(self, *args, **keywords)
86
87
Element = IncreasingArray
88
89
90
91
class IncreasingLists(IncreasingArrays):
92
"""
93
A small (incomplete) parent for testing
94
:class:`~sage.structure.list_clone.ClonableList`
95
96
TESTS::
97
98
sage: from sage.structure.list_clone_demo import IncreasingLists
99
sage: IncreasingLists().element_class
100
<type 'sage.structure.list_clone_demo.IncreasingList'>
101
"""
102
Element = IncreasingList
103
104
cdef class IncreasingList(ClonableList):
105
"""
106
A small extension class for testing
107
:class:`~sage.structure.list_clone.ClonableList`
108
109
TESTS::
110
111
sage: from sage.structure.list_clone_demo import IncreasingLists
112
sage: TestSuite(IncreasingLists()([1,2,3])).run()
113
sage: TestSuite(IncreasingLists()([])).run()
114
"""
115
116
cpdef check(self):
117
"""
118
Check that ``self`` is increasing
119
120
EXAMPLES::
121
122
sage: from sage.structure.list_clone_demo import IncreasingLists
123
sage: IncreasingLists()([1,2,3]) # indirect doctest
124
[1, 2, 3]
125
sage: IncreasingLists()([3,2,1]) # indirect doctest
126
Traceback (most recent call last):
127
...
128
ValueError: array is not increasing
129
"""
130
cdef int i
131
for i in range(len(self)-1):
132
if self._getitem(i) >= self._getitem(i+1):
133
raise ValueError , "array is not increasing"
134
135
136
137
cdef class IncreasingIntArray(ClonableIntArray):
138
"""
139
A small extension class for testing
140
:class:`~sage.structure.list_clone.ClonableIntArray`.
141
142
TESTS::
143
144
sage: from sage.structure.list_clone_demo import IncreasingIntArrays
145
sage: TestSuite(IncreasingIntArrays()([1,2,3])).run()
146
sage: TestSuite(IncreasingIntArrays()([])).run()
147
"""
148
149
cpdef check(self):
150
"""
151
Check that ``self`` is increasing.
152
153
EXAMPLES::
154
155
sage: from sage.structure.list_clone_demo import IncreasingIntArrays
156
sage: IncreasingIntArrays()([1,2,3]) # indirect doctest
157
[1, 2, 3]
158
sage: IncreasingIntArrays()([3,2,1]) # indirect doctest
159
Traceback (most recent call last):
160
...
161
ValueError: array is not increasing
162
"""
163
cdef int i
164
if not self:
165
return
166
for i in range(len(self)-1):
167
if self._getitem(i) >= self._getitem(i+1):
168
raise ValueError, "array is not increasing"
169
170
class IncreasingIntArrays(IncreasingArrays):
171
"""
172
A small (incomplete) parent for testing
173
:class:`~sage.structure.list_clone.ClonableIntArray`
174
175
TESTS::
176
177
sage: from sage.structure.list_clone_demo import IncreasingIntArrays
178
sage: IncreasingIntArrays().element_class
179
<type 'sage.structure.list_clone_demo.IncreasingIntArray'>
180
"""
181
Element = IncreasingIntArray
182
183
184
185
cdef class SortedList(NormalizedClonableList):
186
"""
187
A small extension class for testing
188
:class:`~sage.structure.list_clone.NormalizedClonableList`.
189
190
TESTS::
191
192
sage: from sage.structure.list_clone_demo import IncreasingIntArrays
193
sage: TestSuite(IncreasingIntArrays()([1,2,3])).run()
194
sage: TestSuite(IncreasingIntArrays()([])).run()
195
"""
196
cpdef normalize(self):
197
"""
198
Normalize ``self``
199
200
Sort the list stored in ``self``.
201
202
EXAMPLES::
203
204
sage: from sage.structure.list_clone_demo import SortedList, SortedLists
205
sage: l = SortedList(SortedLists(), [3,1,2], False, False)
206
sage: l # indirect doctest
207
[1, 2, 3]
208
sage: l[1] = 5; l
209
[1, 5, 3]
210
sage: l.normalize(); l
211
[1, 3, 5]
212
"""
213
self._require_mutable()
214
self._get_list().sort()
215
216
cpdef check(self):
217
"""
218
Check that ``self`` is strictly increasing
219
220
EXAMPLES::
221
222
sage: from sage.structure.list_clone_demo import SortedList, SortedLists
223
sage: SortedLists()([1,2,3]) # indirect doctest
224
[1, 2, 3]
225
sage: SortedLists()([3,2,2]) # indirect doctest
226
Traceback (most recent call last):
227
...
228
ValueError: list is not strictly increasing
229
"""
230
for i in range(len(self)-1):
231
if self._getitem(i) >= self._getitem(i+1):
232
raise ValueError, "list is not strictly increasing"
233
234
class SortedLists(IncreasingLists):
235
"""
236
A small (incomplete) parent for testing
237
:class:`~sage.structure.list_clone.NormalizedClonableList`
238
239
TESTS::
240
241
sage: from sage.structure.list_clone_demo import SortedList, SortedLists
242
sage: SL = SortedLists()
243
sage: SL([3,1,2])
244
[1, 2, 3]
245
"""
246
Element = SortedList
247
248