Combine two MIDD with same level of attribute at their roots

@param n1
@param n2
@return
768288
@SuppressWarnings({"rawtypes", "unchecked"})
    private static InternalNode<?> joinWithSameLevel(InternalNode n1,
                                                     InternalNode n2) throws MIDDException {
        if (n1.getID() != n2.getID()) {
            throw new IllegalArgumentException("Both params should have the same variable level at their root");
        }

        if (n1.getType() != n2.getType()) {
            throw new IllegalArgumentException("Both IDD roots should have the same data type");
        }

        Partition p1 = PartitionBuilder.createPartition(n1.getIntervals());
        Partition p2 = PartitionBuilder.createPartition(n2.getIntervals());

        // Union two partitions
        Partition<?> p = PartitionBuilder.union(p1, p2);

        if (p.size() == 0) {
            throw new RuntimeException("Empty partition");
        }

        // Clone n1 to n: should combine two internal node states of n1 & n2?
        InternalNode<?> newIDD = NodeUtils.createInternalNode(n1.getID(), n1.getState(), n1.getType());

        for (Interval<?> interval : p.getIntervals()) {
            AbstractNode op1 = n1.getChild(interval);
            AbstractNode op2 = n2.getChild(interval);
            AbstractNode child = null;
            if (op1 != null && op2 != null) {
                child = join(op1, op2);
            } else if (op1 != null || op2 != null) {
                child = GenericUtils.newInstance(op1 != null ? op1 : op2);
            } else {
                throw new RuntimeException("Error joining two partitions, the output partition is incorrect");
            }

            if (child != null) {
                AbstractEdge<?> edge = EdgeUtils.createEdge(interval, newIDD.getType());
                newIDD.addChild(edge, child);
            } else {
                throw new RuntimeException("Empty child");
            }
        }

        return newIDD;
    }
614054
private Node<K, V> combine(Node<K, V> l) {
        if (l == null) {
            return null;
        }

        assert l.o_s == null;

        // left-right pass
        Node<K, V> pairs = null;
        Node<K, V> it = l, p_it;
        if (comparator == null) { // no comparator
            while (it != null) {
                p_it = it;
                it = it.y_s;

                if (it == null) {
                    // append last node to pair list
                    p_it.y_s = pairs;
                    p_it.o_s = null;
                    pairs = p_it;
                } else {
                    Node<K, V> n_it = it.y_s;

                    // disconnect both
                    p_it.y_s = null;
                    p_it.o_s = null;
                    it.y_s = null;
                    it.o_s = null;

                    // link trees
                    p_it = link(p_it, it);

                    // append to pair list
                    p_it.y_s = pairs;
                    pairs = p_it;

                    // advance
                    it = n_it;
                }
            }
        } else {
            while (it != null) {
                p_it = it;
                it = it.y_s;

                if (it == null) {
                    // append last node to pair list
                    p_it.y_s = pairs;
                    p_it.o_s = null;
                    pairs = p_it;
                } else {
                    Node<K, V> n_it = it.y_s;

                    // disconnect both
                    p_it.y_s = null;
                    p_it.o_s = null;
                    it.y_s = null;
                    it.o_s = null;

                    // link trees
                    p_it = linkWithComparator(p_it, it);

                    // append to pair list
                    p_it.y_s = pairs;
                    pairs = p_it;

                    // advance
                    it = n_it;
                }
            }
        }

        // second pass (reverse order - due to add first)
        it = pairs;
        Node<K, V> f = null;
        if (comparator == null) {
            while (it != null) {
                Node<K, V> nextIt = it.y_s;
                it.y_s = null;
                f = link(f, it);
                it = nextIt;
            }
        } else {
            while (it != null) {
                Node<K, V> nextIt = it.y_s;
                it.y_s = null;
                f = linkWithComparator(f, it);
                it = nextIt;
            }
        }

        return f;
    }
795776
public static <U, P> FibonacciHeap<U, P> merge(FibonacciHeap<U, P> a, FibonacciHeap<U, P> b) {
        Preconditions.checkArgument(a.comparator().equals(b.comparator()),
                "Heaps that use different comparators can't be merged.");

        final FibonacciHeap<U, P> result = FibonacciHeap.create(a.comparator);
        result.oMinEntry = a.mergeLists(a.oMinEntry, b.oMinEntry);
        result.size = a.size + b.size;

        return result;
    }
62017
private MultiValueMap<String, String> union(MultiValueMap<String, String> map1, MultiValueMap<String, String> map2) {
		MultiValueMap<String, String> union = new LinkedMultiValueMap<String, String>(map1);
		Set<Entry<String, List<String>>> map2Entries = map2.entrySet();
		for (Iterator<Entry<String, List<String>>> entryIt = map2Entries.iterator(); entryIt.hasNext();) {
			Entry<String, List<String>> entry = entryIt.next();
			String key = entry.getKey();
			List<String> values = entry.getValue();
			for (String value : values) {
				union.add(key, value);
			}
		}
		return union;
	}
1073409
public double innerproduct(double[] vec1, double[] vec2) {
    double sum = 0;

    for (int i = 0; i < vec1.length; i++) {
      sum += vec1[i] * vec2[i];
    }

    return sum;
  }
498804
@Nonnull
    public static <E> Set<E> union(@Nullable final Set<E> pColl1, @Nullable final Set<E> pColl2)
    {
        final Set<E> result = new HashSet<>();
        if (pColl1 != null) {
            result.addAll(pColl1);
        }
        if (pColl2 != null) {
            result.addAll(pColl2);
        }
        return Collections.unmodifiableSet(result);
    }
194336
public static double getPvalue(int n11, int ndot1, int ndot2, int n1dot, int n2dot) {
        int originalN11=n11;

        int n=ndot1+ndot2;

        int min=Math.max(0,n1dot+ndot1-n);
        int max=Math.min(n1dot,ndot1);

        Set<Integer> combinations = new HashSet<>();
        combinations.add(originalN11);
        for(int r=min;r<=max;++r) {
            combinations.add(r);
        }

        double standardPart = Arithmetics.factorial(n1dot)*Arithmetics.factorial(n2dot)*Arithmetics.factorial(ndot1)*Arithmetics.factorial(ndot2)/Arithmetics.factorial(n);

        double originalPvalue=0.0;
        double Psum=0;
        for(Integer r : combinations) {
            n11=r;

            int n21=ndot1-n11;
            int n12=n1dot-n11;
            int n22=n2dot-n21;

            double pvalue=standardPart/(Arithmetics.factorial(n11)*Arithmetics.factorial(n12)*Arithmetics.factorial(n21)*Arithmetics.factorial(n22));

            if(r==originalN11) {
                originalPvalue=pvalue;
            }

            if(pvalue<=originalPvalue) {
                Psum+=pvalue;
            }
        }
        //combinations = null;    

        return Psum;
    }
801188
public static double dotProduct(Node[] n1, Node[] n2) {
        // logger.info("n1 " + n1.length);
        // logger.info("n2 " + n2.length);

        double sum = 0;
        int i = 0, j = 0;
        while (i < n1.length && j < n2.length) {
            if (n1[i] == null)
                break;
            // logger.error("n1" + n1[i] + " " + i);

            if (n2[j] == null)
                break;
            // logger.error("n2" + n2[j] + " " + j);

            // logger.info(n1[i].index + " == " + n2[j].index);
            if (n1[i].index == n2[j].index) {
                // logger.info(n1[i].index + " == " + n2[j].index);
                sum += n1[i].value * n2[j].value;
                ++i;
                ++j;
            } else {
                if (n1[i].index > n2[j].index) {
                    // logger.info(n1[i].index + " > " + n2[j].index);
                    ++j;
                } else {
                    // logger.info(n1[i].index + " <= " + n2[j].index);
                    ++i;
                }
            }
        }
        return sum;
    }
267538
@Nonnull
    public static <A, B> Pair<A, B> of(A left, B right) {
        return new Pair<>(left, right);
    }
1258951
public static <E> List<E> union(final List<? extends E> list1, final List<? extends E> list2) {
		final List<E> result = new ArrayList<E>(list1.size() + list2.size());
		result.addAll(list1);
		result.addAll(list2);
		return result;
	}
114667
public static <T, M, R> Processor<T, R> combine(
            Processor<T, M> processor1, Processor<M, R> processor2) {
        processor1.subscribe(processor2);
        return new Processor<>() {
            @Override
            public void subscribe(Flow.Subscriber<? super R> subscriber) {
                processor2.subscribe(subscriber);
            }

            @Override
            public void onSubscribe(Flow.Subscription subscription) {
                processor1.onSubscribe(subscription);
            }

            @Override
            public void onNext(T item) {processor1.onNext(item);}

            @Override
            public void onError(Throwable throwable) {
                processor1.onError(throwable);
            }

            @Override
            public void onComplete() {processor1.onComplete();}
        };
    }
739547
private static Collection<JobIdWithStatus> combine(
			Collection<JobIdWithStatus> first,
			Collection<JobIdWithStatus> second) {
		checkNotNull(first);
		checkNotNull(second);

		ArrayList<JobIdWithStatus> result = new ArrayList<>(first.size() + second.size());

		result.addAll(first);
		result.addAll(second);

		return result;
	}
1300337
public static boolean intInsnEqual(IntInsnNode node1, IntInsnNode node2)
	{
		if (node1.operand == -1 || node2.operand == -1)
			return true;

		return node1.operand == node2.operand;
	}
637101
private static Configuration merge(Configuration one, Configuration two) {
    if (one == null) {
      if (two == null) {
        return new Configuration();
      }
      return new Configuration(two);
    }

    Configuration c = new Configuration(one);

    if (two == null) {
      return c;
    }

    for (Map.Entry<String, String> entry : two) {
      c.set(entry.getKey(), entry.getValue());
    }

    return c;
  }
24325
static public Angle sum(Angle angle1, Angle angle2) {
        return new Angle(angle1.value + angle2.value);
    }
813222
public static MtasSpanSequenceItem merge(MtasSpanSequenceItem item1,
      MtasSpanSequenceItem item2, MtasSpanQuery ignoreQuery,
      Integer maximumIgnoreLength) {
    if (item1 == null || item2 == null) {
      return null;
    } else {
      MtasSpanQuery q1 = item1.getQuery();
      MtasSpanQuery q2 = item2.getQuery();
      boolean optional = item1.optional && item2.optional;
      // first spanRecurrenceQuery
      if (q1 instanceof MtasSpanRecurrenceQuery) {
        MtasSpanRecurrenceQuery rq1 = (MtasSpanRecurrenceQuery) q1;
        // both spanRecurrenceQuery
        if (q2 instanceof MtasSpanRecurrenceQuery) {
          MtasSpanRecurrenceQuery rq2 = (MtasSpanRecurrenceQuery) q2;
          // equal query
          if (rq1.getQuery().equals(rq2.getQuery())) {
            // equal ignoreQuery settings
            boolean checkCondition;
            checkCondition = ignoreQuery != null
                && rq1.getIgnoreQuery() != null;
            checkCondition = checkCondition
                ? ignoreQuery.equals(rq1.getIgnoreQuery()) : false;
            checkCondition = checkCondition
                ? maximumIgnoreLength.equals(rq1.getMaximumIgnoreLength())
                : false;
            checkCondition = checkCondition ? rq2.getIgnoreQuery() != null
                : false;
            checkCondition = checkCondition
                ? ignoreQuery.equals(rq2.getIgnoreQuery()) : false;
            checkCondition = checkCondition
                ? maximumIgnoreLength.equals(rq2.getMaximumIgnoreLength())
                : false;
            if (checkCondition) {
              // at least one optional
              if (item1.optional || item2.optional) {
                int minimum = Math.min(rq1.getMinimumRecurrence(),
                    rq2.getMinimumRecurrence());
                int maximum = rq1.getMaximumRecurrence()
                    + rq2.getMaximumRecurrence();
                // only if ranges match
                if ((rq1.getMaximumRecurrence() + 1) >= rq2
                    .getMinimumRecurrence()
                    && (rq2.getMaximumRecurrence() + 1) >= rq1
                        .getMinimumRecurrence()) {
                  return new MtasSpanSequenceItem(
                      new MtasSpanRecurrenceQuery(rq1.getQuery(), minimum,
                          maximum, ignoreQuery, maximumIgnoreLength),
                      optional);
                }
                // not optional
              } else {
                int minimum = rq1.getMinimumRecurrence()
                    + rq2.getMinimumRecurrence();
                int maximum = rq1.getMaximumRecurrence()
                    + rq2.getMaximumRecurrence();
                // only if ranges match
                if ((rq1.getMaximumRecurrence() + 1) >= rq2
                    .getMinimumRecurrence()
                    && (rq2.getMaximumRecurrence() + 1) >= rq1
                        .getMinimumRecurrence()) {
                  return new MtasSpanSequenceItem(
                      new MtasSpanRecurrenceQuery(rq1.getQuery(), minimum,
                          maximum, ignoreQuery, maximumIgnoreLength),
                      optional);
                }
              }
            }
          }
        } else {
          if (rq1.getQuery().equals(q2)) {
            boolean checkCondition;
            checkCondition = ignoreQuery != null;
            checkCondition &= rq1.getIgnoreQuery() != null;
            checkCondition &= ignoreQuery.equals(rq1.getIgnoreQuery());
            checkCondition &= rq1.getMaximumIgnoreLength() != null;
            checkCondition &= maximumIgnoreLength
                .equals(rq1.getMaximumIgnoreLength());
            if (checkCondition) {
              if (!optional) {
                if (item1.optional) {
                  if (rq1.getMinimumRecurrence() == 1) {
                    return new MtasSpanSequenceItem(new MtasSpanRecurrenceQuery(
                        q2, 1, rq1.getMaximumRecurrence() + 1, ignoreQuery,
                        maximumIgnoreLength), false);
                  }
                } else if (item2.optional) {
                  return new MtasSpanSequenceItem(new MtasSpanRecurrenceQuery(
                      q2, rq1.getMinimumRecurrence(),
                      rq1.getMaximumRecurrence() + 1, ignoreQuery,
                      maximumIgnoreLength), false);
                } else {
                  return new MtasSpanSequenceItem(new MtasSpanRecurrenceQuery(
                      q2, rq1.getMinimumRecurrence() + 1,
                      rq1.getMaximumRecurrence() + 1, ignoreQuery,
                      maximumIgnoreLength), false);
                }
              } else {
                if (rq1.getMinimumRecurrence() == 1) {
                  return new MtasSpanSequenceItem(new MtasSpanRecurrenceQuery(
                      q2, 1, rq1.getMaximumRecurrence() + 1, ignoreQuery,
                      maximumIgnoreLength), true);
                }
              }
            }
          }
        }
        // second spanRecurrenceQuery
      } else if (q2 instanceof MtasSpanRecurrenceQuery) {
        MtasSpanRecurrenceQuery rq2 = (MtasSpanRecurrenceQuery) q2;
        if (rq2.getQuery().equals(q1)) {
          boolean checkCondition;
          checkCondition = ignoreQuery != null;
          checkCondition &= rq2.getIgnoreQuery() != null;
          checkCondition &= ignoreQuery.equals(rq2.getIgnoreQuery());
          checkCondition &= maximumIgnoreLength
              .equals(rq2.getMaximumIgnoreLength());
          if (checkCondition) {
            if (!optional) {
              if (item1.optional) {
                return new MtasSpanSequenceItem(new MtasSpanRecurrenceQuery(q1,
                    rq2.getMinimumRecurrence(), rq2.getMaximumRecurrence() + 1,
                    ignoreQuery, maximumIgnoreLength), false);
              } else if (item2.optional) {
                if (rq2.getMinimumRecurrence() == 1) {
                  return new MtasSpanSequenceItem(new MtasSpanRecurrenceQuery(
                      q1, 1, rq2.getMaximumRecurrence() + 1, ignoreQuery,
                      maximumIgnoreLength), false);
                }
              } else {
                return new MtasSpanSequenceItem(new MtasSpanRecurrenceQuery(q1,
                    rq2.getMinimumRecurrence() + 1,
                    rq2.getMaximumRecurrence() + 1, ignoreQuery,
                    maximumIgnoreLength), false);
              }
            } else {
              if (rq2.getMinimumRecurrence() == 1) {
                return new MtasSpanSequenceItem(new MtasSpanRecurrenceQuery(q1,
                    1, rq2.getMaximumRecurrence() + 1, ignoreQuery,
                    maximumIgnoreLength), true);
              }
            }
          }
        }
        // both no spanRecurrenceQuery
      } else if (q1.equals(q2)) {
        // at least one optional
        if (item1.optional || item2.optional) {
          return new MtasSpanSequenceItem(new MtasSpanRecurrenceQuery(q1, 1, 2,
              ignoreQuery, maximumIgnoreLength), optional);
        } else {
          return new MtasSpanSequenceItem(new MtasSpanRecurrenceQuery(q1, 2, 2,
              ignoreQuery, maximumIgnoreLength), optional);
        }
      }
      return null;
    }
  }
1024695
public final Object put2d(Object key1, Object key2, Object value) {
        AssociativeArray tmp = internalData.get(key1);
        if(tmp == null) {
            internalData.put(key1, new AssociativeArray());
        }
        
        return internalData.get(key1).internalData.put(key2, value);
    }