本文整理汇总了Python中bintrees.FastRBTree.pop_min方法的典型用法代码示例。如果您正苦于以下问题:Python FastRBTree.pop_min方法的具体用法?Python FastRBTree.pop_min怎么用?Python FastRBTree.pop_min使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bintrees.FastRBTree
的用法示例。
在下文中一共展示了FastRBTree.pop_min方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: range
# 需要导入模块: from bintrees import FastRBTree [as 别名]
# 或者: from bintrees.FastRBTree import pop_min [as 别名]
#! /usr/bin/env python
# coding:utf-8
from __future__ import division
import heapq
import bintrees
import random
if __name__ == '__main__':
from benchmarker import Benchmarker
from itertools import repeat, izip
from bintrees import FastRBTree
# initialize heapq
h = range(10000)
heapq.heapify(h)
# initialize AVLTree
m = izip(xrange(10000), repeat(True))
t = FastRBTree(m)
for bm in Benchmarker(width=20, loop=100000, cycle=3, extra=1):
for _ in bm.empty():
pass
for _ in bm('heapq'):
heapq.heappop(h)
heapq.heappush(h, random.randint(-100000, 100000))
for _ in bm('FastRBTree'):
t.pop_min()
t[random.randint(-100000, 100000)] = True
示例2: OrderQueue
# 需要导入模块: from bintrees import FastRBTree [as 别名]
# 或者: from bintrees.FastRBTree import pop_min [as 别名]
class OrderQueue(object):
"""
"""
def __init__(self):
self.Orders = FastRBTree()
self.totalAmount = 0.0
def append(self,order):
k = order.timestamp
self.Orders.insert(k,order)
self.totalAmount += self.Orders[k].amount
def count(self):
return self.Orders.count
def min_item(self):
return self.Orders.min_item()
def eat(self,amount):
"""
use this only if amount <= self.amount
return OrderToPop,RestAmount
"""
current_amount = amount
to_pop_orders = []
while self.totalAmount>0 and current_amount>0:
if not self.Orders or self.Orders.count==0:
#print "amount:%.4f" % self.totalAmount
self.totalAmount = 0.0
return []
min_item = self.Orders.min_item()
min_i = min_item[1]
# 1.enough
# 2.not enough
if min_i.amount <= current_amount:
current_amount -= min_i.amount
self.totalAmount -= min_i.amount
to_pop_orders.append(self.Orders.pop_min()[1])
continue
# this is a hard case , need to take care of
# first we split the order , take amount from current order
# and make
elif min_i.amount > current_amount:
new_order = min_i.copy()
new_order.amount = current_amount
to_pop_orders.append(new_order)
min_i.amount -= current_amount
self.totalAmount -= current_amount
current_amount = 0
break
# return:
# 1.orders
# 2.amount to trade
# 3.amount left
return to_pop_orders
def remove(self,order):
k = order.timestamp
self.totalAmount -= self.Orders[k].amount
self.Orders.remove(k)
def get_price_depth(self):
return self.totalAmount
def is_empty(self):
return self.Orders.count == 0