Cidr4 merge algorithm #5

Merged
PavelPatsey merged 91 commits from CIDR4_merge_algorithm into main 2025-01-27 22:05:39 +03:00
Showing only changes of commit 55c71c6eb2 - Show all commits
+2 -20
View File
@@ -1,5 +1,7 @@
import cProfile import cProfile
from .util import cidr4_to_node, get_data, make_cidr4
Node = tuple[int, int] Node = tuple[int, int]
@@ -7,20 +9,6 @@ class Cidr4MergerError(Exception):
pass pass
def get_data(input_file):
with open(input_file, "r") as file:
data = file.read().splitlines()
return data
def cidr4_to_node(cidr4: str) -> Node:
ip_address, mask_len = cidr4.strip().split("/")
a, b, c, d = list(map(int, ip_address.split(".")))
ip = a * 256**3 + b * 256**2 + c * 256**1 + d * 256**0
mask_len = int(mask_len)
return ip, mask_len
def sort_nodes(nodes: list[Node]) -> list[Node]: def sort_nodes(nodes: list[Node]) -> list[Node]:
return sorted(nodes) return sorted(nodes)
@@ -64,12 +52,6 @@ def merge_two_nodes(node_a: Node, node_b: Node) -> tuple[Node, int]:
return parent_node, dip return parent_node, dip
def make_cidr4(ip, mask_len) -> str:
lst = [str(ip >> (i << 3) & 0xFF) for i in reversed(range(4))]
ip_address = ".".join(lst)
return f"{ip_address}/{mask_len}"
def merge_nodes(nodes: list[Node], required_len: int) -> tuple[list[Node], int]: def merge_nodes(nodes: list[Node], required_len: int) -> tuple[list[Node], int]:
sum_dip = 0 sum_dip = 0
while len(nodes) > required_len: while len(nodes) > required_len: