Class: CTioga::Utils::NaturalDistance

Inherits:
Object
  • Object
show all
Defined in:
lib/CTioga/partition.rb

Overview

A class that handles a “natural distance”. Create it by giving the initial distance. You can then get its value, lower it, increase it, find the first/last element of an interval that is a multiple if it

Instance Method Summary collapse

Constructor Details

#initialize(distance) ⇒ NaturalDistance

Creates the biggest ‘natural distance’ that is smaller than distance



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/CTioga/partition.rb', line 85

def initialize(distance)
  @order = distance.log10.floor
  normalized = distance * 10**(-@order)
  @index = 0
  for dist in NaturalDistances
    if dist > normalized
      break
    else
      @index += 1
    end
  end
end

Instance Method Details

#decreaseObject

Goes to the natural distance immediately under the current one



104
105
106
107
108
109
110
111
# File 'lib/CTioga/partition.rb', line 104

def decrease
  if @index > 0
    @index -= 1
  else
    @index = NaturalDistances.size - 1
    @order -= 1
  end
end

#find_minimum(x1, x2) ⇒ Object

Find the minimum element inside the given interval that is a multiple of this distance



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/CTioga/partition.rb', line 125

def find_minimum(x1, x2)
  x1,x2 = x2, x1 if x1 > x2

  dist = value
  v = (x1/dist).ceil * dist
  if v <= x2
    return v
  else
    return false
  end
end

#increaseObject

Goes to the natural distance immediately under the current one



114
115
116
117
118
119
120
121
# File 'lib/CTioga/partition.rb', line 114

def increase
  if @index < NaturalDistances.size - 1
    @index += 1
  else
    @index = 0
    @order += 1
  end
end

#nb_to_next_decade(x) ⇒ Object

Returns the number of elements to #next_decade



152
153
154
155
156
# File 'lib/CTioga/partition.rb', line 152

def nb_to_next_decade(x)
  dist = value
  dec = next_decade(x)
  return dec/dist - (x/dist).ceil + 1
end

#next_decade(x) ⇒ Object

Returns the value of the next decade (ascending)



138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/CTioga/partition.rb', line 138

def next_decade(x)
  decade = 10.**(@order + 1)
  if @index == 0          # We stop at 0.5 if the increase is 0.5
    decade *= 0.5
  end
  nb = Float((x/decade).ceil)
  if nb == x/decade
    return (nb + 1)*decade
  else
    return nb * decade
  end
end

#to_next_decade(x) ⇒ Object

Returns a list of all the values corresponding to the distance



160
161
162
163
164
165
166
167
168
169
# File 'lib/CTioga/partition.rb', line 160

def to_next_decade(x)
  next_decade = next_decade(x)
  dist = value
  start = (x/dist).ceil * dist
  retval = []
  while start + retval.size * dist <= next_decade
    retval << start + retval.size * dist
  end
  return retval
end

#valueObject

Returns the actual value of the distance



99
100
101
# File 'lib/CTioga/partition.rb', line 99

def value
  return NaturalDistances[@index] * 10**(@order)
end