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).

// is allowed to return null when result is empty
// ofs is added to elements of b prior to comparison
static int[] intersectSortedIntArrays_ofs_optimized(int[] a, int[] b, int ofs) {
  int i = 0, j = 0, la = l(a), lb = l(b);
  
  // special case zero elements
  if (la == 0 || lb == 0) null;
  
  // special case one element
  if (la == 1)
    ret Arrays.binarySearch(b, a[0]-ofs) >= 0 ? a : null;
  if (lb == 1)
    ret Arrays.binarySearch(a, b[0]+ofs) >= 0 ? b : null;

  IntBuffer buf = new(min(la, lb));
  while (i < la && j < lb) {
    int x = a[i], y = b[j]+ofs;
    if (x == y) {
      buf.add(x);
      ++i;
      ++j;
    } else if (x < y)
      ++i;
    else
      ++j;
  }
  ret buf.toArray();
}

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: 115 / 183
Version history: 4 change(s)
Referenced in: [show references]