Merge "Extract Node superclass of Leaf and InteriorNode"
This commit is contained in:
@@ -14,12 +14,44 @@
|
|||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
"""Verify that one set of hidden API flags is a subset of another."""
|
"""Verify that one set of hidden API flags is a subset of another."""
|
||||||
|
import dataclasses
|
||||||
|
import typing
|
||||||
|
|
||||||
from itertools import chain
|
from itertools import chain
|
||||||
|
|
||||||
|
|
||||||
|
@dataclasses.dataclass()
|
||||||
|
class Node:
|
||||||
|
|
||||||
|
def values(self, selector):
|
||||||
|
"""Get the values from a set of selected nodes.
|
||||||
|
|
||||||
|
:param selector: a function that can be applied to a key in the nodes
|
||||||
|
attribute to determine whether to return its values.
|
||||||
|
|
||||||
|
:return: A list of iterables of all the values associated with
|
||||||
|
this node and its children.
|
||||||
|
"""
|
||||||
|
raise NotImplementedError("Please Implement this method")
|
||||||
|
|
||||||
|
def append_values(self, values, selector):
|
||||||
|
"""Append the values associated with this node and its children.
|
||||||
|
|
||||||
|
For each item (key, child) in nodes the child node's values are returned
|
||||||
|
if and only if the selector returns True when called on its key. A child
|
||||||
|
node's values are all the values associated with it and all its
|
||||||
|
descendant nodes.
|
||||||
|
|
||||||
|
:param selector: a function that can be applied to a key in the nodes
|
||||||
|
attribute to determine whether to return its values.
|
||||||
|
:param values: a list of a iterables of values.
|
||||||
|
"""
|
||||||
|
raise NotImplementedError("Please Implement this method")
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=line-too-long
|
# pylint: disable=line-too-long
|
||||||
class InteriorNode:
|
@dataclasses.dataclass()
|
||||||
|
class InteriorNode(Node):
|
||||||
"""An interior node in a trie.
|
"""An interior node in a trie.
|
||||||
|
|
||||||
Each interior node has a dict that maps from an element of a signature to
|
Each interior node has a dict that maps from an element of a signature to
|
||||||
@@ -45,16 +77,13 @@ class InteriorNode:
|
|||||||
^- class:UnicodeScript -> Node()
|
^- class:UnicodeScript -> Node()
|
||||||
^- member:of(I)Ljava/lang/Character$UnicodeScript;
|
^- member:of(I)Ljava/lang/Character$UnicodeScript;
|
||||||
-> Leaf([blocked,core-platform-api])
|
-> Leaf([blocked,core-platform-api])
|
||||||
|
|
||||||
Attributes:
|
|
||||||
nodes: a dict from an element of the signature to the Node/Leaf
|
|
||||||
containing the next element/value.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# pylint: enable=line-too-long
|
# pylint: enable=line-too-long
|
||||||
|
|
||||||
def __init__(self):
|
# A dict from an element of the signature to the Node/Leaf containing the
|
||||||
self.nodes = {}
|
# next element/value.
|
||||||
|
nodes: typing.Dict[str, Node] = dataclasses.field(default_factory=dict)
|
||||||
|
|
||||||
# pylint: disable=line-too-long
|
# pylint: disable=line-too-long
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@@ -198,53 +227,28 @@ class InteriorNode:
|
|||||||
return chain.from_iterable(node.values(selector))
|
return chain.from_iterable(node.values(selector))
|
||||||
|
|
||||||
def values(self, selector):
|
def values(self, selector):
|
||||||
""":param selector: a function that can be applied to a key in the nodes
|
|
||||||
|
|
||||||
attribute to determine whether to return its values.
|
|
||||||
|
|
||||||
:return: A list of iterables of all the values associated with
|
|
||||||
this node and its children.
|
|
||||||
"""
|
|
||||||
values = []
|
values = []
|
||||||
self.append_values(values, selector)
|
self.append_values(values, selector)
|
||||||
return values
|
return values
|
||||||
|
|
||||||
def append_values(self, values, selector):
|
def append_values(self, values, selector):
|
||||||
"""Append the values associated with this node and its children.
|
|
||||||
|
|
||||||
For each item (key, child) in nodes the child node's values are returned
|
|
||||||
if and only if the selector returns True when called on its key. A child
|
|
||||||
node's values are all the values associated with it and all its
|
|
||||||
descendant nodes.
|
|
||||||
|
|
||||||
:param selector: a function that can be applied to a key in the nodes
|
|
||||||
attribute to determine whether to return its values.
|
|
||||||
:param values: a list of a iterables of values.
|
|
||||||
"""
|
|
||||||
for key, node in self.nodes.items():
|
for key, node in self.nodes.items():
|
||||||
if selector(key):
|
if selector(key):
|
||||||
node.append_values(values, lambda x: True)
|
node.append_values(values, lambda x: True)
|
||||||
|
|
||||||
|
|
||||||
class Leaf:
|
|
||||||
"""A leaf of the trie
|
|
||||||
|
|
||||||
Attributes:
|
@dataclasses.dataclass()
|
||||||
value: the value associated with this leaf.
|
class Leaf(Node):
|
||||||
"""
|
"""A leaf of the trie"""
|
||||||
|
|
||||||
def __init__(self, value):
|
# The value associated with this leaf.
|
||||||
self.value = value
|
value: typing.Any
|
||||||
|
|
||||||
def values(self, selector): # pylint: disable=unused-argument
|
def values(self, selector):
|
||||||
""":return: A list of a list of the value associated with this node."""
|
|
||||||
return [[self.value]]
|
return [[self.value]]
|
||||||
|
|
||||||
def append_values(self, values, selector): # pylint: disable=unused-argument
|
def append_values(self, values, selector):
|
||||||
"""Appends a list of the value associated with this node to the list.
|
|
||||||
|
|
||||||
:param values: a list of a iterables of values.
|
|
||||||
"""
|
|
||||||
values.append([self.value])
|
values.append([self.value])
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user