def newCatalog(): """ Inicializa el catálogo y retorna el catalogo inicializado. """ capacityMap = map.newMap(maptype='PROBING',comparefunction=compareByKey) stationMap = map.newMap(97,maptype='CHAINING',comparefunction=compareByKey) tree = oms.newMap('RBT') gdir = g.newGraph(97, compareByKey, directed=True) tempHash = map.newMap(179, maptype='CHAINING', comparefunction=compareByKey) tempList = lt.newList('ARRAY_LIST') catalog = {'capacityMap':capacityMap, 'stationMap': stationMap,"dateTree": tree, "GraphDirected":gdir, 'temperatureHash': tempHash, 'tempList':tempList} return catalog
def newMap(capacity=5, prime=109345121, maptype='CHAINING'): """ Crea una tabla de simbolos (map) con capacidad 'capacity' Prime es utilizado para la función de hash maptype indica si se utiliza separate chaining o linear probing como mecanismo de solución de colisiones. """ return ht.newMap(capacity, prime, maptype)
def newMap(capacity=17, prime=109345121, maptype='CHAINING', comparefunction=None): """ Crea una tabla de simbolos (map) para almacenar numelements Prime es utilizado para la función de hash maptype indica si se utiliza separate chaining ('CHAINING' ) o linear probing('PROBING') como mecanismo de solución de colisiones. """ return ht.newMap(capacity, prime, maptype, comparefunction)
def test_get(self): table = ht.newMap(capacity=17, maptype='PROBING') ht.put(table, '1', 'title1', self.comparekeys) ht.put(table, '2', 'title2', self.comparekeys) ht.put(table, '3', 'title3', self.comparekeys) ht.put(table, '4', 'title4', self.comparekeys) ht.put(table, '5', 'title5', self.comparekeys) ht.put(table, '6', 'title6', self.comparekeys) self.assertEqual(ht.size(table), 6) entry = ht.get(table, '5', self.comparekeys) print(entry)
def test_put (self): """ """ table = ht.newMap (capacity= 7, maptype='CHAINING') ht.put (table, 'book1', 'title1', self.compareentryfunction) ht.put (table, 'book2', 'title2', self.compareentryfunction) ht.put (table, 'book3', 'title3', self.compareentryfunction) ht.put (table, 'book4', 'title4', self.compareentryfunction) ht.put (table, 'book5', 'title5', self.compareentryfunction) ht.put (table, 'book6', 'title6', self.compareentryfunction) ht.put (table, 'book2', 'new-title 2', self.compareentryfunction) self.printTable (table) self.assertEqual (ht.size(table), 6)
def test_delete (self): """ """ table = ht.newMap (capacity= 7, maptype='CHAINING') ht.put (table, 'book1', 'title1', self.compareentryfunction) ht.put (table, 'book2', 'title2', self.compareentryfunction) ht.put (table, 'book3', 'title3', self.compareentryfunction) ht.put (table, 'book4', 'title4', self.compareentryfunction) ht.put (table, 'book5', 'title5', self.compareentryfunction) ht.put (table, 'book6', 'title6', self.compareentryfunction) self.printTable (table) ht.remove (table, 'book1', self.comparekeyfunction) self.printTable (table)
def test_contains(self): table = ht.newMap(capacity=17, maptype='PROBING') ht.put(table, '1', 'title1', self.comparekeys) ht.put(table, '2', 'title2', self.comparekeys) ht.put(table, '3', 'title3', self.comparekeys) ht.put(table, '4', 'title4', self.comparekeys) ht.put(table, '5', 'title5', self.comparekeys) ht.put(table, '6', 'title6', self.comparekeys) self.assertTrue(ht.contains(table, '1', self.comparekeys)) self.assertFalse(ht.contains(table, '15', self.comparekeys)) self.assertTrue(ht.contains(table, '6', self.comparekeys)) self.assertEqual(ht.size(table), 6)
def test_get(self): capacity = 10 table = ht.newMap(capacity, maptype='PROBING', comparefunction=self.comparekeys) ht.put(table, '1', 'title1') ht.put(table, '2', 'title2') ht.put(table, '11', 'title3') ht.put(table, '3', 'title4') ht.put(table, '12', 'title5') ht.put(table, '5', 'title6') entry = ht.get(table, '5') print(entry)
def test_delete(self): capacity = 10 table = ht.newMap(capacity, maptype='PROBING', comparefunction=self.comparekeys) ht.put(table, '1', 'title1') ht.put(table, '2', 'title2') ht.put(table, '11', 'title3') ht.put(table, '3', 'title4') ht.put(table, '12', 'title5') ht.put(table, '5', 'title6') self.printTable(table) entry = ht.remove(table, '3') self.printTable(table)
def test_getkeys (self): """ """ table = ht.newMap (capacity= 7, maptype='CHAINING') ht.put (table, 'book1', 'title1', self.compareentryfunction) ht.put (table, 'book2', 'title2', self.compareentryfunction) ht.put (table, 'book3', 'title3', self.compareentryfunction) ht.put (table, 'book4', 'title4', self.compareentryfunction) ht.put (table, 'book5', 'title5', self.compareentryfunction) ht.put (table, 'book6', 'title6', self.compareentryfunction) ltset = ht.keySet(table) iterator = it.newIterator (ltset) while it.hasNext (iterator): info = it.next (iterator) print (info)
def test_get(self): """ """ table = ht.newMap(capacity=7, maptype='CHAINING', comparefunction=self.comparefunction) ht.put(table, 'book1', 'title1') ht.put(table, 'book2', 'title2') ht.put(table, 'book3', 'title3') ht.put(table, 'book4', 'title4') ht.put(table, 'book5', 'title5') ht.put(table, 'book6', 'title6') book = ht.get(table, 'book2') print(book) self.assertEqual(ht.size(table), 6)
def test_contains(self): capacity = 10 table = ht.newMap(capacity, maptype='PROBING', comparefunction=self.comparekeys) ht.put(table, '1', 'title1') ht.put(table, '2', 'title2') ht.put(table, '11', 'title3') ht.put(table, '3', 'title4') ht.put(table, '12', 'title5') ht.put(table, '5', 'title6') self.assertTrue(ht.contains(table, '1')) self.assertFalse(ht.contains(table, '15')) self.assertTrue(ht.contains(table, '11'))
def test_delete(self): table = ht.newMap(capacity=17, maptype='PROBING') ht.put(table, '1', 'title1', self.comparekeys) ht.put(table, '2', 'title2', self.comparekeys) ht.put(table, '3', 'title3', self.comparekeys) ht.put(table, '4', 'title4', self.comparekeys) ht.put(table, '5', 'title5', self.comparekeys) ht.put(table, '6', 'title6', self.comparekeys) self.assertEqual(ht.size(table), 6) self.printTable(table) entry = ht.remove(table, '3', self.comparekeys) self.assertEqual(ht.size(table), 5) entry = ht.get(table, '3', self.comparekeys) self.assertIsNone(entry) self.printTable(table)
def test_getkeys(self): """ """ table = ht.newMap(capacity=17, maptype='PROBING') ht.put(table, '1', 'title1', self.comparekeys) ht.put(table, '2', 'title2', self.comparekeys) ht.put(table, '3', 'title3', self.comparekeys) ht.put(table, '4', 'title4', self.comparekeys) ht.put(table, '5', 'title5', self.comparekeys) ht.put(table, '6', 'title6', self.comparekeys) ltset = lt.newList('SINGLE_LINKED') ltset = ht.keySet(table) iterator = it.newIterator(ltset) while it.hasNext(iterator): info = it.next(iterator) print(info)
def test_getvalues(self): """ """ table = ht.newMap(capacity=7, maptype='CHAINING', comparefunction=self.comparefunction) ht.put(table, 'book1', 'title1') ht.put(table, 'book2', 'title2') ht.put(table, 'book3', 'title3') ht.put(table, 'book4', 'title4') ht.put(table, 'book5', 'title5') ht.put(table, 'book6', 'title6') ltset = lt.newList('SINGLE_LINKED_LIST') ltset = ht.valueSet(table) iterator = it.newIterator(ltset) while it.hasNext(iterator): info = it.next(iterator) print(info)
def test_getkeys(self): """ """ capacity = 10 table = ht.newMap(capacity, maptype='PROBING', comparefunction=self.comparekeys) ht.put(table, '1', 'title1') ht.put(table, '2', 'title2') ht.put(table, '11', 'title3') ht.put(table, '3', 'title4') ht.put(table, '12', 'title5') ht.put(table, '5', 'title6') ltset = lt.newList('SINGLE_LINKED_LIST') ltset = ht.keySet(table) iterator = it.newIterator(ltset) while it.hasNext(iterator): info = it.next(iterator) print(info)
def addToTree (catalog, row): tree=catalog['dateTree'] #Árbol RBT ordenado por fecha date_raw= row['start_date'].split(" ")[0] date=strToDate(date_raw, '%m/%d/%Y') #Convertir fecha a str StationInf = map.get(catalog['stationMap'], row['start_station_id'])['value'][1] #Ciudad de la estación if oms.contains(tree, date, greater): dateValue = oms.get(tree, date, greater) if map.contains(dateValue, StationInf): value = map.get(dateValue, StationInf)['value'] map.put(dateValue, StationInf, value+1) else: map.put(dateValue, StationInf, 1) totalValue = map.get(dateValue, 'total')['value'] totalValue += 1 map.put(dateValue, 'total', totalValue) tree = oms.put(tree, date, dateValue, greater) else: DateValueMap = map.newMap(maptype='PROBING', comparefunction= compareByKey) map.put(DateValueMap, StationInf, 1) map.put(DateValueMap, 'total', 1) tree = oms.put(tree, date, DateValueMap, greater)
def tripCityforDates (catalog, start_date, end_date): start_date=strToDate(start_date, '%m/%d/%Y') #Convertir fecha end_date=strToDate(end_date, '%m/%d/%Y') #Convertir fecha dateList = oms.valueRange(catalog['dateTree'], start_date, end_date, greater) #Lista de llaves entre las fechas dadas response='' tripsCityDays = map.newMap(capacity=11, maptype='CHAINING') #Se almacenan if dateList: print(dateList) iteraDate=it.newIterator(dateList) while it.hasNext(iteraDate): dateElement = it.next(iteraDate) #print(dateElement) if oms.get(catalog['dateTree'],dateElement, greater):#Si el nodo tiene un valor asociado if map.isEmpty(tripsCityDays):#Si cities está vacío, se le asigna el map de accidentes por ciudad del primer nodo tripsCityDays = oms.get(catalog['dateTree'], dateElement, greater) else: #De lo contrario, se compara cada ciudad del map de cada nodo con el map ciudadesNodo = map.keySet(dateElement)#Lista de las ciudades que tuvieron accidentes en esa fecha(nodo) ciudadesCities = map.keySet(tripsCityDays) iteraCiudades = it.newIterator(ciudadesNodo) while it.hasNext(iteraCiudades): ciudadElement=it.next(iteraCiudades)# que está en el map de cada nodo if ciudadElement: if lt.isPresent(ciudadesCities, ciudadElement, compareByKey): #Se verifica si la ciudad está en los valores del map num=map.get(tripsCityDays, ciudadElement)['value'] num+=map.get(dateElement, ciudadElement)['value'] map.put(tripsCityDays, ciudadElement, num) else: num= map.get(dateElement, ciudadElement)['value'] map.put(dateElement, ciudadElement, num) if not map.isEmpty(tripsCityDays): cityList= map.keySet(tripsCityDays) iteracity=it.newIterator(cityList) while it.hasNext(iteracity): cityKey = it.next(iteracity) response += str(cityKey) + ':' + str(map.get(tripsCityDays,cityKey)['value']) + " " return response return None
def tripsPerTemperature(catalog, number): longList = lt.size(catalog['tempList']) leastTemp = lt.subList(catalog['tempList'],longList-number,number) mostTemp = lt.subList(catalog['tempList'], 1, number) #O(1) por ser array counter1 = 0 counter2 = 0 response1='' response2='' tripsMostTempDays = map.newMap(30, comparefunction=compareByKey) tripsLeastTempDays = map.newMap(30, comparefunction=compareByKey) while counter1<number: leastTempIterator = it.newIterator(leastTemp) #Iterar la lista con n menores temperaturas while it.hasNext(leastTempIterator): tempElement = it.next(leastTempIterator) #Temperatura dateListElement = map.get(catalog['temperatureHash'],tempElement)['value']#Lista de todas las fechas para esa temperatura if number - lt.size(dateListElement) < counter1: #si no se pueden agregar todas las fechas de esa temperatura sin alcanzar el n deseado n_dates = lt.subList(dateListElement, 1, number-counter1) #lista de las que si se pueden agragar n_iterator = it.newIterator(n_dates) while it.hasNext(n_iterator): n_dateElement = it.next(n_iterator) #fecha a agregar trips = oms.get(catalog['dateTree'], n_dateElement, greater) #hash de viajes de esa fecha #print(type(trips)) totaltrip = map.get(trips, 'total')['value'] #número de viajes totales en esa fecha value = (tempElement, totaltrip) #tupla que se asigna como valor de cada fecha: temperatura, viajes totales map.put(tripsLeastTempDays, n_dateElement, value) counter1 += lt.size(n_dates)#el número de fechas que se agregó se suma al counter else: #si se pueden agregar todas las fechas de esa temperatura sin alcanzar el n deseado n_dates = dateListElement #se recorre la lista completa n_iterator = it.newIterator(n_dates) while it.hasNext(n_iterator): n_dateElement = it.next(n_iterator) #fecha a agregar trips = oms.get(catalog['dateTree'], n_dateElement, greater) #print(type(trips)) totaltrip = map.get(trips, 'total')['value'] value = (tempElement, totaltrip) map.put(tripsLeastTempDays, n_dateElement, value) counter1 += lt.size(dateListElement) while counter2<number: mostTempIterator = it.newIterator(mostTemp) #Iterar la lista con n temperaturas más altas while it.hasNext(mostTempIterator): tempElement = it.next(mostTempIterator) dateListElement = map.get(catalog['temperatureHash'],tempElement)['value']#Lista de todas las fechas para esa temperatura if number - lt.size(dateListElement) < counter2: #si no se pueden agregar todas las fechas de esa temperatura sin alcanzar el n deseado n_dates = lt.subList(dateListElement, 1, number-counter2) n_iterator = it.newIterator(n_dates) while it.hasNext(n_iterator): n_dateElement = it.next(n_iterator) value = oms.get(catalog['dateTree'], n_dateElement, greater) value = map.get(value, 'total')['value'] value = (tempElement, value) map.put(tripsMostTempDays, n_dateElement, value) counter2 += lt.size(n_dates) else: #si se pueden agregar todas las fechas de esa temperatura sin alcanzar el n deseado n_dates = dateListElement n_iterator = it.newIterator(n_dates) while it.hasNext(n_iterator): n_dateElement = it.next(n_iterator) value = oms.get(catalog['dateTree'], n_dateElement, greater) value = map.get(value, 'total')['value'] value = (tempElement, value) map.put(tripsMostTempDays, n_dateElement, value) counter2 += lt.size(dateListElement) if not map.isEmpty(tripsMostTempDays): tempList= map.keySet(tripsMostTempDays) iteratemp=it.newIterator(tempList) while it.hasNext(iteratemp): tempKey = it.next(iteratemp) response1 += str(tempKey) + ':' + str(map.get(tripsMostTempDays,tempKey)['value']) + " " if not map.isEmpty(tripsLeastTempDays): tempList= map.keySet(tripsLeastTempDays) iteratemp=it.newIterator(tempList) while it.hasNext(iteratemp): tempKey = it.next(iteratemp) response2 += str(tempKey) + ':' + str(map.get(tripsLeastTempDays,tempKey)['value']) + " " return response1, response2
class EntryMapTest (unittest.TestCase): capacity = 10 table = ht.newMap (capacity, maptype='PROBING') def setUp (self): ht.put (self.table, '1', 'title1', self.comparekeys) ht.put (self.table, '2', 'title2', self.comparekeys) ht.put (self.table, '11', 'title3', self.comparekeys) ht.put (self.table, '3', 'title4', self.comparekeys) ht.put (self.table, '12', 'title5', self.comparekeys) ht.put (self.table, '5', 'title6', self.comparekeys) def tearDown (self): pass def printTable (self, table): print ('TABLE:') print ('Capacity: ' + str(table['capacity'])) print ('Scale: ' + str(table['scale'])) print ('Shift: ' + str(table['shift'])) print ('Prime: ' + str(table['prime'])) iterator = it.newIterator(table['table']) pos = 1 while it.hasNext(iterator): print ("[ " + str(pos) + " ]-->", end="") entry = it.next(iterator) print (entry) pos += 1 def comparekeys (self, key1, key2): if ( key1 == key2): return True return False def compareentryfunction (self, element1, element2): if (element1['key'] == element2['key']): return True return False def test_contains(self): self.assertTrue (ht.contains(self.table, '1', self.comparekeys)) self.assertFalse (ht.contains(self.table, '15', self.comparekeys)) self.assertTrue (ht.contains(self.table, '11', self.comparekeys)) def test_get(self): entry = ht.get (self.table, '5', self.comparekeys) print (entry) def test_delete(self): self.printTable (self.table) entry = ht.remove (self.table, '3', self.comparekeys) self.printTable (self.table) def test_getkeys (self): """ """ ltset = lt.newList ('SINGLE_LINKED_LIST') ltset = ht.keySet(self.table) iterator = it.newIterator (ltset) while it.hasNext (iterator): info = it.next (iterator) print (info) def test_getvalues (self): """ """ ltset = lt.newList ('SINGLE_LINKED_LIST') ltset = ht.valueSet (self.table) iterator = it.newIterator (ltset) while it.hasNext (iterator): info = it.next (iterator) print (info)