Commits

Timo Paulssen  committed fa778e5

use StringBuilder for getslice in NumpyBuffer.

  • Participants
  • Parent commits fb2e36c
  • Branches numpy-data-buffer

Comments (0)

Files changed (1)

File pypy/module/_numpy/interp_buffer.py

 from pypy.interpreter.buffer import RWBuffer
 from pypy.rpython.lltypesystem import lltype, rffi
+from pypy.rlib.rstring import StringBuilder
 
 CHAR_TP = lltype.Ptr(lltype.Array(lltype.Char, hints={'nolength': True}))
 
     def getlength(self):
         return self.array.get_concrete().find_size() * self.array.find_dtype().num_bytes
 
-    def getitem(self, index):
-        if index > self.getlength():
-            raise IndexError("Index out of bounds (0<=index<=%d)" % self.getlength())
+    def getitem_noboundcheck(self, index):
         storage = self.array.get_concrete().get_root_storage()
         char_data = rffi.cast(CHAR_TP, storage)
         index = self.calc_index(index)
         return char_data[index]
 
-    def setitem(self, index, value):
+    def getitem(self, index):
         if index > self.getlength():
             raise IndexError("Index out of bounds (0<=index<=%d)" % self.getlength())
+        return self.getitem_noboundcheck(index)
+
+    def setitem_noboundcheck(self, index, value):
         storage = self.array.get_concrete().get_root_storage()
         char_ptr = rffi.cast(CHAR_TP, storage)
         index = self.calc_index(index)
         char_ptr[index] = value
 
+    def setitem(self, index, value):
+        if index > self.getlength():
+            raise IndexError("Index out of bounds (0<=index<=%d)" % self.getlength())
+        self.setitem_noboundcheck(index, value)
+
     def setslice(self, index, newstring):
         if index + len(newstring) > self.getlength():
             raise IndexError("End of slice to set out of bounds (0<=index<=%d)" % self.getlength())
         for idx in range(0, len(newstring)):
-            self.setitem(index + idx, newstring[idx])
+            self.setitem_noboundcheck(index + idx, newstring[idx])
+
+    def getslice(self, start, stop, step, size):
+        builder = StringBuilder(size)
+
+        for index in range(start, stop, step):
+            builder.append(self.getitem_noboundcheck(index))
+
+        return builder.build()
 
     def calc_index(self, index):
         return index