diff --git a/Docs/set:version.md b/Docs/set:version.md index fcf306c..982c3cb 100644 --- a/Docs/set:version.md +++ b/Docs/set:version.md @@ -129,7 +129,7 @@ unsigned *v = {1, 4, 16, 22}; Пример результирующего массива: ```c -unsigned *v = {1, 3, 12, 8}; +unsigned *v = {1, 3, 12, 6}; ``` ### Rice-Golomb diff --git a/reimplement/set.py b/reimplement/set.py new file mode 100644 index 0000000..fd67a59 --- /dev/null +++ b/reimplement/set.py @@ -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"