Fast linear list that excludes duplicates - delphi
I have the following code:
procedure TCellBlock.GeneratePtoQ;
var
x,y: integer;
i: integer;
Change: cardinal;
begin
i:= 0;
//Walk the grid of changed (alive) cells
for x:= GridMaxX downto 1 do begin
for y:= GridMaxY downto 1 do begin
if Active[cIndexP][x, y] then begin
Active[cIndexP][x,y]:= false;
//Put active items on the stack.
ToDo[i]:= x shl 16 or y;
Inc(i);
end; {if}
end; {for y}
end; {for x}
while i > 0 do begin
Dec(i);
y:= ToDo[i] and $FFFF;
x:= ToDo[i] shr 16;
//Calculate the cell, Change = (oldval XOR newval)
Change:= Grid[x,y].GeneratePtoQ;
//Mark the cells in the grid that need to be recalculated next generation.
Active[cIndexQ][x,y]:= Active[cIndexQ][x,y] or (Change <> 0);
Active[cIndexQ][x+1,y+1]:= Active[cIndexQ][x+1,y+1] or ((Change and $cc000000) <> 0);
Active[cIndexQ][x+1,y]:= Active[cIndexQ][x+1,y] or ((Change and $ff000000) <> 0);
Active[cIndexQ][x,y+1]:= Active[cIndexQ][x,y+1] or ((Change and $cccccccc) <> 0);
end; {while}
end;
The above is a code snippet of a test program that calculates conway's game of life.
The code needs to be as fast as possible. And for this purpose I'm trying different approaches.
It walks though a grid of active cells, looks to see which cells are active and puts those
on a stack.
Next it processes the items on the stack and sees which cells have changed.
If a cell has changed it updates the changes into the grid for the next generation.
I store cells in 32bit cardinals (4 bits Y, 8 bits X) and the P (even) generations are offset 1,1 pixel relative to the Q (odd) generations, this way I only have to take 3 neighbors into account instead of 8.
Question
I want to get rid of the grid, I just want to deal with the stack.
How do I implement a stack that eliminates duplicates?
Note that it needs to be as fast as possible and I'm not above using dirty tricks to get that.
if i understood what you asked you want the stack to have no duplication values. i'm not a delphi person but if it was java i would created a hashmap/ map tree and add each value to the map and before adding it to the stack check if it's already in the hash. you can also add all the values th the hash iterate it but you will loose the order of the hash.
Personally I'd take a completely different approach. First I don't see how you don't have to take all neighbours into account just because of using a 1,1 offset and then I doubt that bitshifting tricks make the algorithm much faster (often enough it's the contrary, but then it could be mem bandwidth constrained in which case we'd win a bit)
So I'd just go for the one thing that should bring by far the largest performance gain: Making the algorithm multithreaded. In our world of Quad/Hex/Octacores worrying about a few percent performance increases while wasting 300% or more seems silly. So if we'd ignore the active grids and check all fields the algorithm would be trivial with some great scaling, especially since one could easily vectorize the algorithm, but then that's not especially work efficient so I'd try some different approaches towards multithreading an algorithm that only takes the active cells in account.
First instead of getting rid of the grid I'd double it: One src and one dest grid - that are swapped each round. No locking to access the grid necessary, don't have to worry about when updating the fields and no stale entries (important for multithreading we want to use the cache after all).
Now the simplest solution would be to use some kind of concurrent list structure (no idea about delphi libraries) for the active cells and let each thread steal from it and add new active cells to another. With a good lock-free implementation of a concurrent queue (basically whatever the replacement of this is in delphi) or something similar could be quite nice and simple. For a better performance instead of adding single nodes to the list, I'd think about adding whole chunks to the list, say in sizes of 10 or so - more work with less overhead but if we make the chunks too large we lose parallelism.
I can think of other solutions like giving every thread one list of active cells to work through (or more exactly one list for all and different offsets) but then we have to between each run gather all new entries (not much synchronization overhead but some copying) into a list - worth a try I assume.
If your goal is speed (and only speed). There is a few tricks that can speed things up a LOT. My own implementation of the Conway's Game of Life use those tricks to make it faster. Note that it is VERY expensive on memory.
Each cells are an object
Each cell object contains its X/Y coordinates
Each cell object contains a "live" counters of the number of Alive neighbors. (When a cell turns On/Off, it notify it's neighbor so they update their counters.
To make #3 works, when the next generation is calculated, cells are not turned On/Off right away. They are instead stacked into a list until all cells are calculated.
Each cell has a counter which indicate which is the last generation they changed on. That avoid calculating the same cell twice. (My alternative to the stack that eliminates duplicates)
The list of #5 is reused on the next generation, as only the neighbors of a cell that changed on the previous generation can change on the current one.
There are some of the tricks I use to speed up the generation. Some of the tricks listed here will get you a lot more than multithreading your implementation. But using both those and multithread will get the most performance possible.
As for the multithread subject, read Voo's entry.
I've been thinking about it and I think I have a solution.
some background
Here's how the data is in laid out in memory
00 A 08 B 10 18 The bits of Individual int32's are layout like this:
01 | 09 | 11 19 00 04 08 0C 10 14 18 1C // N-Mask: $33333333
02 | 0A | 12 1A 01 05 09 0D 11 15 19 1D // S-Mask: $cccccccc
03 | 0B | 13 1B 02 06 0A 0E 12 16 1A 1E // W-Mask: $000000ff
04 | 0C | 14 1C 03 07 0B 0F 13 17 1B 1F // E-Mask: $ff000000
05 | 0D | 15 1D //SE-Mask: $cc000000
06 | 0E | 16 1E //NW-Mask: $00000033
07 V 0F V 17 1F I can mask of different portions if need be.
-- Figure A: Grid -- -- Figure B: cell -- -- Table C: masks --
I haven't decided on the size of the building block, but this is the general idea.
Even generations are called P, odd generations are called Q.
They are staggered like this
+----------------+<<<<<<<< P 00 04 08 0C //I use a 64K lookup
|+---------------|+ 01 05* 09* 0D //table to lookup
|| || 02 06* 0A* 0E //the inner* 2x2 bits from
|| || 03 07 0B 0F //a 4x4 grid.
+----------------+| //I need to do 8 lookups for a 32 bit cell
+----------------+<<<<<<<< Q
- Figure D: Cells are staggered - -- Figure E: lookup --
This way when generating P -> Q, I only need to look at P itself and its S, SE, E neighbors, instead of all 8 neighbors, ditto for Q -> P. I need only look at Q itself and its N, NW and W neighbors.
Also notice that the staggering saves me time in translating the result of the lookup, because I have to do less bit shifting to put the results in place.
When I loop though a grid (Figure A) I walk though the cells (Figure B) in the order shown in figure A. Always in strictly increasing order in a P-cycle and always in decreasing order in a Q-cycle.
In fact the Q cycle works in exactly the opposite order from the P-cycle, this speeds things up by reusing the cache as much as possible.
I want to minimize using pointers as much as possible, because pointers cannot be predicted and are not accessed sequentially (they jump all over the place) So I want to use arrays, stacks and queues as much as possible.
What data do to need to keep track of
I need to keep track of only the cells that change. If a cell (that is an int32) does not change from one generation to the next I remove it from consideration.
This is what the code in the question does. It uses a grid to keep track of the changes, but I want to use a stack, not a grid; and I only want to deal with active cells I don't want to know about stable or dead cells.
Some background on the data
Notice how the cell itself is always monotonically increasing. As is its S-neighbor, as well as the E and SE-neighbor. I can use this info to cheat.
The solution
I use a stack to keep track of the cell itself and its S neighbor and a queue to keep track of its E and SE neighbor and when I'm done I merge the two.
Suppose in the Grid the following cells come out as active after I've calculated them:
00, 01, 08 and 15
I make the following two stacks:
stack A stack B
00 08 a) -A: Cell 00 itself in stack A and its E-neighbor in B
01 09 a) Cell 00's S neighbor in stack A and its SE-n'bor in B
02 0A b) -B: Cell 01 is already in the stack, we only add S/SE
08 10 c) -C: Cell 08 goes into the stack as normal
09 11 c) We'll sort out the merge later.
15 1D d) -D: Cell 15 and neighbors go on as usual.
16 1E d)
Now I push members from stack A and B onto a new stack C so that stack C has
no duplicates and it strictly increasing:
Here's the pseudo code to process the two queues:
a:= 0; b:= 0; c:=0;
while not done do begin
if stack[a] <= stack[b] then begin
stack[c]:= stack[a]; inc(a); inc(c);
if stack[a] = stack[b] then inc(b);
end
else begin
stack[c]:= stack[b]; inc(b); inc(c);
end;
end; {while}
And even better
I don't have to actually do the two stacks and the merging as two separate steps, if I make A a stack and B a queue, I can do the second step described in the pseudo code and the building of the two stacks in one pass.
Note
As a cell changes its S, E or SE border does not necessary need to change, but I can test for that using the masks in table C, and only add the cells that really need checking in the next generation to the list.
Benefits
Using this scheme, I only ever have to walk through one stack with active cells when calculating cells, so I don't waste time looking at dead or inactive cells.
I only do sequential memory accesses, maximizing cache usage.
Building the stack with new changes for the next generations only requires one extra temporary queue, which I process in strictly sequential order.
I do no sorting and the minimum of comparisons.
I don't have to keep track of the neighbors of each individual cells (int32), I only need to keep track of the neighbors (S,E,SE, N,W,NW) of the grids, this keeps the memory overhead to a minimum.
I don't need to keep track of a cells status, I only need to count dead cells (A cell is either dead, because it was dead before, or because it changed into dead. All the active cells are in my TODO stack, this saves bookkeeping time and memory space.
The algorithm runs in o(n) time where (n) is the number of active cells, it excludes dead cells, stable cells and cells that oscillate with period 2.
I only ever deal with 32 bit cardinals, which is the much faster than using int16's.
Mostly #Ken, the complete sourcecode for the test program:
Note that 99,9% of the time is spend in displaying, because I haven't done anything to
optimize that.
I've created a new SDI-main app and posted the code in that and because I'm lazy I haven't bothered to rename or repaint any controls.
Project file: sdiapp.dpr
program Sdiapp;
uses
Forms,
SDIMAIN in 'SDIMAIN.pas'; {Form1}
{$R *.RES}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
Main form: sdimain.pas
unit SDIMAIN;
interface
uses Windows, Classes, Graphics, Forms, Controls, Menus,
Dialogs, StdCtrls, Buttons, ExtCtrls, ComCtrls, ImgList, StdActns,
ActnList, ToolWin;
{--------------------------------------------
p and q are bit arrays of 16x16 bits, grouped
as in 8 int32's as follows
P00 P04 P08 P0c P10 P14 P18 P1c
P01 P05 P09 P0d P11 P15 P19 P1d
P02 P06 P0a P0e P12 P16 P1a P1e
P03 P07 P0b P0f P13 P17 P1b P1f
|
+----> The bits per int32 are grouped as follows
The int32's are grouped as follows
P0 P1
P2 P3
P4 P5
P6 P7
P and Q are staggered as follows:
+---------------------------------+ <---- P
| +-------------------------------|-+ <----Q
| | | |
| | | |
... ...
| | | |
+-|-------------------------------+ |
+---------------------------------+
Generations start counting from 0,
all even generations are stored in P.
all odd generations are stored in Q.
When generating P->Q, the S, SE and E neighbors are checked.
When generating Q->P, the N, NW and W neighbors are checked.
The westernmost P edge in a grid is stored inside that grid.
Ditto for all easternmost Q edges.
--------------------------------------------}
const
cClearQState = $fffffff0;
cClearPState = $fffff0ff;
cIndexQ = 1;
cIndexP = 0;
ChangeSelf = 0;
ChangeNW = 1;
ChangeW = 2;
ChangeN = 3;
ChangeSE = 1;
ChangeE = 2;
ChangeS = 3;
const
//A Grid is 128 x 128 pixels.
GridSizeX = 512 div 8; //should be 128/8, 1024 for testing.
GridSizeY = GridSizeX * 2; //32 totaal: 16x32x4bytes = 2048 x 2 (p+q) = 4k per block.
GridMaxX = GridSizeX - 1;
GridMaxY = GridSizeY - 1;
NumberOfCells = GridSizeX * GridSizeY;
CellSizeX = 8;
CellSizeY = 4;
CellMaxX = CellSizeX - 1;
CellMaxY = CellSizeY - 1;
type
TUnit = Cardinal;
TBytes = array[0..3] of byte;
TChange = array[0..3] of boolean;
type
TCellBlock = class;
TFlags = record
case boolean of
true: (whole: cardinal);
false: (part: array[0..3] of byte);
end;
//TActiveList = array[0..GridMaxX, 0..GridMaxY] of boolean;
//TActive = array[0..1] of TActiveList;
TToDoList = array[-1..NumberOfCells] of cardinal; //Padding on both sides.
TNewRow = TFlags;
PCell = ^TCell;
TCell = record
public
p: TUnit;
q: TUnit;
procedure SetPixel(x,y: integer; InP: Boolean = true);
function GeneratePtoQ: cardinal; inline;
function GenerateQtoP: cardinal; inline;
end;
//A grid contains pointers to an other grid, a unit or nil.
//A grid can contain grids (and nils) or units (and nils), but not both.
PGrid = ^TGrid;
TGrid = array[0..GridMaxX,0..GridMaxY] of TCell;
TCellBlock = class(TPersistent)
private
FHasCells: boolean;
FLevel: integer;
FGrid: TGrid;
ToDoP: TToDoList;
ToDoQ: TToDoList;
PCount: integer;
QCount: integer;
FParent: TCellBlock;
FMyX,FMyY: integer;
N,W,NW: TCellBlock;
S,E,SE: TCellBlock;
procedure GeneratePtoQ; virtual;
procedure GenerateQtoP; virtual;
procedure UpdateFlagsPtoQ; virtual;
procedure UpdateFlagsQtoP; virtual;
procedure Generate; virtual;
procedure Display(ACanvas: TCanvas); virtual;
procedure SetPixel(x,y: integer);
property Grid: TGrid read FGrid write FGrid;
public
constructor Create(AParent: TCellBlock);
destructor Destroy; override;
property Parent: TCellBlock read FParent;
property HasCells: boolean read FHasCells;
property Level: integer read FLevel;
property MyX: integer read FMyX;
property MyY: integer read FMyY;
end;
TCellParent = class(TCellBlock)
private
procedure GeneratePtoQ; override;
procedure GenerateQtoP; override;
//procedure Display(Startx,StartY: integer; ACanvas: TCanvas); override;
public
constructor CreateFromChild(AChild: TCellBlock; ChildX, ChildY: integer);
constructor CreateFromParent(AParent: TCellParent);
destructor Destroy; override;
end;
type
TForm1 = class(TForm)
ToolBar1: TToolBar;
ToolButton9: TToolButton;
ToolButton1: TToolButton;
ToolButton2: TToolButton;
ToolButton3: TToolButton;
ToolButton4: TToolButton;
ActionList1: TActionList;
FileNew1: TAction;
FileOpen1: TAction;
FileSave1: TAction;
FileSaveAs1: TAction;
FileExit1: TAction;
EditCut1: TEditCut;
EditCopy1: TEditCopy;
EditPaste1: TEditPaste;
HelpAbout1: TAction;
StatusBar: TStatusBar;
ImageList1: TImageList;
Image1: TImage;
Timer1: TTimer;
Label1: TLabel;
procedure FileNew1Execute(Sender: TObject);
procedure FileSave1Execute(Sender: TObject);
procedure FileExit1Execute(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure FileOpen1Execute(Sender: TObject);
procedure ToolButton4Click(Sender: TObject);
private
MyBlock: TCellBlock;
MyBitmap: TBitmap;
BitmapData: array[0..1024,0..(1024 div 32)] of integer;
procedure InitLookupTable;
procedure RestartScreen;
public
{ Public declarations }
end;
var
Form1: TForm1;
const
cLiveCell = $88888888;
cLiveVerticalP = $40404040;
cLiveVerticalQ = $04040404;
cLiveTop = $00000088;
cLiveBottom = $88000000;
cLivePCorner = $00000040;
cLiveQCorner = $04000000;
cUnstableCell = $22222222;
cUnstableVerticalP = $10101010;
cUnstableVerticalQ = $01010101;
cUnstableTop = $00000022;
cUnstableBottom = $22000000;
cUnstablePCorner = $00000010;
cUnstableQCorner = $01000000;
cAllDead = $00000000;
cAllLive = $ffffffff;
cLiveRow = $8;
cLive2x2 = $4;
cUnstableRow = $2;
cUnstable8x4 = $22;
cUnstable2x2 = $1;
cUnstable2x4 = $11;
cStateMask: array [0..7] of cardinal =
($fffffff0, $ffffff0f, $fffff0ff, $ffff0fff, $fff0ffff, $ff0fffff, $f0ffffff, $0fffffff);
var
LookupTable: array[0..$FFFF] of byte;
Generation: int64;
implementation
uses about, sysutils, clipbrd, Math;
{$R *.dfm}
type
bool = longbool;
procedure getCPUticks(var i : int64);
begin
asm
mov ECX,i;
RDTSC; //cpu clock in EAX,EDX
mov [ECX],EAX;
mov [ECX+4],EDX;
end;
end;
function IntToBin(AInt: integer): string;
var
i: integer;
begin
i:= SizeOf(AInt)*8;
Result:= StringOfChar('0',i);
while (i > 0) do begin
if Odd(AInt) then Result[i]:= '1';
AInt:= AInt shr 1;
Dec(i);
end; {while}
end;
constructor TCellBlock.Create(AParent: TCellBlock);
begin
inherited Create;
FParent:= AParent;
ToDoQ[-1]:= $ffffffff;
ToDoP[-1]:= $ffffffff;
end;
destructor TCellBlock.Destroy;
begin
inherited Destroy;
end;
procedure TCell.SetPixel(x: Integer; y: Integer; InP: Boolean = true);
var
Mask: cardinal;
Offset: Integer;
begin
//0,0 is the topleft pixel, no correction for p,q fase.
x:= x mod 8;
y:= y mod 4;
Offset:= x * 4 + y;
Mask:= 1 shl Offset;
if (InP) then p:= p or Mask else q:= q or Mask;
end;
procedure TCellBlock.SetPixel(x: Integer; y: Integer);
var
GridX, GridY: integer;
x1,y1: integer;
i: integer;
begin
x:= x + (GridSizeX div 2) * CellSizeX;
y:= y + (GridSizeY div 2) * CellSizeY;
if Odd(Generation) then begin
Dec(x); Dec(y);
QCount:= 0;
end
else PCount:= 0;
GridX:= x div CellSizeX;
GridY:= y div CellSizeY;
if (GridX in [0..GridMaxX]) and (GridY in [0..GridMaxY]) then begin
Grid[GridX,GridY].SetPixel(x,y);
i:= 0;
for x1:= 1 to GridMaxX-1 do begin
for y1:= 1 to GridMaxY-1 do begin
case Odd(Generation) of
false: begin
ToDoP[i]:= (x1 shl 16 or y1);
Inc(PCount);
end;
true: begin
ToDoQ[i]:= (x1 shl 16 or y1);
Inc(QCount);
end;
end; {case}
Inc(i);
end; {for y}
end; {for x}
end; {if}
end;
//GeneratePtoQ
//This procedure generates the Q data and QState-flags
//using the P-data and PState-flags.
procedure TCellBlock.Generate;
begin
if Odd(Generation) then GenerateQtoP
else GeneratePtoQ;
Inc(Generation);
end;
const
MaskS = $cccccccc;
MaskE = $ff000000;
MaskSE = $cc000000;
procedure TCellBlock.GeneratePtoQ;
var
x,y: integer;
i: integer;
Change: cardinal;
ToDoA: TToDoList;
ToDoB: TToDoList;
A, B: integer;
done: boolean;
Address: cardinal;
begin
i:= 0;
A:= 0; B:= 0;
ToDoA[-1]:= $ffffffff;
ToDoB[-1]:= $ffffffff;
while (i < PCount) do begin
y:= ToDoP[i] and $FFFF;
x:= ToDoP[i] shr 16;
Inc(i);
if (x = GridMaxX) or (y = GridMaxY) then continue; //Skip the loop.
Change:= Grid[x,y].GeneratePtoQ;
if (Change <> 0) then begin
Address:= (x shl 16 or y);
if ToDoA[A-1] <> Address then begin
ToDoA[A]:= Address; Inc(A);
end;
if (Change and MaskS) <> 0 then begin
ToDoA[A]:= Address + 1;
Inc(A);
end; {if S changed}
if ((Change and MaskE) <> 0) then begin
Address:= Address + (1 shl 16);
if ToDoB[B-1] <> Address then begin
ToDoB[B]:= Address;
Inc(B);
end;
if ((Change and MaskSE) <> 0) then begin
ToDoB[B]:= Address + 1;
Inc(B);
end; {if SE changed}
end; {if E changed}
end; {if whole cell changed}
end; {while}
ToDoA[A]:= $ffffffff;
ToDoB[B]:= $ffffffff;
ToDoB[B+1]:= $ffffffff;
a:= 0; b:= 0; QCount:= 0;
Done:= (ToDoA[a] = $ffffffff) and (ToDoB[b] = $ffffffff);
while not done do begin
if ToDoA[a] <= ToDoB[b] then begin
ToDoQ[QCount]:= ToDoA[a]; inc(a); inc(QCount);
if ToDoA[a] = ToDoB[b] then inc(b);
end
else begin
ToDoQ[QCount]:= ToDoB[b]; inc(b); inc(QCount);
end;
Done:= (ToDoA[a] = $ffffffff) and (ToDoB[b] = $ffffffff);
end; {while}
end;
const
MaskN = $33333333;
MaskW = $000000ff;
MaskNW = $00000033;
procedure TCellBlock.GenerateQtoP;
var
x,y: integer;
i: integer;
Change: cardinal;
ToDoA: TToDoList;
ToDoB: TToDoList;
A, B: integer;
done: boolean;
Address: cardinal;
begin
i:= 0;
A:= 0; B:= 0;
ToDoA[-1]:= $ffffffff;
ToDoB[-1]:= $ffffffff;
while (i < QCount) do begin
y:= ToDoQ[i] and $FFFF;
x:= ToDoQ[i] shr 16;
Inc(i);
if (x = 0) or (y = 0) then Continue; //Skip the rest of the loop.
Change:= Grid[x,y].GenerateQtoP;
if (Change <> 0) then begin
Address:= (x shl 16 or y);
if ToDoA[A-1] <> Address then begin
ToDoA[A]:= Address; Inc(A);
end;
if (Change and MaskN) <> 0 then begin
ToDoA[A]:= Address - 1;
Inc(A);
end; {if N changed}
if ((Change and MaskW) <> 0) then begin
Address:= Address - (1 shl 16);
if ToDoB[B-1] <> Address then begin
ToDoB[B]:= Address;
Inc(B);
end;
if ((Change and MaskNW) <> 0) then begin
ToDoB[B]:= Address - 1;
Inc(B);
end; {if NW changed}
end; {if W changed}
end; {if whole cell changed}
end; {while}
ToDoA[A]:= $ffffffff;
ToDoB[B]:= $ffffffff;
ToDoB[B+1]:= $ffffffff;
a:= 0; b:= 0; PCount:= 0;
Done:= (ToDoA[a] = $ffffffff) and (ToDoB[b] = $ffffffff);
while not done do begin
if ToDoA[a] <= ToDoB[b] then begin
ToDoP[PCount]:= ToDoA[a]; inc(a); inc(PCount);
if ToDoA[a] = ToDoB[b] then inc(b);
end
else begin
ToDoP[PCount]:= ToDoB[b]; inc(b); inc(PCount);
end;
Done:= (ToDoA[a] = $ffffffff) and (ToDoB[b] = $ffffffff);
end; {while}
end;
(*
procedure TCellBlock.GenerateQtoP;
var
x,y: integer;
i: integer;
Change: cardinal;
begin
i:= 0;
for x:= 0 to GridMaxX - 1 do begin
for y:= 0 to GridMaxY -1 do begin
if Active[cIndexQ][x, y] then begin
Active[cIndexQ][x, y]:= false;
ToDo[i]:= x shl 16 or y;
Inc(i);
end; {if}
end; {for y}
end; {for x}
while i > 0 do begin
Dec(i);
y:= ToDo[i] and $FFFF;
x:= ToDo[i] shr 16;
Change:= Grid[x,y].GenerateQtoP;
Active[cIndexP][x,y]:= Active[cIndexP][x,y] or (Change <> 0);
Active[cIndexP][x-1,y-1]:= Active[cIndexP][x-1,y-1] or ((Change and $00000033) <> 0);
Active[cIndexP][x-1,y]:= Active[cIndexP][x-1,y] or ((Change and $000000ff) <> 0);
Active[cIndexP][x,y-1]:= Active[cIndexP][x,y-1] or ((Change and $33333333) <> 0);
end; {while}
end; (**)
procedure TCellBlock.UpdateFlagsPtoQ;
begin
//nog in te vullen.
end;
procedure TCellBlock.UpdateFlagsQtoP;
begin
//nog in te vullen
end;
function TCell.GeneratePtoQ: cardinal;
var
NewQ: cardinal;
Change: cardinal;
const
Mask1 = $f;
Mask2 = $ff;
Mask4 = $ffff;
Row1Mask = $33333333; //0011-0011-0011-0011-0011-0011-0011-0011
Row2Mask = $cccccccc; //1100-1100-1100-1100-1100-1100-1100-1100
function MakeNewBrick(p0,p1,p2,p3: cardinal): cardinal; inline;
var
Row1, Row2: cardinal;
begin
//Generate new Brick using a 2x2 grid of bricks ordered like:
//p0 p1
//p2 p3
//First row inside P0
if (p0 <> 0) then Row1:=
LookupTable[p0 and $ffff] or
LookupTable[(p0 shr 8) and $ffff] shl 8 or
LookupTable[(p0 shr 16)] shl 16 or
LookupTable[(p0 shr 24) or (p1 and $ff) shl 8] shl 24
else Row1:= LookupTable[(p1 and $ff) shl 8] shl 24;
(**)
p0:= ((p0 and $cccccccc)) or ((p2 and $33333333));
p1:= ((p1 and $cc)) or ((p3 and $33));
if (p0 <> 0) then Row2:=
LookupTable[p0 and $ffff] or
LookupTable[(p0 shr 8) and $ffff] shl 8 or
LookupTable[(p0 shr 16)] shl 16 or
LookupTable[(p0 shr 24) or ((p1 and $ff) shl 8)] shl 24
else Row2:= LookupTable[(p1 and $ff) shl 8] shl 24;
Result:= (Row1 and Row1Mask) or (Row2 and Row2Mask);
end;
begin
NewQ:= MakeNewBrick(Self.p, PGrid(#Self)^[1,0].p, PGrid(#Self)^[0,1].p, PGrid(#Self)^[1,1].p);
Result:= NewQ xor q;
q:= NewQ;
end;
function TCell.GenerateQtoP: cardinal;
var
Offset: integer;
NewP: cardinal;
Change: cardinal;
const
Row1Mask = $33333333; //0011-0011-0011-0011-0011-0011-0011-0011
Row2Mask = $cccccccc; //1100-1100-1100-1100-1100-1100-1100-1100
function MakeNewBrick(q0,q1,q2,q3: cardinal): cardinal; inline;
var
Row1, Row2: cardinal;
begin
//Generate new Brick using a 2x2 grid of bricks ordered like:
//q3 q2
//q1 q0
if (q0 <> 0) then Row1:=
LookupTable[(q0 shr 16)] shl 26 or
LookupTable[(q0 shr 8 ) and $ffff] shl 18 or
LookupTable[(q0 ) and $ffff] shl 10 or
LookupTable[((q0 and $ff) shl 8) or (q1 shr 24)] shl 2
else Row1:= LookupTable[(q1 shr 24)] shl 2;
(*
q0:= ((q0 and $33333333) shl 2) or ((q2 and $cccccccc) shr 2);
q1:= ((q1 and $33000000) shl 2) or ((q3 and $cc000000) shr 2);
if (q0 <> 0) then Row2:=
LookupTable[(q0 shr 16) and $ffff] shl 24 or
LookupTable[(q0 shr 8) and $ffff] shl 16 or
LookupTable[(q0 ) and $ffff] shl 8 or
LookupTable[((q0 and $ff) shl 8) or (q1 shr 24)]
else Row2:= LookupTable[(q1 shr 24)];
(**)
q0:= ((q0 and $33333333)) or ((q2 and $cccccccc));
q1:= ((q1 and $33000000)) or ((q3 and $cc000000));
if (q0 <> 0) then Row2:=
LookupTable[(q0 shr 16)] shl 22 or
LookupTable[(q0 shr 8) and $ffff] shl 14 or
LookupTable[(q0 ) and $ffff] shl 6 or
LookupTable[((q0 and $ff) shl 8) or (q1 shr 24)] shr 2
else Row2:= LookupTable[(q1 shr 24)] shr 2;
Result:= (Row1 and Row2Mask) or (Row2 and Row1Mask);
end;
begin
Offset:= -1;
NewP:= MakeNewBrick(Self.q, PGrid(#Self)^[Offset,0].q, PGrid(#Self)^[0,Offset].q, PGrid(#Self)^[Offset, Offset].q);
Result:= NewP xor P;
P:= NewP;
end;
procedure TCellBlock.Display(ACanvas: TCanvas);
var
GridX,GridY: integer;
//Offset: integer;
procedure DisplayCell(ACell: TCell);
var
x,y,x1,y1: integer;
Row, Mask: integer;
DoPixel: boolean;
Offset: integer;
DrawOffset: integer;
InP: boolean;
begin
DrawOffset:= (Generation and 1);
InP:= not(Odd(Generation));
for y:= 0 to CellMaxY do begin
for x:= 0 to CellMaxX do begin
//if (x = 0) or (y = 0) then ACanvas.Pixels[GridX*16+x+Offset,GridY*16+y+Offset]:= clBtnFace;
//0,0 is the topleft pixel, no correction for p,q fase.
x1:= x mod 8;
y1:= y mod 4;
Offset:= x1 * 4 + y1;
Mask:= 1 shl Offset;
if (InP) then DoPixel:= (ACell.p and Mask) <> 0
else DoPixel:= (ACell.q and Mask) <> 0;
if DoPixel then ACanvas.Pixels[GridX*CellSizeX+x+DrawOffset, GridY*CellSizeY+y+DrawOffset]:= clBlack;
end; {for x}
end; {for y}
end; (**)
begin
ACanvas.Rectangle(-1,-1,1000,1000);
FillChar(Form1.BitmapData, SizeOf(Form1.BitmapData), #0);
for GridY:= 0 to GridMaxY do begin
for GridX:= 0 to GridMaxX do begin
if Int64(Grid[GridX, GridY]) <> 0 then begin
DisplayCell(Grid[GridX,GridY]);
end;
end;
end;
end;
//--------------------------------------
//A Parent is every layer above the ground level
//the tree grows from the bottom up.
//A new parent is placed on top of the last one and
//always has one and only one child to start with, from there
//the tree grows down again.
constructor TCellParent.CreateFromChild(AChild: TCellBlock; ChildX: Integer; ChildY: Integer);
begin
inherited Create(nil);
end;
constructor TCellParent.CreateFromParent(AParent: TCellParent);
begin
inherited Create(AParent);
end;
destructor TCellParent.Destroy;
begin
inherited Destroy;
end;
procedure TCellParent.GeneratePtoQ;
begin
end;
procedure TCellParent.GenerateQtoP;
begin
end;
//The bitmap for the lookup table is as follows:
// 0 2 4 6
// +----+
// 1 |3 5| 7
// 8 |A C| E
// +----+
// 9 B D F
// The inner 2x2 cells are looked up.
// so 0241358AC make up bit 3 etc.
procedure TForm1.InitLookupTable;
const
//Masks for normal order.
MaskNW = $0757; //0000-0111-0101-0111
MaskSW = $0EAE; //0000-1110-1010-1110
MaskNE = $7570; //0111-0101-0111-0000
MaskSE = $EAE0; //1110-1010-1110-0000
//Bitlocations for normal order
BitNW = $0020; //0000-0000-0010-0000
BitSW = $0040; //0000-0000-0100-0000
BitNE = $0200; //0000-0020-0000-0000
BitSE = $0400; //0000-0100-0000-0000
//Lookup table also has a shifted order. here the bottom half of the N word
//and the top half of the south word combine.
//Like so:
// 2 6 A E
// 3 7 B F
// 0 4 8 C
// 1 5 9 D
//Mask for split order.
Mask2NW = $0D5D; // 0000-1101-0101-1101
Mask2SW = $0BAB; // 0000-1011-1010-1011
Mask2NE = $D5D0; // 1101-0101-1101-0000
Mask2SE = $BAB0; // 1011-1010-1011-0000
//Bitlocations for split order
Bit2NW = $0080; // 0000-0000-1000-0000
Bit2SW = $0010; // 0000-0000-0001-0000
Bit2NE = $0800; // 0000-1000-0000-0000
Bit2SE = $0100; // 0000-0001-0000-0000
ResultNW = $01;
ResultSW = $02;
ResultNE = $10;
ResultSE = $20;
Result2NW = $04;
Result2SW = $08;
Result2NE = $40;
Result2SE = $80;
var
i: integer;
iNW, iNE, iSW, iSE: cardinal;
Count: integer;
ResultByte: byte;
function GetCount(a: integer): integer;
var
c: integer;
begin
Result:= 0;
for c:= 0 to 15 do begin
if Odd(a shr c) then Inc(Result);
end; {for c}
end; {GetCount}
begin
//Fill the normal lookup.
for i:= 0 to $ffff do begin
ResultByte:= 0;
iNW:= i and MaskNW;
Count:= GetCount(iNW);
case Count of //count excluding bit itself
3: ResultByte:= ResultNW;
2: if ((i and BitNW) <> 0) then ResultByte:= ResultNW;
end;
iSW:= i and MaskSW;
Count:= GetCount(iSW);
case Count of
3: ResultByte:= ResultByte or ResultSW;
2: if ((i and BitSW) <> 0) then ResultByte:= ResultByte or ResultSW;
end;
iNE:= i and MaskNE;
Count:= GetCount(iNE);
case Count of
3: ResultByte:= ResultByte or ResultNE;
2: if ((i and BitNE) <> 0) then ResultByte:= ResultByte or ResultNE;
end;
iSE:= i and MaskSE;
Count:= GetCount(iSE);
case Count of
3: ResultByte:= ResultByte or ResultSE;
2: if ((i and BitSE) <> 0) then ResultByte:= ResultByte or ResultSE;
end;
LookupTable[i]:= ResultByte;
end; {for i}
//Fill the shifted lookup.
for i:= 0 to $ffff do begin
ResultByte:= 0;
iNW:= i and Mask2NW;
Count:= GetCount(iNW);
case Count of //count excluding bit itself
3: ResultByte:= Result2NW;
2: if ((i and Bit2NW) <> 0) then ResultByte:= Result2NW;
end;
iSW:= i and Mask2SW;
Count:= GetCount(iSW);
case Count of
3: ResultByte:= ResultByte or Result2SW;
2: if ((i and Bit2SW) <> 0) then ResultByte:= ResultByte or Result2SW;
end;
iNE:= i and Mask2NE;
Count:= GetCount(iNE);
case Count of
3: ResultByte:= ResultByte or Result2NE;
2: if ((i and Bit2NE) <> 0) then ResultByte:= ResultByte or Result2NE;
end;
iSE:= i and Mask2SE;
Count:= GetCount(iSE);
case Count of
3: ResultByte:= ResultByte or Result2SE;
2: if ((i and Bit2SE) <> 0) then ResultByte:= ResultByte or Result2SE;
end;
LookupTable[i]:= LookupTable[i] or ResultByte;
end; {for i} (**)
end;
procedure TForm1.RestartScreen;
begin
MyBlock.Free;
MyBlock:= TCellBlock.Create(nil);
//MyBlock.SetPixel(5,7);
//MyBlock.SetPixel(6,7);
//MyBlock.SetPixel(7,7);
//MyBlock.SetPixel(7,6);
//MyBlock.SetPixel(6,5);
MyBlock.SetPixel(10,0);
MyBlock.SetPixel(11,0);
MyBlock.SetPixel(9,1);
MyBlock.SetPixel(10,1);
MyBlock.SetPixel(10,2);
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
if Assigned(MyBlock) then begin
MyBlock.Generate;
MyBlock.Display(Image1.Canvas);
end;
end;
procedure TForm1.ToolButton4Click(Sender: TObject);
begin
if Assigned(MyBlock) then begin
MyBlock.Generate;
MyBlock.Display(Image1.Canvas);
end;
end;
procedure TForm1.FileNew1Execute(Sender: TObject);
begin
InitLookupTable;
FillChar(BitmapData, SizeOf(BitmapData), #0);
MyBitmap:= TBitmap.Create;
MyBitmap.SetSize(1024,1024);
MyBitmap.PixelFormat:= pf1bit;
MyBitmap.Monochrome:= true;
//MyBitmap.Handle:= CreateBitmap(1000,1000,1,2,nil);
Generation:= 0;
RestartScreen;
MyBlock.Display(Image1.Canvas);
//if (Sender = FileNew1) then Timer1.Enabled:= not(Timer1.Enabled);
end;
procedure TForm1.FileOpen1Execute(Sender: TObject);
var
i,a: integer;
start, eind: int64;
Diff: double;
LowDiff: double;
begin
LowDiff:= MaxInt;
for a:= 0 to 10 do begin
FileNew1Execute(Sender);
GetCPUTicks(start);
for i:= 0 to 1000 do begin
MyBlock.Generate;
end;
GetCPUTicks(eind);
//Label1.Caption:= IntToStr(Eind - Start);
Diff:= Eind - start;
LowDiff:= Min(Diff, LowDiff);
Label1.Caption:= Format('%10.0n',[lowdiff]) + ' CPU cycles per 1,000 generations';
Clipboard.AsText:= Label1.Caption;
end; {for a}
MyBlock.Display(Image1.Canvas);
end;
procedure TForm1.FileSave1Execute(Sender: TObject);
begin
Timer1.Enabled:= not(Timer1.Enabled);
end;
procedure TForm1.FileExit1Execute(Sender: TObject);
begin
Close;
end;
initialization
Generation:= 0;
end.
Stackoverflow does not allow me to post the form file due to a size limit, but I hope you can manage without.
Related
B-Spline Curves coefficients - division by zero (code in DELPHI)
I was trying to implement the following recursive formula to my code but to my surprise it turns out that after implementing this to DELPHI, I get an error due to division by zero. I am 98% sure that my knot vector is correctly calculated, which in a way means there shouldn't be any divisions by zero. I am 70% sure that the recursive formula is correctly implemented, for that reason I am posting my code here: program project1; uses SysUtils; Type TRealPoint = record x: single; y: single; end; type TSample = Class(TObject) public KnotVector: array of single; FitPoints: array of TRealPoint; Degree: integer; constructor Create; overload; function Coefficient(i, p: integer; Knot: single): single; procedure GetKnots; destructor Destroy; overload; end; constructor TSample.Create; begin inherited; end; function TSample.Coefficient(i, p: integer; Knot: single): single; var s1, s2: single; begin If (p = 0) then begin If (KnotVector[i] <= Knot) And (Knot < KnotVector[i+1]) then Result := 1.0 else Result := 0.0; end else begin s1 := (Knot - KnotVector[i])*Coefficient(i, p-1, Knot)/(KnotVector[i+p] - KnotVector[i]); //THIS LINE ERRORS due to division by zero ??? s2 := (KnotVector[i+p+1]-Knot)*Coefficient(i+1,p-1,Knot)/(KnotVector[i+p+1]-KnotVector[i+1]); Result := s1 + s2; end; end; procedure TSample.GetKnots(); var KnotValue: single; i, MaxKnot: integer; begin // KNOTS KnotValue:= 0.0; SetLength(KnotVector, Length(FitPoints) + 1 + Degree); MaxKnot:= Length(KnotVector) - (2*Degree + 1); for i := Low(KnotVector) to High(KnotVector) do begin if i <= (Degree) then KnotVector[i] := KnotValue / MaxKnot else if i > Length(FitPoints) then KnotVector[i] := KnotValue / MaxKnot else begin KnotValue := KnotValue + 1.0; KnotVector[i] := KnotValue / MaxKnot; end; end; end; destructor TSample.Destroy; begin inherited; end; var i, j: integer; Test: TSample; N: array of array of single; begin Test := TSample.Create; //define degree Test.Degree := 3; //random fit points j := 15; SetLength(Test.FitPoints, j + 1 + Test.Degree); For i := Low(Test.FitPoints) to High(Test.FitPoints) do begin Test.FitPoints[i].x := Random()*2000; Test.FitPoints[i].y := Random()*2000; end; //get knot vector Test.GetKnots; //get coefficients SetLength(N, j+1, j+1); For j := Low(N) to High(N) do begin For i := Low(N[j]) to High(N[j]) do begin N[j, i] := Test.Coefficient(i,3,Test.KnotVector[j]); write(floattostrf(N[j,i], ffFixed, 2, 2) + ', '); end; writeln(); end; readln(); Test.Free; end. Basically I'm not sure how to continue. I would need the values of matrix N (see this link) of basis coefficients but somehow using the formula from this link leads me to division by zero. So... Is there a totally different way how to calculate those coefficients or what is the problem here? UPDATE Instead of using my own idea i tried to implement the algorithm from here as suggested by Dsm in the comments. As a result, there is no more divison by zero, but the result is totally unexpected anyways. For n + 1 = 10 random fit points with spline degree 3 the basis matrix N (see link) is singular - as seen from the attached image. Instead of that I would expect the matrix to be band matrix. Anyway, here is my updated code: program project1; uses SysUtils; Type TRealPoint = record x: single; y: single; end; type TMatrix = array of array of double; type TSample = Class(TObject) public KnotVector: array of double; FitPoints: array of TRealPoint; SplineDegree: integer; Temp: array of double; A: TMatrix; procedure GetKnots; function GetBasis(Parameter: double): boolean; procedure FormBasisMatrix; end; procedure TSample.GetKnots(); var i, j: integer; begin // KNOTS //https://pages.mtu.edu/~shene/COURSES/cs3621/NOTES/INT-APP/PARA-knot-generation.html SetLength(KnotVector, Length(FitPoints) + SplineDegree + 1); for i := Low(KnotVector) to High(KnotVector) do begin if i <= SplineDegree then KnotVector[i] := 0 else if i <= (High(KnotVector) - SplineDegree - 1) then KnotVector[i] := (i - SplineDegree) / (Length(FitPoints) - SplineDegree) else KnotVector[i] := 1; end; end; function TSample.GetBasis(Parameter: double): boolean; var m, d, k: integer; FirstTerm, SecondTerm: double; begin //http://pages.mtu.edu/~shene/COURSES/cs3621/NOTES/spline/B-spline/bspline-curve-coef.html Result := False; //initialize to 0 SetLength(Temp, Length(FitPoints)); For m := Low(Temp) to High(Temp) do Temp[m] := 0.0; //special cases If Abs(Parameter - KnotVector[0]) < 1e-8 then begin Temp[0] := 1; end else if Abs(Parameter - KnotVector[High(KnotVector)]) < 1e-8 then begin Temp[High(Temp)] := 1; end else begin //find knot span [u_k, u_{k+1}) for k := Low(KnotVector) to High(KnotVector) do if Abs(KnotVector[k] - Parameter) < 1e-8 then break; Temp[k] := 1.0; for d := 1 to SplineDegree do begin Temp[k - d] := (KnotVector[k + 1] - Parameter) * Temp[k - d + 1] / (KnotVector[k + 1] - KnotVector[k - d + 1]); for m := k - d + 1 to k - 1 do begin FirstTerm := (Parameter - KnotVector[m]) / (KnotVector[m + d] - KnotVector[m]); SecondTerm := (KnotVector[m + d + 1] - Parameter) / (KnotVector[m + d + 1] - KnotVector[m + 1]); Temp[m] := FirstTerm * Temp[m] + SecondTerm * Temp[m + 1]; end; Temp[k] := (Parameter - KnotVector[k]) * Temp[k] / (KnotVector[k + d] - KnotVector[k]); end; end; Result := True; end; procedure TSample.FormBasisMatrix; var i, j: integer; begin SetLength(A, Length(FitPoints), Length(FitPoints)); for j := Low(A) to High(A) do begin for i := low(A[j]) to High(A[j]) do //j - row, i - column begin If GetBasis(KnotVector[j + SplineDegree]) then A[j, i] := Temp[i]; end; end; end; var i, j, iFitPoints: integer; Test: TSample; N: array of array of single; begin Test := TSample.Create; //define degree Test.SplineDegree := 3; //random fit points iFitPoints := 10; SetLength(Test.FitPoints, iFitPoints); For i := Low(Test.FitPoints) to High(Test.FitPoints) do begin Test.FitPoints[i].x := Random()*200; Test.FitPoints[i].y := Random()*200; end; //get knot vector Test.GetKnots; //get B-Spline basis matrix Test.FormBasisMatrix; // print matrix for j := Low(Test.A) to High(Test.A) do begin for i := Low(Test.A) to High(Test.A) do write(FloatToStrF(Test.A[j, i], ffFixed, 2, 2) + ', '); writeln(); end; readln(); Test.Free; end.
This does not appear to be the complete answer, but it may help you on your way, and the result is closer to what you expect, but as I say, not completely there. First of all the knots do not look right to me. The knots appear to form a 'ramp' function (clamped line), and though I can't work out if 'm' has any specific value, I would expect the function to be continuous, which yours is not. Making it continuous gives better results, e.g. procedure TSample.GetKnots(); var i, j: integer; iL : integer; begin // KNOTS //https://pages.mtu.edu/~shene/COURSES/cs3621/NOTES/INT-APP/PARA-knot-generation.html iL := Length( FitPoints ); SetLength(KnotVector, iL + SplineDegree + 1); // set outer knot values and sum used to geterate first internal value for i := 0 to SplineDegree - 1 do begin KnotVector[ i ] := 0; KnotVector[ High(KnotVector)-i] := 1; end; // and internal ones for i := 0 to High(KnotVector) - 2* SplineDegree + 1 do begin KnotVector[ SplineDegree + i - 1] := i / (iL - 1); end; end; I introduced iL = Length( Fitpoints ) for convenience - it is not important. The second issue I spotted is more of a programming one. In the GetBasis routine, you evaluate k by breaking a for loop. The problem with that is that k is not guaranteed to persist outside the loop, so your use of it later is not guaranteed to succeed (although it may) Finally, in the same place, your range determination is completely wrong in my opinion. You should be looking for parameter to lie in a half open line segment, but instead you are looking for it to lie close to an endpoint of that line. Putting these two together for k := Low(KnotVector) to High(KnotVector) do if Abs(KnotVector[k] - Parameter) < 1e-8 then break; should be replaced by k1 := 0; for k1 := High(KnotVector) downto Low(KnotVector) do begin if Parameter >= KnotVector[k1] then begin k := k1; break; end; end; where k1 is an integer. I can't help feeling that there is a plus 1 error somewhere, but I can't spot it. Anyway, I hope that this helps you get a bit further.
To build recursive pyramid for coefficient calculation at intervals, you have to start top level of recursion (inner loop of calculations) from the first real (not duplicate) knot index: For i := Test.Degree... Also check the last loop index. P.S. You can remove constructor and destructor from class description and implementation if they have nothing but inherited.
Byte array to Signed integer in Delphi
source array(4 bytes) [$80,$80,$80,$80] =integer 0 [$80,$80,$80,$81] = 1 [$80,$80,$80,$FF] = 127 [$80,$80,$81,$01] = 128 need to convert this to integer. below is my code and its working at the moment. function convert(b: array of Byte): Integer; var i, st, p: Integer; Negative: Boolean; begin result := 0; st := -1; for i := 0 to High(b) do begin if b[i] = $80 then Continue // skip leading 80 else begin st := i; Negative := b[i] < $80; b[i] := abs(b[i] - $80); Break; end; end; if st = -1 then exit; for i := st to High(b) do begin p := round(Power(254, High(b) - i)); result := result + b[i] * p; result := result - (p div 2); end; if Negative then result := -1 * result end; i'm looking for a better function? Update: file link https://drive.google.com/file/d/0ByBA4QF-YOggZUdzcXpmOS1aam8/view?usp=sharing in uploaded file ID field offset is from 5 to 9 NEW: Now i got into new problem which is decoding date field Date field hex [$80,$8F,$21,$C1] -> possible date 1995-12-15 * in uploaded file date field offset is from 199 to 203
Just an example of some improvements as outlined by David. The array is passed by reference as a const. The array is fixed in size. The use of floating point calculations are converted directly into a constant array. Const MaxRange = 3; Type TMySpecial = array[0..MaxRange] of Byte; function Convert(const b: TMySpecial): Integer; var i, j: Integer; Negative: Boolean; Const // Pwr[i] = Round(Power(254,MaxRange-i)); Pwr: array[0..MaxRange] of Cardinal = (16387064,64516,254,1); begin for i := 0 to MaxRange do begin if (b[i] <> $80) then begin Negative := b[i] < $80; Result := Abs(b[i] - $80)*Pwr[i] - (Pwr[i] shr 1); for j := i+1 to MaxRange do Result := Result + b[j]*Pwr[j] - (Pwr[j] shr 1); if Negative then Result := -Result; Exit; end; end; Result := 0; end; Note that less code lines is not always a sign of good performance. Always measure performance before optimizing the code in order to find real bottlenecks. Often code readability is better than optimizing over the top. And for future references, please tell us what the algorithm is supposed to do. Code for testing: const X : array[0..3] of TMySpecial = (($80,$80,$80,$80), // =integer 0 ($80,$80,$80,$81), // = 1 ($80,$80,$80,$FF), // = 127 ($80,$80,$81,$01)); // = 128 var i,j: Integer; sw: TStopWatch; begin sw := TStopWatch.StartNew; for i := 1 to 100000000 do for j := 0 to 3 do Convert(X[j]); WriteLn(sw.ElapsedMilliseconds); ReadLn; end.
SIGSEV in custom QuickSort implementation
I slept over the answer to question Quicksort drama and wanted to recode it from scratch, implementing your tip with the call-by-reference var. And again: I cannot find any failure I made again. I compare the code to your program one by one and I cannot find the problem. The following code produces an Exception (External:SIGSEV at address 11602) during compilation/run program quicksort; var iArray : array[0..8] of integer; procedure fillArray(var iArray : array of integer); begin; iArray[0] := 3; iArray[1] := 1; iArray[2] := 8; iArray[3] := 4; iArray[4] := 9; iArray[5] := 0; iArray[6] := 8; iArray[7] := 2; iArray[8] := 5; end; procedure writeArray(iArray : array of integer); var i:integer; begin for i:=low(iArray) to high(iArray) do begin write(iArray[i]); end; writeln(''); end; procedure quickSort(var iArray : array of integer; links : integer; rechts:integer); var l,r,pivot, temp: integer; begin if (rechts > links) then begin l := links; r := rechts; pivot := iArray[(rechts+links) div 2]; while (l<r) do begin while (iArray[l] < pivot) do l:=l+1; while (iArray[r] > pivot) do r:=r-1; if (l<=r) then begin temp := iArray[l]; iArray[l] := iArray[r]; iArray[r] := temp; end; end; if (links < r) then quickSort(iArray, links, r); if (l < rechts) then quickSort(iArray, l, rechts); end; end; begin fillArray(iArray); quickSort(iArray,low(iArray),high(iArray)); writeArray(iArray); end.
The block of code that swaps, also needs to increment l and decrement r once the swap is complete: if (l <= r) then begin temp := iArray[l]; iArray[l] := iArray[r]; iArray[r] := temp; inc(l); // <-- this was missing dec(r); // <-- as was this end; The complete program, with some other minor tidy ups: program quicksort24340509; var iArray: array [0 .. 8] of integer; Procedure fillArray(var iArray: array of integer); begin; iArray[0] := 3; iArray[1] := 1; iArray[2] := 8; iArray[3] := 4; iArray[4] := 9; iArray[5] := 0; iArray[6] := 8; iArray[7] := 2; iArray[8] := 5; end; Procedure writeArray(const iArray: array of integer); var i: integer; begin for i := low(iArray) to high(iArray) do begin write(iArray[i], ' '); end; writeln; end; Procedure quickSort(var iArray: array of integer; links, rechts: integer); var l, r, pivot, temp: integer; begin if (rechts > links) then begin l := links; r := rechts; pivot := iArray[(rechts + links) div 2]; while l < r do begin while iArray[l] < pivot do inc(l); while iArray[r] > pivot do dec(r); if l <= r then begin temp := iArray[l]; iArray[l] := iArray[r]; iArray[r] := temp; inc(l); dec(r); end; end; if links < r then quickSort(iArray, links, r); if l < rechts then quickSort(iArray, l, rechts); end; end; begin fillArray(iArray); quickSort(iArray, low(iArray), high(iArray)); writeArray(iArray); readln; end. Output 0 1 2 3 4 5 8 8 9 The reason that your version fails, without the missing lines, is that the recursive calls to quickSort operate on the wrong ranges. For example, Given your input of 3 1 8 4 9 0 8 2 5 the partitioning step pivots on 9 and results in 3 1 8 4 5 0 8 2 9 Now, the recursive step should be to sort all the values to the left of the pivot, and all the values to the right. And we leave the pivot alone because partitioning ensured that it is in its final position. There are no values to the right of the pivot so we should be making a recursive call for the range 0 to 7. But if you inspect what happens with your code you will find that it does not. Instead it makes a recursive call for the range 0 to 8. That in itself is a little benign, but once the ranges become small, at the stopping condition, it's different. Try asking your program to sort these values: 1 2 The code pivots on 1. At the end of partitioning we have: links = 0 rechts = 1 l = 0 r = 0 So we recursively call quickSort passing l and rechts as the ranges. But that's exactly the same call as we initially made. And that therefore leads to a stack overflow. So the point is that we must make sure that when we partition on a pivot, we exclude that pivot from all future recursive calls to quickSort. If we don't do that we don't sub-divide the problem, and the recursion does not terminate.
Longest arithmetic and geometric progression sequence error
I need input sequence of Integer number and find the longest arithmetic and geometric progression sequence. I had wrote this code( I must use Delphi 7) program arithmeticAndGeometricProgression; { 203. In specifeied sequence of integer numbers find the longest sequence, which is arithmetic or geometric progression. } {$APPTYPE CONSOLE} uses SysUtils; var sequence, longArithmSequ, longGeomSequ: Array of Integer; curArithmSequ, curGeomSequ: Array of Integer; // Current progress q, q1: Double; d1, d: Double; i, k: Integer; begin i := 0; d := 0; k := 0; d1 := 0; Repeat SetLength(sequence, i + 1); // Make room for another item in the array try read(sequence[i]); except // If the input character is not an integer interrupt cycle Break; end; inc(i); Until False; k := 0; curArithmSequ := NIL; curGeomSequ := NIL; longArithmSequ := NIL; longGeomSequ := NIL; d1 := sequence[1] - sequence[0]; q1 := sequence[1] / sequence[0]; i := 1; repeat d := d1; q := q1; d1 := sequence[i] - sequence[i - 1]; q1 := sequence[i] / sequence[i - 1]; if d = d1 then begin SetLength(curArithmSequ, Length(curArithmSequ) + 1); curArithmSequ[Length(curArithmSequ) - 1] := sequence[i]; end; if q = q1 then begin SetLength(curGeomSequ, Length(curGeomSequ) + 1); curGeomSequ[Length(curGeomSequ) - 1] := sequence[i]; end; if Length(curArithmSequ) > Length(longArithmSequ) then begin longArithmSequ := NIL; SetLength(longArithmSequ, Length(curArithmSequ)); for k := 0 to Length(curArithmSequ) - 1 do longArithmSequ[k] := curArithmSequ[k]; end; if Length(curGeomSequ) > Length(longGeomSequ) then begin longGeomSequ := NIL; SetLength(longGeomSequ, Length(curGeomSequ)); for k := 0 to Length(curGeomSequ) - 1 do longGeomSequ[k] := curGeomSequ[k]; end; if d <> d1 then curArithmSequ := NIL; if q <> q1 then curGeomSequ := NIL; inc(i); Until i >= Length(sequence) - 1; writeLn('The Longest Arithmetic Progression'); for k := 0 to Length(longArithmSequ) - 1 do Write(longArithmSequ[k], ' '); writeLn('The Longest Geometric Progression'); for k := 0 to Length(longGeomSequ) - 1 do Write(longGeomSequ[k], ' '); Readln(k); end. I have such question: Why it can't print first 1-2 members of arithmetic progression Why it always print '2' as geometric progression Is there monkey-style code in my programm? Please mention to me where are my mistakes.
Updated: You need to change the logic inside the repeat loop in this way: if d = d1 then begin if (Length(curArithmSequ) = 0) then begin if (i > 1) then SetLength(curArithmSequ,3) else SetLength(curArithmSequ,2); end else SetLength(curArithmSequ,Length(curArithmSequ)+1); for k := 0 to Length(curArithmSequ) - 1 do curArithmSequ[k] := sequence[i - (Length(curArithmSequ) - k - 1)]; end else SetLength(curArithmSequ,0); if q = q1 then begin if (Length(curGeomSequ) = 0) then begin if (i > 1) then SetLength(curGeomSequ,3) else SetLength(curGeomSequ,2); end else SetLength(curGeomSequ,Length(curGeomSequ)+1); for k := 0 to Length(curGeomSequ) - 1 do curGeomSequ[k] := sequence[i - (Length(curGeomSequ) - k - 1)]; end else SetLength(curGeomSequ,0); An input sequence of: 2,6,18,54 gives LAP=2,6 and LGP=2,6,18,54 while an input sequence of: 1,3,5,7,9 gives: LAP=1,3,5,7,9 and LGP=1,3 And a sequence of 5,4,78,2,3,4,5,6,18,54,16 gives LAP=2,3,4,5,6 and LGP=6,18,54 Here is my complete test (see comments below): program arithmeticAndGeometricProgression; { 203. In specified sequence of integer numbers find the longest sequence, which is arithmetic or geometric progression. } {$APPTYPE CONSOLE} uses SysUtils; Type TIntArr = array of integer; TValidationProc = function( const sequence : array of integer) : Boolean; function IsValidArithmeticSequence( const sequence : array of integer) : Boolean; begin Result := (Length(sequence) = 2) // Always true for a sequence of 2 values or // An arithmetic sequence is defined by: a,a+n,a+2*n, ... // This gives: a+n - a = a+2*n - (a+n) // s[1] - s[0] = s[2] - s[1] <=> 2*s[1] = s[2] + s[0] (2*sequence[1] = (Sequence[2] + sequence[0])); end; function IsValidGeometricSequence( const sequence : array of integer) : Boolean; var i,zeroCnt : Integer; begin // If a zero exists in a sequence all members must be zero zeroCnt := 0; for i := 0 to High(sequence) do if (sequence[i] = 0) then Inc(zeroCnt); if (Length(sequence) = 2) then Result := (zeroCnt in [0,2]) else // A geometric sequence is defined by: a*r^0,a*r^1,a*r^2 + ... ; r <> 0 // By comparing sequence[i]*sequence[i-2] with Sqr(sequence[i-1]) // i.e. a*(a*r^2) with Sqr(a*r) we can establish a valid geometric sequence Result := (zeroCnt in [0,3]) and (Sqr(sequence[1]) = sequence[0]*Sequence[2]); end; procedure AddSequence( var arr : TIntArr; sequence : array of Integer); var i,len : Integer; begin len := Length(arr); SetLength(arr,len + Length(sequence)); for i := 0 to High(sequence) do arr[len+i] := sequence[i]; end; function GetLongestSequence( IsValidSequence : TValidationProc; const inputArr : array of integer) : TIntArr; var i : Integer; currentSequence : TIntArr; begin SetLength(Result,0); SetLength(currentSequence,0); if (Length(inputArr) <= 1) then Exit; for i := 1 to Length(inputArr)-1 do begin if (Length(Result) = 0) then // no valid sequence found so far begin if IsValidSequence([inputArr[i-1],inputArr[i]]) then AddSequence(currentSequence,[inputArr[i-1],inputArr[i]]); end else begin if IsValidSequence([inputArr[i-2],inputArr[i-1],inputArr[i]]) then begin if (Length(currentSequence) = 0) then AddSequence(currentSequence,[inputArr[i-2],inputArr[i-1],inputArr[i]]) else AddSequence(currentSequence,inputArr[i]); end else // Reset currentSequence SetLength(currentSequence,0); end; // Longer sequence ? if (Length(currentSequence) > Length(Result)) then begin SetLength(Result,0); AddSequence(Result,currentSequence); end; end; end; procedure OutputSequence( const arr : TIntArr); var i : Integer; begin for i := 0 to High(arr) do begin if i <> High(arr) then Write(arr[i],',') else WriteLn(arr[i]); end; end; begin WriteLn('Longest Arithmetic Sequence:'); OutputSequence(GetLongestSequence(IsValidArithmeticSequence,[0,1,2,3,4,5,6])); OutputSequence(GetLongestSequence(IsValidArithmeticSequence,[1,0,1,2,3,4,5,6])); OutputSequence(GetLongestSequence(IsValidArithmeticSequence,[0,0,0,0,0,0])); OutputSequence(GetLongestSequence(IsValidArithmeticSequence,[0,0,1,2,4,8,16])); OutputSequence(GetLongestSequence(IsValidArithmeticSequence,[0,0,6,9,12,4,8,16])); OutputSequence(GetLongestSequence(IsValidArithmeticSequence,[9,12,16])); OutputSequence(GetLongestSequence(IsValidArithmeticSequence,[1,0,1,-1,-3])); OutputSequence(GetLongestSequence(IsValidArithmeticSequence,[5,4,78,2,3,4,5,6,18,54,16])); WriteLn('Longest Geometric Sequence:'); OutputSequence(GetLongestSequence(IsValidGeometricSequence,[0,1,2,3,4,5,6])); OutputSequence(GetLongestSequence(IsValidGeometricSequence,[1,0,1,2,3,4,5,6])); OutputSequence(GetLongestSequence(IsValidGeometricSequence,[0,0,0,0,0,0])); OutputSequence(GetLongestSequence(IsValidGeometricSequence,[0,0,1,2,4,8,16])); OutputSequence(GetLongestSequence(IsValidGeometricSequence,[0,0,6,9,12,4,8,16])); OutputSequence(GetLongestSequence(IsValidGeometricSequence,[9,12,16])); OutputSequence(GetLongestSequence(IsValidGeometricSequence,[1,0,9,-12,16])); OutputSequence(GetLongestSequence(IsValidGeometricSequence,[5,4,78,2,3,4,5,6,18,54,16])); ReadLn; end. As commented by David, mixing floating point calculations with integers can cause unwanted behavior. Eg. input sequence 9,12,16 with a geometric factor of 4/3 will work here, but other similar non-integer geometric factors may fail. More extensive testing is required to verify this. In order to remove the dependency of floating point operations, following change in the loop can be made: // A geometric function is defined by a + n*a + n^2*a + ... // By comparing sequence[i]*sequence[i-2] with Sqr(sequence[i-1]) // i.e. n^2*a*a with Sqr(n*a) we can establish a valid geometric sequence q := Sqr(sequence[i-1]); if (i < 2) then q1 := q // Special case, always true else q1 := sequence[i] * sequence[i - 2]; Change the declarations of d,d1,q,q1 to Integer and remove the assignment of q1 before the loop. The test code is updated to reflect these changes. There is a problem when a sequence has one or more zeroes for the geometric sequence calculations. Zero is only considered a member of a geometric sequence if all values are zero. Geometric sequence: a*r^0, a*r^1, a*r^2, etc; r <> 0. With a = 0 the progression consists of zeroes only. This also implies that a valid geometric sequence can not hold both non-zero and zero values. To rectify this with current structure it became messy. So I updated my test above with a better structured program that handles all input sequences.
This is quite an interesting problem. LU RD has given you an answer that fixes your code. I offer as an alternative, the way I would address the problem: program LongestSubsequence; {$APPTYPE CONSOLE} type TSubsequence = record Start: Integer; Length: Integer; end; function Subsequence(Start, Length: Integer): TSubsequence; begin Result.Start := Start; Result.Length := Length; end; type TTestSubsequenceRule = function(a, b, c: Integer): Boolean; function FindLongestSubsequence( const seq: array of Integer; const TestSubsequenceRule: TTestSubsequenceRule ): TSubsequence; var StartIndex, Index: Integer; CurrentSubsequence, LongestSubsequence: TSubsequence; begin LongestSubsequence := Subsequence(-1, 0); for StartIndex := low(seq) to high(seq) do begin CurrentSubsequence := Subsequence(StartIndex, 0); for Index := CurrentSubsequence.Start to high(seq) do begin if (CurrentSubsequence.Length<2) or TestSubsequenceRule(seq[Index-2], seq[Index-1], seq[Index]) then begin inc(CurrentSubsequence.Length); if CurrentSubsequence.Length>LongestSubsequence.Length then LongestSubsequence := CurrentSubsequence; end else break; end; end; Result := LongestSubsequence; end; function TestArithmeticSubsequence(a, b, c: Integer): Boolean; begin Result := (b-a)=(c-b); end; function FindLongestArithmeticSubsequence(const seq: array of Integer): TSubsequence; begin Result := FindLongestSubsequence(seq, TestArithmeticSubsequence); end; function TestGeometricSubsequence(a, b, c: Integer): Boolean; begin Result := (b*b)=(a*c); end; function FindLongestGeometricSubsequence(const seq: array of Integer): TSubsequence; begin Result := FindLongestSubsequence(seq, TestGeometricSubsequence); end; procedure OutputSubsequence(const seq: array of Integer; const Subsequence: TSubsequence); var Index: Integer; begin for Index := 0 to Subsequence.Length-1 do begin Write(seq[Subsequence.Start + Index]); if Index<Subsequence.Length-1 then Write(','); end; Writeln; end; procedure OutputLongestArithmeticSubsequence(const seq: array of Integer); begin OutputSubsequence(seq, FindLongestArithmeticSubsequence(seq)); end; procedure OutputLongestGeometricSubsequence(const seq: array of Integer); begin OutputSubsequence(seq, FindLongestGeometricSubsequence(seq)); end; begin Writeln('Testing arithmetic sequences:'); OutputLongestArithmeticSubsequence([]); OutputLongestArithmeticSubsequence([1]); OutputLongestArithmeticSubsequence([1,2]); OutputLongestArithmeticSubsequence([1,2,3]); OutputLongestArithmeticSubsequence([1,2,4]); OutputLongestArithmeticSubsequence([6,1,2,4,7]); OutputLongestArithmeticSubsequence([6,1,2,4,6,7]); Writeln('Testing geometric sequences:'); OutputLongestGeometricSubsequence([]); OutputLongestGeometricSubsequence([1]); OutputLongestGeometricSubsequence([1,2]); OutputLongestGeometricSubsequence([1,2,4]); OutputLongestGeometricSubsequence([7,1,2,4,-12]); OutputLongestGeometricSubsequence([-16,-12,-9]); OutputLongestGeometricSubsequence([4,-16,-12,-9]); Readln; end. The key point to stress is that your code is hard to understand because all the different aspects are mixed in with each other. I have attempted here to break the algorithm down into smaller parts which can be understood in isolation.
Improve speed on Crc16 calculation
I need to calculate Crc16 checksums with a $1021 polynom over large files, below is my current implementation but it's rather slow on large files (eg a 90 MB file takes about 9 seconds). So my question is how to improve my current implementation (to make it faster), I have googled and looked at some samples implementing a table lookup but my problem is that I don't understand how to modify them to include the polynom (probably my math is failing). { based on http://miscel.dk/MiscEl/CRCcalculations.html } function Crc16(const Buffer: PByte; const BufSize: Int64; const Polynom: WORD=$1021; const Seed: WORD=0): Word; var i,j: Integer; begin Result := Seed; for i:=0 to BufSize-1 do begin Result := Result xor (Buffer[i] shl 8); for j:=0 to 7 do begin if (Result and $8000) <> 0 then Result := (Result shl 1) xor Polynom else Result := Result shl 1; end; end; Result := Result and $FFFF; end;
If you want this to be fast, you need to implement a table-lookup CRC algorithm. See chapter 10 of A PAINLESS GUIDE TO CRC ERROR DETECTION ALGORITHMS INDEX V3.00 (9/24/96)
Look for CRC routines from jclMath.pas unit of Jedi Code Library. It uses CRC lookup tables. http://jcl.svn.sourceforge.net/viewvc/jcl/trunk/jcl/source/common/
Your Result variable is a Word, which means there are 64k possible values it could have upon entry to the inner loop. Calculate the 64k possible results that the loop could generate and store them in an array. Then, instead of looping eight times for each byte of the input buffer, simply look up the next value of the checksum in the array. Something like this: function Crc16(const Buffer: PByte; const BufSize: Int64; const Polynom: Word = $1021; const Seed: Word = 0): Word; {$J+} const Results: array of Word = nil; OldPolynom: Word = 0; {$J-} var i, j: Integer; begin if (Polynom <> OldPolynom) or not Assigned(Results) then begin SetLength(Results, 65535); for i := 0 to Pred(Length(Results)) do begin Results[i] := i; for j := 0 to 7 do if (Results[i] and $8000) <> 0 then Results[i] := (Results[i] shl 1) xor Polynom else Results[i] := Results[i] shl 1; end; OldPolynom := Polynom; end; Result := Seed; for i := 0 to Pred(BufSize) do Result := Results[Result xor (Buffer[i] shl 8)]; end; That code recalculates the lookup table any time Polynom changes. If that parameter varies among a set of values, then consider caching the lookup tables you generate for them so you don't waste time calculating the same tables repeatedly. If Polynom will always be $1021, then don't even bother having a parameter for it. Calculate all 64k values in advance and hard-code them in a big array, so your entire function is reduced to just the last three lines of my function above.
Old thread, i know. Here is my implementation (just one loop): function crc16( s : string; bSumPos : Boolean = FALSE ) : Word; var L, crc, sum, i, x, j : Word; begin Result:=0; L:=length(s); if( L > 0 ) then begin crc:=$FFFF; sum:=length(s); for i:=1 to L do begin j:=ord(s[i]); sum:=sum+((i) * j); x:=((crc shr 8) xor j) and $FF; x:=x xor (x shr 4); crc:=((crc shl 8) xor (x shl 12) xor (x shl 5) xor x) and $FFFF; end; Result:=crc+(Byte(bSumPos) * sum); end; end; Nice thing is also that you can create an unique id with it, for example to get an unique identifier for a filename, like: function uniqueId( s : string ) : Word; begin Result:=crc16( s, TRUE ); end; Cheers, Erwin Haantjes