/*
(non-Javadoc)

@see
mtas.codec.util.DataCollector.MtasDataCollector#minimumForComputingSegment(
java.lang.Number, java.lang.Number)
194805
@Override
  protected Long lastForComputingSegment(Long value, Long boundary)
      throws IOException {
    if (segmentRegistration.equals(SEGMENT_SORT_ASC)
        || segmentRegistration.equals(SEGMENT_BOUNDARY_ASC)) {
      return Math.max(value, boundary);
    } else if (segmentRegistration.equals(SEGMENT_SORT_DESC)
        || segmentRegistration.equals(SEGMENT_BOUNDARY_DESC)) {
      return Math.min(value, boundary);
    } else {
      throw new IOException(
          "can't compute last for segmentRegistration " + segmentRegistration);
    }
  }
637115
public void ensureCapacity(int minCapacity) {
        int minExpand = (elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA)
            // any size if not default element table
            ? 0
            // larger than default for default empty table. It's already
            // supposed to be at default size.
            : DEFAULT_CAPACITY;

        if (minCapacity > minExpand) {
            ensureExplicitCapacity(minCapacity);
        }
    }
1189584
int numOpenSegments() {
        int numOpen = 0;
        for (PBDSegment segment : m_segments.values()) {
            if (!segment.isClosed()) {
                numOpen++;
            }
        }

        return numOpen;
    }
382899
public static int min(int[][] matrix) {
    int min = Integer.MAX_VALUE;
    for (int[] row : matrix) {
      for (int elem : row) {
        min = Math.min(min, elem);
      }
    }
    return min;
  }
343566
public int getMinimumDigits() {
        String value = Optional.fromNullable(getParameter(SignalParameters.MINIMUM_NUM_DIGITS.symbol())).or("1");
        return Integer.parseInt(value);
    }
674893
public static List<ByteBuffer> minComponents(List<ByteBuffer> minSeen, Composite candidate, CellNameType comparator)
    {
        // For a cell name, no reason to look more than the clustering prefix
        // (and comparing the collection element would actually crash)
        int size = Math.min(candidate.size(), comparator.clusteringPrefixSize());

        if (minSeen.isEmpty())
            return getComponents(candidate, size);

        // In most case maxSeen is big enough to hold the result so update it in place in those cases
        minSeen = maybeGrow(minSeen, size);

        for (int i = 0; i < size; i++)
            minSeen.set(i, min(minSeen.get(i), candidate.get(i), comparator.subtype(i)));

        return minSeen;
    }
727496
private void computeFullStartPositionMinimum(int position)
      throws IOException {
    if (ignoreSpans != null && !minFullStartPosition.containsKey(position)) {
      HashSet<Integer> list = baseStartPositionList.get(position);
      HashSet<Integer> newList = new HashSet<>();
      int minimumStartPosition = position;
      while (list != null && !list.isEmpty()) {
        newList.clear();
        for (int startPosition : list) {
          if (minFullStartPosition.containsKey(startPosition)) {
            minimumStartPosition = Math.min(minimumStartPosition,
                minFullStartPosition.get(startPosition));
          } else if (baseStartPositionList.containsKey(startPosition)) {
            newList.addAll(baseStartPositionList.get(startPosition));
          } else {
            if (startPosition < minimumStartPosition) {
              minimumStartPosition = startPosition;
            }
          }
        }
        list.clear();
        list.addAll(newList);
      }
      minFullStartPosition.put(position, minimumStartPosition);
    }
  }
654691
@Override
  protected double computeCost(
      final DataSegment proposalSegment,
      final ServerHolder server,
      final boolean includeCurrentServer
  )
  {
    double cost = super.computeCost(proposalSegment, server, includeCurrentServer);

    if (cost == Double.POSITIVE_INFINITY) {
      return cost;
    }

    int nSegments = 1;
    if (server.getServer().getLazyAllSegments().size() > 0) {
      nSegments = server.getServer().getLazyAllSegments().size();
    }

    double normalizedCost = cost / nSegments;
    double usageRatio = (double) server.getSizeUsed() / (double) server.getServer().getMaxSize();

    return normalizedCost * usageRatio;
  }
29175
public static <N extends Number> Number min(List<N> numberList) {
        return cal(numberList, DoubleStream::min);
    }
962919
public static void checkMinimumSdkLevel(int minimumSdkLevel) {
    Preconditions.checkArgument(
        isMinimumSdkLevel(minimumSdkLevel),
        "running on Android SDK level %s but requires minimum %s",
        Build.VERSION.SDK_INT,
        minimumSdkLevel);
  }
554546
public Pair<T> min() {
        int minCount = Integer.MAX_VALUE;
        TLongIntIterator iter = counts.iterator();
        long minIndex = 0;

        while (!iter.hasNext()) {
            iter.advance();

            int count = iter.value();
            if (count < minCount) {
                minIndex = iter.key();
                minCount = count;
            }
        }
        if (minCount == Integer.MAX_VALUE)
            return null;
        
        int i = (int)(minIndex >>> 32);
        int j = (int)(minIndex & 0xfffffff);
        return new Pair<T>(elementIndices.lookup(i), 
                           elementIndices.lookup(j));
    }
1035253
public static void minMax(Integer min, Integer max) {
		if(!validation) return;
		Validate.notNull(min);
		Validate.notNull(max);
		if(min > max)
			throw new ParameterException(ErrorCode.RANGEVIOLATION, "Minumum is bigger than maximum.");
	}
720535
private SegmentScoreEntry computeSegmentScore(String segment) {
		if(this.stopList.contains(segment) )
			return SegmentScoreEntry.SCORE_ZERO;
		CompostIndexEntry closestEntry = compostIndex.getEntry(segment);
		double indexSimilarity = 0.0;
		
		if(closestEntry == null) {
			if(this.opt.getSegmentSimilarityThreshold() == 1)
				// do not compare similarity of this segment to the index
				return SegmentScoreEntry.SCORE_ZERO;
					
			// Find an entry by similarity
			Iterator<CompostIndexEntry> it = compostIndex.closedEntryCandidateIterator(segment);
			int entryLength = segment.length();
			double dist = 0;
			CompostIndexEntry entry;
			while(it.hasNext()) {
				entry = it.next();
				dist = distance.computeNormalized(segment, entry.getText());
				if(Math.abs(entry.getText().length() - entryLength) <= 3 
						&& dist >= this.opt.getSegmentSimilarityThreshold()) {
					indexSimilarity = dist;
					closestEntry = entry;
				}
			}
			if(closestEntry == null) {
				// could not find any close entry in the compost index
				return SegmentScoreEntry.SCORE_ZERO;
			}
		} else {
			indexSimilarity = 1f;
		}
		int inCorpus = 0;
		int inDico = closestEntry.isInDico() || closestEntry.isInNeoClassicalPrefix() ? 1 : 0;

		// retrieves all sw terms that have the same lemma
		Collection<Term> corpusTerms = swtLemmaIndex.getTerms(segment);
		double wr = 0f;
		for(Iterator<Term> it = corpusTerms.iterator(); it.hasNext();)
			wr+=Math.pow(10, it.next().getSpecificity());
		
		double dataCorpus;
		if(closestEntry.isInCorpus() && !corpusTerms.isEmpty()) {
			dataCorpus = wr / getMaxSpec();
			inCorpus = 1;
		} else {
			dataCorpus = 0;
			inCorpus = closestEntry.isInNeoClassicalPrefix() ? 1 : 0;
		}
		double score = this.opt.getAlpha() * indexSimilarity + this.opt.getBeta() * inDico + this.opt.getGamma() * inCorpus + this.opt.getDelta() * dataCorpus;
		if(logger.isTraceEnabled()) {
			logger.trace("Score for {} is {} [alpha: {} beta: {} gamma: {} delta: {}]", 
					segment, 
					score,
					indexSimilarity,
					inDico,
					inCorpus,
					dataCorpus);
		}
		return new SegmentScoreEntry(segment, findSegmentLemma(segment), score, closestEntry);
	}
30839
private SegmentMeta findSegmentMeta(int size)
  {
    for (SegmentMeta segmentMeta : this._segmentMeta) {
      if (segmentMeta.size() == size) {
        return segmentMeta;
      }
    }
    
    throw new IllegalStateException(L.l("{0} is an invalid segment size", size));
  }
1320185
private static <T extends Comparable<T>> void ensureValidRange(T min, T max) {
    if (min == null) {
      throw new InvalidOperationException("Null value for minimum.");
    }
    if (max == null) {
      throw new InvalidOperationException("Null value for maximum.");
    }
    if (min.compareTo(max) >= 0) {
      throw new InvalidOperationException("Invalid range: " + min + " >= " + max);
    }
  }
1126149
public SDVariable unsortedSegmentMax(SDVariable data, SDVariable segmentIds, int numSegments) {
        return unsortedSegmentMax(null, data, segmentIds, numSegments);
    }
880873
public static double estimateSelectivity(
      final Iterator<ImmutableBitmap> bitmaps,
      final long totalNumRows
  )
  {
    long numMatchedRows = 0;
    while (bitmaps.hasNext()) {
      final ImmutableBitmap bitmap = bitmaps.next();
      numMatchedRows += bitmap.size();
    }

    return Math.min(1., (double) numMatchedRows / totalNumRows);
  }