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

""" 

Example of a class wrapping an STL vector 

  

EXAMPLES:: 

  

sage: from sage.tests.stl_vector import stl_int_vector 

sage: v = stl_int_vector() 

sage: v 

A vector of integers 

vector<int>: 

data[0] = 123 

data[1] = 456 

  

AUTHORS: 

  

- Volker Braun (2012-01-18): initial version 

""" 

  

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

# Copyright (C) 2012 Volker Braun <vbraun.name@gmail.com> 

# 

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

# as published by the Free Software Foundation; either version 2 of 

# the License, or (at your option) any later version. 

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

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

  

from cysignals.signals cimport sig_on, sig_off 

  

from sage.structure.sage_object cimport SageObject 

from sage.rings.integer cimport Integer 

from sage.libs.gmp.mpz cimport mpz_add_ui 

from libcpp.vector cimport vector 

from libcpp.string cimport string 

from sage.structure.richcmp cimport richcmp_not_equal, rich_to_bool 

  

  

cdef class stl_int_vector(SageObject): 

""" 

Example class wrapping an STL vector 

  

EXAMPLES:: 

  

sage: from sage.tests.stl_vector import stl_int_vector 

sage: v = stl_int_vector() 

""" 

  

cdef vector[int] *data 

cdef string *name 

  

def __cinit__(self): 

""" 

The Cython constructor. 

  

EXAMPLES:: 

  

sage: from sage.tests.stl_vector import stl_int_vector 

sage: v = stl_int_vector() # indirect doctest 

sage: TestSuite(v) 

Test suite for A vector of integers 

vector<int>: 

data[0] = 123 

data[1] = 456 

""" 

self.data = new vector[int]() 

self.name = new string(<char*>"A vector of integers\n") 

self.data.push_back(123) 

self.data.push_back(456) 

  

def __dealloc__(self): 

""" 

The Cython destructor. 

  

EXAMPLES:: 

  

sage: from sage.tests.stl_vector import stl_int_vector 

sage: v = stl_int_vector() # indirect doctest 

""" 

del self.data 

del self.name 

  

def __getitem__(self, int i): 

""" 

Return the ``i``-th element. 

  

EXAMPLES:: 

  

sage: from sage.tests.stl_vector import stl_int_vector 

sage: v = stl_int_vector() 

sage: v[1] 

456 

""" 

assert i>=0 and i<self.data.size() 

return self.data.at(i) 

  

def __repr__(self): 

""" 

Return a string representation. 

  

EXAMPLES:: 

  

sage: from sage.tests.stl_vector import stl_int_vector 

sage: v = stl_int_vector() 

sage: v 

A vector of integers 

vector<int>: 

data[0] = 123 

data[1] = 456 

""" 

s = self.name.c_str() 

s += 'vector<int>:\n' 

for i in range(0,self.data.size()): 

s += ' data['+str(i)+'] = '+str(self.data.at(i))+'\n' 

return s.strip() 

  

cpdef sum(self): 

""" 

Add the elements. 

  

EXAMPLES:: 

  

sage: from sage.tests.stl_vector import stl_int_vector 

sage: v = stl_int_vector() 

sage: v.sum() 

579 

""" 

cdef Integer accumulator = Integer(0) 

cdef vector[int].iterator i = self.data.begin() 

sig_on() 

while i != self.data.end(): 

mpz_add_ui(accumulator.value, accumulator.value, <int>(i[0])) 

i += 1 

sig_off() 

return accumulator 

  

def __richcmp__(left, stl_int_vector right, int op): 

""" 

Compare with ``other``. 

  

EXAMPLES:: 

  

sage: from sage.tests.stl_vector import stl_int_vector 

sage: u = stl_int_vector() 

sage: v = stl_int_vector() 

sage: u == v 

True 

""" 

cdef stl_int_vector lhs = left 

cdef stl_int_vector rhs = right 

  

lx = lhs.data.size() 

rx = rhs.data.size() 

if lx != rx: 

return richcmp_not_equal(lx, rx, op) 

  

cdef vector[int].iterator lhs_iter = lhs.data.begin() 

cdef vector[int].iterator rhs_iter = rhs.data.begin() 

sig_on() 

try: 

while lhs_iter != lhs.data.end(): 

left_i = <int>(lhs_iter[0]) 

right_i = <int>(rhs_iter[0]) 

if left_i != right_i: 

return richcmp_not_equal(left_i, right_i, op) 

lhs_iter += 1 

rhs_iter += 1 

finally: 

sig_off() 

return rich_to_bool(op, 0)