This repository was archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitMatrix.cs
More file actions
157 lines (134 loc) · 4.8 KB
/
BitMatrix.cs
File metadata and controls
157 lines (134 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using System.Collections;
using System.Collections.Generic;
// prostokątna macierz bitów o wymiarach m x n
public class BitMatrix : IEquatable<BitMatrix>
{
private BitArray data;
public int NumberOfRows { get; }
public int NumberOfColumns { get; }
public bool IsReadOnly => false;
public BitMatrix(int numberOfRows, int numberOfColumns, int defaultValue = 0)
{
if (numberOfRows < 1 || numberOfColumns < 1)
throw new ArgumentOutOfRangeException("Incorrect size of matrix");
data = new BitArray(numberOfRows*numberOfColumns, BitToBool(defaultValue));
NumberOfRows = numberOfRows;
NumberOfColumns = numberOfColumns;
}
public BitMatrix(int numberOfRows, int numberOfColumns, params int[] bits)
{
if (numberOfRows < 1 || numberOfColumns < 1)
throw new ArgumentOutOfRangeException("Incorrect size of matrix");
data = new BitArray(numberOfRows * numberOfColumns, false);
if (bits != null)
{
for (int i = 0; i < bits.Length && i < data.Length; i++)
{
if (bits[i] != 0)
data[i] = true;
}
}
NumberOfRows = numberOfRows;
NumberOfColumns = numberOfColumns;
}
public BitMatrix(int[,] bits)
{
if (bits is null)
throw new NullReferenceException("bits cannot be null");
int numberOfRows = bits.GetLength(0);
int numberOfColumns = bits.GetLength(1);
if (numberOfRows < 1 || numberOfColumns < 1)
throw new ArgumentOutOfRangeException("Incorrect size of matrix");
data = new BitArray(numberOfRows * numberOfColumns, false);
for (int row = 0; row < numberOfRows; row++)
{
for (int col = 0; col < numberOfColumns; col++)
{
if (bits[row, col] != 0)
data[row * numberOfColumns + col] = true;
}
}
NumberOfRows = numberOfRows;
NumberOfColumns = numberOfColumns;
}
public BitMatrix(bool[,] bits)
{
if (bits is null)
throw new NullReferenceException("bits cannot be null");
int numberOfRows = bits.GetLength(0);
int numberOfColumns = bits.GetLength(1);
if (numberOfRows < 1 || numberOfColumns < 1)
throw new ArgumentOutOfRangeException("Incorrect size of matrix");
data = new BitArray(numberOfRows * numberOfColumns, false);
for (int row = 0; row < numberOfRows; row++)
{
for (int col = 0; col < numberOfColumns; col++)
{
if (bits[row, col])
data[row * numberOfColumns + col] = true;
}
}
NumberOfRows = numberOfRows;
NumberOfColumns = numberOfColumns;
}
public static int BoolToBit(bool boolValue) => boolValue ? 1 : 0;
public static bool BitToBool(int bit) => bit != 0;
public override string ToString()
{
var result = "";
for (int row = 0; row < NumberOfRows; row++)
{
for (int col = 0; col < NumberOfColumns; col++)
{
int index = row * NumberOfColumns + col;
result += BoolToBit(data[index]);
}
result += Environment.NewLine; // dodanie znaku końca linii
}
return result;
}
// Przesłonięcie metody Equals zdefiniowanej w interfejsie IEquatable<BitMatrix>
public bool Equals(BitMatrix other)
{
if (other == null) return false;
if (NumberOfRows != other.NumberOfRows || NumberOfColumns != other.NumberOfColumns) return false;
for (int i = 0; i < data.Length; i++){
if (data[i] != other.data[i])
return false;
}
return true;
}
// Przesłonięcie metody Equals z klasy object
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
return false;
return Equals(obj as BitMatrix);
}
// Przesłonięcie metody GetHashCode z klasy object
public override int GetHashCode()
{
int hash = 17;
hash = hash * 23 + NumberOfRows.GetHashCode();
hash = hash * 23 + NumberOfColumns.GetHashCode();
for (int i = 0; i < data.Length; i++)
hash = hash * 23 + data[i].GetHashCode();
return hash;
}
// Przeciążenie operatora ==
public static bool operator ==(BitMatrix lhs, BitMatrix rhs)
{
if (ReferenceEquals(lhs, null))
{
if (ReferenceEquals(rhs, null)) return true;
return false;
}
return lhs.Equals(rhs);
}
// Przeciążenie operatora !=
public static bool operator !=(BitMatrix lhs, BitMatrix rhs)
{
return !(lhs == rhs);
}
}