diff --git a/Makefile b/Makefile index 207db3f..bf85073 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,7 @@ CXXFLAGS = \ ifneq (, $(findstring -g, $(CXXFLAGS))) STRIP = true else - STRIP = strip + STRIP = true #strip endif ENGINE_CXXFLAGS = \ diff --git a/game/menu.cpp b/game/menu.cpp index 74eedde..f3b0460 100644 --- a/game/menu.cpp +++ b/game/menu.cpp @@ -305,7 +305,7 @@ static void menuOptions() { int findResolution = -1; for (auto &it : kResolutionPairs) { for (auto &jt : it) { - if (jt.first() == width.get() && jt.second() == height.get()) { + if (jt.first == width.get() && jt.second == height.get()) { findRatio = &it - kResolutionPairs.begin(); findResolution = &jt - it.begin(); break; @@ -322,8 +322,8 @@ static void menuOptions() { D(resolution) = gui::selector(nullptr, D(resolution), kResolutionStrings[D(ratio)]); auto resolution = kResolutionPairs[D(ratio)][D(resolution)]; - width.set(resolution.first()); - height.set(resolution.second()); + width.set(resolution.first); + height.set(resolution.second); gui::dedent(); } diff --git a/model.cpp b/model.cpp index eed8a41..30d439f 100644 --- a/model.cpp +++ b/model.cpp @@ -356,8 +356,8 @@ struct inData { } void operator=(const u::pair &data) { - isHalf = data.first() == iqm::kHalf; - asOpaque = data.second(); + isHalf = data.first == iqm::kHalf; + asOpaque = data.second; } void endianSwap(size_t count) { diff --git a/texture.cpp b/texture.cpp index d53e80e..2bf466a 100644 --- a/texture.cpp +++ b/texture.cpp @@ -200,19 +200,21 @@ struct jpeg : decoder { static constexpr int kW7 = 565; // fast integer discrete cosine transform + #define sls(X, Y) (int)((unsigned int)(X) << (Y)) + void rowIDCT(int* blk) { int x0, x1, x2, x3, x4, x5, x6, x7, x8; - if (!((x1 = u::sls(blk[4], 11)) + if (!((x1 = sls(blk[4], 11)) | (x2 = blk[6]) | (x3 = blk[2]) | (x4 = blk[1]) | (x5 = blk[7]) | (x6 = blk[5]) | (x7 = blk[3]))) { - int value = u::sls(blk[0], 3); + int value = sls(blk[0], 3); for (size_t i = 0; i < 8; i++) blk[i] = value; return; } - x0 = u::sls(blk[0], 11) + 128; + x0 = sls(blk[0], 11) + 128; x8 = kW7 * (x4 + x5); x4 = x8 + (kW1 - kW7) * x4; x5 = x8 - (kW1 + kW7) * x5; @@ -246,7 +248,7 @@ struct jpeg : decoder { void columnIDCT(const int* blk, unsigned char *out, int stride) { int x0, x1, x2, x3, x4, x5, x6, x7, x8; - if (!((x1 = u::sls(blk[8*4], 8)) + if (!((x1 = sls(blk[8*4], 8)) | (x2 = blk[8*6]) | (x3 = blk[8*2]) | (x4 = blk[8*1]) | (x5 = blk[8*7]) | (x6 = blk[8*5]) | (x7 = blk[8*3]))) @@ -258,7 +260,7 @@ struct jpeg : decoder { } return; } - x0 = u::sls(blk[0], 8) + 8192; + x0 = sls(blk[0], 8) + 8192; x8 = kW7 * (x4 + x5) + 4; x4 = (x8 + (kW1 - kW7) * x4) >> 3; x5 = (x8 - (kW1 + kW7) * x5) >> 3; @@ -296,14 +298,14 @@ struct jpeg : decoder { return 0; while (m_bufbits < bits) { if (m_size <= 0) { - m_buf = u::sls(m_buf, 8) | 0xFF; + m_buf = sls(m_buf, 8) | 0xFF; m_bufbits += 8; continue; } newbyte = *m_position++; m_size--; m_bufbits += 8; - m_buf = u::sls(m_buf, 8) | newbyte; + m_buf = sls(m_buf, 8) | newbyte; if (newbyte == 0xFF) { if (m_size) { unsigned char marker = *m_position++; @@ -318,7 +320,7 @@ struct jpeg : decoder { if ((marker & 0xF8) != 0xD0) m_error = kMalformatted; else { - m_buf = u::sls(m_buf, 8) | marker; + m_buf = sls(m_buf, 8) | marker; m_bufbits += 8; } } @@ -523,7 +525,7 @@ struct jpeg : decoder { if (!(bits = value & 15)) return 0; if ((value = getBits(bits)) < (1 << (bits - 1))) - value += u::sls(-1, bits) + 1; + value += sls(-1, bits) + 1; return value; } diff --git a/u_map.h b/u_map.h index 674e8db..caba4b1 100644 --- a/u_map.h +++ b/u_map.h @@ -35,10 +35,14 @@ class map { const_iterator find(const K &key) const; iterator find(const K &key); + pair insert(const pair &p); + pair insert(pair &&p); + void erase(const_iterator where); V &operator[](const K &key); + V &operator[](K &&key); void swap(map &other); @@ -147,8 +151,13 @@ inline typename map::const_iterator map::find(const K &key) const { template inline pair::iterator, bool> map::insert(const pair &p) { - auto result = make_pair(find(p.first()), false); - if (result.first().node != 0) + return insert(u::move(pair(p))); +} + +template +inline pair::iterator, bool> map::insert(pair &&p) { + auto result = make_pair(find(p.first), false); + if (result.first.node != 0) return result; size_t nbuckets = (m_base.buckets.last - m_base.buckets.first); @@ -158,29 +167,34 @@ inline pair::iterator, bool> map::insert(const pair::iterator &it = result.first(); + const size_t hh = hash(p.first) & (nbuckets - 2); + typename map::iterator &it = result.first; it.node = detail::hash_insert_new(m_base, hh); it.node->first.~hash_elem(); - new (&it.node->first) hash_elem(p.first(), p.second()); + new (&it.node->first) hash_elem(u::move(p.first), u::move(p.second)); - result.second() = true; - return result; + result.second = true; + return u::move(result); } template -void map::erase(const_iterator where) { +inline void map::erase(const_iterator where) { detail::hash_erase(m_base, where.node); } template -V &map::operator[](const K &key) { - return insert(make_pair(key, V())).first()->second; +inline V &map::operator[](const K &key) { + return insert(u::move(make_pair(key, V()))).first->second; +} + +template +inline V &map::operator[](K &&key) { + return insert(u::move(make_pair(u::forward(key), V()))).first->second; } template -void map::swap(map &other) { +inline void map::swap(map &other) { detail::hash_swap(m_base, other.m_base); } diff --git a/u_misc.h b/u_misc.h index ec23bc2..588a9a2 100644 --- a/u_misc.h +++ b/u_misc.h @@ -116,13 +116,6 @@ inline void print(const char *fmt, const Ts&... ts) { fflush(stdout); } -template -inline typename enable_if::value && is_signed::value, - typename make_signed::type>::type -sls(T x, T n) { - return (typename make_signed::type)((typename make_unsigned::type)x << n); -} - void *moveMemory(void *dest, const void *src, size_t n); // Random number generation facilities diff --git a/u_pair.h b/u_pair.h index 0142305..2bbec99 100644 --- a/u_pair.h +++ b/u_pair.h @@ -4,367 +4,78 @@ namespace u { -namespace detail { - enum class pair_type { - none, first, second, both - }; - - template - struct choose_pair; - template - struct choose_pair : integral_constant {}; - template - struct choose_pair : integral_constant {}; - template - struct choose_pair : integral_constant {}; - template <> - struct choose_pair : integral_constant {}; - template <> - struct choose_pair : integral_constant {}; - - template - using choose_compress = choose_pair< - is_same::type, - typename remove_cv::type>::value, - is_empty::value, - is_empty::value - >; - - template ::value> - struct compressed_pair; - - ///! NONE - template - struct compressed_pair { - using first_type = T1; - using second_type = T2; - using first_reference = typename remove_reference::type &; - using second_reference = typename remove_reference::type &; - using first_const_reference = typename remove_reference::type const &; - using second_const_reference = typename remove_reference::type const &; - - constexpr compressed_pair(); - constexpr compressed_pair(T1 f, T2 s); - - first_reference first(); - constexpr first_const_reference first() const; - second_reference second(); - constexpr second_const_reference second() const; - - void swap(compressed_pair &other); - - private: - T1 m_first; - T2 m_second; - }; - - template - inline constexpr compressed_pair::compressed_pair() - : m_first() - , m_second() - { - } - - template - inline constexpr compressed_pair::compressed_pair(T1 f, T2 s) - : m_first(f) - , m_second(s) - { - } - - template - inline typename compressed_pair::first_reference - compressed_pair::first() { - return m_first; - } - - template - inline constexpr typename compressed_pair::first_const_reference - compressed_pair::first() const { - return m_first; - } - - template - inline typename compressed_pair::second_reference - compressed_pair::second() { - return m_second; - } - - template - inline constexpr typename compressed_pair::second_const_reference - compressed_pair::second() const { - return m_second; - } - - template - inline void compressed_pair::swap(compressed_pair &other) { - swap(m_first, other.m_first); - swap(m_second, other.m_second); - } - - ///! FIRST - template - struct compressed_pair : private T1 { - using first_type = T1; - using second_type = T2; - using first_reference = T1 &; - using second_reference = typename remove_reference::type &; - using first_const_reference = T1 const &; - using second_const_reference = typename remove_reference::type const &; - - constexpr compressed_pair(); - constexpr compressed_pair(T1 f, T2 s); - - first_reference first(); - constexpr first_const_reference first() const; - second_reference second(); - constexpr second_const_reference second() const; - - void swap(compressed_pair &other); - - private: - T2 m_second; - }; - - template - inline constexpr compressed_pair::compressed_pair() - : T1() - , m_second() - { - } - - template - inline constexpr compressed_pair::compressed_pair(T1 f, T2 s) - : T1(forward(f)) - , m_second(forward(s)) - { - } - - template - inline typename compressed_pair::first_reference - compressed_pair::first() { - return *this; - } - - template - inline constexpr typename compressed_pair::first_const_reference - compressed_pair::first() const { - return *this; - } - - template - inline typename compressed_pair::second_reference - compressed_pair::second() { - return m_second; - } - - template - inline constexpr typename compressed_pair::second_const_reference - compressed_pair::second() const { - return m_second; - } - - template - inline void compressed_pair::swap(compressed_pair &other) { - swap(m_second, other.m_second); - } - - ///! SECOND - template - struct compressed_pair : private T2 { - using first_type = T1; - using second_type = T2; - using first_reference = typename remove_reference::type &; - using second_reference = T2 &; - using first_const_reference = typename remove_reference::type const &; - using second_const_reference = T2 const &; - - constexpr compressed_pair(); - constexpr compressed_pair(T1 f, T2 s); - - first_reference first(); - constexpr first_const_reference first() const; - second_reference second(); - constexpr second_const_reference second() const; - - void swap(compressed_pair &); - - private: - T1 m_first; - }; - - template - inline constexpr compressed_pair::compressed_pair() - : T2() - , m_first() - { - } - - template - inline constexpr compressed_pair::compressed_pair(T1 f, T2 s) - : T2(forward(s)) - , m_first(forward(f)) - { - } - - template - inline typename compressed_pair::first_reference - compressed_pair::first() { - return m_first; - } - - template - inline constexpr typename compressed_pair::first_const_reference - compressed_pair::first() const { - return m_first; - } - - template - inline typename compressed_pair::second_reference - compressed_pair::second() { - return *this; - } - - template - inline constexpr typename compressed_pair::second_const_reference - compressed_pair::second() const { - return *this; - } - - template - inline void compressed_pair::swap(compressed_pair &other) { - swap(m_first, other.m_first); - } - - ///! BOTH - template - struct compressed_pair : private T1, T2 { - using first_type = T1; - using second_type = T2; - using first_reference = T1 &; - using second_reference = T2 &; - using first_const_reference = T1 const &; - using second_const_reference = T2 const &; - - constexpr compressed_pair(); - constexpr compressed_pair(T1 f, T2 s); - - first_reference first(); - constexpr first_const_reference first() const; - second_reference second(); - constexpr second_const_reference second() const; - - void swap(compressed_pair &); - }; - - template - inline constexpr compressed_pair::compressed_pair() - : T1() - , T2() - { - } - - template - inline constexpr compressed_pair::compressed_pair(T1 f, T2 s) - : T1(forward(f)) - , T2(forward(s)) - { - } - - template - inline typename compressed_pair::first_reference - compressed_pair::first() { - return *this; - } - - template - inline constexpr typename compressed_pair::first_const_reference - compressed_pair::first() const { - return *this; - } - - template - inline typename compressed_pair::second_reference - compressed_pair::second() { - return *this; - } - - template - inline constexpr typename compressed_pair::second_const_reference - compressed_pair::second() const { - return *this; - } - - template - inline void compressed_pair::swap(compressed_pair &) { - // Cannot swap - } -} - template -struct pair : private detail::compressed_pair { -private: - using base = detail::compressed_pair; -public: - using first_type = typename base::first_type; - using second_type = typename base::second_type; - using first_reference = typename base::first_reference; - using second_reference = typename base::second_reference; - using first_const_reference = typename base::first_const_reference; - using second_const_reference = typename base::second_const_reference; +struct pair { + typedef T1 first_type; + typedef T2 second_type; + + T1 first; + T2 second; constexpr pair(); - constexpr pair(T1 f, T2 s); + constexpr pair(const T1 &x, const T2 &y); - first_reference first(); - constexpr first_const_reference first() const; - second_reference second(); - constexpr second_const_reference second() const; + pair(const pair &p) = default; + pair(pair &&p) = default; - void swap(pair &other); + pair &operator=(const pair &p); + pair &operator=(pair &&p); }; template inline constexpr pair::pair() - : base() + : first() + , second() { } template -inline constexpr pair::pair(T1 f, T2 s) - : base(forward(f), forward(s)) +inline constexpr pair::pair(const T1 &x, const T2 &y) + : first(x) + , second(y) { } template -inline typename pair::first_reference pair::first() { - return base::first(); +inline pair &pair::operator=(const pair &p) { + first = p.first; + second = p.second; + return *this; } template -inline constexpr typename pair::first_const_reference pair::first() const { - return base::first(); +inline pair &pair::operator=(pair &&p) { + first = u::forward(p.first); + second = u::forward(p.second); + return *this; } -template -inline typename pair::second_reference pair::second() { - return base::second(); -} +namespace detail { + template + struct reference_wrapper; -template -inline constexpr typename pair::second_const_reference pair::second() const { - return base::second(); -} + template + struct make_pair_type { + typedef T type; + }; + template + struct make_pair_type> { + typedef T &type; + }; -template -inline void pair::swap(pair &other) { - base::swap(other); + template + struct make_pair_return { + typedef typename make_pair_type::type>::type type; + }; } template -inline pair::type, typename decay::type> -make_pair(T1 &&first, T2 &&second) { - return pair::type, typename decay::type>(forward(first), forward(second)); +inline constexpr pair::type, + typename detail::make_pair_return::type> +make_pair(T1 &&t1, T2 &&t2) { + return pair::type, + typename detail::make_pair_return::type>(u::forward(t1), + u::forward(t2)); } } diff --git a/u_set.h b/u_set.h index da33079..2fe561f 100644 --- a/u_set.h +++ b/u_set.h @@ -28,8 +28,11 @@ struct set { bool empty() const; size_t size() const; - iterator find(const K &key) const; + const_iterator find(const K &key) const; + iterator find(const K &key); + pair insert(const K &key); + pair insert(K &&key); void erase(iterator where); size_t erase(const K &key); @@ -110,7 +113,14 @@ inline void set::clear() { } template -inline typename set::iterator set::find(const K &key) const { +typename set::const_iterator set::find(const K &key) const { + iterator result; + result.node = detail::hash_find(m_base, key); + return result; +} + +template +typename set::iterator set::find(const K &key) { iterator result; result.node = detail::hash_find(m_base, key); return result; @@ -118,8 +128,13 @@ inline typename set::iterator set::find(const K &key) const { template inline pair::iterator, bool> set::insert(const K &key) { + return insert(u::move(K(key))); +} + +template +inline pair::iterator, bool> set::insert(K &&key) { auto result = make_pair(find(key), false); - if (result.first().node != 0) + if (result.first.node != 0) return result; size_t nbuckets = (m_base.buckets.last - m_base.buckets.first); @@ -129,15 +144,15 @@ inline pair::iterator, bool> set::insert(const K &key) { nbuckets = (m_base.buckets.last - m_base.buckets.first); } - size_t hh = hash(key) & (nbuckets - 2); - typename set::iterator &it = result.first(); + const size_t hh = hash(key) & (nbuckets - 2); + typename set::iterator &it = result.first; it.node = detail::hash_insert_new(m_base, hh); it.node->first.~hash_elem(); - new ((void *)&it.node->first) hash_elem(key); + new ((void *)&it.node->first) hash_elem(u::move(key)); - result.second() = true; - return result; + result.second = true; + return u::move(result); } template diff --git a/u_traits.h b/u_traits.h index 7f839ce..cb743a9 100644 --- a/u_traits.h +++ b/u_traits.h @@ -475,190 +475,6 @@ inline constexpr T &&forward(typename remove_reference::type &&t) noexcept { return static_cast(t); } -namespace detail { - template - struct types { - typedef H head; - typedef T tail; - }; - - // nat: not a type - struct nat { - nat() = delete; - nat(const nat&) = delete; - nat &operator=(const nat&) = delete; - ~nat() = delete; - }; - - typedef types>>>> signed_types; - - typedef types>>>> unsigned_types; - - // Given a type list recursively instantiate until we find the given type - template - struct find_first_type; - template - struct find_first_type, S, true> { - typedef H type; - }; - template - struct find_first_type, S, false> { - typedef typename find_first_type::type type; - }; - - // For make_signed, make_unsigned we need to also apply the right cv - template ::type>::value, - bool = is_volatile::type>::value> - struct apply_cv { - typedef U type; - }; - template - struct apply_cv { // is_const - typedef const U type; - }; - template - struct apply_cv { // is_volatile - typedef volatile U type; - }; - template - struct apply_cv { // is_const && is_volatile - typedef const volatile U type; - }; - template - struct apply_cv { // T& is_const - typedef const U& type; - }; - template - struct apply_cv { // T& is_volatile - typedef volatile U& type; - }; - template - struct apply_cv { // T& is_const && T& is_volatile - typedef const volatile U& type; - }; - - template ::value || is_enum::value> - struct make_signed {}; - template ::value || is_enum::value> - struct make_unsigned {}; - template - struct make_signed { - typedef typename find_first_type::type type; - }; - template - struct make_unsigned { - typedef typename find_first_type::type type; - }; - - template <> - struct make_signed {}; - template <> - struct make_signed { - typedef short type; - }; - template <> - struct make_signed { - typedef short type; - }; - template <> - struct make_signed { - typedef int type; - }; - template <> - struct make_signed { - typedef int type; - }; - template <> - struct make_signed { - typedef long type; - }; - template <> - struct make_signed { - typedef long type; - }; - template <> - struct make_signed { - typedef long long type; - }; - template <> - struct make_signed { - typedef long long type; - }; - - template <> - struct make_unsigned {}; - template <> - struct make_unsigned { - typedef unsigned short type; - }; - template <> - struct make_unsigned { - typedef unsigned short type; - }; - template <> - struct make_unsigned { - typedef unsigned int type; - }; - template <> - struct make_unsigned { - typedef unsigned int type; - }; - template <> - struct make_unsigned { - typedef unsigned long type; - }; - template <> - struct make_unsigned { - typedef unsigned long type; - }; - template <> - struct make_unsigned { - typedef unsigned long long type; - }; - template <> - struct make_unsigned { - typedef unsigned long long type; - }; -} - -/// make_signed -template -struct make_signed { - typedef typename detail::apply_cv::type>::type>::type type; -}; - -/// make_unsigned -template -struct make_unsigned { - typedef typename detail::apply_cv::type>::type>::type type; -}; - -namespace detail { - template ::value> - struct is_signed_switch : integral_constant {}; - template - struct is_signed_switch : true_type {}; // floating point - - template ::value> - struct is_signed : is_signed_switch {}; - template - struct is_signed : false_type {}; -} - -/// is_signed -template -struct is_signed : detail::is_signed {}; - /// add_pointer template struct add_pointer {