-
Notifications
You must be signed in to change notification settings - Fork 132
Implement discard and remove function #19
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1364,13 +1364,35 @@ def add(self, key): | |
self._trie[key:] = True | ||
|
||
def discard(self, key): | ||
raise NotImplementedError( | ||
'Removing keys from PrefixSet is not implemented.') | ||
try: | ||
node, _ = self._get_node(key) | ||
except: | ||
pass | ||
|
||
if node and node.children: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don’t understand why you have this special case. |
||
for child in node.children: | ||
self.discard(child) | ||
else: | ||
node.children = {} | ||
node.value = _SENTINEL | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This leaves out empty nodes which don’t lead to anything. For example, if you first add
Only
|
||
|
||
|
||
|
||
# Raises KeyError if elem is not contained in the set. | ||
def remove(self, key): | ||
raise NotImplementedError( | ||
'Removing keys from PrefixSet is not implemented.') | ||
try: | ||
node, _ = self._get_node(key) | ||
except KeyError: | ||
return 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Uh? Why are you catching the exception? It should be propagated. More importantly, why are you duplicating the implementation? It’s much better to implement
|
||
|
||
if node and node.children: | ||
for child in node.children: | ||
self.discard(child) | ||
else: | ||
node.children = {} | ||
node.value = _SENTINEL | ||
|
||
def pop(self): | ||
|
||
raise NotImplementedError( | ||
'Removing keys from PrefixSet is not implemented.') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clearly, this
pass
should bereturn
, no?