Flutter Impeller
impeller::Rational Class Reference

#include <rational.h>

Public Member Functions

constexpr Rational (int32_t num)
 
constexpr Rational (int32_t num, uint32_t den)
 
int32_t GetNumerator () const
 
uint32_t GetDenominator () const
 
bool operator== (const Rational &that) const
 
bool operator!= (const Rational &that) const
 
bool operator< (const Rational &that) const
 
uint64_t GetHash () const
 
 operator Scalar () const
 
Rational Invert () const
 

Detailed Description

Definition at line 13 of file rational.h.

Constructor & Destructor Documentation

◆ Rational() [1/2]

constexpr impeller::Rational::Rational ( int32_t  num)
inlineexplicitconstexpr

Definition at line 15 of file rational.h.

15 : num_(num), den_(1) {}

Referenced by Invert().

◆ Rational() [2/2]

constexpr impeller::Rational::Rational ( int32_t  num,
uint32_t  den 
)
inlineconstexpr

Definition at line 17 of file rational.h.

17 : num_(num), den_(den) {}

Member Function Documentation

◆ GetDenominator()

uint32_t impeller::Rational::GetDenominator ( ) const
inline

Definition at line 21 of file rational.h.

21 { return den_; }

Referenced by impeller::TextFrame::RoundScaledFontSize().

◆ GetHash()

uint64_t impeller::Rational::GetHash ( ) const

Definition at line 42 of file rational.cc.

42  {
43  if (num_ == 0) {
44  return 0;
45  }
46  uint64_t gcd = std::gcd(num_, den_);
47  return ((num_ / gcd) << 32) | (den_ / gcd);
48 }

Referenced by impeller::TextShadowCache::TextShadowCacheKey::Hash::operator()().

◆ GetNumerator()

int32_t impeller::Rational::GetNumerator ( ) const
inline

Definition at line 19 of file rational.h.

19 { return num_; }

Referenced by impeller::TextFrame::RoundScaledFontSize().

◆ Invert()

Rational impeller::Rational::Invert ( ) const

Definition at line 50 of file rational.cc.

50  {
51  if (num_ >= 0) {
52  return Rational(den_, num_);
53  } else {
54  return Rational(-1 * static_cast<int32_t>(den_), std::abs(num_));
55  }
56 }
constexpr Rational(int32_t num)
Definition: rational.h:15

References Rational().

Referenced by impeller::TextContents::ComputeVertexData().

◆ operator Scalar()

impeller::Rational::operator Scalar ( ) const
inlineexplicit

Definition at line 31 of file rational.h.

31 { return static_cast<float>(num_) / den_; }

◆ operator!=()

bool impeller::Rational::operator!= ( const Rational that) const

Definition at line 28 of file rational.cc.

28  {
29  return !(*this == that);
30 }

◆ operator<()

bool impeller::Rational::operator< ( const Rational that) const

Definition at line 32 of file rational.cc.

32  {
33  if (den_ == that.den_) {
34  return num_ < that.num_;
35  } else if ((num_ >= 0) != (that.num_ >= 0)) {
36  return num_ < that.num_;
37  } else {
38  return AbsToUnsigned(num_) * that.den_ < AbsToUnsigned(that.num_) * den_;
39  }
40 }

◆ operator==()

bool impeller::Rational::operator== ( const Rational that) const

Definition at line 18 of file rational.cc.

18  {
19  if (den_ == that.den_) {
20  return num_ == that.num_;
21  } else if ((num_ >= 0) != (that.num_ >= 0)) {
22  return false;
23  } else {
24  return AbsToUnsigned(num_) * that.den_ == AbsToUnsigned(that.num_) * den_;
25  }
26 }

The documentation for this class was generated from the following files: