66from pykit import rangeset
77from pykit .dictutil import FixedKeysDict
88
9- from .block_desc import BlockDesc
109from .block_id import BlockID
1110
1211MERGE_COEF = 4
@@ -37,11 +36,11 @@ def _levels(levels=None):
3736 if levels is None :
3837 levels = []
3938
40- for level , blocks in enumerate (levels ):
41- for b in blocks :
42- b [2 ] = BlockDesc (b [2 ])
39+ for level , range_bids in enumerate (levels ):
40+ for b in range_bids :
41+ b [2 ] = BlockID (b [2 ])
4342
44- levels [level ] = rangeset .RangeDict (blocks )
43+ levels [level ] = rangeset .RangeDict (range_bids )
4544
4645 return levels
4746
@@ -54,16 +53,6 @@ class Region(FixedKeysDict):
5453 levels = _levels ,
5554 )
5655
57- def need_merge (self , source , targets ):
58-
59- src_blk_size = source [2 ]['size' ]
60-
61- target_blk_size = 0
62- for target in targets :
63- target_blk_size += target [2 ]['size' ]
64-
65- return MERGE_COEF * src_blk_size >= target_blk_size
66-
6756 def find_moved_to_level (self , src_block , src_level , region_levels ):
6857
6958 for level in reversed (range (src_level )):
@@ -102,31 +91,12 @@ def move_down(self):
10291
10392 return moved_blocks
10493
105- def find_merge (self ):
106-
107- region_levels = self ['levels' ]
108-
109- for level , src_blocks in enumerate (region_levels ):
110- if level == 0 :
111- continue
112-
113- lower_blocks = region_levels [level - 1 ]
114-
115- for src in src_blocks :
116- overlapped = lower_blocks .find_overlapped (src )
117-
118- if len (overlapped ) == 0 :
119- continue
120-
121- if self .need_merge (src , overlapped ):
122- return (level , src , overlapped )
123-
12494 def list_block_ids (self , start_block_id = None ):
12595
12696 block_ids = []
12797
12898 for blocks in self ['levels' ]:
129- level_bids = [b [2 ][ 'block_id' ] for b in blocks ]
99+ level_bids = [b [2 ] for b in blocks ]
130100 block_ids .extend (level_bids )
131101
132102 block_ids .sort ()
@@ -145,13 +115,13 @@ def replace_block_id(self, block_id, new_block_id):
145115 for blocks in self ['levels' ]:
146116
147117 for block in blocks :
148- if block [2 ][ 'block_id' ] == block_id :
149- block [2 ][ 'block_id' ] = new_block_id
118+ if block [2 ] == block_id :
119+ block [2 ] = new_block_id
150120 return
151121
152122 raise BlockNotInRegion ('block_id: %s' % str (block_id ))
153123
154- def delete_block (self , block , active_range = None , level = None , move = True ):
124+ def delete_block (self , block_id , active_range = None , level = None , move = True ):
155125 region_levels = self ['levels' ]
156126
157127 for lvl , level_blocks in enumerate (region_levels ):
@@ -164,7 +134,7 @@ def delete_block(self, block, active_range=None, level=None, move=True):
164134 if active_range is not None and active_range != blk [:2 ]:
165135 continue
166136
167- if blk [2 ] == block :
137+ if blk [2 ] == block_id :
168138 level_blocks .remove (blk )
169139 break
170140 else :
@@ -175,13 +145,13 @@ def delete_block(self, block, active_range=None, level=None, move=True):
175145
176146 return
177147
178- raise BlockNotInRegion ('block : {bi }' .format (bi = block ))
148+ raise BlockNotInRegion ('block_id : {bid }' .format (bid = block_id ))
179149
180- def add_block (self , active_range , block , level = None , allow_exist = False ):
150+ def add_block (self , active_range , block_id , level = None , allow_exist = False ):
181151
182- if self .has (block ):
152+ if self .has (block_id ):
183153 if not allow_exist :
184- raise BlockAreadyInRegion ('block {bi }' .format (bi = block ))
154+ raise BlockAreadyInRegion ('block_id {bid }' .format (bid = block_id ))
185155 return
186156
187157 max_level = len (self ['levels' ]) - 1
@@ -196,8 +166,7 @@ def add_block(self, active_range, block, level=None, allow_exist=False):
196166 if level == max_level + 1 :
197167 self ['levels' ].append (rangeset .RangeDict ())
198168
199- desc = BlockDesc (block )
200- self ['levels' ][level ].add (active_range , desc )
169+ self ['levels' ][level ].add (active_range , BlockID (block_id ))
201170
202171 def get_block_ids_by_needle_id (self , needle_id ):
203172
@@ -215,30 +184,34 @@ def get_block_ids_by_needle_id(self, needle_id):
215184 for level in reversed (levels ):
216185
217186 try :
218- block_desc = level .get (needle_id )
187+ block_id = level .get (needle_id )
219188 except KeyError :
220189 continue
221190
222- rst .append (block_desc [ ' block_id' ] )
191+ rst .append (block_id )
223192
224193 return rst
225194
226- def has (self , block , active_range = None ):
195+ def has (self , block_id , active_range = None ):
227196 for blocks in self ['levels' ]:
228197 for blk in blocks :
229198
230199 if active_range is not None and active_range != blk [:2 ]:
231200 continue
232201
233- if blk [2 ] == block :
202+ if blk [2 ] == block_id :
234203 return True
235204
236205 return False
237206
238- def get_block_byid (self , block_id , raise_error = True ):
207+ def get_block_byid (self , block_id , active_range = None , raise_error = True ):
239208 for blocks in self ['levels' ]:
240209 for blk in blocks :
241- if blk [2 ]["block_id" ] == block_id :
210+
211+ if active_range is not None and active_range != blk [:2 ]:
212+ continue
213+
214+ if blk [2 ] == block_id :
242215 return blk
243216
244217 if raise_error :
0 commit comments