00001 var SetCache = require('./_SetCache'),
00002 arrayIncludes = require('./_arrayIncludes'),
00003 arrayIncludesWith = require('./_arrayIncludesWith'),
00004 arrayMap = require('./_arrayMap'),
00005 baseUnary = require('./_baseUnary'),
00006 cacheHas = require('./_cacheHas');
00007
00008
00009 var nativeMin = Math.min;
00010
00021 function baseIntersection(arrays, iteratee, comparator) {
00022 var includes = comparator ? arrayIncludesWith : arrayIncludes,
00023 length = arrays[0].length,
00024 othLength = arrays.length,
00025 othIndex = othLength,
00026 caches = Array(othLength),
00027 maxLength = Infinity,
00028 result = [];
00029
00030 while (othIndex--) {
00031 var array = arrays[othIndex];
00032 if (othIndex && iteratee) {
00033 array = arrayMap(array, baseUnary(iteratee));
00034 }
00035 maxLength = nativeMin(array.length, maxLength);
00036 caches[othIndex] = !comparator && (iteratee || (length >= 120 && array.length >= 120))
00037 ? new SetCache(othIndex && array)
00038 : undefined;
00039 }
00040 array = arrays[0];
00041
00042 var index = -1,
00043 seen = caches[0];
00044
00045 outer:
00046 while (++index < length && result.length < maxLength) {
00047 var value = array[index],
00048 computed = iteratee ? iteratee(value) : value;
00049
00050 value = (comparator || value !== 0) ? value : 0;
00051 if (!(seen
00052 ? cacheHas(seen, computed)
00053 : includes(result, computed, comparator)
00054 )) {
00055 othIndex = othLength;
00056 while (--othIndex) {
00057 var cache = caches[othIndex];
00058 if (!(cache
00059 ? cacheHas(cache, computed)
00060 : includes(arrays[othIndex], computed, comparator))
00061 ) {
00062 continue outer;
00063 }
00064 }
00065 if (seen) {
00066 seen.push(computed);
00067 }
00068 result.push(value);
00069 }
00070 }
00071 return result;
00072 }
00073
00074 module.exports = baseIntersection;