WIP set.py

This commit is contained in:
2026-07-07 21:00:25 +03:00
parent f7671826d4
commit 6fb676978f
2 changed files with 65 additions and 1 deletions
+64
View File
@@ -0,0 +1,64 @@
# 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"