def DFSIterative(vertex: Vertex, queue: BasicQueue, cache: VectorArray, invertedVertexes: VectorArray): currentVertex = vertex vertex.passed = True queue.enqueue(vertex.index) stack = BasicStack() stack.push(Node(None)) stack.push(Node(currentVertex.index)) cache.add(vertex.index) while True: for bindedVertex in currentVertex.bindedVertexes: if cache.getIndexByValue(bindedVertex.index) is not None: continue else: cache.add(bindedVertex.index) queue.enqueue(bindedVertex.index) stack.push(Node(bindedVertex.index)) bindedVertex.passed = True stackValue = stack.pop().getItem() if stackValue is None: return currentVertex = invertedVertexes[stackValue]
class AdjacencyVector: def __init__(self, rows_count: int): self.vector = VectorArray(rows_count) for i in range(rows_count): self.vector.add(VectorArray(rows_count)) def bind(self, slaveVertexIndex: int, masterVertexIndex: int): self.vector.get(masterVertexIndex).add(slaveVertexIndex)
class AdjacencyVector: def __init__(self, rows_count: int): self.vector = VectorArray(rows_count) for i in range(rows_count): self.vector.add(VectorArray(rows_count)) def bind(self, slaveVertex: Vertex, masterVertex: Vertex, weight: int): self.vector.get(masterVertex.index).add({ 'slaveVertex': slaveVertex, 'masterVertex': masterVertex, 'weight': weight })
def put(self, key, data): hashDuplicated = False collisionInfo = None hashedKey = self.hashFunction(key) if self.data[hashedKey] is None: self.data[hashedKey] = VectorArray() else: hashDuplicated = True collisionInfo = { 'key': str(key), 'hashedKey': str(hashedKey), 'before': str(self.data[hashedKey].getAll()) } item = self.data[hashedKey].getByKey(key) if item is None: self.data[hashedKey].add({'key': key, 'value': data}) else: self.data[hashedKey].replace({ 'key': key, 'value': data }, self.data[hashedKey].getIndexByItem(item)) if hashDuplicated: collisionInfo['after'] = str(self.data[hashedKey].getAll()) self.collisions.append(collisionInfo)
def get_knights(self, color): figures = VectorArray(2) if color == 'w': figure_code = FiguresEnum.WHITE_KNIGHT.value else: figure_code = FiguresEnum.BLACK_KNIGHT.value for i in range(self._figures.rows_size()): row = self._figures.get_row(i) for j in range(row.get_memory_count()): figure = row.get(j) if figure is not None and figure.code == figure_code: figures.add(figure) return figures
def __init__(self, rows_count: int = None, vector: int = None): self._array = SingleArray() self._vector = vector if vector else 10 self._size = 0 self._rows_size = 0 if rows_count is not None: for i in range(rows_count): self._array.add(VectorArray(self._vector)) self._rows_size += 1
def makeVertexesArray(vertexIndexes: [], adjacencyVector: AdjacencyVector): vertexes = VectorArray(len(vertexIndexes)) for masterVertexIndex, slaveVIndexesArr in enumerate(vertexIndexes): masterVertex = vertexes[masterVertexIndex] if masterVertex is None: masterVertex = Vertex(masterVertexIndex) vertexes.replace(masterVertex, masterVertexIndex) for slaveVertexIndex in slaveVIndexesArr: slaveVertex = vertexes[slaveVertexIndex] if slaveVertex is None: slaveVertex = Vertex(slaveVertexIndex) vertexes.replace(slaveVertex, slaveVertexIndex) masterVertex.addBindedVertex(slaveVertex) adjacencyVector.bind(slaveVertex.index, masterVertex.index) vertexes.clear_unused_memory() return vertexes
class AdjacencyVector: def __init__(self, rows_count: int): self.vector = VectorArray(rows_count) for i in range(rows_count): self.vector.add(SingleArray()) def bind(self, slaveVertexIndex: int, masterVertexIndex: int): self.vector.get(masterVertexIndex).add(slaveVertexIndex) def getInvertedVector(self): invertedVector = AdjacencyVector(self.vector.size()) for rowIndex in range(self.vector.size()): row = self.vector.get(rowIndex) for colIndex in range(row.size()): colVertexIndex = row.get(colIndex) invertedVector.bind(rowIndex, colVertexIndex) return invertedVector
def __init__(self, rows_count: int): self.vector = VectorArray(rows_count) for i in range(rows_count): self.vector.add(VectorArray(rows_count))
def cosarayuIterative(vertexIndexes: []): adjacencyVector = makeAdjacencyVector(vertexIndexes) vertexes = makeVertexesArray(vertexIndexes, adjacencyVector) invertedVector = adjacencyVector.getInvertedVector() invertedVertexes = deepcopy(vertexes) for rowVertexIndex, rowBindedVertexIndexes in enumerate( invertedVector.vector): invertedVertexes[rowVertexIndex].clearBindedIndexes() for bindedVertexIndex in rowBindedVertexIndexes: invertedVertexes[rowVertexIndex].addBindedVertex( invertedVertexes[bindedVertexIndex]) components = Dictionary() compVertexesQueue = BasicQueue() compVertexesStack = BasicStack() invertedMatrix = adjacencyVector.getInvertedVector() for rowVertexIndex, rowBindedVertexIndexes in enumerate( invertedMatrix.vector): invertedVertexes[rowVertexIndex].clearBindedIndexes() for bindedVertexIndex in rowBindedVertexIndexes: invertedVertexes[rowVertexIndex].addBindedVertex( invertedVertexes[bindedVertexIndex]) invertedVertexes[0].passed = True cache = VectorArray() for rowVertexIndex, rowBindedVertexIndexes in enumerate( invertedMatrix.vector): indexCachedVertex = cache.getIndexByValue(rowVertexIndex) if indexCachedVertex is not None: continue cache.add(invertedVertexes[rowVertexIndex].index) compVertexesQueue.enqueue(rowVertexIndex) for vertexIndex in rowBindedVertexIndexes: cachedVertex = cache.getIndexByValue(vertexIndex) if cachedVertex is not None: continue vertex = invertedVertexes[vertexIndex] if vertex.passed is False: DFSIterative(vertex, compVertexesQueue, cache, invertedVertexes) while not compVertexesQueue.isEmpty(): compVertexesStack.push(compVertexesQueue.dequeue()) component_index = 0 cache = SingleArray() while not compVertexesStack.isEmpty(): vertexIndex = compVertexesStack.pop() if components[vertexIndex] is None: DFS2Iterative(vertexIndex, components, component_index, vertexes, cache) component_index += 1 return components.get_all()
def test_vector_array(self): vectorArray = VectorArray() for i in range(30): vectorArray.add(i) print(vectorArray.get(15)) print(vectorArray.size()) vectorArray.insert('test', 15) print(vectorArray.get(15)) print(vectorArray.size()) print(vectorArray.remove(15)) print(vectorArray.get(15)) print(vectorArray.size())
def __resize_insert(self): if self._array.size() == 0 or self._array.get( self._array.size() - 1).size() == self._vector: self._array.add(VectorArray(self._vector)) self._rows_size += 1
def __resize_replace(self): if self.size() == self._array.size() * self._vector: self._array.add(VectorArray(self._vector)) self._rows_size += 1
def __str__(self): table_figures_codes = VectorArray(8) figures = self.board.get_figures() result_parts = VectorArray(6) for i in range(7, -1, -1): figures_codes_row = VectorArray(8) empty_cells_count = 0 figures_row = figures.get_row(i) for j in range(figures_row.get_memory_count()): figures_col = figures_row.get(j, None) if figures_col is not None: if empty_cells_count > 0: figures_codes_row.add(str(empty_cells_count)) empty_cells_count = 0 figures_codes_row.add(figures_col.code) else: empty_cells_count += 1 if empty_cells_count > 0: figures_codes_row.add(str(empty_cells_count)) table_figures_codes.add( figures_codes_row.remove_empty_elements().join()) result_parts.add(table_figures_codes.join('/')) result_parts.add(self.move_order) result_parts.add(self.castling) result_parts.add(self.pawn_taking_field) result_parts.add(str(self.half_move_count)) result_parts.add(str(self.move_count)) return result_parts.join(' ')
def __init__(self, moves: str = None): self._moves = VectorArray() self._figure_moves = {} if moves is not None: self.add_multiple_moves(moves)
class MoveSorter(): def __init__(self, moves: str = None): self._moves = VectorArray() self._figure_moves = {} if moves is not None: self.add_multiple_moves(moves) def add_multiple_moves(self, moves: str): moves_count = 0 for line in moves.splitlines(): if moves_count == 0: moves_count = line self._moves.set_vector(int(line)) else: self._moves.add(line) self._moves.clear_unused_memory() def add_move(self, move: str): self._moves.add(move) def sort(self): self._moves.alphabetical_sort() def print_square_moves_table(self): elems_begin_with_count = { 'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0, 'f': 0, 'g': 0, 'h': 0 } for elem_index in range(self._moves.size()): elem = self._moves.get(elem_index) for i in ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']: if isinstance(elem, str) and elem[0] == i: elems_begin_with_count[i] += 1 min_len = 0 for k, v in elems_begin_with_count.items(): if v != 0: if min_len == 0: min_len = v else: if min_len > v: min_len = v res = str(self._moves.size()) + "\n" str_array = MatrixArray(None, min_len) for elem_index in range(self._moves.size() + 1): elem = self._moves.get(elem_index) if elem_index > 0 and elem_index % min_len == 0: res += str_array.get_row(int(elem_index / min_len) - 1).join(' ') res += "\n" str_array.add(elem) return res[:-1]