|
| 1 | +# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 2 | +# See https://llvm.org/LICENSE.txt for license information. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +from .circt import ir, support |
| 8 | +from .rtg import rtg |
| 9 | +from .core import Value |
| 10 | + |
| 11 | + |
| 12 | +class Set(Value): |
| 13 | + """ |
| 14 | + Represents a statically typed set for any kind of values that allows picking |
| 15 | + elements at random. |
| 16 | + """ |
| 17 | + |
| 18 | + def __init__(self, value: ir.Value) -> Set: |
| 19 | + """ |
| 20 | + Intended for library internal usage only. |
| 21 | + """ |
| 22 | + |
| 23 | + self._value = value |
| 24 | + |
| 25 | + def create_empty(elementType: ir.Type) -> Set: |
| 26 | + """ |
| 27 | + Create an empty set that can hold elements of the provided type. |
| 28 | + """ |
| 29 | + |
| 30 | + return rtg.SetCreateOp(rtg.SetType.get(elementType), []) |
| 31 | + |
| 32 | + def create(*elements: Value) -> Set: |
| 33 | + """ |
| 34 | + Create a set containing the provided values. At least one element must be |
| 35 | + provided. |
| 36 | + """ |
| 37 | + |
| 38 | + assert len( |
| 39 | + elements) > 0, "use 'create_empty' to create sets with no elements" |
| 40 | + assert all([e.get_type() == elements[0].get_type() for e in elements |
| 41 | + ]), "all elements must have the same type" |
| 42 | + return rtg.SetCreateOp(rtg.SetType.get(elements[0].get_type()), elements) |
| 43 | + |
| 44 | + def __add__(self, other: Value) -> Set: |
| 45 | + """ |
| 46 | + If another set is provided their types must match and a new Set will be |
| 47 | + returned containing all elements of both sets (set union). If a value that |
| 48 | + is not a Set is provided, it must match the element type of this Set. A new |
| 49 | + Set will be returned containing all elements of this Set plus the provided |
| 50 | + value. |
| 51 | + """ |
| 52 | + |
| 53 | + if isinstance(other, Set): |
| 54 | + assert self.get_type() == other.get_type( |
| 55 | + ), "sets must be of the same type" |
| 56 | + return rtg.SetUnionOp([self._value, other._value]) |
| 57 | + |
| 58 | + assert support.type_to_pytype( |
| 59 | + self.get_type()).element_type == other.get_type( |
| 60 | + ), "type of the provided value must match element type of the set" |
| 61 | + return self + Set.create(other) |
| 62 | + |
| 63 | + def __sub__(self, other: Value) -> Set: |
| 64 | + """ |
| 65 | + If another set is provided their types must match and a new Set will be |
| 66 | + returned containing all elements in this Set that are not contained in the |
| 67 | + 'other' Set. If a value that is not a Set is provided, it must match the |
| 68 | + element type of this Set. A new Set will be returned containing all |
| 69 | + elements of this Set except the provided value in that case. |
| 70 | + """ |
| 71 | + |
| 72 | + if isinstance(other, Set): |
| 73 | + assert self.get_type() == other.get_type( |
| 74 | + ), "sets must be of the same type" |
| 75 | + return rtg.SetDifferenceOp(self._value, other._value) |
| 76 | + |
| 77 | + assert support.type_to_pytype( |
| 78 | + self.get_type()).element_type == other.get_type( |
| 79 | + ), "type of the provided value must match element type of the set" |
| 80 | + return self - Set.create(other) |
| 81 | + |
| 82 | + def get_random(self) -> Value: |
| 83 | + """ |
| 84 | + Returns an element from the set picked uniformly at random. If the set is |
| 85 | + empty, calling this method is undefined behavior. |
| 86 | + """ |
| 87 | + |
| 88 | + return rtg.SetSelectRandomOp(self._value) |
| 89 | + |
| 90 | + def get_random_and_exclude(self) -> Value: |
| 91 | + """ |
| 92 | + Returns an element from the set picked uniformly at random and removes it |
| 93 | + from the set. If the set is empty, calling this method is undefined |
| 94 | + behavior. |
| 95 | + """ |
| 96 | + |
| 97 | + r = self.get_random() |
| 98 | + self = self - r |
| 99 | + return r |
| 100 | + |
| 101 | + def _get_ssa_value(self) -> ir.Value: |
| 102 | + return self._value |
| 103 | + |
| 104 | + def get_type(self) -> ir.Type: |
| 105 | + return self._value.type |
0 commit comments