Janilabo

Boolean.scar

Sep 19th, 2012
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 10.25 KB | None | 0 0
  1. {==============================================================================|  
  2.   function MSSL_StrIsBoolean(str: string): Boolean;
  3.   Contributors: Janilabo
  4.   Description: Returns true if str is Boolean.
  5.   Example: 'true' = TRUE; 'false' = TRUE; ..everything else returns FALSE.
  6.   Created: September 30th, 2012.                  
  7. [==============================================================================}
  8. function MSSL_StrIsBoolean(str: string): Boolean;
  9. begin
  10.   str := Lowercase(str);
  11.   Result := ((str = 'true') or (str = 'false'));
  12. end;
  13.  
  14. {==============================================================================|  
  15.   function MSSL_BooleanLine(x: Boolean; count: Integer): TBoolArray;
  16.   Contributors: Janilabo
  17.   Description: Creates a line of (x) booleans, count being the size of the result.
  18.   Example: MSSL_BooleanLine(True, 3) returns [True, True, True]
  19.   Created: September 30th, 2012.                  
  20. [==============================================================================}
  21. function MSSL_BooleanLine(x: Boolean; count: Integer): TBoolArray;
  22. var
  23.   i: Integer;
  24. begin
  25.   if (count < 1) then
  26.     Exit;
  27.   SetLength(Result, count);
  28.   for i:= 0 to (count - 1) do
  29.     Result[i] := x;
  30. end;
  31.  
  32. {==============================================================================|  
  33.   procedure MSSL_TBlACopy(source: TBoolArray; var target: TBoolArray; resetTarget: Boolean);
  34.   Contributors: Janilabo
  35.   Description: Copies source to target. If resetTarget is set to true,
  36.                target will be cleared before the copying process.
  37.   Created: September 30th, 2012.                  
  38. [==============================================================================}
  39. procedure MSSL_TBlACopy(source: TBoolArray; var target: TBoolArray; resetTarget: Boolean);
  40. var
  41.   i, tAL, sAL: Integer;
  42. begin
  43.   if resetTarget then
  44.     SetLength(target, 0);
  45.   tAL := Length(target);
  46.   sAL := Length(source);
  47.   SetLength(target, (tAL + sAL));
  48.   for i := 0 to (sAL - 1) do
  49.     target[(tAL + i)] := source[i];
  50. end;
  51.  
  52. {==============================================================================|  
  53.   procedure MSSL_TBlAMove(var source, target: TBoolArray; resetTarget: Boolean);
  54.   Contributors: Janilabo
  55.   Description: Moves source to target. If resetTarget is set to true,
  56.                target will be cleared before the moving process.
  57.   Created: September 30th, 2012.                  
  58. [==============================================================================}
  59. procedure MSSL_TBlAMove(var source, target: TBoolArray; resetTarget: Boolean);
  60. begin
  61.   MSSL_TBlACopy(resetTarget, source, target);
  62.   SetLength(source, 0);
  63. end;
  64.  
  65. {==============================================================================|  
  66.   function MSSL_TBlAPos(bool: Boolean; arr: TBoolArray): Integer;
  67.   Contributors: Janilabo
  68.   Description: Returns the position of bool in arr.
  69.                Result will be -1, if bool is not found in arr.
  70.   Example: MSSL_TBlAPos(False, [True, True, False]) returns 2
  71.   Created: September 30th, 2012.                  
  72. [==============================================================================}
  73. function MSSL_TBlAPos(bool: Boolean; arr: TBoolArray): Integer;
  74. var
  75.   h: Integer;
  76. begin
  77.   h := High(arr);
  78.   for Result := 0 to h do
  79.     if (arr[Result] = bool) then
  80.       Exit;
  81.   Result := -1;
  82. end;
  83.  
  84. {==============================================================================|  
  85.   function MSSL_TBlALastPos(bool: Boolean; arr: TBoolArray): Integer;
  86.   Contributors: Janilabo
  87.   Description: Returns the last position of bool in arr.
  88.                Result will be -1, if bool is not found in arr.
  89.   Example: MSSL_TBlAPos(True, [True, True, False]) returns 1
  90.   Created: September 30th, 2012.                  
  91. [==============================================================================}
  92. function MSSL_TBlALastPos(bool: Boolean; arr: TBoolArray): Integer;
  93. var
  94.   h: Integer;
  95. begin
  96.   h := High(arr);
  97.   for Result := h downto 0 do
  98.     if (arr[Result] = bool) then
  99.       Exit;
  100.   Result := -1;
  101. end;
  102.  
  103. {==============================================================================|  
  104.   function MSSL_TBlAPositions(bool: Boolean; arr: TBoolArray): TIntArray;
  105.   Contributors: Janilabo
  106.   Description: Returns all positions of bool in arr.
  107.   Example: MSSL_TBlAPos(True, [True, False, True]) returns  [0, 2]
  108.   Created: September 30th, 2012.                  
  109. [==============================================================================}
  110. function MSSL_TBlAPositions(bool: Boolean; arr: TBoolArray): TIntArray;
  111. var
  112.   i, rC, h: Integer;
  113. begin
  114.   h := High(arr);
  115.   SetLength(Result, (h + 1));
  116.   for i := 0 to h do
  117.     if (arr[i] = bool) then
  118.     begin
  119.       Result[rC] := i;
  120.       inc(rC);
  121.     end;
  122.   SetLength(Result, rC);
  123. end;                                  
  124.  
  125. {==============================================================================|  
  126.   procedure MSSL_TBlAInsert(var arr: TBoolArray; index: Integer; bool: Boolean);
  127.   Contributors: Janilabo
  128.   Description: Inserts bool to arr using index as the position.
  129.   Created: September 30th, 2012.                  
  130. [==============================================================================}
  131. procedure MSSL_TBlAInsert(var arr: TBoolArray; index: Integer; bool: Boolean);
  132. var                    
  133.   i, l: Integer;
  134. begin
  135.   l := Length(arr);
  136.   SetLength(arr, (l + 1));    
  137.   if (index < 0) then
  138.     index := 0;
  139.   if (index > l) then
  140.     index := l;
  141.   if (l > index) then
  142.   for i := (l - 1) downto index do
  143.     arr[(i + 1)] := arr[i];
  144.   arr[index] := bool;
  145. end;
  146.  
  147. {==============================================================================|  
  148.   function MSSL_TBlAContains(arr: TBoolArray; x: Boolean): Boolean;
  149.   Contributors: Janilabo
  150.   Description: Returns true if x is found in arr (arr contains x).
  151.   Created: September 30th, 2012.                  
  152. [==============================================================================}
  153. function MSSL_TBlAContains(arr: TBoolArray; x: Boolean): Boolean;
  154. var
  155.   i, h: Integer;
  156. begin
  157.   h := High(arr);
  158.   for i := 0 to h do
  159.   begin
  160.     Result := (x = arr[i]);
  161.     if Result then
  162.       Exit;
  163.   end;
  164. end;
  165.  
  166. {==============================================================================|  
  167.   procedure MSSL_TBlADelete(var arr: TBoolArray; x: Integer);
  168.   Contributors: Janilabo
  169.   Description: Deletes item from arr by position (x).
  170.   Created: September 30th, 2012.                  
  171. [==============================================================================}
  172. procedure MSSL_TBlADelete(var arr: TBoolArray; x: Integer);
  173. var
  174.   i, h: Integer;            
  175. begin
  176.   h := High(arr);
  177.   if (x > h) then
  178.     Exit;
  179.   for i := x to (h - 1) do
  180.     arr[i] := arr[(i + 1)];
  181.   SetLength(arr, h);
  182. end;
  183.  
  184. {==============================================================================|  
  185.   procedure MSSL_TBlAAppend(var arr: TBoolArray; x: Boolean);
  186.   Contributors: Janilabo
  187.   Description: Appends arr with x (adds x to arr).
  188.   Created: September 30th, 2012.                  
  189. [==============================================================================}
  190. procedure MSSL_TBlAAppend(var arr: TBoolArray; x: Boolean);
  191. var        
  192.   aL: Integer;
  193. begin
  194.   aL := (Length(arr) + 1);
  195.   SetLength(arr, aL);
  196.   arr[(aL - 1)] := x;
  197. end;
  198.  
  199. {==============================================================================|  
  200.   function MSSL_TBlALine(x: TBoolArray; count: Integer;): T2DBoolArray;
  201.   Contributors: Janilabo
  202.   Description: Creates a line of (x) boolean arrays, count being the size of the result.
  203.   Created: September 30th, 2012.                  
  204. [==============================================================================}
  205. function MSSL_TBlALine(x: TBoolArray; count: Integer): T2DBoolArray;
  206. var
  207.   i: Integer;
  208. begin
  209.   if (count < 1) then
  210.     Exit;
  211.   SetLength(Result, count);
  212.   for i := 0 to (count - 1) do
  213.     Result[i] := x;
  214. end;
  215.  
  216. {==============================================================================|  
  217.   function MSSL_TBlAToATBlAByPartSize(TBlA: TBoolArray; partSize: Integer): T2DBoolArray;
  218.   Contributors: Janilabo
  219.   Description: Breaks TBlA to T2DBlA by part size.
  220.   Created: September 30th, 2012.                  
  221. [==============================================================================}
  222. function MSSL_TBlAToATBlAByPartSize(TBlA: TBoolArray; partSize: Integer): T2DBoolArray;
  223. var
  224.   i, i2, r, h, d: Integer;
  225. begin
  226.   h := High(TBlA);
  227.   if ((h >= 0) and (partSize > 0)) then
  228.     if (partSize <= h) then
  229.     begin
  230.       Inc(h);
  231.       r := (h div partSize);
  232.       if ((r * partSize) < h) then
  233.         Inc(r);
  234.       SetLength(Result, r);
  235.       for i := 0 to (r - 1) do
  236.         for i2 := 0 to (partSize - 1) do
  237.         begin
  238.           SetLength(Result[i], partSize);
  239.           if (d < h) then
  240.           begin
  241.             Result[i][i2] := TBlA[d];
  242.             Inc(d);
  243.           end else
  244.           begin
  245.             SetLength(Result[i], i2);
  246.             Exit;
  247.           end;
  248.         end;
  249.     end else
  250.       Result := [TBlA];
  251. end;
  252.  
  253. {==============================================================================|  
  254.   function MSSL_TBlAToATBlAByPartAmount(TBlA: TBoolArray; amount: Integer): T2DBoolArray;
  255.   Contributors: Janilabo
  256.   Description: Breaks TBlA to T2DBlA by part amount.
  257.   Created: September 30th, 2012.                  
  258. [==============================================================================}
  259. function MSSL_TBlAToATBlAByPartAmount(TBlA: TBoolArray; amount: Integer): T2DBoolArray;
  260. var
  261.   p, h, e, i, i2, h2, a: Integer;
  262. begin
  263.   h := High(TBlA);
  264.   if ((h <= 0) or (amount < 2)) then
  265.   begin
  266.     if (amount = 1) then
  267.       Result := [TBlA]
  268.     else
  269.       if (amount > 0) then
  270.         SetLength(Result, Amount);
  271.     Exit;
  272.   end;
  273.   if (h < (amount - 1)) then
  274.     amount := (h + 1);
  275.   p := Floor((h + 1) / amount);
  276.   if (p = 0) then
  277.     p := 1;
  278.   e := ((h + 1) - (p * amount));
  279.   if (e >= (h + 1)) then
  280.     e := 0;
  281.   SetLength(Result, amount);
  282.   for i := 0 to (amount - 1) do
  283.   begin
  284.     if ((e >= (i + 1)) and (e > 0)) then
  285.       SetLength(Result[i], (p + 1))
  286.     else
  287.       if (i <= h) then
  288.         SetLength(Result[i], p);
  289.     h2 := High(Result[i]);
  290.     for i2 := 0 to h2 do
  291.     begin
  292.       Result[i][i2] := TBlA[a];
  293.       Inc(a);
  294.     end;
  295.   end;
  296. end;
Advertisement
Add Comment
Please, Sign In to add comment