Ejemplo n.º 1
0
        /// <summary>
        /// Essaie d'inserer un ligne vide à l'index spécifié, renvoie True si réussie, False sinon.
        /// Appelle l'évennement OnInsertRow sur la ligne ainsi insérée.
        /// </summary>
        /// <param name="index"></param>
        /// <returns>bool</returns>
        public bool InsertRow(int index)
        {
            if (!CanInsertRow(index))
            {
                return(false);
            }

            List <GateStruct> row = new List <GateStruct>(NbCol);

            for (int j = 0; j < NbCol; j++)
            {
                GateStruct gateStruct = CreateGateStruct(index, j, Gate.IDENTITY);
                row.Add(gateStruct);
            }
            rows.Insert(index, row);

            for (int i = index + 1; i < NbRow; i++)
            {
                List <GateStruct> updatedRow = rows[i];
                for (int j = 0; j < updatedRow.Count; j += updatedRow[j].gate.NbEntries)
                {
                    updatedRow[j].row++;
                }
            }

            OnInsertRow?.Invoke(row);

            return(true);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///  Essaie de déplacer la porte de la position source à la position cible, renvoie True si réussie, False sinon.
        ///  Appelle l'évennement OnMoveGate sur les portes ainsi déplacées.
        /// </summary>
        /// <param name="sourceRow"></param>
        /// <param name="sourceCol"></param>
        /// <param name="targetRow"></param>
        /// <param name="targetCol"></param>
        /// <returns>bool</returns>
        public bool MoveGate(int sourceRow, int sourceCol, int targetRow, int targetCol)
        {
            if (!CanMoveGate(sourceRow, sourceCol, targetRow, targetCol))
            {
                return(false);
            }
            ;

            GateStruct gateStruct = rows[sourceRow][sourceCol];

            sourceCol = gateStruct.col;

            gateStruct.row = targetRow;
            gateStruct.col = targetCol;

            for (int i = 0; i < gateStruct.gate.NbEntries; i++)
            {
                GateStruct movedGateStruct = rows[targetRow][targetCol + i];

                movedGateStruct.row = sourceRow;
                movedGateStruct.col = sourceCol + i;

                rows[sourceRow][sourceCol + i] = movedGateStruct;
                OnMoveGate?.Invoke(movedGateStruct);

                rows[targetRow][targetCol + i] = gateStruct;
            }

            OnMoveGate?.Invoke(gateStruct);

            return(true);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Retourne True si l'opération correspondante est garantie de réussir, False sinon (attention à l'atomicité de la transaction).
        /// </summary>
        /// <param name="sourceRow"></param>
        /// <param name="sourceCol"></param>
        /// <param name="targetRow"></param>
        /// <param name="targetCol"></param>
        /// <returns>bool</returns>
        public bool CanMoveGate(int sourceRow, int sourceCol, int targetRow, int targetCol)
        {
            if (sourceRow < 0 || sourceRow >= NbRow || targetRow < 0 || targetRow >= NbRow || sourceCol < 0 || sourceCol >= NbCol || targetCol < 0 || targetCol >= NbCol)
            {
                throw new ArgumentOutOfRangeException("CanMoveGate: sourceRow = " + sourceRow + " " + targetRow + " sourceCol" + sourceCol + " targetCol = " + targetCol);
            }

            if (sourceRow == targetRow && sourceCol == targetCol)
            {
                return(false);
            }

            GateStruct gateStruct = rows[sourceRow][sourceCol];

            Debug.Log("NbEntries : " + gateStruct.gate.NbEntries);
            if (gateStruct.gate.NbEntries > 1 && targetCol >= (NbCol - 1))
            {
                return(false);
            }

            if (targetCol < 0 || targetCol + gateStruct.gate.NbEntries > NbCol)
            {
                return(false);
            }

            for (int i = 0; i < gateStruct.gate.NbEntries; i++)
            {
                if (rows[targetRow][targetCol + i].gate != Gate.IDENTITY && rows[targetRow][targetCol + i] != gateStruct)
                {
                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 4
0
        /**
         * ####################################################################################################
         * ############################################### GATE ###############################################
         * ####################################################################################################
         */

        /// <summary>
        /// Crée un GateStruct et appelle l'évennement OnCreateGate.
        /// </summary>
        /// <param name="row"></param>
        /// <param name="col"></param>
        /// <param name="gate"></param>
        /// <returns>GateStruct</returns>
        private GateStruct CreateGateStruct(int row, int col, Gate gate)
        {
            GateStruct gateStruct = new GateStruct(row, col, gate);

            OnCreateGate?.Invoke(gateStruct);
            return(gateStruct);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Essaie de supprimer la porte correspondante à la position spécifiée, renvoie True si réussi, False sinon.
        /// Appelle l'évennement OnRemoveGate sur la porte ainsi supprimée.
        /// </summary>
        /// <param name="row"></param>
        /// <param name="col"></param>
        /// <returns>bool</returns>
        public bool RemoveGate(int row, int col)
        {
            if (!CanRemoveGate(row, col))
            {
                return(false);
            }

            GateStruct gateStruct = rows[row][col];

            for (int i = 0; i < gateStruct.gate.NbEntries; i++)
            {
                GateStruct newGateStruct = CreateGateStruct(row, gateStruct.col + i, Gate.IDENTITY);
                rows[row][gateStruct.col + i] = newGateStruct;
                OnPutGate?.Invoke(newGateStruct);
            }

            OnRemoveGate?.Invoke(gateStruct);

            return(true);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Essaie de mettre la porte correspondante à la position spécifiée, renvoie True si réussi, False sinon.
        /// Une porte ne peut être posée uniquement si tous les emplacements nécessaires sont vides.
        /// Appelle l'évennement OnPutGate sur la nouvelle porte et OnRemoveGate sur les portes remplacées.
        /// </summary>
        /// <param name="row"></param>
        /// <param name="col"></param>
        /// <param name="gate"></param>
        /// <returns>bool</returns>
        public bool PutGate(int row, int col, Gate gate)
        {
            if (!CanPutGate(row, col, gate))
            {
                return(false);
            }

            GateStruct newGateStruct = CreateGateStruct(row, col, gate);

            for (int i = 0; i < gate.NbEntries; i++)
            {
                GateStruct oldGateStruct = rows[row][col + i];
                rows[row][col + i] = newGateStruct;
                OnRemoveGate?.Invoke(oldGateStruct);
            }

            OnPutGate?.Invoke(newGateStruct);

            return(true);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Essaie d'insérer une colone vide à l'index spécifié, renvoie True si réussie, False sinon.
        /// Associe à la colone insérée une entrée de valeur zéro.
        /// Echoue dans le cas ou la colone en question passerait au milieu d'une porte à plusieurs entrées.
        /// Appelle l'evennement OnInsertCol sur la colone ainsi insérée.
        /// </summary>
        /// <param name="index"></param>
        /// <returns>bool</returns>
        public bool InsertCol(int index)
        {
            if (!CanInsertCol(index))
            {
                return(false);
            }

            // insert the entry
            EntryStruct insertedEntry = CreateEntryStruct(index, Qubit.Zero);

            entries.Insert(index, insertedEntry);

            // insert the column
            List <GateStruct> insertedCol = new List <GateStruct>(NbRow);

            for (int i = 0; i < NbRow; i++)
            {
                GateStruct gateStruct = CreateGateStruct(i, index, Gate.IDENTITY);
                insertedCol.Add(gateStruct);
                rows[i].Insert(index, gateStruct);
            }

            for (int j = NbCol - 1; j >= index + 1; j--)
            {
                entries[j].col++;
                for (int i = 0; i < NbRow; i++)
                {
                    if (rows[i][j].col == j - 1)
                    {
                        rows[i][j].col++;
                    }
                }
            }

            OnInsertCol?.Invoke(insertedEntry, insertedCol);

            return(true);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Essaie de supprime la colone d'index spécifié, renvoie True si réussie, False sinon.
        /// Echoue dans le cas ou une porte à plusieurs entrées occupe la colone en question.
        /// Appelle l'évennement OnRemoveCol sur la colone ainsi supprimée.
        /// </summary>
        /// <param name="index"></param>
        /// <returns>bool</returns>
        public bool RemoveCol(int index)
        {
            if (!CanRemoveCol(index))
            {
                return(false);
            }

            EntryStruct removedEntry = entries[index];

            entries.RemoveAt(index);

            List <GateStruct> removedCol = new List <GateStruct>();

            for (int i = 0; i < NbRow; i++)
            {
                GateStruct gateStruct = rows[i][index];
                removedCol.Add(gateStruct);
                rows[i].RemoveAt(index);
            }

            for (int j = index; j < NbCol; j++)
            {
                entries[j].col--;

                for (int i = 0; i < NbRow; i++)
                {
                    if (rows[i][j].col == j + 1)
                    {
                        rows[i][j].col--;
                    }
                }
            }

            OnRemoveCol?.Invoke(removedEntry, removedCol);

            return(true);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Essaie de déplacer la colone source à l'index cible, renvoie True si réussie, False sinon.
        /// Echoue dans le cas ou une porte à plusieurs entrées occupe la colone source ou que la colone en question passerait au milieu d'une porte à plusieurs entrées à l'index cible.
        /// Appelle l'évennement OnMoveCol sur chaque colone dont l'index est mis à jour.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="target"></param>
        /// <returns></returns>
        public bool MoveCol(int source, int target)
        {
            if (!CanMoveCol(source, target))
            {
                return(false);
            }

            // move the entry
            EntryStruct movedEntry = entries[source];

            entries.RemoveAt(source);
            movedEntry.col = target;
            entries.Insert(target, movedEntry);

            // move the column
            List <GateStruct> movedCol = new List <GateStruct>(NbRow);

            for (int i = 0; i < NbRow; i++)
            {
                List <GateStruct> currentRow = rows[i];
                GateStruct        gateStruct = currentRow[source];
                currentRow.RemoveAt(source);
                gateStruct.col = target;
                currentRow.Insert(target, gateStruct);
                movedCol.Add(gateStruct);
            }

            OnMoveCol?.Invoke(movedEntry, movedCol);

            // update col property of moved gates
            if (source < target)
            {
                for (int j = source; j < target; j++)
                {
                    EntryStruct updatedEntry = entries[j];
                    updatedEntry.col--;

                    List <GateStruct> updatedCol = new List <GateStruct>(NbRow);
                    for (int i = 0; i < NbRow; i++)
                    {
                        if (rows[i][j].col == j + 1)
                        {
                            rows[i][j].col--;
                            updatedCol.Add(rows[i][j]);
                        }
                    }
                    OnMoveCol?.Invoke(updatedEntry, updatedCol);
                }
            }
            else
            {
                for (int j = source; j > target; j--)
                {
                    EntryStruct updatedEntry = entries[j];
                    updatedEntry.col++;

                    List <GateStruct> updatedCol = new List <GateStruct>(NbRow);
                    for (int i = 0; i < NbRow; i++)
                    {
                        if (rows[i][j].col == j - 1)
                        {
                            rows[i][j].col++;
                            updatedCol.Add(rows[i][j]);
                        }
                    }
                    OnMoveCol?.Invoke(updatedEntry, updatedCol);
                }
            }

            return(true);
        }