Skip to content

SetTrie

settrie.SetTrie

Result(iter_id)

Container holding the results of several operations of SetTrie. It behaves, basically, like an iterator.

Source code in settrie/SetTrie.py
44
45
def __init__(self, iter_id):
    self.iter_id = iter_id

SetTrie(binary_image=None)

Mapping container for efficient storage of key-value pairs where the keys are sets. Uses an efficient trie implementation. Supports querying for values associated to subsets or supersets of stored key sets.

Example
>>> from mercury.dynamics.SetTrie import SetTrie
>>> s = SetTrie()
>>> s.insert({2,3}, 'id1')
>>> s.insert({2,3,4}, 'id2')
Source code in settrie/SetTrie.py
73
74
75
76
77
def __init__(self, binary_image=None):
    self.st_id = new_settrie()

    if binary_image is not None:
        self.load_from_binary_image(binary_image)

__getstate__()

Used by pickle.dump() (See https://docs.python.org/3/library/pickle.html)

Source code in settrie/SetTrie.py
82
83
84
85
def __getstate__(self):
    """ Used by pickle.dump() (See https://docs.python.org/3/library/pickle.html)
    """
    return self.save_as_binary_image()

__setstate__(state)

Used by pickle.load() (See https://docs.python.org/3/library/pickle.html)

Source code in settrie/SetTrie.py
87
88
89
90
91
def __setstate__(self, state):
    """ Used by pickle.load() (See https://docs.python.org/3/library/pickle.html)
    """
    self.st_id = new_settrie()
    self.load_from_binary_image(state)

find(set)

Finds the ID of the set matching the one provided.

Parameters:

Name Type Description Default
set

Set for searching

required

Returns:

Type Description
str

id of the set with the exact match. An empty string if no match was found.

Source code in settrie/SetTrie.py
102
103
104
105
106
107
108
109
110
def find(self, set) -> str:
    """ Finds the ID of the set matching the one provided.

    Args:
        set: Set for searching
    Returns:
        id of the set with the exact match. An empty string if no match was found.
    """
    return find(self.st_id, str(set))

insert(set, id)

Inserts a new set into a SetTrie object.

Parameters:

Name Type Description Default
set Set

Set to add

required
id str

String representind the ID for the test

required
Source code in settrie/SetTrie.py
 93
 94
 95
 96
 97
 98
 99
100
def insert(self, set: Set, id: str):
    """ Inserts a new set into a SetTrie object.

    Args:
        set: Set to add
        id: String representind the ID for the test
    """
    insert(self.st_id, str(set), id)

load_from_binary_image(binary_image)

Load the state of the c++ SetTrie object from a binary_image returned by a previous save_as_binary_image() call.

Parameters:

Name Type Description Default
binary_image

A list of strings returned by save_as_binary_image()

required

Returns:

Type Description

True on success, destroys, initializes and returns false on failure.

Source code in settrie/SetTrie.py
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
def load_from_binary_image(self, binary_image):
    """ Load the state of the c++ SetTrie object from a binary_image
        returned by a previous save_as_binary_image() call.

    Args:
        binary_image: A list of strings returned by save_as_binary_image()

    Returns:
        True on success, destroys, initializes and returns false on failure.
    """
    failed = False

    for binary_image_block in binary_image:
        if not push_binary_image_block(self.st_id, binary_image_block):
            failed = True
            break

    if not failed:
        failed = not push_binary_image_block(self.st_id, '')

    if failed:
        destroy_settrie(self.st_id)
        self.st_id = new_settrie()
        return False

    return True

save_as_binary_image()

Saves the state of the c++ SetTrie object as a Python list of strings referred to a binary_image.

Returns:

Type Description

The binary_image containing the state of the SetTrie. There is

not much you can do with it except serializing it as a Python

(e.g., pickle) object and loading it into another SetTrie object.

Pass it to the constructor to create an initialized object,

Source code in settrie/SetTrie.py
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
def save_as_binary_image(self):
    """ Saves the state of the c++ SetTrie object as a Python
        list of strings referred to a binary_image.

    Returns:
        The binary_image containing the state of the SetTrie. There is
        not much you can do with it except serializing it as a Python
        (e.g., pickle) object and loading it into another SetTrie object.
        Pass it to the constructor to create an initialized object,
    """
    bi_idx = save_as_binary_image(self.st_id)
    if bi_idx == 0:
        return None

    bi = []
    bi_size = binary_image_size(bi_idx)
    for t in range(bi_size):
        bi.append(binary_image_next(bi_idx))

    destroy_binary_image(bi_idx)

    return bi

subsets(set)

Find all the subsets for a given set.

Parameters:

Name Type Description Default
set

set for which we want to find all the supersets

required

Returns:

Type Description
Result

Iterator object with the IDs of the matching subsets.

Source code in settrie/SetTrie.py
123
124
125
126
127
128
129
130
131
132
def subsets(self, set) -> Result:
    """ Find all the subsets for a given set.

    Args:
        set: set for which we want to find all the supersets

    Returns:
        Iterator object with the IDs of the matching subsets.
    """
    return Result(subsets(self.st_id, str(set)))

supersets(set)

Find all the supersets of a given set.

Parameters:

Name Type Description Default
set

set for which we want to find all the supersets

required

Returns:

Type Description
Result

Iterator object with the IDs of the matching supersets.

Source code in settrie/SetTrie.py
112
113
114
115
116
117
118
119
120
121
def supersets(self, set) -> Result:
    """ Find all the supersets of a given set.

    Args:
        set: set for which we want to find all the supersets

    Returns:
        Iterator object with the IDs of the matching supersets.
    """
    return Result(supersets(self.st_id, str(set)))