Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <resourcemodel/Fraction.hxx>
21 :
22 : namespace writerfilter {
23 : namespace resourcemodel {
24 :
25 : // Stein's binary GCD for non-negative integers
26 : // https://en.wikipedia.org/wiki/Binary_GCD_algorithm
27 0 : static sal_uInt32 gcd(sal_uInt32 a, sal_uInt32 b)
28 : {
29 0 : if (a == 0 || b == 0)
30 0 : return a | b;
31 :
32 0 : sal_uInt32 nShift = 0;
33 0 : while (((a | b) & 1) == 0)
34 : {
35 0 : a >>= 1;
36 0 : b >>= 1;
37 0 : ++nShift;
38 : }
39 :
40 0 : while ((a & 1) == 0)
41 0 : a >>= 1;
42 :
43 0 : do
44 : {
45 0 : while ((b & 1) == 0)
46 0 : b >>= 1;
47 :
48 0 : if (a < b)
49 : {
50 0 : b -= a;
51 : }
52 : else
53 : {
54 0 : sal_uInt32 nDiff = a - b;
55 0 : a = b;
56 0 : b = nDiff;
57 : }
58 :
59 0 : b >>= 1;
60 : }
61 : while (b != 0);
62 :
63 0 : return a << nShift;
64 : }
65 :
66 0 : static sal_uInt32 lcm(sal_Int32 a, sal_Int32 b)
67 : {
68 0 : return abs(a * b) / gcd(abs(a), abs(b));
69 : }
70 :
71 0 : Fraction::Fraction(sal_Int32 nNumerator, sal_Int32 nDenominator)
72 : {
73 0 : init(nNumerator, nDenominator);
74 0 : }
75 :
76 0 : Fraction::~Fraction()
77 : {
78 0 : }
79 :
80 0 : void Fraction::init(sal_Int32 nNumerator, sal_Int32 nDenominator)
81 : {
82 0 : sal_uInt32 nGCD = gcd(abs(nNumerator), abs(nDenominator));
83 :
84 0 : mnNumerator = nNumerator/ nGCD;
85 0 : mnDenominator = nDenominator / nGCD;
86 0 : }
87 :
88 0 : void Fraction::assign(const Fraction & rFraction)
89 : {
90 0 : init(rFraction.mnNumerator, rFraction.mnDenominator);
91 0 : }
92 :
93 0 : Fraction Fraction::inverse() const
94 : {
95 0 : return Fraction(mnDenominator, mnNumerator);
96 : }
97 :
98 0 : Fraction Fraction::operator + (const Fraction & rFraction) const
99 : {
100 0 : sal_uInt32 nLCM = lcm(mnDenominator, rFraction.mnDenominator);
101 :
102 0 : return Fraction(mnNumerator * nLCM / mnDenominator + rFraction.mnNumerator * nLCM / rFraction.mnDenominator, nLCM);
103 : }
104 :
105 0 : Fraction Fraction::operator - (const Fraction & rFraction) const
106 : {
107 0 : sal_uInt32 nLCM = lcm(mnDenominator, rFraction.mnDenominator);
108 :
109 0 : return Fraction(mnNumerator * nLCM / mnDenominator - rFraction.mnNumerator * nLCM / rFraction.mnDenominator, nLCM);
110 : }
111 :
112 0 : Fraction Fraction::operator * (const Fraction & rFraction) const
113 : {
114 0 : return Fraction(mnNumerator * rFraction.mnNumerator, mnDenominator * rFraction.mnDenominator);
115 : }
116 :
117 0 : Fraction Fraction::operator / (const Fraction & rFraction) const
118 : {
119 0 : return *this * rFraction.inverse();
120 : }
121 :
122 0 : Fraction Fraction::operator = (const Fraction & rFraction)
123 : {
124 0 : assign(rFraction);
125 :
126 0 : return *this;
127 : }
128 :
129 0 : Fraction::operator sal_Int32() const
130 : {
131 0 : return mnNumerator / mnDenominator;
132 : }
133 :
134 0 : Fraction::operator float() const
135 : {
136 0 : return static_cast<float>(mnNumerator) / static_cast<float>(mnDenominator);
137 : }
138 :
139 : }}
140 :
141 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|