Method: LEON.type_gcd

Defined in:
lib/leon.rb

.type_gcd(arr) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/leon.rb', line 67

def self.type_gcd(arr)
  type = LEON::type_check(arr[0])
  if type === Constants::BOOLEAN + 1
    type = Constants::BOOLEAN
  end
  case type
    when Constants::ARRAY
      return [ self.type_gcd(arr[0]) ]
    when Constants::OBJECT
      ret = Hash.new
      arr[0].each { |k, v|
        ret[k] = self.type_gcd(self.pluck(arr, k))
      }
      return ret
    when Constants::NATIVE_OBJECT
      ret = Hash.new
      vars = arr[0].instance_variables.map { |v|
        v.to_sym.sub("@", "")
      }
      vars.each { |v|
        ret[v] = self.type_gcd(self.pluck(arr, v))
      }
      return ret
    when Constants::UNSIGNED_CHAR, Constants::CHAR, Constants::UNSIGNED_SHORT, Constants::SHORT, Constants::UNSIGNED_INT, Constants::INT, Constants::FLOAT, Constants::DOUBLE
      highestMagnitude = arr[0].abs
      if arr[0] < 0
        sign = 1
      else
        sign = 0
      end
      if type === Constants::FLOAT or type === Constants::DOUBLE
        fp = 1
      else
        fp = 0
      end
      for i in 1...arr.length
        type = LEON::type_check(arr[i])
        if not arr[i].kind_of? Float and not arr[i].kind_of? Fixnum
          raise "Expected a numerical value but got #{self.type_to_str(type)}."
        end
        if arr[i].abs > highestMagnitude
          highestMagnitude = arr[1]
        end
        if arr[i].ceil != arr[i]
          fp = 1
        else
          fp = 0
        end
      end
      return self.type_check((fp ? Float((sign ? -highestMagnitude : highestMagnitude )) : (sign ? -highestMagnitude : highestMagnitude )))
    else
      for i in 1...(arr.length)
        thisType = self.type_check(arr[i])
        if thisType === Constants::BOOLEAN + 1
          thisType = Constants::BOOLEAN
        end
        if type != thisType
          raise "Was expecting a #{self.type_to_str(type)} but got a #{self.type_to_str(thisType)}."
        end
      end
      return type
  end
  return type
end