axeefectushka

Untitled

Dec 7th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.52 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6.  
  7. class Program
  8. {
  9.     static void Main()
  10.     {
  11.         SparseMatrix matrix1 = new SparseMatrix(3, 5);
  12.  
  13.         matrix1.NotSparseMatrix += delegate { Console.WriteLine($"Matrix isnt sparse, because sparsity is lower than 0.5"); };
  14.  
  15.         matrix1[0, 0] = 1;
  16.         matrix1[0, 1] = 2;
  17.         matrix1[0, 2] = 0;
  18.         matrix1[0, 3] = 0;
  19.         matrix1[0, 4] = 3;
  20.         matrix1[1, 0] = 4;
  21.         matrix1[1, 1] = 0;
  22.         matrix1[1, 2] = 0;
  23.         matrix1[1, 3] = 0;
  24.         matrix1[1, 4] = 0;
  25.         matrix1[2, 0] = 0;
  26.         matrix1[2, 1] = 5;
  27.         matrix1[2, 2] = 0;
  28.         matrix1[2, 3] = 6;
  29.         matrix1[2, 4] = 0;
  30.         Console.WriteLine(matrix1[0,1]);
  31.         Console.WriteLine(matrix1);
  32.  
  33.        
  34.         foreach(var item in matrix1)
  35.         {
  36.             Console.WriteLine(item);
  37.         }
  38.  
  39.         var NotZeroElements = matrix1.GetNonzeroElements();
  40.         foreach(var item in NotZeroElements)
  41.         {
  42.             Console.WriteLine(item);
  43.         }
  44.  
  45.         Console.WriteLine(matrix1.GetCount(0));
  46.         Console.WriteLine(matrix1.GetCount(1));
  47.     }
  48. }
  49.  
  50. class SparseMatrix: IEnumerable<int>
  51. {
  52.  
  53.     public int N { get; }
  54.  
  55.     public int M { get; }
  56.  
  57.     private int _version;
  58.  
  59.     private List<(int, int, int)> NotZeroElements = new List<(int, int, int)> { };
  60.  
  61.     public SparseMatrix(int n = 0, int m = 0)
  62.     {
  63.         if (n > 0 && m > 0)
  64.         {
  65.             N = n;
  66.             M = m;
  67.         }
  68.         else
  69.         {
  70.             throw new ArgumentException();
  71.         }
  72.     }
  73.  
  74.     public int this[int i, int j]
  75.     {
  76.         get
  77.         {
  78.             if (IsCorrect(i, j))
  79.             {
  80.                 for (int k = 0; k < NotZeroElements.Count; k++)
  81.                 {
  82.                     if (NotZeroElements[k].Item1 == i && NotZeroElements[k].Item2 == j)
  83.                     {
  84.                         return NotZeroElements[k].Item3;
  85.                     }
  86.                 }
  87.                 return 0;
  88.             }
  89.             throw new IndexOutOfRangeException();
  90.         }
  91.  
  92.         set
  93.         {
  94.             if (IsCorrect(i, j))
  95.             {
  96.                 if (value != 0)
  97.                 {
  98.                     NotZeroElements.Add((i, j, value));
  99.                     _version++;
  100.                 }
  101.  
  102.                 double Sparsity = 1 - ((double) NotZeroElements.Count / (N * M));
  103.  
  104.  
  105.                 if (Sparsity <= 0.5)
  106.                 {
  107.                     OnNotSparseMatrix(new NotSparseMatrixEventArgs(Sparsity));
  108.                 }
  109.             }
  110.             else throw new IndexOutOfRangeException();
  111.         }
  112.  
  113.     }
  114.     public event EventHandler NotSparseMatrix;
  115.  
  116.     public delegate void EventHandler(object sender, NotSparseMatrixEventArgs e);
  117.  
  118.     public override string ToString()
  119.     {
  120.         var result = new StringBuilder();
  121.         var k = 0;
  122.  
  123.         for (var i = 0; i < N; i++)
  124.         {
  125.             for (var j = 0; j < M; j++)
  126.             {
  127.                 if(k==NotZeroElements.Count)
  128.                 {
  129.                     result.Append("0");
  130.                     continue;
  131.                 }
  132.                 for (; k < NotZeroElements.Count;)
  133.                 {
  134.                     if (NotZeroElements[k].Item1 == i && NotZeroElements[k].Item2 == j)
  135.                     {
  136.                         result.Append(NotZeroElements[k].Item3.ToString());
  137.                         k++;
  138.                         break;
  139.                     }
  140.                     result.Append("0");
  141.                     break;
  142.                 }
  143.             }
  144.             result.Append(Environment.NewLine);
  145.         }
  146.         return result.ToString();
  147.     }
  148.  
  149.     protected virtual void OnNotSparseMatrix(NotSparseMatrixEventArgs e)
  150.     {
  151.         NotSparseMatrix?.Invoke(this, e);
  152.     }
  153.  
  154.     private bool IsCorrect(int i, int j) => i >= 0 && j >= 0 && i < N && j < M;
  155.  
  156.     public IEnumerable<(int,int,int)> GetNonzeroElements()
  157.     {
  158.        for (int i = 0; i < NotZeroElements.Count; i++)
  159.         {
  160.             for (int j = 0; j < NotZeroElements.Count - 1; j++)
  161.             {
  162.                 if (NotZeroElements[j].Item2 > NotZeroElements[j + 1].Item2)
  163.                 {
  164.                     var tmp = NotZeroElements[j];
  165.                     NotZeroElements[j] = NotZeroElements[j + 1];
  166.                     NotZeroElements[j + 1]=tmp;
  167.                 }
  168.             }
  169.         }
  170.         return NotZeroElements;
  171.     }
  172.  
  173.     public string GetCount(int x)
  174.     {
  175.         if (x != 0)
  176.         {
  177.             int count = 0;
  178.             for (int i = 0; i < NotZeroElements.Count; i++)
  179.             {
  180.                 if (NotZeroElements[i].Item3 == x)
  181.                 {
  182.                     count++;
  183.                 }
  184.             }
  185.             return $"The element {x} occurs {count} times";
  186.         }
  187.         return $"The element {x} occurs {N * M - NotZeroElements.Count} times";
  188.     }
  189.  
  190.     public IEnumerator<int> GetEnumerator()
  191.     {
  192.         return new SparseMatrixEnumerator(this);
  193.     }
  194.  
  195.     IEnumerator IEnumerable.GetEnumerator()
  196.     {
  197.         return GetEnumerator();
  198.     }
  199.  
  200.     private class SparseMatrixEnumerator: IEnumerator<int>
  201.     {
  202.         private readonly SparseMatrix _matrix;
  203.         private readonly int _capturedVersion;
  204.         private int _position = -1;
  205.  
  206.         private readonly string str;
  207.         private Regex _pattern = new Regex(@"\s+");
  208.  
  209.         public SparseMatrixEnumerator(SparseMatrix matrix)
  210.         {
  211.             _matrix = matrix;
  212.  
  213.             str = _pattern.Replace(matrix.ToString(), "");
  214.  
  215.             _capturedVersion = matrix._version;
  216.         }
  217.         public int Current => int.Parse(str[_position].ToString());
  218.  
  219.         object IEnumerator.Current => Current;
  220.  
  221.         public bool MoveNext()
  222.         {
  223.             if(_capturedVersion!=_matrix._version)
  224.             {
  225.                 throw new InvalidOperationException();
  226.             }
  227.  
  228.             if(_position<str.Length-1)
  229.             {
  230.                 _position++;
  231.                 return true;
  232.             }
  233.  
  234.             return false;
  235.         }
  236.  
  237.         public void Reset()
  238.         {
  239.             _position = -1;
  240.         }
  241.         public void Dispose()
  242.         {
  243.             // :)
  244.         }
  245.     }
  246. }
  247.  
  248.  
  249. public class NotSparseMatrixEventArgs : EventArgs
  250. {
  251.     public double Sparsity { get; }
  252.  
  253.     public NotSparseMatrixEventArgs(double sparsity)
  254.     {
  255.         Sparsity = sparsity;
  256.     }
  257. }
Add Comment
Please, Sign In to add comment