Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

r""" 

Elements, Array and Lists With Clone Protocol, demonstration classes 

  

This module demonstrate the usage of the various classes defined in 

:mod:`~sage.structure.list_clone` 

""" 

  

#***************************************************************************** 

# Copyright (C) 2011 Florent Hivert <Florent.Hivert@univ-rouen.fr> 

# 

# Distributed under the terms of the GNU General Public License (GPL) 

# http://www.gnu.org/licenses/ 

#***************************************************************************** 

  

from sage.categories.sets_cat import Sets 

from sage.structure.unique_representation import UniqueRepresentation 

from sage.structure.list_clone cimport ( 

ClonableArray, ClonableList, NormalizedClonableList, ClonableIntArray ) 

from sage.structure.parent import Parent 

  

cdef class IncreasingArray(ClonableArray): 

""" 

A small extension class for testing 

:class:`~sage.structure.list_clone.ClonableArray`. 

  

TESTS:: 

  

sage: from sage.structure.list_clone_demo import IncreasingArrays 

sage: TestSuite(IncreasingArrays()([1,2,3])).run() 

sage: TestSuite(IncreasingArrays()([])).run() 

""" 

  

cpdef check(self): 

""" 

Check that ``self`` is increasing. 

  

EXAMPLES:: 

  

sage: from sage.structure.list_clone_demo import IncreasingArrays 

sage: IncreasingArrays()([1,2,3]) # indirect doctest 

[1, 2, 3] 

sage: IncreasingArrays()([3,2,1]) # indirect doctest 

Traceback (most recent call last): 

... 

ValueError: array is not increasing 

""" 

cdef int i 

for i in range(len(self)-1): 

if self._getitem(i) > self._getitem(i+1): 

raise ValueError("array is not increasing") 

  

  

class IncreasingArrays(UniqueRepresentation, Parent): 

""" 

A small (incomplete) parent for testing 

:class:`~sage.structure.list_clone.ClonableArray` 

  

TESTS:: 

  

sage: from sage.structure.list_clone_demo import IncreasingArrays 

sage: IncreasingArrays().element_class 

<... 'sage.structure.list_clone_demo.IncreasingArray'> 

""" 

  

def __init__(self): 

""" 

TESTS:: 

  

sage: from sage.structure.list_clone_demo import IncreasingArrays 

sage: IncreasingArrays() 

<sage.structure.list_clone_demo.IncreasingArrays_with_category object at ...> 

sage: IncreasingArrays() == IncreasingArrays() 

True 

""" 

Parent.__init__(self, category = Sets()) 

  

def _element_constructor_(self, *args, **keywords): 

""" 

TESTS:: 

  

sage: from sage.structure.list_clone_demo import IncreasingArrays 

sage: IncreasingArrays()([1]) # indirect doctest 

[1] 

""" 

return self.element_class(self, *args, **keywords) 

  

Element = IncreasingArray 

  

  

  

class IncreasingLists(IncreasingArrays): 

""" 

A small (incomplete) parent for testing 

:class:`~sage.structure.list_clone.ClonableList` 

  

TESTS:: 

  

sage: from sage.structure.list_clone_demo import IncreasingLists 

sage: IncreasingLists().element_class 

<... 'sage.structure.list_clone_demo.IncreasingList'> 

""" 

Element = IncreasingList 

  

cdef class IncreasingList(ClonableList): 

""" 

A small extension class for testing 

:class:`~sage.structure.list_clone.ClonableList` 

  

TESTS:: 

  

sage: from sage.structure.list_clone_demo import IncreasingLists 

sage: TestSuite(IncreasingLists()([1,2,3])).run() 

sage: TestSuite(IncreasingLists()([])).run() 

""" 

  

cpdef check(self): 

""" 

Check that ``self`` is increasing 

  

EXAMPLES:: 

  

sage: from sage.structure.list_clone_demo import IncreasingLists 

sage: IncreasingLists()([1,2,3]) # indirect doctest 

[1, 2, 3] 

sage: IncreasingLists()([3,2,1]) # indirect doctest 

Traceback (most recent call last): 

... 

ValueError: array is not increasing 

""" 

cdef int i 

for i in range(len(self)-1): 

if self._getitem(i) >= self._getitem(i+1): 

raise ValueError("array is not increasing") 

  

  

  

cdef class IncreasingIntArray(ClonableIntArray): 

""" 

A small extension class for testing 

:class:`~sage.structure.list_clone.ClonableIntArray`. 

  

TESTS:: 

  

sage: from sage.structure.list_clone_demo import IncreasingIntArrays 

sage: TestSuite(IncreasingIntArrays()([1,2,3])).run() 

sage: TestSuite(IncreasingIntArrays()([])).run() 

""" 

  

cpdef check(self): 

""" 

Check that ``self`` is increasing. 

  

EXAMPLES:: 

  

sage: from sage.structure.list_clone_demo import IncreasingIntArrays 

sage: IncreasingIntArrays()([1,2,3]) # indirect doctest 

[1, 2, 3] 

sage: IncreasingIntArrays()([3,2,1]) # indirect doctest 

Traceback (most recent call last): 

... 

ValueError: array is not increasing 

""" 

cdef int i 

if not self: 

return 

for i in range(len(self)-1): 

if self._getitem(i) >= self._getitem(i+1): 

raise ValueError("array is not increasing") 

  

class IncreasingIntArrays(IncreasingArrays): 

""" 

A small (incomplete) parent for testing 

:class:`~sage.structure.list_clone.ClonableIntArray` 

  

TESTS:: 

  

sage: from sage.structure.list_clone_demo import IncreasingIntArrays 

sage: IncreasingIntArrays().element_class 

<... 'sage.structure.list_clone_demo.IncreasingIntArray'> 

""" 

Element = IncreasingIntArray 

  

  

  

cdef class SortedList(NormalizedClonableList): 

""" 

A small extension class for testing 

:class:`~sage.structure.list_clone.NormalizedClonableList`. 

  

TESTS:: 

  

sage: from sage.structure.list_clone_demo import IncreasingIntArrays 

sage: TestSuite(IncreasingIntArrays()([1,2,3])).run() 

sage: TestSuite(IncreasingIntArrays()([])).run() 

""" 

cpdef normalize(self): 

""" 

Normalize ``self`` 

  

Sort the list stored in ``self``. 

  

EXAMPLES:: 

  

sage: from sage.structure.list_clone_demo import SortedList, SortedLists 

sage: l = SortedList(SortedLists(), [3,1,2], False, False) 

sage: l # indirect doctest 

[1, 2, 3] 

sage: l[1] = 5; l 

[1, 5, 3] 

sage: l.normalize(); l 

[1, 3, 5] 

""" 

self._require_mutable() 

self._get_list().sort() 

  

cpdef check(self): 

""" 

Check that ``self`` is strictly increasing 

  

EXAMPLES:: 

  

sage: from sage.structure.list_clone_demo import SortedList, SortedLists 

sage: SortedLists()([1,2,3]) # indirect doctest 

[1, 2, 3] 

sage: SortedLists()([3,2,2]) # indirect doctest 

Traceback (most recent call last): 

... 

ValueError: list is not strictly increasing 

""" 

for i in range(len(self)-1): 

if self._getitem(i) >= self._getitem(i+1): 

raise ValueError("list is not strictly increasing") 

  

class SortedLists(IncreasingLists): 

""" 

A small (incomplete) parent for testing 

:class:`~sage.structure.list_clone.NormalizedClonableList` 

  

TESTS:: 

  

sage: from sage.structure.list_clone_demo import SortedList, SortedLists 

sage: SL = SortedLists() 

sage: SL([3,1,2]) 

[1, 2, 3] 

""" 

Element = SortedList