1 | unit Memory;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, Cpu;
|
---|
7 |
|
---|
8 | type
|
---|
9 |
|
---|
10 | { TMemory }
|
---|
11 |
|
---|
12 | TMemory = class
|
---|
13 | private
|
---|
14 | FCapacity: Integer;
|
---|
15 | FSize: Integer;
|
---|
16 | function GetItem(Index: Integer): TInteger;
|
---|
17 | procedure SetCapacity(AValue: Integer);
|
---|
18 | procedure SetItem(Index: Integer; AValue: TInteger);
|
---|
19 | procedure SetSize(AValue: Integer);
|
---|
20 | procedure UpdateCapacity(ASize: Integer);
|
---|
21 | public
|
---|
22 | Data: array of TInteger;
|
---|
23 | Position: Integer;
|
---|
24 | procedure Write(Value: TInteger);
|
---|
25 | procedure WriteString(Value: string);
|
---|
26 | procedure WriteMemory(Memory: TMemory);
|
---|
27 | function Read: TInteger;
|
---|
28 | procedure Assign(Source: TMemory);
|
---|
29 | procedure SaveToFile(FileName: string);
|
---|
30 | property Size: Integer read FSize write SetSize;
|
---|
31 | property Capacity: Integer read FCapacity write SetCapacity;
|
---|
32 | property Items[Index: Integer]: TInteger read GetItem write SetItem; default;
|
---|
33 | end;
|
---|
34 |
|
---|
35 |
|
---|
36 | implementation
|
---|
37 |
|
---|
38 | { TMemory }
|
---|
39 |
|
---|
40 | procedure TMemory.SetCapacity(AValue: Integer);
|
---|
41 | begin
|
---|
42 | if FCapacity = AValue then Exit;
|
---|
43 | FCapacity := AValue;
|
---|
44 | SetLength(Data, FCapacity);
|
---|
45 | if FSize > FCapacity then FSize := FCapacity;
|
---|
46 | end;
|
---|
47 |
|
---|
48 | function TMemory.GetItem(Index: Integer): TInteger;
|
---|
49 | begin
|
---|
50 | Result := Data[Index];
|
---|
51 | end;
|
---|
52 |
|
---|
53 | procedure TMemory.SetItem(Index: Integer; AValue: TInteger);
|
---|
54 | begin
|
---|
55 | Data[Index] := AValue;
|
---|
56 | end;
|
---|
57 |
|
---|
58 | procedure TMemory.SetSize(AValue: Integer);
|
---|
59 | begin
|
---|
60 | if FSize = AValue then Exit;
|
---|
61 | UpdateCapacity(AValue);
|
---|
62 | FSize := AValue;
|
---|
63 | end;
|
---|
64 |
|
---|
65 | procedure TMemory.UpdateCapacity(ASize: Integer);
|
---|
66 | const
|
---|
67 | MinStep = 1000;
|
---|
68 | begin
|
---|
69 | if ASize > FSize then begin // Grow
|
---|
70 | if (ASize > Capacity) then begin
|
---|
71 | if ((ASize - Size) < MinStep) then Capacity := Capacity + MinStep
|
---|
72 | else Capacity := ASize;
|
---|
73 | end;
|
---|
74 | end else
|
---|
75 | if ASize < FSize then begin // Shrink
|
---|
76 | if (Capacity - ASize) > MinStep then
|
---|
77 | Capacity := ASize;
|
---|
78 | end;
|
---|
79 | end;
|
---|
80 |
|
---|
81 | procedure TMemory.Write(Value: TInteger);
|
---|
82 | begin
|
---|
83 | if Position + 1 > FSize then Size := Position + 1;
|
---|
84 | Data[Position] := Value;
|
---|
85 | Inc(Position);
|
---|
86 | end;
|
---|
87 |
|
---|
88 | procedure TMemory.WriteString(Value: string);
|
---|
89 | var
|
---|
90 | I: Integer;
|
---|
91 | begin
|
---|
92 | if Length(Value) > 0 then begin
|
---|
93 | if Position + Length(Value) > FSize then Size := Position + Length(Value);
|
---|
94 | for I := 0 to Length(Value) - 1 do
|
---|
95 | Data[Position + I] := Ord(Value[I + 1]);
|
---|
96 | Inc(Position, Length(Value));
|
---|
97 | end;
|
---|
98 | end;
|
---|
99 |
|
---|
100 | procedure TMemory.WriteMemory(Memory: TMemory);
|
---|
101 | begin
|
---|
102 | if Memory.Size > 0 then begin
|
---|
103 | if Position + Memory.Size > FSize then Size := Position + Memory.Size;
|
---|
104 | Move(Memory.Data[0], Data[Position], Memory.Size * SizeOf(TInteger));
|
---|
105 | Inc(Position, Memory.Size);
|
---|
106 | end;
|
---|
107 | end;
|
---|
108 |
|
---|
109 | function TMemory.Read: TInteger;
|
---|
110 | begin
|
---|
111 | Result := Data[Position];
|
---|
112 | Inc(Position);
|
---|
113 | end;
|
---|
114 |
|
---|
115 | procedure TMemory.Assign(Source: TMemory);
|
---|
116 | begin
|
---|
117 | Size := Source.Size;
|
---|
118 | if Size > 0 then
|
---|
119 | Move(Source.Data[0], Data[0], Size * SizeOf(TInteger));
|
---|
120 | Position := Source.Position;
|
---|
121 | end;
|
---|
122 |
|
---|
123 | procedure TMemory.SaveToFile(FileName: string);
|
---|
124 | var
|
---|
125 | F: TFileStream;
|
---|
126 | begin
|
---|
127 | if FileExists(FileName) then
|
---|
128 | F := TFileStream.Create(FileName, fmOpenWrite)
|
---|
129 | else F := TFileStream.Create(FileName, fmCreate);
|
---|
130 | try
|
---|
131 | if Size > 0 then F.Write(Data[0], Size * SizeOf(TInteger));
|
---|
132 | finally
|
---|
133 | F.Free;
|
---|
134 | end;
|
---|
135 | end;
|
---|
136 |
|
---|
137 | end.
|
---|
138 |
|
---|