Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

58
LINES

< > BotCompany Repo | #1029031 // intersectSortedIntArrays_ofs_optimized2 [OK] - recursive version!

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (149L/2K).

1  
scope intersectSortedIntArrays_ofs_optimized2.
2  
3  
// is allowed to return null when result is empty
4  
// ofs is added to elements of b prior to comparison
5  
static int[] intersectSortedIntArrays_ofs_optimized2(int[] a, int[] b, int ofs, IntBuffer buf default null) {
6  
  int i = 0, j = 0, la = lIntArray(a), lb = lIntArray(b);
7  
  
8  
  // swap if a is longer than b
9  
  bool swapped = la > lb;
10  
  if (swapped) {
11  
    int[] temp = a; a = b; b = temp;
12  
    int temp2 = la; la = lb; lb = temp2;
13  
    ofs = -ofs;
14  
  }
15  
  
16  
  // special case zero elements
17  
  if (la == 0) null;
18  
  
19  
  // special case one element
20  
  if (la == 1 && !swapped)
21  
    ret intArrayBinarySearch(b, a[0]-ofs) >= 0 ? a : null;
22  
    
23  
  if (buf == null) buf = new IntBuffer; else buf.reset();
24  
  recurse(a, b, ofs, buf, 0, la, 0, lb);
25  
  int[] out = buf.toArray();
26  
  if (swapped) {
27  
    int n = lIntArray(out);
28  
    for k to n:
29  
      out[k] -= ofs;
30  
  }
31  
  ret out;
32  
}
33  
34  
svoid #recurse(int[] a, int[] b, int ofs, IntBuffer buf, int aFrom, int aTo, int bFrom, int bTo) {
35  
  if (aFrom >= aTo || bFrom >= bTo) ret; // nothing to do
36  
  
37  
  // start in the middle of a, search this element in b
38  
  int i = (aFrom+aTo)/2;
39  
  int x = a[i];
40  
  int j = intArrayBinarySearch(b, bFrom, bTo, x-ofs);
41  
  
42  
  ifdef intersectSortedIntArrays_ofs_optimized2_debug
43  
    printVars_str(+aFrom, +aTo, +i, +x, +ofs, +bFrom, +bTo, +j);
44  
  endifdef
45  
  
46  
  if (j >= 0) {
47  
    // element found
48  
    recurse(a, b, ofs, buf, aFrom, i, bFrom, j);
49  
    buf.add(x);
50  
    recurse(a, b, ofs, buf, i+1, aTo, j+1, bTo);
51  
  } else {
52  
    j = -j-1;
53  
    recurse(a, b, ofs, buf, aFrom, i, bFrom, j);
54  
    recurse(a, b, ofs, buf, i+1, aTo, j, bTo);
55  
  }
56  
}
57  
58  
end scope

Author comment

Began life as a copy of #1029030

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1029031
Snippet name: intersectSortedIntArrays_ofs_optimized2 [OK] - recursive version!
Eternal ID of this version: #1029031/18
Text MD5: 0ef1906cb6321c33c41a428ad735e488
Transpilation MD5: c0ab1bdf47921c5f44e28bb1350e9422
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-07-17 14:13:08
Source code size: 1695 bytes / 58 lines
Pitched / IR pitched: No / No
Views / Downloads: 256 / 402
Version history: 17 change(s)
Referenced in: [show references]