class Histogram: # Creates a histogram containing the given categories def __init__(self, catSeq): self._freqCounts = HashMap() for cat in catSeq: self._freqCounts.add(cat, 0) # Returns the frequency count for the given category def getCount(self, category): assert category in self._freqCounts, "Invalid histogram category" return self._freqCounts.valueOf(category) # Increments the counter for the given category def incCount(self, category): assert category in self._freqCounts, "Invalid histogram category" value = self._freqCounts.valueOf(category) self._freqCounts.add(category, value + 1) # Returns the sum of the frequency counts def totalCount(self): total = 0 for cat in self._freqCounts: total += self._freqCounts.valueOf(cat) return total # Returns an iterator for traversing the categories def __iter__(self): return iter(self._freqCounts)
class Histogram: """ Implementation of the Histogram ADT using a Hash Map """ def __init__(self, catSeq): """ Creates a histogram containing the given categories """ self._freqCounts = HashMap() for cat in catSeq: self._freqCounts.add(cat, 0) def getCount(self, category): """ Returns the frequency count for the given category. """ assert category in self._freqCounts, "Invalid histogram category." return self._freqCounts.valueOf(category) def incCount(self, category): """ Increments the counter for the given category """ assert category in self._freqCounts, "Invalid histogram category." value = self._freqCounts.valueOf(category) self._freqCounts.add(category, value + 1) def totalCount(self): """ Returns the sum of the frequency counts. """ total = 0 for cat in self._freqCounts: total += self._freqCounts.valueOf(cat) return total def __iter__(self): """ Returns an iterator for traversing the categories """ return iter(self._freqCounts)
class Histogram(object): # Create a histogram containing the given categories. def __init__(self, catSeq): self._freqCounts = HashMap() for cat in catSeq: self._freqCounts.add(cat, 0) # Return the frequency count for the given category. def getCount(self, category): assert category in self._freqCounts, 'Invalid histogram category.' return self._freqCounts.valueOf(category) # Increment the counter for the given category. def incCount(self, category): assert category in self._freqCounts, 'Invalid histogram category.' value = self._freqCounts.valueOf(category) self._freqCounts.add(category, value + 1) # Return the sum of the frequency counts. def totalCount(self): total = 0 for cat in self._freqCounts: total += self._freqCounts.valueOf(cat) return total # Return an iterator for traversing the categories. def __iter__(self): return iter(self._freqCounts)
def get_scene_find_xml(xml, scene_id): """ 解析XML,判断是否有重复的监控项,如果正常保存场景与监控项的关联关系 :param xml: :param scene_id: :return: """ root_a = ElementTree.XML(xml) parent = root_a.find("root", "mxGraphModel") list = parent._children # 判断绑定的监控项是否有重复 items_map = HashMap() # 批量入库数组 add_scene_item_list = [] for dto in list: if dto.tag == "object": dto_item_id = str(dto.attrib.get("item_id")) # 关联的监控项id非数字直接进入下一个循环 if dto_item_id.isdigit() is False: continue int_item_id = int(dto_item_id) item_name = str(dto.attrib.get("label")) # 添加了重复的监控项,直接返回提示 if items_map.get(int_item_id) is not None: return item_name items_map.add(int_item_id, item_name) temp_dto = Scene_monitor.objects.filter(scene_id=scene_id, item_id=int_item_id) if temp_dto.count() == 0: # 只有等于零时才新增 scene_dto = Scene_monitor() scene_dto.item_id = int_item_id scene_dto.scene_id = scene_id scene_dto.x = 0 scene_dto.y = 0 scene_dto.scale = 0.0 scene_dto.score = 0 scene_dto.order = 0 add_scene_item_list.append(scene_dto) # scene_dto.save() print "场景 " + str(scene_id) + " " + dto_item_id + "保存成功" else: print "场景 " + str(scene_id) + " " + dto_item_id + "已存在,不处理" # 如果数组不为空,执行批量入库 if add_scene_item_list.__len__() > 0: Scene_monitor.objects.bulk_create(add_scene_item_list) return "true"
class Histogram: def __init__( self, catSeq ): self._freqCounts = HashMap() for cat in catSeq: self._freqCounts.add( cat, 0 ) def getCount( self, category ): assert category in self._freqCounts, "Invalid histogram category." return self._freqCounts.valueOf( category ) def incCount( self, category ): assert category in self._freqCounts, "Invalid histogram category." value = self._freqCounts.valueOf( category ) self._freqCounts.add( category, value + 1 ) def totalCount( self ): total = 0 for cat in self._freqCounts: total += self._freqCounts.valueOf( cat ) return total def __iter__( self ): return iter( self._freqCounts )
First run some small tests to make sure basic operations in hashmap work correctly. Then run a large test to examine the number of collisions when different hash and collision resolution methods are used. """ # Create a hashmap myMap = HashMap() # Keys and values for testing keys = [ 6, 13, 100, 29, 12, 5, 15, 18, 101, 56 ] values = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ] # Test insertion for i in range( len( keys ) ): print( 'inserting ', keys[ i] , '/', values[ i] ) myMap.add( keys[ i ], values[ i ] ) # (key, value) pair # Test the iterator print( 'original map : ' ) for v in myMap: print( '[', v.key, '/', v.value, ']', end = ' ' ) print() # Test search print( 'value of key 6 should be 0, it is ', myMap.valueOf( 6 ) ) print( 'value of key 56 should be 9, it is ', myMap.valueOf( 56 ) ) print( 'value of key 12 should be 4, it is ', myMap.valueOf( 12 ) ) # Test removal myMap.remove( 100 ) myMap.remove( 56 )
#!/usr/bin/python # -*- coding: utf-8 -*- from hashmap import HashMap h = HashMap(4) # 4 cells h.add("erkan", "444-44-44") h.add("maxime", "333-33-33") h.add("bob", "555-55-55") h.add("mike", "666-66-66") h.add("aude", "777-77-77") h.add("kosta", "888-88-88") h.get("mike") h.delete("bob") h.show()
def checkMintorDir(app_dir, mintor_dir_path, mintor_md5_filepath): print "checkMintorDir()...%s from file: %s where module is:%s" % ( mintor_dir_path, mintor_md5_filepath, app_dir) if not app_dir or not mintor_dir_path or not mintor_md5_filepath: print "###########ERROR###############" print "checkMintorDir param error app_dir:%s mintor_dir_path:%s mintor_md5_filepath:%s" % ( app_dir, mintor_dir_path, mintor_md5_filepath) return pass # 读区已经压缩过文件和md5到hashmap md5HashMap = HashMap() if os.path.exists(mintor_md5_filepath): for line in open(mintor_md5_filepath): # print line line = line.replace('\r', '').replace('\n', '').replace('\t', '') lineItems = line.rsplit('--MD5-->', 2) #print "%s %s" %(lineItems[0],lineItems[1]) md5HashMap.add(lineItems[0], lineItems[1]) pass else: f = open(mintor_md5_filepath, 'w') f.close() pass # 遍历被监控的文件夹,计算新的md5值和老的md5Hashmap值是否相等 for dirpath, dirnames, filenames in os.walk(mintor_dir_path): # print "dirpath:%s dirnames:%s filenames:%s" %(dirpath,dirnames,filenames) # if "build" not in dirpath and "intermediates" not in dirpath and "generated" not in dirpath and "outputs" not in dirpath and "gen" not in dirpath: # continue for file in filenames: fullpath = os.path.join(dirpath, file) simplepath = fullpath.replace(app_dir, '') if simplepath and simplepath.startswith('/'): simplepath = simplepath[1:len(simplepath)] pass # print "this is: %s" %simplepath # 9.png 压缩了之后不能缩放 if simplepath.endswith(".9.png"): print "ignore: %s" % simplepath elif simplepath.endswith(".png") or simplepath.endswith(".jpg"): oldMd5 = None try: oldMd5 = md5HashMap.get(simplepath) except KeyError, e: pass md5 = util.generateMd5_2(fullpath) print "\ncompressing %s oldMd5:%s md5:%s" % (simplepath, oldMd5, md5) if cmp(oldMd5, md5) == 0: print "%s is already compressed!!! skiped" % simplepath else: if JUST_CHECK: title = "you have picture not compressed yet!!!" content = "please local run:python lcba/lintCompress.py to compress Resources!!!" print title print content glo.set_value('_reason', title) glo.set_value('_title', title) glo.set_value('_content', content) util.safeQuit(None, None) pass # 2,上传tinypng压缩图片 compressSinglePicLogic(fullpath, fullpath + ".tiny", simplepath, mintor_md5_filepath) pass pass pass