Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program new;
- function Slice(arr:TIntegerArray; start,stop:Int64; step:Int32=1): TIntegerArray;
- var
- len:Int32;
- P,R:^Int32;
- L:PtrUInt;
- begin
- len := Length(arr);
- if len = 0 then Exit();
- if (step > 0) and (stop >= len) then
- stop := len; //not inclusive
- if (step < 0) and (start >= len) then
- start := len-1; //inclusive
- if ((stop < 0) and ((abs(stop) > abs(start)) or (step > 0))) then
- stop := len+stop;
- if (start < 0) then
- start := len+start;
- SetLength(Result, Max(0, Ceil((stop-start) / step)));
- if Length(Result) = 0 then Exit();
- //WriteLn('>>> ', [start, stop, step]);
- P := @arr[start];
- R := @Result[0];
- L := PtrUInt(@Result[High(Result)]);
- while PtrUInt(R) <= L do
- begin
- R^ := P^;
- Inc(R);
- Inc(P, step);
- end;
- end;
- function Equal(Left, Right: TIntegerArray): Boolean;
- begin
- //WriteLn([Left, Right]);
- if Length(Left) <> Length(Right) then Exit(False);
- If (Length(Left) = Length(Right)) and (Length(Left) = 0) then Exit(True);
- Result := CompareMem(Left[0], Right[0], SizeOf(Int32)*Length(Left));
- end;
- var
- x: TIntegerArray = [0,1,2,3,4,5,6,7,8,9];
- begin
- WriteLn Equal(Slice(x, 0,High(Int32),1), [0, 1, 2, 3, 4, 5, 6, 7, 8,9]);
- WriteLn Equal(Slice(x, High(Int32),0,-1), [9, 8, 7, 6, 5, 4, 3, 2, 1]);
- WriteLn Equal(Slice(x, 0,-1,1), [0, 1, 2, 3, 4, 5, 6, 7, 8]);
- WriteLn Equal(Slice(x, 6,0,-1), [6, 5, 4, 3, 2, 1]);
- WriteLn Equal(Slice(x, 0,6,1), [0, 1, 2, 3, 4, 5]);
- WriteLn Equal(Slice(x, 0,10,1), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
- WriteLn Equal(Slice(x, 0,7,3), [0, 3, 6]);
- WriteLn Equal(Slice(x, 5,9,1), [5, 6, 7, 8]);
- WriteLn Equal(Slice(x, -1,0,-1), [9, 8, 7, 6, 5, 4, 3, 2, 1]);
- WriteLn Equal(Slice(x, -1,-1,-1), [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]);
- WriteLn Equal(Slice(x, -3,-1, 1), [7, 8]);
- WriteLn Equal(Slice(x, -1,-1, 1), []);
- WriteLn Equal(Slice(x, 5,0, 1), []);
- WriteLn Equal(Slice(x, 0,5,-1), []);
- WriteLn Equal(Slice(x, 7,2,-3), [7,4]);
- WriteLn Equal(Slice(x, 7,2,-100), [7]);
- WriteLn Equal(Slice(x, -4,-1, 1), [6,7,8]);
- WriteLn Equal(Slice(x, -1,-4,-1), [9,8,7]);
- WriteLn Equal(Slice(x, -1, 3,-1), [9, 8, 7, 6, 5, 4]);
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement