aboutsummaryrefslogtreecommitdiff
path: root/0013/Vector.tpl.pas
diff options
context:
space:
mode:
authorMistivia <i@mistivia.com>2025-08-27 15:28:59 +0800
committerMistivia <i@mistivia.com>2025-08-27 15:28:59 +0800
commit9f4bae54801d78426b7089d0ab13f45f57656b3b (patch)
tree593ba04a95f3e6e7fe926af2168ffa6bffecc4d7 /0013/Vector.tpl.pas
parent16bb535cdf949eaed2c4981166d82db092915532 (diff)
solve 13
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.
+