本文整理汇总了Python中trie.Trie.remove_one方法的典型用法代码示例。如果您正苦于以下问题:Python Trie.remove_one方法的具体用法?Python Trie.remove_one怎么用?Python Trie.remove_one使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类trie.Trie
的用法示例。
在下文中一共展示了Trie.remove_one方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: LBSClientManager
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import remove_one [as 别名]
class LBSClientManager(object):
def __init__(self):
self.trie = Trie(4, ord('0'))
self.max_len = 20
self.clients = {}
def show_all_clients(self):
print "show all clients"
objs = self.trie.root.get_offsprings()
for o in objs:
print o.longitude, o.latitude
def get(self, id):
return self.clients.get(id)
def get_prefix(self, client):
lon = client.longitude
lat = client.latitude
if lon < -180:
lon = -180.0
if lon > 180:
lon = 180.0
if lat > 90:
lat = 90.0
if lat < -90:
lat = -90.0
lon_min = -180.0
lon_max = 180.0
lat_min = -90.0
lat_max = 90.0
ret = ''
for i in range(self.max_len):
cur = 0
lon_mid = (lon_min + lon_max) / 2
lat_mid = (lat_min + lat_max) / 2
#print lon_min, lon_mid, lon_max
#print lat_min, lat_mid, lat_max
if lat < lat_mid:
cur += 2
lat_max = lat_mid
else:
lat_min = lat_mid
if lon > lon_mid:
cur += 1
lon_min = lon_mid
else:
lon_max = lon_mid
ret += str(cur)
return ret
def add_client(self, client):
if client.id in self.clients:
logger.error("id %d already in" % client.id)
return
logger.debug("add client longitude %f latitude %f id %d" % (client.longitude, client.latitude, client.id))
prefix = self.get_prefix(client)
logger.debug("client %d prefix %s" % (client.id, prefix))
self.trie.add_one(prefix, client)
self.clients[client.id] = client
def remove_client(self, client):
if client.id not in self.clients:
logger.error("id %d not in")
return
prefix = self.get_prefix(client)
logger.debug("remove client %d longitude %f latitude %f prefix %s" % (client.id, client.longitude, client.latitude, prefix))
self.trie.remove_one(prefix, client)
del self.clients[client.id]
def get_left(self, prefix_num, len):
if len <= 0:
return -1
if prefix_num == -1:
return -1
m = prefix_num % 4
if m == 1 or m == 3:
return prefix_num - 1
up = self.get_left(prefix_num >> 2, len - 1)
if up < 0:
return -1
cur = up << 2
return cur + m + 1
def get_right(self, prefix_num, len):
if len <= 0:
return -1
if prefix_num == -1:
return -1
m = prefix_num % 4
if m == 0 or m == 2:
return prefix_num + 1
up = self.get_right(prefix_num >> 2, len - 1)
if up < 0:
return -1
cur = up << 2
#.........这里部分代码省略.........