WIP set.py
This commit is contained in:
+1
-1
@@ -129,7 +129,7 @@ unsigned *v = {1, 4, 16, 22};
|
|||||||
Пример результирующего массива:
|
Пример результирующего массива:
|
||||||
|
|
||||||
```c
|
```c
|
||||||
unsigned *v = {1, 3, 12, 8};
|
unsigned *v = {1, 3, 12, 6};
|
||||||
```
|
```
|
||||||
|
|
||||||
### Rice-Golomb
|
### Rice-Golomb
|
||||||
|
|||||||
@@ -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"
|
||||||
Reference in New Issue
Block a user