Exemple #1
0
    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 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 __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 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 makeVertexesArray(vertexIndexes: [], adjacencyVector: AdjacencyVector):

    vertexes = VectorArray(len(vertexIndexes))
    inputVertexesCountArray = VectorArray(len(vertexIndexes), 0)

    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)
            inputVertexesCountArray[slaveVertexIndex] += 1
            adjacencyVector.bind(slaveVertex.index, masterVertex.index)
    inputVertexesCountArray.clear_unused_memory()
    vertexes.clear_unused_memory()

    return [vertexes, inputVertexesCountArray]
Exemple #6
0
    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
Exemple #7
0
 def __init__(self, rows_count: int):
     self.vector = VectorArray(rows_count)
     for i in range(rows_count):
         self.vector.add(VectorArray(rows_count))
Exemple #8
0
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 __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 __init__(self, moves: str = None):
     self._moves = VectorArray()
     self._figure_moves = {}
     if moves is not None:
         self.add_multiple_moves(moves)