Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/tests/stl_vector.pyx
4057 views
1
"""
2
Example of a class wrapping an STL vector
3
4
EXAMPLES::
5
6
sage: from sage.tests.stl_vector import stl_int_vector
7
sage: v = stl_int_vector()
8
sage: v
9
A vector of integers
10
vector<int>:
11
data[0] = 123
12
data[1] = 456
13
14
AUTHORS:
15
16
- Volker Braun (2012-01-18): initial version
17
"""
18
19
#*****************************************************************************
20
# Copyright (C) 2012 Volker Braun <[email protected]>
21
#
22
# Distributed under the terms of the GNU General Public License (GPL)
23
# as published by the Free Software Foundation; either version 2 of
24
# the License, or (at your option) any later version.
25
# http://www.gnu.org/licenses/
26
#*****************************************************************************
27
28
include '../ext/interrupt.pxi'
29
30
from sage.structure.sage_object cimport SageObject
31
from sage.rings.integer cimport Integer
32
from sage.libs.gmp.mpz cimport mpz_add_ui
33
from libcpp.vector cimport vector
34
from libcpp.string cimport string
35
36
cdef class stl_int_vector(SageObject):
37
"""
38
Example class wrapping an STL vector
39
40
EXAMPLES::
41
42
sage: from sage.tests.stl_vector import stl_int_vector
43
sage: v = stl_int_vector()
44
"""
45
46
cdef vector[int] *data
47
cdef string *name
48
49
def __cinit__(self):
50
"""
51
The Cython constructor.
52
53
EXAMPLES::
54
55
sage: from sage.tests.stl_vector import stl_int_vector
56
sage: v = stl_int_vector() # indirect doctest
57
sage: TestSuite(v)
58
Test suite for A vector of integers
59
vector<int>:
60
data[0] = 123
61
data[1] = 456
62
"""
63
self.data = new vector[int]()
64
self.name = new string(<char*>"A vector of integers\n")
65
self.data.push_back(123)
66
self.data.push_back(456)
67
68
def __dealloc__(self):
69
"""
70
The Cython destructor.
71
72
EXAMPLES::
73
74
sage: from sage.tests.stl_vector import stl_int_vector
75
sage: v = stl_int_vector() # indirect doctest
76
"""
77
del self.data
78
del self.name
79
80
def __getitem__(self, int i):
81
"""
82
Return the ``i``-th element.
83
84
EXAMPLES::
85
86
sage: from sage.tests.stl_vector import stl_int_vector
87
sage: v = stl_int_vector()
88
sage: v[1]
89
456
90
"""
91
assert i>=0 and i<self.data.size()
92
return self.data.at(i)
93
94
def __repr__(self):
95
"""
96
Return a string representation.
97
98
EXAMPLES::
99
100
sage: from sage.tests.stl_vector import stl_int_vector
101
sage: v = stl_int_vector()
102
sage: v
103
A vector of integers
104
vector<int>:
105
data[0] = 123
106
data[1] = 456
107
"""
108
s = self.name.c_str()
109
s += 'vector<int>:\n'
110
for i in range(0,self.data.size()):
111
s += ' data['+str(i)+'] = '+str(self.data.at(i))+'\n'
112
return s.strip()
113
114
cpdef sum(self):
115
"""
116
Add the elements.
117
118
EXAMPLES::
119
120
sage: from sage.tests.stl_vector import stl_int_vector
121
sage: v = stl_int_vector()
122
sage: v.sum()
123
579
124
"""
125
cdef Integer accumulator = Integer(0)
126
cdef vector[int].iterator i = self.data.begin()
127
sig_on()
128
while i != self.data.end():
129
mpz_add_ui(accumulator.value, accumulator.value, <int>(i[0]))
130
i += 1
131
sig_off()
132
return accumulator
133
134
def __cmp__(lhs, stl_int_vector rhs):
135
"""
136
Compare with ``other``.
137
138
EXAMPLES::
139
140
sage: from sage.tests.stl_vector import stl_int_vector
141
sage: u = stl_int_vector()
142
sage: v = stl_int_vector()
143
sage: cmp(u,v)
144
0
145
"""
146
cdef int c = cmp(lhs.data.size(), rhs.data.size())
147
if c != 0:
148
return c
149
cdef vector[int].iterator lhs_iter = lhs.data.begin()
150
cdef vector[int].iterator rhs_iter = rhs.data.begin()
151
try:
152
sig_on()
153
while lhs_iter != lhs.data.end():
154
c = cmp(<int>(lhs_iter[0]), <int>(rhs_iter[0]))
155
if c != 0:
156
return c
157
lhs_iter += 1
158
rhs_iter += 1
159
finally:
160
sig_off()
161
return 0
162
163
164