Python Deque Array添加到数组时使python崩溃

问题描述 投票:1回答:1

我正在python中处理双端队列,除了我的preappend(添加到最前面)方法外,其他一切似乎都工作正常。当我在main中调用此方法时,它使python崩溃,并且我对为什么感到超级困惑,这是我的代码:

import ctypes

class dequeArray:

def __init__(self):
    """Create an empty Array """
    self._capacity = 4
    self._data = self.makeArray(self._capacity)
    self._dataSize = 0
    self._front = 0

def makeArray(self, capacity):
    capacity = self._capacity
    return (self._capacity * ctypes.py_object)()

def isEmpty(self):
    return self._dataSize == 0

def __len__(self):
    return self._dataSize

def _userIndex2BlockIndex(self, userIndex):
    return (self._front + userIndex)% self._capacity

def __getitem__(self, userIndex):
    return self._data[userIndex]

def __setitem__(self, userIndex, value):
    self._data[self._front(userIndex)] = value

def preappend(self, item):
    if self._dataSize == 0:
        self._data[self._front] = item
        self._dataSize += 1

    elif self._dataSize != self._capacity:
        for e in range(self._dataSize-1,0,-1):
            self._data[e] = self._data[e-1]
        self._data[self._front] = item
        self._dataSize += 1
    else:
         for e in range(self._capacity-1,0,-1):
            self._data[e] = self._data[e-1]
         self._data[self._front] = item

主要是创建一个空的双端队列d = dequeArray()

然后测试len(d),效果很好,但是当我这样做时d.preappend(2)它使python崩溃...请帮助

python arrays deque
1个回答
0
投票

通常,与普通Python对象一起使用时,deques()绝不会崩溃。

使用ctypes,所有投注均关闭,因为C调用通过了所有不变性检查,类型安全性检查,指针/索引范围检查等。

双端队列仅访问使用ctypes创建的对象的有限功能。至少,它需要支持引用计数。要显示,它需要repr。要删除()或索引(),它需要支持eq()。

© www.soinside.com 2019 - 2024. All rights reserved.