aboutsummaryrefslogtreecommitdiff
path: root/0013/Vector.tpl.pas
blob: 646337733b62da91e19bc6b3605f12aeb9e652e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
unit Vector_{UNIT}.{T};

interface
uses
    {UNIT};

type
    TVector = record
        len: Longint;
        capacity: Longint;
        buf: Array of {T};
    end;
    PElem = ^{T};
    TElem = {T};

procedure init(var this: TVector);
function get_elem(var this: TVector; n: Longint): PElem; 
procedure set_elem(var this: TVector; n: Longint; elem: {T});
procedure push_elem(var this: TVector; elem: {T});

implementation
procedure init(var this: TVector);
begin
    this.len := 0;
    this.capacity := 16;
    setLength(this.buf, 16);
end;

function get_elem(var this: TVector; n: Longint): PElem;
begin
    get_elem := @(this.buf[n]);
end;

procedure set_elem(var this: TVector; n: Longint; elem: {T});
begin
    this.buf[n] := elem;
end;

procedure push_elem(var this: TVector; elem: {T});
var
    newcap: Longint;
begin
    if this.len = this.capacity - 1 then
    begin
        newcap := this.capacity * 2;
        setLength(this.buf, newcap);
        this.capacity := newcap;
    end;
    this.buf[this.len] := elem;
    this.len := this.len + 1;
end;

end.