def _stringToBuffer(datastring): """_stringToBuffer(datastring) allocates a buffer, copies datastring into it, and returns the buffer. """ abuff = memory.new_memory(len(datastring)) memory.writeable_buffer(abuff)[:] = datastring return abuff
def _modify_buffer(self, offset, size): """_modify_buffer(self, offset, size) replaces the slice's mmap buffer with a resized RAM buffer, and copies the contents. """ self._checkDeleted() self._dirty = 1 olen = len(self) nlen = olen + size nm = memory.new_memory(nlen) nm[0:offset] = self._buffer[0:offset] if size > 0: nm[offset + size:] = self._buffer[offset:] else: nm[offset:] = self._buffer[offset - size:] self._buffer = nm
def _modify_buffer(self, offset, size): """_modify_buffer(self, offset, size) replaces the slice's mmap buffer with a resized RAM buffer, and copies the contents. """ self._checkDeleted() self._dirty = 1 olen = len(self) nlen = olen+size nm = memory.new_memory(nlen) nm[0:offset] = self._buffer[0:offset] if size > 0: nm[offset+size:] = self._buffer[offset:] else: nm[offset:] = self._buffer[offset-size:] self._buffer = nm
def copy(self): """Return a new array with the same shape and type, but a copy of the data""" arr = self.view() arr._data = memory.new_memory(arr._itemsize * arr.nelements()) arr._byteoffset = 0 arr._bytestride = arr._itemsize arr._strides = arr._stridesFromShape() arr._itemsize = self._itemsize # now copy data, if possible using larger units if product(self._shape): fname = "copy"+str(self._itemsize)+"bytes" copyfunction = ((self.isaligned() and _bytes.functionDict.get(fname)) or _bytes.functionDict["copyNbytes"]) copyfunction(arr._shape, self._data, self._byteoffset, self._strides, arr._data, 0, arr._strides, arr._itemsize) return arr
def resize(self, shape, *args): """ resize() shrinks/grows 'self' to new 'shape', possibly replacing the underlying buffer object. """ shape = getShape(shape, *args) nlen = product(shape) if nlen < 0: raise ValueError("Negative shape dims don't work with resize") olen = self.nelements() if (isinstance(self._data, _types.BufferType) or isinstance(self._data, memory.MemoryType)): if self.iscontiguous(): oself = self.view() else: oself = self.copy() self._data = memory.new_memory(nlen*self._itemsize) self._bytestride = self._itemsize self._byteoffset = 0 blen = min(nlen, olen) self.ravel() oself.ravel() self[:blen] = oself[:blen] else: # Memmap self._data.resize(nlen*self._itemsize) self._shape = (nlen,) self._strides = self._stridesFromShape() if olen: # use any existing data as a pattern to be repeated if nlen > olen: offset = olen while offset+olen <= nlen: self[offset:offset+olen] = self[0:olen] offset += olen self[offset:nlen] = self[0:nlen-offset] else: # zero fill resized zero-length numarray self[:] = 0 self._shape = shape self._strides = self._stridesFromShape() return self
def __init__(self, shape=(), itemsize=1, buffer=None, byteoffset=0, bytestride=None, byteorder=sys.byteorder, aligned=1): _ndarray.__init__(self); self._itemsize = itemsize self._byteoffset = byteoffset self._shape = getShape(shape) if bytestride is None: self._bytestride = itemsize elif bytestride < itemsize: raise ValueError('bytestride must be >= itemsize') else: self._bytestride = bytestride if buffer is not None: self._data = buffer else: size = self._bytestride * self.nelements() self._data = memory.new_memory(size) self._strides = self._stridesFromShape()
def copy(self): """Return a new array with the same shape and type, but a copy of the data >>> c = fromlist(["this","that", "another"]) >>> d = c.copy() >>> d CharArray(['this', 'that', 'another']) >>> int(c._data is d._data) 0 """ arr = self.view() arr._data = memory.new_memory(arr._itemsize * arr.nelements()) arr._byteoffset = 0 arr._bytestride = arr._itemsize arr._strides = arr._stridesFromShape() arr._itemsize = self._itemsize if _na.product(self._shape): copyfunction = _bytes.functionDict["copyNbytes"] copyfunction( arr._shape, self._data, self._byteoffset, self._strides, arr._data, 0, arr._strides, arr._itemsize ) return arr
def copy(self): """Return a new array with the same shape and type, but a copy of the data >>> c = fromlist(["this","that", "another"]) >>> d = c.copy() >>> d CharArray(['this', 'that', 'another']) >>> int(c._data is d._data) 0 """ arr = self.view() arr._data = memory.new_memory(arr._itemsize * arr.nelements()) arr._byteoffset = 0 arr._bytestride = arr._itemsize arr._strides = arr._stridesFromShape() arr._itemsize = self._itemsize if _na.product(self._shape): copyfunction = _bytes.functionDict["copyNbytes"] copyfunction(arr._shape, self._data, self._byteoffset, self._strides, arr._data, 0, arr._strides, arr._itemsize) return arr
def insert(self, offset, size=None, buffer=None): """ insert(self, offset, size) inserts a new slice of 'size' bytes, possibly between two adjacent slices, at byte 'offset'. It is not legal to insert into the middle of another slice, but pretty much everything else is legal. The resulting MemmapSlice is returned. >>> m = open("memmap.tst",mode="w+",len=100) >>> n = m[0:50] >>> p = m[50:100] >>> q=m.insert(0, 200) >>> r=m.insert(50, 100) >>> s=m.insert(100, 300) >>> t=m.insert(45, 100) Traceback (most recent call last): ... IndexError: Slice overlaps prior slice of same file. >>> m.flush() >>> len(m) 700 >>> m.close() """ self._chkIndex(offset, isSlice=1) self._chkOverlaps(offset, offset) if buffer is None: mem = memory.new_memory(size) mem = memory.writeable_buffer(mem) elif size is None or len(buffer) == size: mem = buffer size = len(buffer) else: raise ValueError("buffer and size don't match.") obj = MemmapSlice(mem, dirty=1) self._slices.append((offset, offset, obj)) return obj
def _stridesFromShape(shape): """_stridesFromShape() computes the strides which correspond to the specified shape.""" if len(shape) > 1: head = [_gen.product(shape[1:])] tail = _stridesFromShape(shape[1:]) head.extend(tail) return head else: return [1] _dummyBuffer = _mem.new_memory(0) if _PROTOTYPE: class _objarr(_gen.NDArray): def __init__(self, shape, objects=None): if objects is None: objects = [None] * _gen.product(shape) _gen.NDArray.__init__(self, shape=shape, buffer=_dummyBuffer, itemsize=1) self._objects = objects[:] def _getitem(self, offset): return self._objects[offset] def _setitem(self, offset, value): self._objects[offset] = value
def _stridesFromShape(shape): """_stridesFromShape() computes the strides which correspond to the specified shape.""" if len(shape) > 1: head = [_gen.product(shape[1:])] tail = _stridesFromShape(shape[1:]) head.extend(tail) return head else: return [1] _dummyBuffer = _mem.new_memory(0) if _PROTOTYPE: class _objarr(_gen.NDArray): def __init__(self, shape, objects=None): if objects is None: objects = [None] * _gen.product(shape) _gen.NDArray.__init__(self, shape=shape, buffer=_dummyBuffer, itemsize=1) self._objects = objects[:] def _getitem(self, offset): return self._objects[offset]