dongtaogu8510 2017-04-05 03:45
浏览 49
已采纳

无法从空堆中提取

I am implementing a SplPriorityQueue within this class. The class contains methods to essentially put PDOStatement objects along with the correct priority. I then have a method that executes all PDOStatement objects in the queue.

When testing this class, I have pushed one PDOStatement object into the queue and then I print the results from $this->queries_to_run->extract(). Upon doing this I get my expected result, but also a RuntimeExeption with the message "Can't extract from an empty heap." I am aware that the SplPriorityQueue is essentially a heap under the hood, but I do not know why I am getting both my expected result and the exception.

Here is the subset of the bothersome PHP code (I have the extract flags set to 'both' upon initialization of the PriorityQueue):

$top = $this->queries_to_run->top();
//print_r($top); // Debug

while($top['priority'] === self::$UPDATE_PRIORITY || $top['priority'] === self::$REMOVAL_PRIORITY) {
    print("This is the extracted node
"); // Debug
    print_r($this->queries_to_run->extract()); // Debug
}

This is my output:

This is the extracted node Array ( [data] => PDOStatement Object ( [queryString] => [--omitted--] ) [priority] => 4 ) This is the extracted node

Fatal error: Uncaught exception 'RuntimeException' with message 'Can't extract from an empty heap' in ... on line 943

I hope I have provided enough information. I am a student, so I hope this problem isn't extremely obvious

  • 写回答

1条回答 默认 最新

  • douyao3895 2017-04-05 17:26
    关注

    The error was from not checking if the PriorityQueue was empty after each iteration. Simply inserting the following fixed it:

    $top = ($this->queries_to_run->isEmpty()) ? 0 : $this->queries_to_run->top();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看