-
Notifications
You must be signed in to change notification settings - Fork 1
/
caarrays.pas
143 lines (114 loc) · 3.37 KB
/
caarrays.pas
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
unit caArrays;
{$INCLUDE ca.inc}
interface
uses
Classes,
SysUtils;
const
cByteSize = SizeOf(Byte);
type
PWord = ^Word;
//----------------------------------------------------------------------------
// TcaBitArray
//----------------------------------------------------------------------------
TcaBitArray = class(TObject)
private
// Private fields
FArray: PWord;
protected
public
// Create/Destroy
constructor Create(ASize: Int64);
destructor Destroy; override;
end;
//----------------------------------------------------------------------------
// TcaBitMatrix
//----------------------------------------------------------------------------
TcaBitMatrix = class(TObject)
private
// Private fields
FBitOffset: Byte;
FCols: Integer;
FRows: Integer;
FMatrix: PWord;
FWordPtr: PWord;
// Private methods
procedure UpdateBitPosition(ACol, ARow: Integer);
procedure Initialize;
// Property methods
function GetBit(ACol, ARow: Integer): Boolean;
procedure SetBit(ACol, ARow: Integer; const AValue: Boolean);
protected
public
// Create/Destroy
constructor Create(ACols, ARows: Integer);
destructor Destroy; override;
// Properties
property Bits[ACol, ARow: Integer]: Boolean read GetBit write SetBit;
end;
//----------------------------------------------------------------------------
// TcaNibbleArray
//----------------------------------------------------------------------------
TcaNibbleArray = class(TObject)
private
protected
public
end;
implementation
const
cWordSize = SizeOf(Word);
//----------------------------------------------------------------------------
// TcaBitArray
//----------------------------------------------------------------------------
constructor TcaBitArray.Create(ASize: Int64);
begin
inherited Create;
GetMem(FArray, ASize);
end;
destructor TcaBitArray.Destroy;
begin
FreeMem(FArray);
inherited;
end;
//----------------------------------------------------------------------------
// TcaBitMatrix
//----------------------------------------------------------------------------
// Create/Destroy
constructor TcaBitMatrix.Create(ACols, ARows: Integer);
begin
inherited Create;
FCols := ACols;
FRows := ARows;
Initialize;
end;
destructor TcaBitMatrix.Destroy;
begin
FreeMem(FMatrix);
inherited;
end;
// Private methods
procedure TcaBitMatrix.Initialize;
begin
GetMem(FMatrix, FCols * FRows div cByteSize);
end;
procedure TcaBitMatrix.UpdateBitPosition(ACol, ARow: Integer);
var
BitPos: Integer;
begin
BitPos := ARow * FCols + ACol;
FWordPtr := FMatrix;
Inc(FWordPtr, BitPos div cWordSize);
FBitOffset := BitPos mod cWordSize;
end;
// Property methods
function TcaBitMatrix.GetBit(ACol, ARow: Integer): Boolean;
begin
UpdateBitPosition(ACol, ARow);
Result := (FWordPtr^ and (1 shl FBitOffset)) <> 0;
end;
procedure TcaBitMatrix.SetBit(ACol, ARow: Integer; const AValue: Boolean);
begin
UpdateBitPosition(ACol, ARow);
FWordPtr^ := FWordPtr^ or (1 shl FBitOffset);
end;
end.