Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SymmetricMatrix.cs
1using System;
2
4{
9 public class SymmetricMatrix<T>
10 {
11 private T[] elements;
12 private int size;
13 private int len;
14
19 : this(1)
20 {
21 }
22
28 {
29 if (Size <= 0)
30 throw new ArgumentOutOfRangeException(nameof(Size), "Must be positive.");
31
32 this.size = Size;
33 this.len = (this.size + 1) * this.size / 2;
34 this.elements = new T[this.len];
35 }
36
41 public int Size => this.size;
42
50 public T this[int X, int Y]
51 {
52 get
53 {
54 int i = this.GetIndex(X, Y);
55 if (i >= this.len)
56 return default;
57 else
58 return this.elements[i];
59 }
60
61 set
62 {
63 int i = this.GetIndex(X, Y);
64 if (i >= this.len)
65 {
66 this.size = Math.Max(X, Y) + 1;
67 this.len = i + 1;
68 Array.Resize(ref this.elements, this.len);
69 }
70
71 this.elements[i] = value;
72 }
73 }
74
75 private int GetIndex(int X, int Y)
76 {
77 if (X < 0)
78 throw new ArgumentOutOfRangeException(nameof(X), "Negative cooridates not allowed.");
79
80 if (Y < 0)
81 throw new ArgumentOutOfRangeException(nameof(Y), "Negative cooridates not allowed.");
82
83 if (X > Y)
84 return X * (X - 1) / 2 + Y;
85 else if (Y == 0)
86 return 0;
87 else
88 return Y * (Y - 1) / 2 + X;
89 }
90 }
91}
Implements a dynamic symmetric matrix.
int Size
Size of Matrix. The size represents both width and height, as a symmetric matrix have the same width ...
SymmetricMatrix()
Implements a dynamic symmetric matrix.
SymmetricMatrix(int Size)
Implements a dynamic symmetric matrix.