def isNotPresent(lst,elem): size = lt.size(lst) respuesta = True p=0 while p < lt.size(lst): x=lt.getElement(lst,p) if str(x) == str(elem): respuesta=False break p+=1 return respuesta
def rehash(map): """ Se aumenta la capacidad de la tabla al doble y se hace rehash de todos los elementos de la tabla. """ try: newtable = lt.newList('ARRAY_LIST', map['comparefunction']) capacity = nextPrime(map['capacity'] * 2) for _ in range(capacity): entry = me.newMapEntry(None, None) lt.addLast(newtable, entry) oldtable = map['table'] map['size'] = 0 map['currentfactor'] = 0 map['table'] = newtable map['capacity'] = capacity for pos in range(lt.size(oldtable)): entry = lt.getElement(oldtable, pos + 1) if (entry['key'] is not None and entry['key'] != '__EMPTY__'): hash = hashValue(map, entry['key']) pos = findSlot(map, entry['key'], hash, map['comparefunction']) lt.changeInfo(map['table'], abs(pos), entry) if (pos < 0): map['size'] += 1 map['currentfactor'] = map['size'] / map['capacity'] return map except Exception as exp: error.reraise(exp, 'Probe:rehash')
def rehash(map): """ Se aumenta la capacidad de la tabla al doble y se hace rehash de todos los elementos de la tabla. """ try: newtable = lt.newList("ARRAY_LIST", map["comparefunction"]) capacity = nextPrime(map["capacity"] * 2) for _ in range(capacity): entry = me.newMapEntry(None, None) lt.addLast(newtable, entry) oldtable = map["table"] map["size"] = 0 map["currentfactor"] = 0 map["table"] = newtable map["capacity"] = capacity for pos in range(lt.size(oldtable)): entry = lt.getElement(oldtable, pos + 1) if entry["key"] is not None and entry["key"] != "__EMPTY__": hash = hashValue(map, entry["key"]) pos = findSlot(map, entry["key"], hash, map["comparefunction"]) lt.changeInfo(map["table"], abs(pos), entry) if pos < 0: map["size"] += 1 map["currentfactor"] = map["size"] / map["capacity"] return map except Exception as exp: error.reraise(exp, "Probe:rehash")
def valueSet(map): """ Retorna una lista con todos los valores de la tabla de hash Args: map: El map Returns: lista de valores Raises: Exception """ ltset = lt.newList('SINGLE_LINKED', map['comparefunction']) for pos in range(lt.size(map['table'])): bucket = lt.getElement(map['table'], pos + 1) for element in range(lt.size(bucket)): entry = lt.getElement(bucket, element + 1) lt.addLast(ltset, entry['value']) return ltset
def rehash(map_, resize="BIGGER"): actual_number = map_["size"] load = map_["loadfactor"] prime = map_["prime"] cmp = map_["comparefunction"] if resize == "BIGGER": nuevo = newMap(actual_number * 2, prime, load, cmp) elif resize == "SMALLER": nuevo = newMap(actual_number / 2, prime, load, cmp) else: return map_ for pos in range(lt.size(map_['table'])): bucket = lt.getElement(map_['table'], pos + 1) for element in range(lt.size(bucket)): entry = lt.getElement(bucket, element + 1) nuevo = put(nuevo, entry['key'], entry['value']) return nuevo
def size(lst): """ Informa el número de elementos de la lista. Args lst: La lista a examinar Raises: Exception """ try: return lt.size(lst) except Exception as exp: error.reraise(exp, 'TADList->size: ')
def heapsort1(lst): """ Implementa el heapsort sobre una lista. """ size=lt.size(lst) mid=size//2 cmpfunction=lst['cmpfunction'] i=mid while i in range(1,mid+1): funcionsink(lst,i, size, cmpfunction) i-=1 #invirtiendo pos j=size-1 while j in range(1,size): hp.exchange(lst, 1, j) funcionsink(lst,i,size, cmpfunction) j-=1
def valueSet(map): """ Retorna una lista con todos los valores de la tabla de hash Args: map: El map Returns: lista de valores Raises: Exception """ ltset = lt.newList() for pos in range(lt.size(map['table'])): entry = lt.getElement(map['table'], pos + 1) if (entry['value'] is not None and entry['value'] != '__EMPTY__'): lt.addLast(ltset, entry['value']) return ltset
def isEmpty(map): """ Informa si la tabla de hash se encuentra vacia Args: map: El map Returns: True: El map esta vacio False: El map no esta vacio Raises: Exception """ empty = True for pos in range(lt.size(map['table'])): entry = lt.getElement(map['table'], pos + 1) if (entry['key'] is not None and entry['key'] != '__EMPTY__'): empty = False break return empty
def degree(graph, vertex): """ Retorna el numero de arcos asociados al vertice vertex Args: graph: El grafo sobre el que se ejecuta la operacion vertex: El vertice del que se desea conocer el grado Returns: El grado del vertice Raises: Exception """ try: element = map.get(graph['vertices'], vertex) lst = element['value'] return (lt.size(lst)) except Exception as exp: error.reraise(exp, 'ajlist:degree')
def isEmpty(map): """ Informa si la tabla de hash se encuentra vacia Args: map: El map Returns: True: El map esta vacio False: El map no esta vacio Raises: Exception """ bucket = lt.newList() empty = True for pos in range(lt.size(map['table'])): bucket = lt.getElement(map['table'], pos + 1) if lt.isEmpty(bucket) is False: empty = False break return empty
def keySet(map): """ Retorna una lista con todas las llaves de la tabla de hash Args: map: El map Returns: lista de llaves Raises: Exception """ try: ltset = lt.newList() for pos in range(lt.size(map['table'])): entry = lt.getElement(map['table'], pos + 1) if (entry['key'] is not None and entry['key'] != '__EMPTY__'): lt.addLast(ltset, entry['key']) return ltset except Exception as exp: error.reraise(exp, 'Probe:keyset')
def valueSet(map): """ Retorna una lista con todos los valores de la tabla de hash Args: map: El map Returns: lista de valores Raises: Exception """ try: ltset = lt.newList("ARRAY_LIST") for pos in range(lt.size(map["table"])): entry = lt.getElement(map["table"], pos + 1) if entry["value"] is not None and entry["value"] != "__EMPTY__": lt.addLast(ltset, entry["value"]) return ltset except Exception as exp: error.reraise(exp, "Probe:valueset")
def isEmpty(map): """Informa si la tabla de hash se encuentra vacia Args: map: El map Returns: True: El map esta vacio False: El map no esta vacio Raises: Exception """ try: empty = True for pos in range(lt.size(map["table"])): entry = lt.getElement(map["table"], pos + 1) if entry["key"] is not None and entry["key"] != "__EMPTY__": empty = False break return empty except Exception as exp: error.reraise(exp, "Probe:isEmpty")
def rehash(map, resize="BIGGER"): actual_number = map["size"] load = map["loadfactor"] prime = map["prime"] cmp = map["comparefunction"] if resize == "BIGGER": nuevo = newMap(actual_number * 2, prime, load, cmp) elif resize == "SMALLER": nuevo = newMap(actual_number / 2, prime, load, cmp) else: print("resize no valido") return None for pos in range(lt.size(map['table'])): entry = lt.getElement(map['table'], pos + 1) if entry['key'] is not None and entry['key'] != '__EMPTY__': put(nuevo, entry['key'], entry['value']) return nuevo