File: percentiles.rb

package info (click to toggle)
ruby-mongo 2.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,020 kB
  • sloc: ruby: 110,810; makefile: 5
file content (31 lines) | stat: -rw-r--r-- 891 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# frozen_string_literal: true

module Mongo
  module DriverBench
    # A utility class for returning the list item at a given percentile
    # value.
    class Percentiles
      # @return [ Array<Number> ] the sorted list of numbers to consider
      attr_reader :list

      # Create a new Percentiles object that encapsulates the given list of
      # numbers.
      #
      # @param [ Array<Number> ] list the list of numbers to considier
      def initialize(list)
        @list = list.sort
      end

      # Finds and returns the element in the list that represents the given
      # percentile value.
      #
      # @param [ Number ] percentile a number in the range [1,100]
      #
      # @return [ Number ] the element of the list for the given percentile.
      def [](percentile)
        i = (list.size * percentile / 100.0).ceil - 1
        list[i]
      end
    end
  end
end