Skip to content
This repository was archived by the owner on Jan 10, 2023. It is now read-only.

Commit fd72748

Browse files
pymiamiachang
authored and
miachang
committed
add discard and remove function
1 parent 64ee083 commit fd72748

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

pygtrie.py

+26-4
Original file line numberDiff line numberDiff line change
@@ -1364,13 +1364,35 @@ def add(self, key):
13641364
self._trie[key:] = True
13651365

13661366
def discard(self, key):
1367-
raise NotImplementedError(
1368-
'Removing keys from PrefixSet is not implemented.')
1367+
try:
1368+
node, _ = self._get_node(key)
1369+
except:
1370+
pass
1371+
1372+
if node and node.children:
1373+
for child in node.children:
1374+
self.discard(child)
1375+
else:
1376+
node.children = {}
1377+
node.value = _SENTINEL
1378+
1379+
13691380

1381+
# Raises KeyError if elem is not contained in the set.
13701382
def remove(self, key):
1371-
raise NotImplementedError(
1372-
'Removing keys from PrefixSet is not implemented.')
1383+
try:
1384+
node, _ = self._get_node(key)
1385+
except KeyError:
1386+
return 0
1387+
1388+
if node and node.children:
1389+
for child in node.children:
1390+
self.discard(child)
1391+
else:
1392+
node.children = {}
1393+
node.value = _SENTINEL
13731394

13741395
def pop(self):
1396+
13751397
raise NotImplementedError(
13761398
'Removing keys from PrefixSet is not implemented.')

0 commit comments

Comments
 (0)