| 1 | // LAF Base Library | 
|---|
| 2 | // Copyright (c) 2019 Igara Studio S.A. | 
|---|
| 3 | // | 
|---|
| 4 | // This file is released under the terms of the MIT license. | 
|---|
| 5 | // Read LICENSE.txt for more information. | 
|---|
| 6 |  | 
|---|
| 7 | #ifndef BASE_GCD_H_INCLUDED | 
|---|
| 8 | #define BASE_GCD_H_INCLUDED | 
|---|
| 9 | #pragma once | 
|---|
| 10 |  | 
|---|
| 11 | namespace base { | 
|---|
| 12 |  | 
|---|
| 13 | template<typename T> | 
|---|
| 14 | T gcd(T a, T b) { | 
|---|
| 15 | a = (a < 0 ? -a: a); | 
|---|
| 16 | b = (b < 0 ? -b: b); | 
|---|
| 17 | if (a > 1 && b > 1) { | 
|---|
| 18 | if (a == b) | 
|---|
| 19 | return a; | 
|---|
| 20 | else { | 
|---|
| 21 | while (a != b) { | 
|---|
| 22 | if (a > b) { | 
|---|
| 23 | a = a - b; | 
|---|
| 24 | } | 
|---|
| 25 | else if (a < b) { | 
|---|
| 26 | b = b - a; | 
|---|
| 27 | } | 
|---|
| 28 | if (a == b) { | 
|---|
| 29 | return a; | 
|---|
| 30 | } | 
|---|
| 31 | } | 
|---|
| 32 | } | 
|---|
| 33 | } | 
|---|
| 34 | return T(1); | 
|---|
| 35 | } | 
|---|
| 36 |  | 
|---|
| 37 | } // namespace base | 
|---|
| 38 |  | 
|---|
| 39 | #endif | 
|---|
| 40 |  | 
|---|