summaryrefslogtreecommitdiff
path: root/plugins/HistoryPlusPlus/hpp_arrays.pas
blob: c11060ad04e02235468fa399ab60b6e45ed2c19a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
(*
  History++ plugin for Miranda IM: the free IM client for Microsoft* Windows*

  Copyright (C) 2006-2009 theMIROn, 2003-2006 Art Fedorov.
  History+ parts (C) 2001 Christian Kastner

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*)

unit hpp_arrays;

interface

uses hpp_global;

function IntSortedArray_Add(var A: TIntArray; Value: Integer): Integer;
procedure IntSortedArray_Remove(var A: TIntArray; Value: Integer);
function IntSortedArray_Find(var A: TIntArray; Value: Integer): Integer;
procedure IntSortedArray_Sort(var A: TIntArray);
function IntSortedArray_NonIntersect(var A, B: TIntArray): TIntArray;

procedure IntArrayRemove(var A: TIntArray; Index: Integer);
procedure IntArrayInsert(var A: TIntArray; Index: Integer; Value: Integer);

implementation

uses hpp_jclSysUtils;

procedure IntArrayRemove(var A: TIntArray; Index: Integer);
var
  i: Integer;
begin
  for i := Index to Length(A) - 2 do
    A[i] := A[i + 1];
  SetLength(A, Length(A) - 1);
end;

procedure IntArrayInsert(var A: TIntArray; Index: Integer; Value: Integer);
var
  i: Integer;
begin
  SetLength(A, Length(A) + 1);
  for i := Length(A) - 1 downto Index do
    A[i] := A[i - 1];
  A[Index] := Value;
end;

function IntSortedArray_Add(var A: TIntArray; Value: Integer): Integer;
begin
  Result := SearchDynArray(A, SizeOf(Integer), DynArrayCompareInteger, @Value, True);
  if Result <> -1 then // we have nearest or match
  begin
    if A[Result] = Value then
      exit;
    if A[Result] < Value then
      Inc(Result);
  end
  else // we don't have any nearest values, array is empty
    Result := 0;
  IntArrayInsert(A, Result, Value);
end;

procedure IntSortedArray_Remove(var A: TIntArray; Value: Integer);
var
  idx: Integer;
begin
  idx := SearchDynArray(A, SizeOf(Integer), DynArrayCompareInteger, @Value);
  if idx = -1 then
    exit;
  IntArrayRemove(A, idx);
end;

function IntSortedArray_Find(var A: TIntArray; Value: Integer): Integer;
begin
  Result := SearchDynArray(A, SizeOf(Integer), DynArrayCompareInteger, @Value);
end;

procedure IntSortedArray_Sort(var A: TIntArray);
begin
  SortDynArray(A, SizeOf(Integer), DynArrayCompareInteger);
end;

function IntSortedArray_NonIntersect(var A, B: TIntArray): TIntArray;
var
  ia, ib: Integer;
  lenr, lena, lenb: Integer;

  procedure AddToResult(Item: Integer);
  begin
    Inc(lenr);
    SetLength(Result, lenr);
    Result[lenr - 1] := Item;
  end;

begin
  SetLength(Result, 0);
  lenr := 0;
  lena := Length(A);
  lenb := Length(B);
  ib := 0;
  ia := 0;

  while ia < lena do
  begin

    if ib >= lenb then
    begin
      AddToResult(A[ia]);
      Inc(ia);
      continue;
    end;

    if A[ia] = B[ib] then
    begin
      Inc(ib);
      Inc(ia);
      continue;
    end;

    if A[ia] > B[ib] then
    begin
      while A[ia] > B[ib] do
      begin
        AddToResult(B[ib]);
        Inc(ib);
        if ib >= lenb then
          break;
      end;
      continue;
    end;

    if A[ia] < B[ib] then
    begin
      AddToResult(A[ia]);
      Inc(ia);
      continue;
    end;

  end;

  while ib < lenb do
  begin
    AddToResult(B[ib]);
    Inc(ib);
  end;
end;

end.