intポインタをPythonで配列の様に扱う。
こんな感じ。だけど、微妙に釈然としない感じ。
コレくらいは、標準搭載されてても良いんじゃないのかなぁ。
ちなみに、このコードだとデータの書きこみは出来ませぬ。
cdef class IntArrayIterator: cdef int index cdef int length cdef array def __init__(self, array): self.index = 0 self.length = len(array) self.array = array def __next__(self): if(self.index < self.length): pos = self.index self.index = self.index + 1 return self.array[pos] raise StopIteration def __iter__(self): return self cdef class IntArray: cdef int length cdef int *data #http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/version/Doc/Manual/special_methods.html def __len__(self): return self.length def __iter__(self): return IntArrayIterator(self) cdef int getitem(self, int x): if(x < self.length): return self.data[x] raise IndexError, "index out of range %s" % x def __getitem__(self, int x): return self.getitem(x) def __getslice__(self,int f,int t): if(f < t and f < self.length and t < self.length): a = [] for i in range(t-f): c = f + i a.append(self.getitem(c)) return a raise IndexError, "index out of range [%(from)s:%(to)s]" % {"from":f,"to":t} def __str__(self): cdef r = [] cdef int i = 0 r.append("[") for i in range(self.length): r.append(str(self.getitem(i))) if(i + 1 < self.length): r.append(",") r.append("]") return "".join(r)