Forum > General
Slice through a pointer
zamtmn:
There is a great Slice function , but its use is clouded by the need to have Array type. Often Array this type is superfluous and is not used anywhere, only for slice calling. I think it would be great to add the ability to call through a pointer to a type. Or is there such a possibility and I don’t know it?
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program project1;{$mode delphi}{$H+}uses gvector;type TTest2=TVector<integer>; TDummyArr=array[0..0]of integer; PTDummyArr=^TDummyArr;procedure WriteArgs(const Args:array of integer);var i:integer;begin for i:=low(args) to high(args) do writeln(args[i]);end;var arr:TTest2;begin arr:=TTest2.Create; arr.PushBack(1); arr.PushBack(2); arr.PushBack(3); arr.PushBack(4); WriteArgs(PTDummyArr(arr.Mutable[1])^[0..1]);//this work WriteArgs(slice(PTDummyArr(arr.Mutable[1])^,2));//this work WriteArgs(slice(PInteger(arr.Mutable[1]),2));//this better, bun not work(( WriteArgs(slice(arr.Mutable[1]),2);//ideal)) readln;end.
zamtmn:
No function will help here, it's the compiler magic
zamtmn:
>>Oh really?
Yes.
Drinking coffee and thinking about how it works will be very good))
zamtmn:
You're copying the data. It's an easy but slow way.
I never do superfluous work and I don't recommend this to you))
zamtmn:
jamie
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- Function TVectorWithSlice<T>.Slice(aStart,ACount:Integer):TArray<T>; Begin SetLength(Result, ACount); Result := Copy(fData,aStart,ACount); end;Try it on trunk fpc, you will not be pleasantly surprised((
Navigation
[0] Message Index
[#] Next page