@@ -113,3 +113,55 @@ def print_usage():
113113 protocol_index , aggregator_index , accept_numerical_payload )
114114
115115 serialize_dataset (dataset )
116+
117+ class Trie (object ):
118+ def __init__ (self ):
119+ self ._trie_dict = dict ()
120+ self ._protocol_count = dict ()
121+
122+ def add (self , term ):
123+ if len (term ) == 0 :
124+ return
125+
126+ ch = term [0 ]
127+ next_level = self ._trie_dict .get (ch )
128+ if not next_level :
129+ next_level = Trie ()
130+ self ._trie_dict [ch ] = next_level
131+
132+ next_level .add (term [1 :])
133+
134+ def contains (self , term ):
135+ if len (term ) == 0 :
136+ return self
137+
138+ ch = term [0 ]
139+ next_level = self ._trie_dict .get (ch );
140+
141+ return next_level .contains (term [1 :]) if next_level else False
142+
143+ def inc_if_contains (self , term , protocol ):
144+ last_trie = self .contains (term )
145+ if not last_trie :
146+ return
147+
148+ count = last_trie ._protocol_count .get (protocol , 0 )
149+ last_trie ._protocol_count [protocol ] = count + 1
150+
151+ if len (term ) == 0 :
152+ return True
153+
154+ def get_protocol_counts (self , term ):
155+ trie = self .contains (term )
156+ return trie ._protocol_count if trie else dict ()
157+
158+ def get_protocol_count (self , term , protocol ):
159+ return self .get_protocol_counts (term ).get (protocol )
160+
161+ def print_protocol_counts (self , prefix = '' ):
162+ for protocol , count in self ._protocol_count .items ():
163+ print ('%s|%d|%s' % (protocol , count , prefix ))
164+
165+ for ch , trie in self ._trie_dict .items ():
166+ trie .print_protocol_counts (prefix + ch )
167+
0 commit comments