IntersectWithLinPacked performs the liner intersection between two compressed uid lists.
(u, v *pb.UidPack)
| 33 | |
| 34 | // IntersectWithLinPacked performs the liner intersection between two compressed uid lists. |
| 35 | func IntersectWithLinPacked(u, v *pb.UidPack) *pb.UidPack { |
| 36 | if u == nil || v == nil { |
| 37 | return nil |
| 38 | } |
| 39 | |
| 40 | uDec := codec.NewDecoder(u) |
| 41 | uuids := uDec.Uids() |
| 42 | vDec := codec.NewDecoder(v) |
| 43 | vuids := vDec.Uids() |
| 44 | uIdx, vIdx := 0, 0 |
| 45 | result := codec.Encoder{BlockSize: int(u.BlockSize)} |
| 46 | |
| 47 | for { |
| 48 | // Break if the end of a list has been reached. |
| 49 | if len(uuids) == 0 || len(vuids) == 0 { |
| 50 | break |
| 51 | } |
| 52 | |
| 53 | // Load the next block of the encoded lists if necessary. |
| 54 | if uIdx == len(uuids) { |
| 55 | if uDec.Valid() { |
| 56 | uuids = uDec.Next() |
| 57 | uIdx = 0 |
| 58 | } else { |
| 59 | break |
| 60 | } |
| 61 | |
| 62 | } |
| 63 | if vIdx == len(vuids) { |
| 64 | if vDec.Valid() { |
| 65 | vuids = vDec.Next() |
| 66 | vIdx = 0 |
| 67 | } else { |
| 68 | break |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | uLen := len(uuids) |
| 73 | vLen := len(vuids) |
| 74 | |
| 75 | for uIdx < uLen && vIdx < vLen { |
| 76 | uid := uuids[uIdx] |
| 77 | vid := vuids[vIdx] |
| 78 | switch { |
| 79 | case uid > vid: |
| 80 | for vIdx = vIdx + 1; vIdx < vLen && vuids[vIdx] < uid; vIdx++ { |
| 81 | } |
| 82 | case uid == vid: |
| 83 | result.Add(uid) |
| 84 | vIdx++ |
| 85 | uIdx++ |
| 86 | default: |
| 87 | for uIdx = uIdx + 1; uIdx < uLen && uuids[uIdx] < vid; uIdx++ { |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | return result.Done() |