@@ -45,3 +45,254 @@ class Vector:
45
45
46
46
def __init__ (self , hnd ):
47
47
self .hnd = hnd
48
+
49
+ @classmethod
50
+ def empty (cls , nrows ):
51
+
52
+ hnd = ctypes .c_void_p (0 )
53
+
54
+ status = wrapper .loaded_dll .cuBool_Vector_New (
55
+ ctypes .byref (hnd ), ctypes .c_uint (nrows )
56
+ )
57
+
58
+ bridge .check (status )
59
+
60
+ return Vector (hnd )
61
+
62
+ @classmethod
63
+ def from_list (cls , nrows , rows , is_sorted = False , no_duplicates = False ):
64
+ out = cls .empty (nrows )
65
+ out .build (rows , is_sorted = is_sorted , no_duplicates = no_duplicates )
66
+ return out
67
+
68
+ @classmethod
69
+ def generate (cls , nrows , density : float ):
70
+ density = min (1.0 , max (density , 0.0 ))
71
+ nvals_max = nrows
72
+ nvals_to_gen = int (nvals_max * density )
73
+
74
+ m = nrows
75
+ rows = list ()
76
+
77
+ for i in range (nvals_to_gen ):
78
+ rows .append (random .randrange (0 , m ))
79
+
80
+ return Vector .from_list (nrows = nrows , rows = rows , is_sorted = False , no_duplicates = False )
81
+
82
+ def build (self , rows , is_sorted = False , no_duplicates = False ):
83
+
84
+ nvals = len (rows )
85
+ t_rows = (ctypes .c_uint * len (rows ))(* rows )
86
+
87
+ status = wrapper .loaded_dll .cuBool_Vector_Build (
88
+ self .hnd , t_rows ,
89
+ ctypes .c_uint (nvals ),
90
+ ctypes .c_uint (bridge .get_build_hints (is_sorted , no_duplicates ))
91
+ )
92
+
93
+ bridge .check (status )
94
+
95
+ def dup (self ):
96
+ hnd = ctypes .c_void_p (0 )
97
+
98
+ status = wrapper .loaded_dll .cuBool_Vector_Duplicate (
99
+ self .hnd , ctypes .byref (hnd )
100
+ )
101
+
102
+ bridge .check (status )
103
+ return Vector (hnd )
104
+
105
+ def set_marker (self , marker : str ):
106
+ assert marker is not None
107
+
108
+ status = wrapper .loaded_dll .cuBool_Vector_SetMarker (
109
+ self .hnd , marker .encode ("utf-8" )
110
+ )
111
+
112
+ bridge .check (status )
113
+
114
+ @property
115
+ def marker (self ):
116
+ size = ctypes .c_uint (0 )
117
+ status = wrapper .loaded_dll .cuBool_Vector_Marker (
118
+ self .hnd , ctypes .POINTER (ctypes .c_char )(), ctypes .byref (size )
119
+ )
120
+
121
+ bridge .check (status )
122
+
123
+ c_buffer = (ctypes .c_char * int (size .value ))()
124
+ status = wrapper .loaded_dll .cuBool_Vector_Marker (
125
+ self .hnd , c_buffer , ctypes .byref (size )
126
+ )
127
+
128
+ bridge .check (status )
129
+ return c_buffer .value .decode ("utf-8" )
130
+
131
+ @property
132
+ def nrows (self ) -> int :
133
+ result = ctypes .c_uint (0 )
134
+
135
+ status = wrapper .loaded_dll .cuBool_Vector_Nrows (
136
+ self .hnd , ctypes .byref (result )
137
+ )
138
+
139
+ bridge .check (status )
140
+ return int (result .value )
141
+
142
+ @property
143
+ def nvals (self ) -> int :
144
+ result = ctypes .c_uint (0 )
145
+
146
+ status = wrapper .loaded_dll .cuBool_Vector_Nvals (
147
+ self .hnd , ctypes .byref (result )
148
+ )
149
+
150
+ bridge .check (status )
151
+ return int (result .value )
152
+
153
+ def to_list (self ):
154
+ count = self .nvals
155
+
156
+ rows = (ctypes .c_uint * count )()
157
+ nvals = ctypes .c_uint (count )
158
+
159
+ status = wrapper .loaded_dll .cuBool_Vector_ExtractValues (
160
+ self .hnd , rows , ctypes .byref (nvals )
161
+ )
162
+
163
+ bridge .check (status )
164
+
165
+ return rows
166
+
167
+ def to_string (self , width = 3 ):
168
+ nrows = self .nrows
169
+ nvals = self .nvals
170
+ rows = self .to_list ()
171
+
172
+ cell_empty = "."
173
+ cell_filled = "1"
174
+ cell_sep = " "
175
+ format_str = "{:>%s}" % width
176
+
177
+ result = ""
178
+
179
+ v = 0
180
+ for i in range (nrows ):
181
+ line = format_str .format (i ) + " |" + cell_sep
182
+ if v < nvals and rows [v ] == i :
183
+ line += format_str .format (cell_filled ) + cell_sep
184
+ v += 1
185
+ else :
186
+ line += format_str .format (cell_empty ) + cell_sep
187
+ line += "| " + format_str .format (i ) + "\n "
188
+ result += line
189
+
190
+ result += "\n "
191
+ return result
192
+
193
+ def extract_vector (self , i , nrows , out = None , time_check = False ):
194
+ if out is None :
195
+ out = Vector .empty (nrows )
196
+
197
+ status = wrapper .loaded_dll .cuBool_Vector_ExtractSubVector (
198
+ out .hnd , self .hnd ,
199
+ ctypes .c_uint (i ),
200
+ ctypes .c_uint (nrows ),
201
+ ctypes .c_uint (bridge .get_sub_vector_hints (time_check = time_check ))
202
+ )
203
+
204
+ bridge .check (status )
205
+ return out
206
+
207
+ def vxm (self , other , out = None , time_check = False ):
208
+ if out is None :
209
+ out = Vector .empty (other .ncols )
210
+
211
+ status = wrapper .loaded_dll .cuBool_VxM (
212
+ out .hnd ,
213
+ self .hnd ,
214
+ other .hnd ,
215
+ ctypes .c_uint (bridge .get_vxm_hints (time_check = time_check ))
216
+ )
217
+
218
+ bridge .check (status )
219
+ return out
220
+
221
+ def ewiseadd (self , other , out = None , time_check = False ):
222
+ if out is None :
223
+ out = Vector .empty (self .nrows )
224
+
225
+ status = wrapper .loaded_dll .cuBool_Vector_EWiseAdd (
226
+ out .hnd ,
227
+ self .hnd ,
228
+ other .hnd ,
229
+ ctypes .c_uint (bridge .get_ewiseadd_hints (time_check = time_check ))
230
+ )
231
+
232
+ def reduce (self , time_check = False ):
233
+ value = ctypes .c_uint (0 )
234
+
235
+ status = wrapper .loaded_dll .cuBool_Vector_Reduce (
236
+ ctypes .byref (value ),
237
+ self .hnd ,
238
+ ctypes .c_uint (bridge .get_reduce_hints (time_check = time_check ))
239
+ )
240
+
241
+ bridge .check (status )
242
+ return int (value .value )
243
+
244
+ def equals (self , other ) -> bool :
245
+ if not self .nrows == other .nrows :
246
+ return False
247
+ if not self .nvals == other .nvals :
248
+ return False
249
+
250
+ self_rows = self .to_list ()
251
+ other_rows = self .to_list ()
252
+
253
+ for i in range (len (self_rows )):
254
+ if self_rows [i ] != other_rows [i ]:
255
+ return False
256
+
257
+ return True
258
+
259
+ def __str__ (self ):
260
+ return self .to_string ()
261
+
262
+ def __iter__ (self ):
263
+ return self .to_list ()
264
+
265
+ def __getitem__ (self , item ):
266
+ if isinstance (item , slice ):
267
+ i = item .start
268
+ nrows = item .stop
269
+
270
+ assert item .step is None
271
+
272
+ if i is None :
273
+ i = 0
274
+
275
+ assert 0 <= i < self .nrows
276
+
277
+ if nrows is None :
278
+ nrows = self .nrows
279
+
280
+ return self .extract_vector (i , nrows - i )
281
+
282
+ raise Exception ("Invalid vector slicing" )
283
+
284
+ def __setitem__ (self , key , value ):
285
+ assert value is True
286
+
287
+ if isinstance (key , int ):
288
+ i = key
289
+
290
+ status = wrapper .loaded_dll .cuBool_Vector_SetElement (
291
+ self .hnd ,
292
+ ctypes .c_uint (i )
293
+ )
294
+
295
+ bridge .check (status )
296
+ return
297
+
298
+ raise Exception ("Invalid item assignment" )
0 commit comments