1*b4a4f18cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*b4a4f18cSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*b4a4f18cSAndrew Rist * or more contributor license agreements. See the NOTICE file
5*b4a4f18cSAndrew Rist * distributed with this work for additional information
6*b4a4f18cSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*b4a4f18cSAndrew Rist * to you under the Apache License, Version 2.0 (the
8*b4a4f18cSAndrew Rist * "License"); you may not use this file except in compliance
9*b4a4f18cSAndrew Rist * with the License. You may obtain a copy of the License at
10*b4a4f18cSAndrew Rist *
11*b4a4f18cSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*b4a4f18cSAndrew Rist *
13*b4a4f18cSAndrew Rist * Unless required by applicable law or agreed to in writing,
14*b4a4f18cSAndrew Rist * software distributed under the License is distributed on an
15*b4a4f18cSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b4a4f18cSAndrew Rist * KIND, either express or implied. See the License for the
17*b4a4f18cSAndrew Rist * specific language governing permissions and limitations
18*b4a4f18cSAndrew Rist * under the License.
19*b4a4f18cSAndrew Rist *
20*b4a4f18cSAndrew Rist *************************************************************/
21*b4a4f18cSAndrew Rist
22*b4a4f18cSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir #include <resourcemodel/Fraction.hxx>
25cdf0e10cSrcweir
26cdf0e10cSrcweir namespace writerfilter {
27cdf0e10cSrcweir namespace resourcemodel {
28cdf0e10cSrcweir
gcd(sal_uInt32 a,sal_uInt32 b)29cdf0e10cSrcweir sal_uInt32 gcd(sal_uInt32 a, sal_uInt32 b)
30cdf0e10cSrcweir {
31cdf0e10cSrcweir if (a == 0 || b == 0)
32cdf0e10cSrcweir return a | b;
33cdf0e10cSrcweir
34cdf0e10cSrcweir sal_uInt32 nShift = 0;
35cdf0e10cSrcweir while (((a | b) & 1) == 0)
36cdf0e10cSrcweir {
37cdf0e10cSrcweir a >>= 1;
38cdf0e10cSrcweir b >>= 1;
39cdf0e10cSrcweir ++nShift;
40cdf0e10cSrcweir }
41cdf0e10cSrcweir
42cdf0e10cSrcweir while ((a & 1) == 0)
43cdf0e10cSrcweir a >>= 1;
44cdf0e10cSrcweir
45cdf0e10cSrcweir do
46cdf0e10cSrcweir {
47cdf0e10cSrcweir while ((b & 1) == 0)
48cdf0e10cSrcweir b >>= 1;
49cdf0e10cSrcweir
50cdf0e10cSrcweir if (a < b)
51cdf0e10cSrcweir {
52cdf0e10cSrcweir b -= a;
53cdf0e10cSrcweir }
54cdf0e10cSrcweir else
55cdf0e10cSrcweir {
56cdf0e10cSrcweir sal_uInt32 nDiff = a - b;
57cdf0e10cSrcweir a = b;
58cdf0e10cSrcweir b = nDiff;
59cdf0e10cSrcweir }
60cdf0e10cSrcweir
61cdf0e10cSrcweir b >>= 1;
62cdf0e10cSrcweir }
63cdf0e10cSrcweir while (b != 0);
64cdf0e10cSrcweir
65cdf0e10cSrcweir return a << nShift;
66cdf0e10cSrcweir }
67cdf0e10cSrcweir
lcm(sal_Int32 a,sal_Int32 b)68cdf0e10cSrcweir sal_uInt32 lcm(sal_Int32 a, sal_Int32 b)
69cdf0e10cSrcweir {
70cdf0e10cSrcweir return abs(a * b) / gcd(abs(a), abs(b));
71cdf0e10cSrcweir }
72cdf0e10cSrcweir
Fraction(sal_Int32 nNumerator,sal_Int32 nDenominator)73cdf0e10cSrcweir Fraction::Fraction(sal_Int32 nNumerator, sal_Int32 nDenominator)
74cdf0e10cSrcweir {
75cdf0e10cSrcweir init(nNumerator, nDenominator);
76cdf0e10cSrcweir }
77cdf0e10cSrcweir
Fraction(const Fraction & a,const Fraction & b)78cdf0e10cSrcweir Fraction::Fraction(const Fraction & a, const Fraction & b)
79cdf0e10cSrcweir {
80cdf0e10cSrcweir init(a.mnNumerator * b.mnDenominator, a.mnDenominator * b.mnNumerator);
81cdf0e10cSrcweir }
82cdf0e10cSrcweir
~Fraction()83cdf0e10cSrcweir Fraction::~Fraction()
84cdf0e10cSrcweir {
85cdf0e10cSrcweir }
86cdf0e10cSrcweir
init(sal_Int32 nNumerator,sal_Int32 nDenominator)87cdf0e10cSrcweir void Fraction::init(sal_Int32 nNumerator, sal_Int32 nDenominator)
88cdf0e10cSrcweir {
89cdf0e10cSrcweir sal_uInt32 nGCD = gcd(nNumerator, nDenominator);
90cdf0e10cSrcweir
91cdf0e10cSrcweir mnNumerator = nNumerator/ nGCD;
92cdf0e10cSrcweir mnDenominator = nDenominator / nGCD;
93cdf0e10cSrcweir }
94cdf0e10cSrcweir
assign(const Fraction & rFraction)95cdf0e10cSrcweir void Fraction::assign(const Fraction & rFraction)
96cdf0e10cSrcweir {
97cdf0e10cSrcweir init(rFraction.mnNumerator, rFraction.mnDenominator);
98cdf0e10cSrcweir }
99cdf0e10cSrcweir
inverse() const100cdf0e10cSrcweir Fraction Fraction::inverse() const
101cdf0e10cSrcweir {
102cdf0e10cSrcweir return Fraction(mnDenominator, mnNumerator);
103cdf0e10cSrcweir }
104cdf0e10cSrcweir
operator +(const Fraction & rFraction) const105cdf0e10cSrcweir Fraction Fraction::operator + (const Fraction & rFraction) const
106cdf0e10cSrcweir {
107cdf0e10cSrcweir sal_uInt32 nLCM = lcm(mnDenominator, rFraction.mnDenominator);
108cdf0e10cSrcweir
109cdf0e10cSrcweir return Fraction(mnNumerator * nLCM / mnDenominator + rFraction.mnNumerator * nLCM / rFraction.mnDenominator, nLCM);
110cdf0e10cSrcweir }
111cdf0e10cSrcweir
operator -(const Fraction & rFraction) const112cdf0e10cSrcweir Fraction Fraction::operator - (const Fraction & rFraction) const
113cdf0e10cSrcweir {
114cdf0e10cSrcweir sal_uInt32 nLCM = lcm(mnDenominator, rFraction.mnDenominator);
115cdf0e10cSrcweir
116cdf0e10cSrcweir return Fraction(mnNumerator * nLCM / mnDenominator - rFraction.mnNumerator * nLCM / rFraction.mnDenominator, nLCM);
117cdf0e10cSrcweir }
118cdf0e10cSrcweir
operator *(const Fraction & rFraction) const119cdf0e10cSrcweir Fraction Fraction::operator * (const Fraction & rFraction) const
120cdf0e10cSrcweir {
121cdf0e10cSrcweir return Fraction(mnNumerator * rFraction.mnNumerator, mnDenominator * rFraction.mnDenominator);
122cdf0e10cSrcweir }
123cdf0e10cSrcweir
operator /(const Fraction & rFraction) const124cdf0e10cSrcweir Fraction Fraction::operator / (const Fraction & rFraction) const
125cdf0e10cSrcweir {
126cdf0e10cSrcweir return *this * rFraction.inverse();
127cdf0e10cSrcweir }
128cdf0e10cSrcweir
operator =(const Fraction & rFraction)129cdf0e10cSrcweir Fraction Fraction::operator = (const Fraction & rFraction)
130cdf0e10cSrcweir {
131cdf0e10cSrcweir assign(rFraction);
132cdf0e10cSrcweir
133cdf0e10cSrcweir return *this;
134cdf0e10cSrcweir }
135cdf0e10cSrcweir
operator sal_Int32() const136cdf0e10cSrcweir Fraction::operator sal_Int32() const
137cdf0e10cSrcweir {
138cdf0e10cSrcweir return mnNumerator / mnDenominator;
139cdf0e10cSrcweir }
140cdf0e10cSrcweir
141cdf0e10cSrcweir Fraction::operator float() const
142cdf0e10cSrcweir {
143cdf0e10cSrcweir return static_cast<float>(mnNumerator) / static_cast<float>(mnDenominator);
144cdf0e10cSrcweir }
145cdf0e10cSrcweir
146cdf0e10cSrcweir }}
147