Changeset 38 for trunk/Packages/Common/UMemory.pas
- Timestamp:
- May 10, 2018, 9:39:53 AM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Packages/Common/UMemory.pas
r37 r38 10 10 type 11 11 12 { T Block}12 { TMemory } 13 13 14 T Block= class14 TMemory = class 15 15 private 16 16 FData: PByte; 17 17 FSize: Integer; 18 function GetItem(Index: Integer): Byte; virtual; abstract;19 procedure SetItem(Index: Integer; AValue: Byte); virtual; abstract;20 procedure SetSize(AValue: Integer); virtual; abstract;18 function GetItem(Index: Integer): Byte; 19 procedure SetItem(Index: Integer; AValue: Byte); 20 procedure SetSize(AValue: Integer); virtual; 21 21 public 22 procedure ReadBlock(Block: TBlock; Position: Integer); virtual; 23 procedure WriteBlock(Block: TBlock; Position: Integer); virtual; 24 procedure Clear(Value: Byte = 0); virtual; 25 procedure Assign(Source: TBlock); virtual; 26 property Size: Integer read FSize write SetSize; 27 property Items[Index: Integer]: Byte read GetItem write SetItem; default; 28 end; 29 30 { TMemory } 31 32 TMemory = class(TBlock) 33 private 34 FData: PByte; 35 FSize: Integer; 36 function GetItem(Index: Integer): Byte; override; 37 procedure SetItem(Index: Integer; AValue: Byte); override; 38 procedure SetSize(AValue: Integer); override; 39 public 40 procedure Clear(Value: Byte = 0); override; 41 procedure Assign(Source: TBlock); override; 22 procedure Clear(Value: Byte = 0); 23 procedure Assign(Source: TMemory); 42 24 constructor Create; 43 25 destructor Destroy; override; 44 26 property Data: PByte read FData; 27 property Size: Integer read FSize write SetSize; 28 property Items[Index: Integer]: Byte read GetItem write SetItem; default; 45 29 end; 46 30 … … 58 42 end; 59 43 60 { TBitBlock }61 62 TBitBlock = class63 private64 function GetItem(Index: Integer): Byte; virtual;65 function GetSize: Integer; virtual;66 procedure SetItem(Index: Integer; AValue: Byte); virtual;67 procedure SetSize(AValue: Integer); virtual;68 public69 procedure Invert; virtual;70 function GetInteger: Integer; virtual;71 procedure SetInteger(Value: Integer); virtual;72 procedure ReadBlock(Block: TBitBlock; Position: Integer); virtual;73 procedure WriteBlock(Block: TBitBlock; Position: Integer); virtual;74 procedure Clear(Value: Byte = 0); virtual;75 procedure Assign(Source: TBlock); virtual;76 property Size: Integer read GetSize write SetSize;77 property Items[Index: Integer]: Byte read GetItem write SetItem; default;78 end;79 80 { TBitMemory }81 82 TBitMemory = class(TBitBlock)83 private84 FData: PByte;85 FSize: Integer;86 function GetSize: Integer; override;87 procedure SetSize(AValue: Integer); override;88 function GetItem(Index: Integer): Byte; override;89 procedure SetItem(Index: Integer; AValue: Byte); override;90 public91 constructor Create;92 destructor Destroy; override;93 function GetInteger: Integer; override;94 procedure SetInteger(Value: Integer); override;95 procedure Clear(Value: Byte = 0); override;96 procedure ReadBlock(Block: TBitBlock; Position: Integer); override;97 procedure WriteBlock(Block: TBitBlock; Position: Integer); override;98 property Data: PByte read FData;99 procedure Invert; override;100 end;101 102 103 44 implementation 104 105 { TBitMemory }106 107 procedure TBitMemory.Clear(Value: Byte);108 begin109 if (Size and 7) = 0 then begin110 if Value = 0 then FillChar(FData^, Size shr 3, 0)111 else FillChar(FData^, Size shr 3, $ff);112 end else inherited;113 end;114 115 procedure TBitMemory.ReadBlock(Block: TBitBlock; Position: Integer);116 begin117 if Block is TBitMemory then begin118 if (Position and 7) = 0 then begin119 if (Block.Size and 7) = 0 then120 Move(PByte(FData + Position shr 3)^, TBitMemory(Block).Data^, Block.Size shr 3)121 else inherited;122 end else inherited;123 end else inherited;124 end;125 126 procedure TBitMemory.WriteBlock(Block: TBitBlock; Position: Integer);127 begin128 if Block is TBitMemory then begin129 if (Position and 7) = 0 then begin130 if (Block.Size and 7) = 0 then131 Move(TBitMemory(Block).Data^, PByte(FData + Position shr 3)^, Block.Size shr 3)132 else inherited;133 end else inherited;134 end else inherited;135 end;136 137 procedure TBitMemory.Invert;138 var139 I: Integer;140 begin141 if (Size and 7) = 0 then begin142 for I := 0 to (Size shr 3) - 1 do143 PByte(FData + I)^ := PByte(FData + I)^ xor $ff;144 end145 else inherited;146 147 end;148 149 function TBitMemory.GetInteger: Integer;150 var151 I: Integer;152 V: Integer;153 begin154 Result := 0;155 I := 0;156 while (I < 32) and (I < Size) do begin157 V := FData[I shr 3];158 V := V shl I;159 Result := Result or V;160 // Result := Result or (FData[I shr 3] shl I);161 Inc(I, 8);162 end;163 if Size < 32 then164 Result := Result and ((1 shl Size) - 1);165 end;166 167 procedure TBitMemory.SetInteger(Value: Integer);168 var169 I: Integer;170 begin171 I := 0;172 while (I < 32) and (I < Size) do begin173 FData[I shr 3] := (Value shr I) and $ff;174 Inc(I, 8);175 end;176 end;177 178 function TBitMemory.GetSize: Integer;179 begin180 Result := FSize;181 end;182 183 procedure TBitMemory.SetSize(AValue: Integer);184 var185 ByteSize: Integer;186 begin187 if AValue = FSize then Exit;188 FSize := AValue;189 ByteSize := FSize shr 3;190 if (FSize and 7) > 0 then Inc(ByteSize);191 FData := ReAllocMem(FData, ByteSize);192 end;193 194 function TBitMemory.GetItem(Index: Integer): Byte;195 begin196 if Index >= Size then raise Exception.Create('Out of range');197 Result := (PByte(FData + (Index shr 3))^ shr (Index and 7)) and 1;198 end;199 200 procedure TBitMemory.SetItem(Index: Integer; AValue: Byte);201 begin202 if Index >= Size then raise Exception.Create('Out of range, Size:' + IntToStr(Size) + ', Index:' + IntToStr(Index));203 PByte(FData + (Index shr 3))^ := PByte(FData + (Index shr 3))^ and not (1 shl (Index and 7))204 or ((AValue and 1) shl (Index and 7));205 end;206 207 constructor TBitMemory.Create;208 begin209 FData := nil;210 end;211 212 destructor TBitMemory.Destroy;213 begin214 FreeMem(FData);215 inherited Destroy;216 end;217 218 { TBitBlock }219 220 function TBitBlock.GetItem(Index: Integer): Byte;221 begin222 Result := 0;223 end;224 225 function TBitBlock.GetSize: Integer;226 begin227 Result := 0;228 end;229 230 procedure TBitBlock.SetItem(Index: Integer; AValue: Byte);231 begin232 233 end;234 235 procedure TBitBlock.SetSize(AValue: Integer);236 begin237 end;238 239 procedure TBitBlock.Invert;240 var241 I: Integer;242 begin243 for I := 0 to Size - 1 do244 Items[I] := not Items[I];245 end;246 247 function TBitBlock.GetInteger: Integer;248 begin249 Result := 0;250 end;251 252 procedure TBitBlock.SetInteger(Value: Integer);253 begin254 255 end;256 257 procedure TBitBlock.ReadBlock(Block: TBitBlock; Position: Integer);258 var259 I: Integer;260 begin261 for I := 0 to Block.Size - 1 do262 Block.Items[I] := Items[Position + I];263 end;264 265 procedure TBitBlock.WriteBlock(Block: TBitBlock; Position: Integer);266 var267 I: Integer;268 begin269 for I := 0 to Block.Size - 1 do270 Items[Position + I] := Block.Items[I];271 end;272 273 procedure TBitBlock.Clear(Value: Byte);274 var275 I: Integer;276 begin277 for I := 0 to Size - 1 do278 Items[I] := Value;279 end;280 281 procedure TBitBlock.Assign(Source: TBlock);282 var283 I: Integer;284 begin285 Size := Source.Size;286 for I := 0 to Size - 1 do287 Items[I] := Source.Items[I];288 end;289 290 { TBlock }291 292 procedure TBlock.ReadBlock(Block: TBlock; Position: Integer);293 var294 I: Integer;295 begin296 if Position + Block.Size > Size then raise Exception.Create('');297 for I := 0 to Block.Size - 1 do298 Items[I] := Items[Position + I];299 end;300 301 procedure TBlock.WriteBlock(Block: TBlock; Position: Integer);302 var303 I: Integer;304 begin305 if Position + Block.Size > Size then raise Exception.Create('');306 for I := 0 to Block.Size - 1 do307 Items[Position + I] := Items[I];308 end;309 310 procedure TBlock.Clear(Value: Byte);311 var312 I: Integer;313 begin314 for I := 0 to Size - 1 do315 Items[I] := Value;316 end;317 318 procedure TBlock.Assign(Source: TBlock);319 var320 I: Integer;321 begin322 Size := Source.Size;323 for I := 0 to Size - 1 do324 Items[I] := Source.Items[I];325 end;326 45 327 46 { TPositionMemory } … … 371 90 end; 372 91 373 procedure TMemory.Assign(Source: T Block);92 procedure TMemory.Assign(Source: TMemory); 374 93 begin 375 if Source is TMemory then begin 376 Size := Source.Size; 377 Move(TMemory(Source).Data^, FData^, Size); 378 end else inherited; 94 Size := Source.Size; 95 Move(Source.Data^, FData^, Size); 379 96 end; 380 97
Note:
See TracChangeset
for help on using the changeset viewer.