[pypy-commit] pypy numpy-data-buffer: use StringBuilder for getslice in NumpyBuffer.

timo_jbo noreply at buildbot.pypy.org
Mon Oct 3 16:48:46 CEST 2011


Author: Timo Paulssen <timonator at perpetuum-immobile.de>
Branch: numpy-data-buffer
Changeset: r47795:fa778e5f7b0f
Date: 2011-10-03 16:48 +0200
http://bitbucket.org/pypy/pypy/changeset/fa778e5f7b0f/

Log:	use StringBuilder for getslice in NumpyBuffer.

diff --git a/pypy/module/_numpy/interp_buffer.py b/pypy/module/_numpy/interp_buffer.py
--- a/pypy/module/_numpy/interp_buffer.py
+++ b/pypy/module/_numpy/interp_buffer.py
@@ -1,5 +1,6 @@
 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}))
 
@@ -10,27 +11,41 @@
     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


More information about the pypy-commit mailing list