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

28
LINES

< > BotCompany Repo | #1029030 // intersectSortedIntArrays_ofs_optimized

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

Libraryless. Click here for Pure Java version (2531L/16K).

1  
// is allowed to return null when result is empty
2  
// ofs is added to elements of b prior to comparison
3  
static int[] intersectSortedIntArrays_ofs_optimized(int[] a, int[] b, int ofs) {
4  
  int i = 0, j = 0, la = l(a), lb = l(b);
5  
  
6  
  // special case zero elements
7  
  if (la == 0 || lb == 0) null;
8  
  
9  
  // special case one element
10  
  if (la == 1)
11  
    ret Arrays.binarySearch(b, a[0]-ofs) >= 0 ? a : null;
12  
  if (lb == 1)
13  
    ret Arrays.binarySearch(a, b[0]+ofs) >= 0 ? b : null;
14  
15  
  IntBuffer buf = new(min(la, lb));
16  
  while (i < la && j < lb) {
17  
    int x = a[i], y = b[j]+ofs;
18  
    if (x == y) {
19  
      buf.add(x);
20  
      ++i;
21  
      ++j;
22  
    } else if (x < y)
23  
      ++i;
24  
    else
25  
      ++j;
26  
  }
27  
  ret buf.toArray();
28  
}

Author comment

Began life as a copy of #1029029

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: #1029030
Snippet name: intersectSortedIntArrays_ofs_optimized
Eternal ID of this version: #1029030/5
Text MD5: 6be262fcc7cb8bfbec386295fd171e98
Transpilation MD5: cb86aac92cb2509642cb28dd389cab81
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-07-17 01:35:27
Source code size: 734 bytes / 28 lines
Pitched / IR pitched: No / No
Views / Downloads: 121 / 192
Version history: 4 change(s)
Referenced in: [show references]