Remove list usage for IpTreeNode

This commit is contained in:
Geoffrey Frogeye 2019-12-16 06:54:18 +01:00
parent a0e68f0848
commit 3197fa1663
Signed by: geoffrey
GPG key ID: D8A7ECA00A8CD3DD

View file

@ -88,7 +88,8 @@ class DomainTreeNode():
class IpTreeNode(): class IpTreeNode():
def __init__(self) -> None: def __init__(self) -> None:
self.children: typing.List[typing.Optional[IpTreeNode]] = [None, None] self.zero: typing.Optional[IpTreeNode] = None
self.one: typing.Optional[IpTreeNode] = None
self.match = Match() self.match = Match()
@ -131,7 +132,7 @@ class Profiler():
class Database(Profiler): class Database(Profiler):
VERSION = 10 VERSION = 11
PATH = "blocking.p" PATH = "blocking.p"
def initialize(self) -> None: def initialize(self) -> None:
@ -258,7 +259,7 @@ class Database(Profiler):
callback(_par, _dic, arg) callback(_par, _dic, arg)
# 0 # 0
dic = _dic.children[0] dic = _dic.zero
if dic: if dic:
addr0 = _par.value & (0xFFFFFFFF ^ (1 << (32-_par.prefixlen))) addr0 = _par.value & (0xFFFFFFFF ^ (1 << (32-_par.prefixlen)))
assert addr0 == _par.value assert addr0 == _par.value
@ -269,7 +270,7 @@ class Database(Profiler):
_par=Ip4Path(addr0, _par.prefixlen+1) _par=Ip4Path(addr0, _par.prefixlen+1)
) )
# 1 # 1
dic = _dic.children[1] dic = _dic.one
if dic: if dic:
addr1 = _par.value | (1 << (32-_par.prefixlen)) addr1 = _par.value | (1 << (32-_par.prefixlen))
yield from self.exec_each_ip4( yield from self.exec_each_ip4(
@ -351,7 +352,7 @@ class Database(Profiler):
self.enter_step('get_ip4_yield') self.enter_step('get_ip4_yield')
yield Ip4Path(ip4.value, 32-i) yield Ip4Path(ip4.value, 32-i)
self.enter_step('get_ip4_brws') self.enter_step('get_ip4_brws')
next_dic = dic.children[part] next_dic = dic.one if part else dic.zero
if next_dic is None: if next_dic is None:
return return
dic = next_dic dic = next_dic
@ -432,10 +433,13 @@ class Database(Profiler):
if dic.match.active(): if dic.match.active():
# Refuse to add ip4* whose network is already matching # Refuse to add ip4* whose network is already matching
return return
next_dic = dic.children[part] next_dic = dic.one if part else dic.zero
if next_dic is None: if next_dic is None:
next_dic = IpTreeNode() next_dic = IpTreeNode()
dic.children[part] = next_dic if part:
dic.one = next_dic
else:
dic.zero = next_dic
dic = next_dic dic = next_dic
dic.match.set( dic.match.set(
updated, updated,