65 lines
1.6 KiB
Python
65 lines
1.6 KiB
Python
# set.py - base64, golomb and set-string routines. analog set.c used in alt-rpm
|
|
|
|
import hashlib
|
|
from typing import List
|
|
from typing import Tuple
|
|
from dataclasses import dataclass
|
|
|
|
|
|
@dataclass
|
|
class Set:
|
|
"""just a bag of strings and their hash values.
|
|
|
|
Attributes:
|
|
count (int): The number of elements in the set.
|
|
labels (List[Tuple[str, int]]): A list of tuples, where each tuple contains a string and its corresponding hash value.
|
|
"""
|
|
|
|
count: int
|
|
labels: List[Tuple[str, int]] # List of tuples (string, hash value)
|
|
|
|
|
|
def rpmsetcmp():
|
|
pass
|
|
|
|
|
|
def set_new() -> Set:
|
|
"""Create a new Set instance with count initialized to 0 and an empty list of labels."""
|
|
return Set(count=0, labels=[])
|
|
|
|
|
|
def set_add(set: Set, label: str) -> None:
|
|
"""Add a label to the set."""
|
|
hash_value = hash(label)
|
|
set.labels.append((label, hash_value))
|
|
set.count += 1
|
|
|
|
|
|
def set_free(set: Set) -> None:
|
|
"""Free the resources associated with the set."""
|
|
set.labels.clear()
|
|
set.count = 0
|
|
|
|
|
|
def hash(label: str) -> int:
|
|
"""Compute a stable 64-bit hash value for an ASCII label."""
|
|
digest = hashlib.blake2b(label.encode("ascii"), digest_size=8).digest()
|
|
return int.from_bytes(digest, byteorder="little", signed=False)
|
|
|
|
|
|
def set_fini(set: Set, bpp: int) -> str | None:
|
|
"""Finalize the set and return a set-string representation."""
|
|
|
|
if set.count < 1:
|
|
return None
|
|
|
|
# bpp < 10 in original code
|
|
if bpp < 2:
|
|
return None
|
|
|
|
if bpp > 64:
|
|
return None
|
|
|
|
# Placeholder implementation - replace with actual finalization logic
|
|
return f"Set with {set.count} elements"
|