Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace LPP
- {
- class Simplifier
- {
- private List<List<char>> trueRows;
- private List<List<char>> falseRows;
- public Simplifier(List<List<char>> originalRows)
- {
- trueRows = GetRowsWithValue(originalRows,true);
- falseRows = GetRowsWithValue(originalRows, false);
- }
- private SortedDictionary<int,List<List<char>>> GroupInSortedDictionary(List<List<char>> rows)
- {
- SortedDictionary<int, List<List<char>>> groups = new SortedDictionary<int, List<List<char>>>();
- foreach (var row in rows)
- {
- int trueCount = 0;
- for (int i = 0; i < row.Count-1; i++)
- {
- if (row[i] == '1')
- {
- trueCount++;
- }
- }
- if (!groups.Keys.Contains(trueCount))
- {
- groups.Add(trueCount, new List<List<char>>());
- groups[trueCount].Add(row);
- }
- else
- {
- groups[trueCount].Add(row);
- }
- }
- return groups;
- }
- public List<List<char>> GetSimplifiedTable()
- {
- List<List<char>> allsimplified = new List<List<char>>();
- List<List<char>> trueSimplified = Simplify(trueRows);
- List<List<char>> falseSimplified = Simplify(falseRows);
- foreach (var row in falseSimplified)
- {
- allsimplified.Add(row);
- }
- foreach (var row in trueSimplified)
- {
- allsimplified.Add(row);
- }
- allsimplified = Uniquify(allsimplified);
- return allsimplified;
- }
- private List<List<char>> Simplify(List<List<char>> rows)
- {
- var grouped = GroupInSortedDictionary(rows);
- List<List<char>> simplified = new List<List<char>>();
- bool nothing_changed = true;
- if (grouped.Count==1)
- {
- return grouped.ElementAt(0).Value;
- }
- int keyAtFirstRecord = grouped.ElementAt(0).Key; // is the smallest key
- for (int i = keyAtFirstRecord; i < grouped.Count; i++) // i is the group number - m0,m1 (minterms)
- {
- try
- {
- for (int j = 0; j < grouped[i].Count; j++)
- {
- for (int k = 0; k < grouped[i + 1].Count; k++)
- {
- int index = GetIndexOfDifferent(grouped[i][j], grouped[i + 1][k]);
- if (index != -1)
- {
- simplified.Add(GetRowWithStarAtIndex(grouped[i][j], index));
- nothing_changed = false;
- }
- }
- }
- }
- catch (Exception) // For cases where you have keys that are not in succession, e.g 0,2/1,3 etc
- {
- continue;
- }
- }
- if (nothing_changed)
- {
- return rows;
- }
- return Simplify(simplified);
- }
- private List<char> GetRowWithStarAtIndex(List<char> row, int index)
- {
- List<char> toReturn = new List<char>();
- for (int i = 0; i < row.Count; i++)
- {
- toReturn.Add(row[i]);
- }
- toReturn[index] = '*';
- return toReturn;
- }
- private int GetIndexOfDifferent(List<char> up, List<char> down)
- {
- int diffCount = 0;
- int indexToReturn = -1;
- for (int i = 0; i < up.Count - 1; i++)
- {
- if (up[i] != down[i])
- {
- diffCount++;
- indexToReturn = i;
- }
- }
- if (diffCount == 1)
- {
- return indexToReturn;
- }
- return -1;
- }
- private List<List<char>> GetRowsWithValue(List<List<char>> rows, bool value)
- {
- List<List<char>> toReturn = new List<List<char>>();
- foreach (var row in rows)
- {
- if (value)
- {
- if (row[row.Count - 1] == '1')
- {
- toReturn.Add(row);
- }
- }
- else
- {
- if (row[row.Count - 1] == '0')
- {
- toReturn.Add(row);
- }
- }
- }
- return toReturn;
- }
- private List<List<char>> Uniquify(List<List<char>> simplifiedRows)
- {
- List<List<char>> unique = new List<List<char>>();
- unique.Add(simplifiedRows[0]);
- for (int i = 1; i < simplifiedRows.Count; i++)
- {
- for (int j = 0; j < simplifiedRows[0].Count; j++)
- {
- if (simplifiedRows[i-1][j] != simplifiedRows[i][j])
- {
- unique.Add(simplifiedRows[i]);
- break;
- }
- }
- }
- return unique;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement