/// <summary> /// This method expects both lists to be sorted /// </summary> private IntersectDocs GetIntersectedDocuments(int[] a, int[] b, HashSet<IndexSearcherHolder.StringCollectionValue> alreadySeen, bool needToApplyAggregation) { int[] n,m; if (a.Length > b.Length) { n = a; m = b; } else { n = b; m = a; } int nSize = n.Length; int mSize = m.Length; double o1 = nSize + mSize; double o2 = nSize * Math.Log(mSize, 2); var isDistinct = IndexQuery.IsDistinct; var result = new IntersectDocs(); if (needToApplyAggregation) result.Documents = new List<int>(); if (o1 < o2) { int mi = 0, ni = 0; while (mi < mSize && ni < nSize) { if (n[ni] > m[mi]) { mi++; } else if (n[ni] < m[mi]) { ni++; } else { int docId = n[ni]; if (isDistinct == false || IsDistinctValue(docId, alreadySeen)) { result.AddIntersection(docId); } ni++; mi++; } } } else { for (int i = 0; i < mSize; i++) { if (Array.BinarySearch(n, m[i]) >= 0) { int docId = m[i]; if (isDistinct == false || IsDistinctValue(docId, alreadySeen)) { result.AddIntersection(docId); } } } } return result; }
/// <summary> /// This method expects both lists to be sorted /// </summary> private IntersectDocs GetIntersectedDocuments(ArraySegment <int> a, ArraySegment <int> b, bool needToApplyAggregation) { ArraySegment <int> n, m; if (a.Count > b.Count) { n = a; m = b; } else { n = b; m = a; } int nSize = n.Count; int mSize = m.Count; double o1 = nSize + mSize; double o2 = mSize * Math.Log(nSize, 2); var result = new IntersectDocs(); if (needToApplyAggregation) { result.Documents = IntArraysPool.Instance.AllocateArray(); } if (o1 < o2) { int mi = m.Offset, ni = n.Offset; while (mi < mSize && ni < nSize) { var nVal = n.Array[ni]; var mVal = m.Array[mi]; if (nVal > mVal) { mi++; } else if (nVal < mVal) { ni++; } else { int docId = nVal; result.AddIntersection(docId); ni++; mi++; } } } else { for (int i = m.Offset; i < mSize; i++) { int docId = m.Array[i]; if (Array.BinarySearch(n.Array, n.Offset, n.Count, docId) >= 0) { result.AddIntersection(docId); } } } return(result); }
/// <summary> /// This method expects both lists to be sorted /// </summary> private IntersectDocs GetIntersectedDocuments(ArraySegment <int> a, ArraySegment <int> b, HashSet <IndexSearcherHolder.StringCollectionValue> alreadySeen, bool needToApplyAggregation) { ArraySegment <int> n, m; if (a.Count > b.Count) { n = a; m = b; } else { n = b; m = a; } int nSize = n.Count; int mSize = m.Count; double o1 = nSize + mSize; double o2 = nSize * Math.Log(mSize, 2); var isDistinct = IndexQuery.IsDistinct; var result = new IntersectDocs(); if (needToApplyAggregation) { result.Documents = IntArraysPool.Instance.AllocateArray(); } if (o1 < o2) { int mi = m.Offset, ni = n.Offset; while (mi < mSize && ni < nSize) { var nVal = n.Array[ni]; var mVal = m.Array[mi]; if (nVal > mVal) { mi++; } else if (nVal < mVal) { ni++; } else { int docId = nVal; if (isDistinct == false || IsDistinctValue(docId, alreadySeen)) { result.AddIntersection(docId); } ni++; mi++; } } } else { for (int i = m.Offset; i < mSize; i++) { int docId = m.Array[i]; if (Array.BinarySearch(n.Array, n.Offset, n.Count, docId) >= 0) { if (isDistinct == false || IsDistinctValue(docId, alreadySeen)) { result.AddIntersection(docId); } } } } return(result); }
/// <summary> /// This method expects both lists to be sorted /// </summary> private IntersectDocs GetIntersectedDocuments(ArraySegment <int> a, ArraySegment <int> b, bool needToApplyAggregation) { if (a.Count == 0 || b.Count == 0) { return(IntersectDocs.Empty); } ArraySegment <int> n, m; if (a.Count > b.Count) { n = a; m = b; } else { n = b; m = a; } int nSize = n.Count; int mSize = m.Count; int[] nArray = n.Array; int[] mArray = m.Array; if (n[0] > m[^ 1] || m[0] > n[^ 1]) // quick check if intersection is even possible { return(IntersectDocs.Empty); } double o1 = nSize + mSize; double o2 = mSize * Math.Log(nSize, 2); var result = new IntersectDocs(); if (needToApplyAggregation) { result.Documents = IntArraysPool.Instance.AllocateArray(); } if (o1 < o2) { int mi = m.Offset, ni = n.Offset; while (mi < mSize && ni < nSize) { var nVal = nArray[ni]; var mVal = mArray[mi]; if (nVal > mVal) { mi++; } else if (nVal < mVal) { ni++; } else { int docId = nVal; result.AddIntersection(docId); ni++; mi++; } } } else { for (int i = m.Offset; i < mSize; i++) { int docId = mArray[i]; if (Array.BinarySearch(nArray, n.Offset, n.Count, docId) >= 0) { result.AddIntersection(docId); } } } if (result.Count == 0 && needToApplyAggregation) { IntArraysPool.Instance.FreeArray(result.Documents); result.Documents = null; } return(result); }
/// <summary> /// This method expects both lists to be sorted /// </summary> private IntersectDocs GetIntersectedDocuments(int[] a, int[] b, HashSet <IndexSearcherHolder.StringCollectionValue> alreadySeen, bool needToApplyAggregation) { int[] n, m; if (a.Length > b.Length) { n = a; m = b; } else { n = b; m = a; } int nSize = n.Length; int mSize = m.Length; double o1 = nSize + mSize; double o2 = nSize * Math.Log(mSize, 2); var isDistinct = IndexQuery.IsDistinct; var result = new IntersectDocs(); if (needToApplyAggregation) { result.Documents = new List <int>(); } if (o1 < o2) { int mi = 0, ni = 0; while (mi < mSize && ni < nSize) { if (n[ni] > m[mi]) { mi++; } else if (n[ni] < m[mi]) { ni++; } else { int docId = n[ni]; if (isDistinct == false || IsDistinctValue(docId, alreadySeen)) { result.AddIntersection(docId); } ni++; mi++; } } } else { for (int i = 0; i < mSize; i++) { if (Array.BinarySearch(n, m[i]) >= 0) { int docId = m[i]; if (isDistinct == false || IsDistinctValue(docId, alreadySeen)) { result.AddIntersection(docId); } } } } return(result); }
/// <summary> /// This method expects both lists to be sorted /// </summary> private IntersectDocs GetIntersectedDocuments(ArraySegment<int> a, ArraySegment<int> b, HashSet<IndexSearcherHolder.StringCollectionValue> alreadySeen, bool needToApplyAggregation) { ArraySegment<int> n, m; if (a.Count > b.Count) { n = a; m = b; } else { n = b; m = a; } int nSize = n.Count; int mSize = m.Count; double o1 = nSize + mSize; double o2 = nSize * Math.Log(mSize, 2); var isDistinct = IndexQuery.IsDistinct; var result = new IntersectDocs(); if (needToApplyAggregation) { result.Documents = IntArraysPool.Instance.AllocateArray(); } if (o1 < o2) { int mi = m.Offset, ni = n.Offset; while (mi < mSize && ni < nSize) { var nVal = n.Array[ni]; var mVal = m.Array[mi]; if (nVal > mVal) { mi++; } else if (nVal < mVal) { ni++; } else { int docId = nVal; if (isDistinct == false || IsDistinctValue(docId, alreadySeen)) { result.AddIntersection(docId); } ni++; mi++; } } } else { for (int i = m.Offset; i < mSize; i++) { int docId = m.Array[i]; if (Array.BinarySearch(n.Array,n.Offset, n.Count, docId) >= 0) { if (isDistinct == false || IsDistinctValue(docId, alreadySeen)) { result.AddIntersection(docId); } } } } return result; }