@@ -56,7 +56,7 @@ TStationForSort = class of TStationForSortClass;
5656
5757 // static arrays
5858 // size entry_name_array to hold longest (by byte count) name
59- // Dolores Hidalgo Cuna de la Independencia Nacional = 98 bytes
59+ // Dolores Hidalgo Cuna de la Independencia Nacional = 98 bytes
6060 // size entry_temp_array to hold -9999
6161 entry_name_array: array [0 .. 255 ] of Byte; // will hold place name
6262 entry_temp_array: array [0 .. 15 ] of Byte; // will hold place temperature
@@ -95,7 +95,7 @@ function MeanFixup(total: Integer; count: Integer): String; inline;
9595var
9696 temp: string;
9797 ratio: Integer;
98- // remainder: Integer;
98+ // remainder: Integer;
9999 neg: Boolean;
100100begin
101101 if total < 0 then
@@ -148,29 +148,31 @@ function BytesToTemp(count: Integer): Integer; inline;
148148// entry_temp_array is global, only pass length
149149// convert entry_chars_temp to signed integer, '0' is ascii 48 decimal
150150// note: we always have at least two bytes
151- // temperatures range from -99.9 to 99.8
152- // that appears here as -999 to 998
151+ // temperatures range from -99.9 to 99.8
152+ // that appears here as -999 to 998
153153var
154154 BTT: Integer;
155155begin
156156 if entry_temp_array[0 ] = 45 then // negative number
157157 begin
158- // BTT := -(entry_temp_array[count -1] -48) - 10 * (entry_temp_array[count -2] - 48);
159- // BTT := 48 - entry_temp_array[count -1] - 10 * (entry_temp_array[count -2] - 48);
160- // BTT := 48 + 480 - entry_temp_array[count -1] - 10 * (entry_temp_array[count -2]);
161- BTT := 528 - entry_temp_array[count -1 ] - 10 * (entry_temp_array[count -2 ]);
162- if count = 4 then // do one more digit
158+ // BTT := -(entry_temp_array[count -1] -48) - 10 * (entry_temp_array[count -2] - 48);
159+ // BTT := 48 - entry_temp_array[count -1] - 10 * (entry_temp_array[count -2] - 48);
160+ // BTT := 48 + 480 - entry_temp_array[count -1] - 10 * (entry_temp_array[count -2]);
161+ BTT := 528 - entry_temp_array[count - 1 ] - 10 *
162+ (entry_temp_array[count - 2 ]);
163+ if count = 4 then // do one more digit
163164 begin
164165 BTT := BTT - 100 * (entry_temp_array[1 ] - 48 );
165166 end ;
166167 end
167168 else
168169 begin
169- // BTT := (entry_temp_array[count -1] -48) + 10 * (entry_temp_array[count -2] - 48);
170- // BTT := entry_temp_array[count -1] -48 + 10 * (entry_temp_array[count -2] - 48);
171- // BTT := entry_temp_array[count -1] -48 - 480 + 10 * (entry_temp_array[count -2]);
172- BTT := -528 + (entry_temp_array[count -1 ]) + 10 * (entry_temp_array[count -2 ]);
173- if count = 3 then // do one more digit
170+ // BTT := (entry_temp_array[count -1] -48) + 10 * (entry_temp_array[count -2] - 48);
171+ // BTT := entry_temp_array[count -1] -48 + 10 * (entry_temp_array[count -2] - 48);
172+ // BTT := entry_temp_array[count -1] -48 - 480 + 10 * (entry_temp_array[count -2]);
173+ BTT := -528 + (entry_temp_array[count - 1 ]) + 10 *
174+ (entry_temp_array[count - 2 ]);
175+ if count = 3 then // do one more digit
174176 begin
175177 BTT := BTT + 100 * (entry_temp_array[0 ] - 48 );
176178 end ;
@@ -193,15 +195,6 @@ function BytesToIndexHash(count: Integer): Integer; inline;
193195 i: Integer;
194196 Hash: Integer;
195197begin
196- // Hash := 0;
197- // for i := 1 to length(A) do begin
198- // Hash := (Hash shl 4) + A[i];
199- // G := Hash and $F0000000;
200- // if (G <> 0) then
201- // Hash := (Hash xor (G shr 24)) xor G;
202- // end;
203- // BytesToIndexHash := Hash mod aTableSize; // original
204-
205198 Hash := 0 ;
206199 for i := 0 to count - 1 do
207200 begin
@@ -240,11 +233,10 @@ procedure InitializeThings; inline;
240233 StationsForSort.Sorted := False; // sort later
241234end ;
242235
243-
244236function StaticBytesToString (K: Integer): String; inline;
245237// entry_name_array is global, only pass length
246238var
247- ugly: TArray<Byte>; // prefer not to use TArray, but for now...
239+ ugly: TArray<Byte>; // prefer not to use TArray, but for now...
248240 i: Integer;
249241begin
250242 SetLength(ugly, K);
@@ -260,29 +252,22 @@ function StaticBytesToString(K: Integer): String; inline;
260252procedure HashAndSave (K: Integer; J: Integer); inline;
261253var
262254 entry_integer: Integer;
263- // entry_WS: WideString; // will hold place name
264255 entry_Unicode: String;
265256 my_data_item: TStationForSortClass;
266257
267- // HashHandled: Boolean;
268258 entry_hash: Integer;
269259 PlaceIndex: Integer; // location in index array
270260
271261begin
272262 entry_integer := BytesToTemp(J); // only pass J
273263
274- // entry_WS := TEncoding.UTF8.GetString(a_name);
275- // entry_Unicode := TEncoding.Unicode.GetString(a_name[0]); // error of unknown character - try again with fixed gloobal array
276-
277264 entry_Unicode := StaticBytesToString(K);
278265 // entry_name_array is global, only pass length
279266
280267 entry_hash := BytesToIndexHash(K);
281268 // entry_name_array is global, only pass length
282269
283- // HashHandled := False;
284- // while Not(HashHandled) do
285- while True do // success is handled by breaking out of while loop
270+ while True do // success is handled by breaking out of while loop
286271 begin
287272 PlaceIndex := HashMap[entry_hash];
288273
@@ -299,7 +284,6 @@ procedure HashAndSave(K: Integer; J: Integer); inline;
299284 StationsForSort.AddObject(entry_Unicode, TObject(my_data_item));
300285
301286 Inc(PlaceCount);
302- // HashHandled := True;
303287 Break;
304288 end
305289 else // hash has been used, check for collision
@@ -324,7 +308,6 @@ procedure HashAndSave(K: Integer; J: Integer); inline;
324308 (StationsForSort.Objects[PlaceIndex] as TStationForSortClass).DataMin
325309 := entry_integer;
326310
327- // HashHandled := True;
328311 Break;
329312 end
330313 else // collision, try next spot in HashMap array
@@ -334,8 +317,7 @@ procedure HashAndSave(K: Integer; J: Integer); inline;
334317 entry_hash := 0 ; // wrap around
335318 end ;
336319 end ;
337- // end; // of: while Not(HashHandled) do
338- end ; // of: while True do
320+ end ; // of: while True do
339321
340322end ;
341323
@@ -354,6 +336,8 @@ procedure FileToArrays(inFile: String; UseStdOut: Boolean); inline;
354336 iBytesRead: Integer;
355337 Buffer: PByte;
356338
339+ Challenge: TFileStream;
340+
357341begin
358342
359343 if Not (UseStdOut) then
@@ -368,23 +352,23 @@ procedure FileToArrays(inFile: String; UseStdOut: Boolean); inline;
368352
369353 if FileExists(inFile) then
370354 begin
355+
371356 try
372- iFileHandle := FileOpen(inFile, fmOpenRead);
373- FileSeek(iFileHandle, 0 , 0 ); // position to BOF
374- Buffer := System.AllocMem(myBufferSize + 1 );
357+ Challenge := TFileStream.Create(inFile, fmOpenRead);
358+ Challenge.Seek(0 , soFromBeginning);
359+ // Buffer := System.AllocMem(myBufferSize +1); // why +1 ??
360+ Buffer := System.AllocMem(myBufferSize); // seems OK
375361 i := 0 ;
376362 J := 0 ;
377363 K := 0 ;
378364 LF := False;
379365 SC := False;
380- // SetLength(entry_chars, 0);
381- // SetLength(entry_chars_temp, 0);
382366 move(empty_entry_name_array[0 ], entry_name_array[0 ], 256 );
383367 // will hold place name
384368 move(empty_entry_temp_array[0 ], entry_temp_array[0 ], 16 );
385369 // will hold temperature
386370
387- iBytesRead := FileRead(iFileHandle, Buffer^, myBufferSize);
371+ iBytesRead := Challenge.Read( Buffer^, myBufferSize);
388372 while iBytesRead > 0 do
389373 begin
390374 while Not LF do // read byte by byte
@@ -420,7 +404,7 @@ procedure FileToArrays(inFile: String; UseStdOut: Boolean); inline;
420404 Inc(i); // next byte
421405 if i >= iBytesRead then // need next buffer
422406 begin
423- iBytesRead := FileRead(iFileHandle, Buffer^, myBufferSize);
407+ iBytesRead := Challenge.Read( Buffer^, myBufferSize);
424408 i := 0 ; // reset to beggining of buffer
425409 end ;
426410
@@ -437,8 +421,6 @@ procedure FileToArrays(inFile: String; UseStdOut: Boolean); inline;
437421 // reset to get next line
438422 LF := False;
439423 SC := False;
440- // SetLength(entry_chars, 0);
441- // SetLength(entry_chars_temp, 0);
442424 J := 0 ;
443425 K := 0 ;
444426 move(empty_entry_name_array[0 ], entry_name_array[0 ], 256 );
@@ -447,11 +429,12 @@ procedure FileToArrays(inFile: String; UseStdOut: Boolean); inline;
447429 // will hold temperature
448430
449431 end ; // of while iBytesRead > 0 do --- implies end of file
450- FileClose(iFileHandle) ;
432+ Challenge.Free ;
451433
452434 finally
453435 // nothing here
454436 end ;
437+
455438 end
456439 else
457440 begin
@@ -469,17 +452,12 @@ procedure ArrayToFile(outFile: String; UseStdOut: Boolean);
469452// Inline; // inline not help
470453var
471454 outputFileStream: TFileStream;
472- // StationCount: Integer;
473455 PlaceIndex: Integer; // location in array
474456 bufferStr: String;
475- // EntryName: WideString;
476457 EntrySum: Integer;
477458 EntryCount: Integer;
478459 EntryMax: Integer;
479460 EntryMin: Integer;
480- // temp: UnicodeString;
481-
482- // LookUpIndex: Integer;
483461
484462begin
485463 try
@@ -512,16 +490,6 @@ procedure ArrayToFile(outFile: String; UseStdOut: Boolean);
512490 IntegerFixup(EntryMin) + ' /' + MeanFixup(EntrySum, EntryCount) + ' /' +
513491 IntegerFixup(EntryMax);
514492
515- // LookUpIndex := StationName[PlaceIndex].sIndex;
516- // EntryName := StationName[PlaceIndex].sName;
517- // EntrySum := StationData[LookUpIndex].DataSum;
518- // EntryCount := StationData[LookUpIndex].DataCount;
519- // EntryMax := StationData[LookUpIndex].DataMax;
520- // EntryMin := StationData[LookUpIndex].DataMin;
521- //
522- // bufferStr := bufferStr + EntryName + '=' + IntegerFixup(EntryMin) + '/' +
523- // MeanFixup(EntrySum, EntryCount) + '/' + IntegerFixup(EntryMax);
524-
525493 if UseStdOut then // send to STDOUT, where it gets mangled
526494 begin
527495 write(bufferStr);
@@ -531,7 +499,6 @@ procedure ArrayToFile(outFile: String; UseStdOut: Boolean);
531499 outputFileStream.WriteBuffer(TEncoding.UTF8.GetBytes(bufferStr),
532500 TEncoding.UTF8.GetByteCount(bufferStr));
533501 end ;
534-
535502 end ;
536503
537504 bufferStr := ' }' + Chr(10 ); // linefeed appears at end of baseline file
0 commit comments