whatever.hpp
1 /* ************************************************************************** */ 2 /* */ 3 /* ::: :::::::: */ 4 /* whatever.hpp :+: :+: :+: */ 5 /* +:+ +:+ +:+ */ 6 /* By: gychoi <gychoi@student.42seoul.kr> +#+ +:+ +#+ */ 7 /* +#+#+#+#+#+ +#+ */ 8 /* Created: 2023/12/20 23:51:34 by gychoi #+# #+# */ 9 /* Updated: 2023/12/22 17:12:10 by gychoi ### ########.fr */ 10 /* */ 11 /* ************************************************************************** */ 12 13 #pragma once 14 #ifndef __WHATEVER_HPP__ 15 #define __WHATEVER_HPP__ 16 17 template <typename T> 18 void swap(T& a, T& b) 19 { 20 T temp = a; 21 22 a = b; 23 b = temp; 24 } 25 26 // for volatile type swap 27 template <typename T> 28 void swap(T volatile& a, T volatile& b) 29 { 30 T volatile temp = a; 31 32 a = b; 33 b = temp; 34 } 35 36 template <typename T> 37 T& min(T& a, T& b) 38 { 39 if (a < b) 40 { 41 return a; 42 } 43 else 44 { 45 return b; 46 } 47 } 48 49 template <typename T> 50 T& max(T& a, T& b) 51 { 52 if (a > b) 53 { 54 return a; 55 } 56 else 57 { 58 return b; 59 } 60 } 61 62 // for constant type comparision 63 template <typename T> 64 T const& min(T const& a, T const& b) 65 { 66 if (a < b) 67 { 68 return a; 69 } 70 else 71 { 72 return b; 73 } 74 } 75 76 // for constant type comparision 77 template <typename T> 78 T const& max(T const& a, T const& b) 79 { 80 if (a > b) 81 { 82 return a; 83 } 84 else 85 { 86 return b; 87 } 88 } 89 90 // for volatile type comparision 91 template <typename T> 92 T volatile& min(T volatile& a, T volatile& b) 93 { 94 if (a < b) 95 { 96 return a; 97 } 98 else 99 { 100 return b; 101 } 102 } 103 104 // for volatile type comparision 105 template <typename T> 106 T volatile& max(T volatile& a, T volatile& b) 107 { 108 if (a > b) 109 { 110 return a; 111 } 112 else 113 { 114 return b; 115 } 116 } 117 118 // for const volatile type comparision 119 template <typename T> 120 T const volatile& min(T const volatile& a, T const volatile& b) 121 { 122 if (a < b) 123 { 124 return a; 125 } 126 else 127 { 128 return b; 129 } 130 } 131 132 // for const volatile type comparision 133 template <typename T> 134 T const volatile& max(T const volatile& a, T const volatile& b) 135 { 136 if (a > b) 137 { 138 return a; 139 } 140 else 141 { 142 return b; 143 } 144 } 145 146 #endif /* __WHATEVER_HPP__ */