Revision | 264 (tree) |
---|---|
Zeit | 2016-05-30 01:07:43 |
Autor | twm |
* better error message
* summary comment
@@ -8,6 +8,13 @@ | ||
8 | 8 | u_dzTranslator; |
9 | 9 | |
10 | 10 | type |
11 | + ///<summary> | |
12 | + /// A List for storing Int64 values | |
13 | + /// Note: If you use Add to add a huge amount of items to the list, you will | |
14 | + /// likely get an EOutOfMemory exception before actually running out of memory | |
15 | + /// because of memory framentation. If you know how many items you want to | |
16 | + /// store, set Capacity to that number. Capacity can be set to a maximum of | |
17 | + /// Classes.MaxListSize which means the list takes up around 1 GB of memory. </summary> | |
11 | 18 | TInt64List = class |
12 | 19 | private |
13 | 20 | FData: array of Int64; |
@@ -16,7 +23,7 @@ | ||
16 | 23 | procedure Grow; |
17 | 24 | procedure SetCapacity(_NewCapacity: integer); |
18 | 25 | function GetItems(_Idx: integer): Int64; |
19 | - procedure SetItems(_Idx: integer; const Value: Int64); | |
26 | + procedure SetItems(_Idx: integer; const _Value: Int64); | |
20 | 27 | public |
21 | 28 | function Add(_Value: Int64): integer; |
22 | 29 | property Items[_Idx: integer]: Int64 read GetItems write SetItems; |
@@ -26,9 +33,6 @@ | ||
26 | 33 | |
27 | 34 | implementation |
28 | 35 | |
29 | -uses | |
30 | - RTLConsts; | |
31 | - | |
32 | 36 | { TInt64List } |
33 | 37 | |
34 | 38 | function TInt64List.Add(_Value: Int64): integer; |
@@ -55,9 +59,14 @@ | ||
55 | 59 | |
56 | 60 | procedure TInt64List.SetCapacity(_NewCapacity: integer); |
57 | 61 | begin |
58 | - if (_NewCapacity < FCount) or (_NewCapacity > MaxListSize) then | |
59 | - raise Exception.CreateFmt(_('List capacity out of bounds (%d)'), [_NewCapacity]); | |
60 | 62 | if _NewCapacity <> FCapacity then begin |
63 | + if (_NewCapacity < FCount) then | |
64 | + raise Exception.CreateFmt(_('Cannot set list capacity (%d) to less than current item count (%d).'), | |
65 | + [_NewCapacity, FCount]); | |
66 | + if (_NewCapacity > MaxListSize) then | |
67 | + raise Exception.CreateFmt(_('Cannot set list capacity (%d) higher than MaxListSize (%d).'), | |
68 | + [_NewCapacity, MaxListSize]); | |
69 | + | |
61 | 70 | SetLength(FData, _NewCapacity); |
62 | 71 | FCapacity := _NewCapacity; |
63 | 72 | end; |
@@ -66,13 +75,15 @@ | ||
66 | 75 | function TInt64List.GetItems(_Idx: integer): Int64; |
67 | 76 | begin |
68 | 77 | if (_Idx < 0) or (_Idx >= FCount) then |
69 | - raise Exception.CreateFmt(_('List index out of bounds (%d)'), [_Idx]); | |
78 | + raise Exception.CreateFmt(_('List index out of bounds (%d) (Count=%d)'), [_Idx, FCount]); | |
70 | 79 | Result := FData[_Idx]; |
71 | 80 | end; |
72 | 81 | |
73 | -procedure TInt64List.SetItems(_Idx: integer; const Value: Int64); | |
82 | +procedure TInt64List.SetItems(_Idx: integer; const _Value: Int64); | |
74 | 83 | begin |
75 | - | |
84 | + if (_Idx < 0) or (_Idx >= FCount) then | |
85 | + raise Exception.CreateFmt(_('List index out of bounds (%d) (Count=%d)'), [_Idx, FCount]); | |
86 | + FData[_Idx] := _Value; | |
76 | 87 | end; |
77 | 88 | |
78 | 89 | end. |
@@ -19,7 +19,7 @@ | ||
19 | 19 | FCount: Int64; |
20 | 20 | FTempFile: TdzFile; |
21 | 21 | function GetItems(_Idx: Int64): Int64; |
22 | - procedure SetItems(_Idx: Int64; const Value: Int64); | |
22 | + procedure SetItems(_Idx: Int64; const _Value: Int64); | |
23 | 23 | procedure AssureBufferFor(_Idx: integer); |
24 | 24 | public |
25 | 25 | constructor Create(_fn: string = ''); |
@@ -83,14 +83,17 @@ | ||
83 | 83 | function TInt64ListFile.GetItems(_Idx: Int64): Int64; |
84 | 84 | begin |
85 | 85 | if (_Idx < 0) or (_Idx >= FCount) then |
86 | - raise Exception.CreateFmt(_('List index out of bounds (%d)'), [_Idx]); | |
86 | + raise Exception.CreateFmt(_('List index out of bounds (%d) (Count=%d)'), [_Idx, FCount]); | |
87 | 87 | AssureBufferFor(_Idx); |
88 | 88 | Result := FBuffer[_Idx - FOffset]; |
89 | 89 | end; |
90 | 90 | |
91 | -procedure TInt64ListFile.SetItems(_Idx: Int64; const Value: Int64); | |
91 | +procedure TInt64ListFile.SetItems(_Idx: Int64; const _Value: Int64); | |
92 | 92 | begin |
93 | - | |
93 | + if (_Idx < 0) or (_Idx >= FCount) then | |
94 | + raise Exception.CreateFmt(_('List index out of bounds (%d) (Count=%d)'), [_Idx, FCount]); | |
95 | + AssureBufferFor(_Idx); | |
96 | + FBuffer[_Idx - FOffset] := _Value; | |
94 | 97 | end; |
95 | 98 | |
96 | 99 | end. |