Advertisement
Guest User

input map

a guest
Feb 15th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.46 KB | None | 0 0
  1. '''
  2. [object_id] = object
  3.  
  4. object
  5. {
  6.    id,
  7.    parents,
  8.    children
  9. }
  10.  
  11.                    [4] -> [7] ->
  12. [1] -> [2] -> [3] ->              [6]
  13.                    [5] ->
  14.  
  15. '''
  16.  
  17. class InputData:
  18.     def __init__(self, unique_id, parents, children):
  19.         self.id = unique_id
  20.         self.parents = parents
  21.         self.children = children
  22.         self.visited = False
  23.  
  24. # Gera mapa de entrada e também o ID do objeto inicial.
  25. def getMappedInput(data_input):
  26.     mapped_input, initial_object_id = {}, -1
  27.  
  28.     for i in data_input:
  29.         mapped_input[i.id] = i
  30.  
  31.         if not i.parents:
  32.             initial_object_id = i.id
  33.  
  34.     return mapped_input, initial_object_id
  35.  
  36. # Retorna mapa com novo objeto inserido.
  37. def addNewObject(mapped_input, input):
  38.     if input.id in mapped_input:
  39.         return
  40.  
  41.     mapped_input[input.id] = input
  42.  
  43.     return mapped_input
  44.  
  45. # Retorna mapa com objeto removido.
  46. def removeObject(mapped_input, input):
  47.     mapped_input.pop(input.id, None)
  48.  
  49.     return mapped_input
  50.  
  51. def iterateThroughMap(mapped_input, id_now):
  52.     # Pega o objeto correspodente ao ID atual.
  53.     map_object = mapped_input[id_now]
  54.  
  55.     # Para evitar que um objeto seja processado duas vezes. Se não tiver problema basta remover esta linha.
  56.     if map_object.visited:
  57.         return
  58.  
  59.     print(map_object.id)
  60.  
  61.     map_object.visited = True
  62.  
  63.     # Pega os objetos seguintes (filhos) ao atual.
  64.     next_objects = map_object.children
  65.  
  66.     # Se não tiver filho interrompe a varredura por este ramo.
  67.     if not next_objects:
  68.         return
  69.  
  70.     # Chama, recursivamente, a função nos filhos do objeto atual.
  71.     # Se os filhos devem ser processados concorrentemente USE threading ou multiprocessing neste escopo.
  72.     # Na aplicação atual vai ser processada toda a geração seguinte ao filho atual para depois o próximo ser processado.
  73.     for i in next_objects:
  74.         # No caso os filhos são armazenados, no set de entrada, já diretamente como IDs. Caso eles sejam objetos você usaria aqui i.id.
  75.         iterateThroughMap(mapped_input, i)
  76.  
  77.  
  78. input_data_tuple = (
  79.     # InputData(ID, pais, filhos)
  80.     InputData(1, None, (2,)),
  81.     InputData(6, (5, 7), None),
  82.     InputData(3, (2,), (4, 5)),
  83.     InputData(7, (4,), (6,)),
  84.     InputData(4, (3,), (7,)),
  85.     InputData(2, (1,), (3,)),
  86.     InputData(5, (3,), (6,)),
  87.    
  88. )
  89.  
  90. input_data_map, first_id = getMappedInput(input_data_tuple)
  91.  
  92. iterateThroughMap(input_data_map, first_id)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement