Advertisement
Guest User

Untitled

a guest
May 16th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace LPP
  8. {
  9.     class Simplifier
  10.     {
  11.         private List<List<char>> trueRows;
  12.         private List<List<char>> falseRows;
  13.  
  14.         public Simplifier(List<List<char>> originalRows)
  15.         {
  16.             trueRows = GetRowsWithValue(originalRows,true);
  17.             falseRows = GetRowsWithValue(originalRows, false);
  18.         }
  19.         private SortedDictionary<int,List<List<char>>> GroupInSortedDictionary(List<List<char>> rows)
  20.         {
  21.             SortedDictionary<int, List<List<char>>> groups = new SortedDictionary<int, List<List<char>>>();
  22.             foreach (var row in rows)
  23.             {
  24.                 int trueCount = 0;
  25.                 for (int i = 0; i < row.Count-1; i++)
  26.                 {
  27.                     if (row[i] == '1')
  28.                     {
  29.                         trueCount++;
  30.                     }
  31.                 }
  32.                 if (!groups.Keys.Contains(trueCount))
  33.                 {
  34.                     groups.Add(trueCount, new List<List<char>>());
  35.                     groups[trueCount].Add(row);
  36.                 }
  37.                 else
  38.                 {
  39.                     groups[trueCount].Add(row);
  40.                 }
  41.             }
  42.  
  43.             return groups;
  44.         }
  45.  
  46.         public List<List<char>> GetSimplifiedTable()
  47.         {
  48.             List<List<char>> allsimplified = new List<List<char>>();
  49.             List<List<char>> trueSimplified = Simplify(trueRows);
  50.             List<List<char>> falseSimplified = Simplify(falseRows);
  51.             foreach (var row in falseSimplified)
  52.             {
  53.                 allsimplified.Add(row);
  54.             }
  55.             foreach (var row in trueSimplified)
  56.             {
  57.                 allsimplified.Add(row);
  58.             }
  59.             allsimplified = Uniquify(allsimplified);
  60.             return allsimplified;
  61.         }
  62.  
  63.         private List<List<char>> Simplify(List<List<char>> rows)
  64.         {
  65.             var grouped = GroupInSortedDictionary(rows);
  66.             List<List<char>> simplified = new List<List<char>>();
  67.             bool nothing_changed = true;
  68.  
  69.             if (grouped.Count==1)
  70.             {
  71.                 return grouped.ElementAt(0).Value;
  72.             }
  73.  
  74.             int keyAtFirstRecord = grouped.ElementAt(0).Key; // is the smallest key
  75.  
  76.             for (int i = keyAtFirstRecord; i < grouped.Count; i++) // i is the group number - m0,m1 (minterms)
  77.             {
  78.                 try
  79.                 {
  80.                     for (int j = 0; j < grouped[i].Count; j++)
  81.                     {
  82.                         for (int k = 0; k < grouped[i + 1].Count; k++)
  83.                         {
  84.                             int index = GetIndexOfDifferent(grouped[i][j], grouped[i + 1][k]);
  85.                             if (index != -1)
  86.                             {
  87.                                 simplified.Add(GetRowWithStarAtIndex(grouped[i][j], index));
  88.                                 nothing_changed = false;
  89.                             }
  90.                         }
  91.                     }
  92.                 }
  93.                 catch (Exception) // For cases where you have keys that are not in succession, e.g 0,2/1,3 etc
  94.                 {
  95.                     continue;
  96.                 }
  97.             }
  98.             if (nothing_changed)
  99.             {
  100.                 return rows;
  101.             }
  102.             return Simplify(simplified);
  103.         }
  104.  
  105.         private List<char> GetRowWithStarAtIndex(List<char> row, int index)
  106.         {
  107.             List<char> toReturn = new List<char>();
  108.  
  109.             for (int i = 0; i < row.Count; i++)
  110.             {
  111.                 toReturn.Add(row[i]);
  112.             }
  113.             toReturn[index] = '*';
  114.  
  115.             return toReturn;
  116.         }
  117.  
  118.         private int GetIndexOfDifferent(List<char> up, List<char> down)
  119.         {
  120.             int diffCount = 0;
  121.             int indexToReturn = -1;
  122.             for (int i = 0; i < up.Count - 1; i++)
  123.             {
  124.                 if (up[i] != down[i])
  125.                 {
  126.                     diffCount++;
  127.                     indexToReturn = i;
  128.                 }
  129.             }
  130.             if (diffCount == 1)
  131.             {
  132.                 return indexToReturn;
  133.             }
  134.             return -1;
  135.         }
  136.  
  137.         private List<List<char>> GetRowsWithValue(List<List<char>> rows, bool value)
  138.         {
  139.             List<List<char>> toReturn = new List<List<char>>();
  140.             foreach (var row in rows)
  141.             {
  142.                 if (value)
  143.                 {
  144.                     if (row[row.Count - 1] == '1')
  145.                     {
  146.                         toReturn.Add(row);
  147.                     }
  148.                 }
  149.                 else
  150.                 {
  151.                     if (row[row.Count - 1] == '0')
  152.                     {
  153.                         toReturn.Add(row);
  154.                     }
  155.                 }
  156.             }
  157.             return toReturn;
  158.         }
  159.  
  160.         private List<List<char>> Uniquify(List<List<char>> simplifiedRows)
  161.         {
  162.             List<List<char>> unique = new List<List<char>>();
  163.             unique.Add(simplifiedRows[0]);
  164.             for (int i = 1; i < simplifiedRows.Count; i++)
  165.             {
  166.                 for (int j = 0; j < simplifiedRows[0].Count; j++)
  167.                 {
  168.                     if (simplifiedRows[i-1][j] != simplifiedRows[i][j])
  169.                     {
  170.                         unique.Add(simplifiedRows[i]);
  171.                         break;
  172.                     }
  173.                 }
  174.             }
  175.             return unique;
  176.         }
  177.     }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement