aboutsummaryrefslogtreecommitdiff
path: root/0013/Vector.tpl.pas
diff options
context:
space:
mode:
Diffstat (limited to '0013/Vector.tpl.pas')
-rw-r--r--0013/Vector.tpl.pas54
1 files changed, 54 insertions, 0 deletions
diff --git a/0013/Vector.tpl.pas b/0013/Vector.tpl.pas
new file mode 100644
index 0000000..6463377
--- /dev/null
+++ b/0013/Vector.tpl.pas
@@ -0,0 +1,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.
+