From 7dba2ff3a16bcca29b65bf68ae327bd256cf1e54 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Wed, 16 Jul 2014 19:57:49 +0000 Subject: [PATCH] base: use ptrdiff_t for array and string sizes and some other things. --- src/input/input.h | 26 ++++++------ src/lol/algorithm/sort.h | 12 +++--- src/lol/base/array.h | 90 ++++++++++++++++++++-------------------- src/lol/base/map.h | 16 +++---- src/lol/base/string.h | 49 +++++++++++----------- src/lol/math/array2d.h | 24 +++++------ src/lol/math/array3d.h | 28 ++++++------- src/lol/math/arraynd.h | 54 ++++++++++++------------ src/ticker.cpp | 14 +++---- src/tileset.h | 2 +- test/meshviewer.cpp | 2 +- test/unit/arraynd.cpp | 4 +- test/unit/string.cpp | 34 +++++++-------- 13 files changed, 178 insertions(+), 177 deletions(-) diff --git a/src/input/input.h b/src/input/input.h index 9b2a3a0b..4a8fbeb2 100644 --- a/src/input/input.h +++ b/src/input/input.h @@ -26,26 +26,26 @@ public: const String& GetName() const { return m_name; } /** Gets the index of the corresponding key, needed to call GetKey */ - int GetKeyIndex(const char* name) const { return GetItemIndex(name, m_keynames); } + ptrdiff_t GetKeyIndex(const char* name) const { return GetItemIndex(name, m_keynames); } /** Gets the index of the corresponding axis, needed to call GetAxis */ - int GetAxisIndex(const char* name) const { return GetItemIndex(name, m_axisnames); } + ptrdiff_t GetAxisIndex(const char* name) const { return GetItemIndex(name, m_axisnames); } /** Gets the index of the corresponding cursor, needed to call GetCursor */ - int GetCursorIndex(const char* name) const { return GetItemIndex(name, m_cursornames); } + ptrdiff_t GetCursorIndex(const char* name) const { return GetItemIndex(name, m_cursornames); } /** Gets the current state of the given key, true being pressed and false being released */ - bool GetKey(int index) const { return m_keys[index]; } + bool GetKey(ptrdiff_t index) const { return m_keys[index]; } /** Gets the current value of the given axis. Devices should cap this value between -1 and 1 as much as possible, through it is not guaranteed */ - float GetAxis(int index) const { return m_axis[index].m1 * m_axis[index].m2; } + float GetAxis(ptrdiff_t index) const { return m_axis[index].m1 * m_axis[index].m2; } /** Gets the current value of the given cursor, 0,0 being the bottom-left corner and 1,1 being the top-right corner */ - vec2 GetCursor(int index) const { return m_cursors[index].m1; } + vec2 GetCursor(ptrdiff_t index) const { return m_cursors[index].m1; } /** Gets the coordinate of the pixel the cursor is currently over, 0,0 being the bottom-left corner. */ - ivec2 GetCursorPixel(int index) const { return m_cursors[index].m2; } + ivec2 GetCursorPixel(ptrdiff_t index) const { return m_cursors[index].m2; } /** Sets a per-device-axis sensitivity factor. The value returned by the operating system will be multiplied by this value before being returned by GetAxis */ - void SetAxisSensitivity(int index, float sensitivity) { m_axis[index].m2 = sensitivity; } + void SetAxisSensitivity(ptrdiff_t index, float sensitivity) { m_axis[index].m2 = sensitivity; } /** Gets the per-device-axis sensitivity factor. The value returned by the operating system will be multiplied by this value before being returned by GetAxis */ - float GetAxisSensitivity(int index) const { return m_axis[index].m2; } + float GetAxisSensitivity(ptrdiff_t index) const { return m_axis[index].m2; } /** Gets a list of the name of all available keys in this device */ const array& GetAllKeys() const { return m_keynames; } @@ -86,7 +86,7 @@ protected: ~InputDevice() { - for (int i = 0; i < devices.Count(); ++i) + for (ptrdiff_t i = 0; i < devices.Count(); ++i) { if (devices[i] == this) { @@ -100,9 +100,9 @@ private: static array devices; template - int GetItemIndex(const char* name, const array& array) const + ptrdiff_t GetItemIndex(const char* name, const array& array) const { - for (int i = 0; i < array.Count(); ++i) + for (ptrdiff_t i = 0; i < array.Count(); ++i) { if (array[i] == name) return i; @@ -112,7 +112,7 @@ private: static InputDevice* GetDevice(const char* name) { - for (int i = 0; i < devices.Count(); ++i) + for (ptrdiff_t i = 0; i < devices.Count(); ++i) { if (devices[i]->m_name == name) return devices[i]; diff --git a/src/lol/algorithm/sort.h b/src/lol/algorithm/sort.h index d44fbb8e..9ca60e92 100644 --- a/src/lol/algorithm/sort.h +++ b/src/lol/algorithm/sort.h @@ -25,7 +25,7 @@ void array_base::Sort(int sort) if (s++ == sort) { int d = 1; - for (int i = 0; i < Count() - 1; i = lol::max(i + d, 0)) + for (ptrdiff_t i = 0; i < Count() - 1; i = lol::max(i + d, (ptrdiff_t)0)) { if (i <= 0 || m_data[i] < m_data[i + 1]) d = 1; @@ -44,9 +44,9 @@ void array_base::Sort(int sort) } template -void array_base::SortQuickSwap(int start, int stop) +void array_base::SortQuickSwap(ptrdiff_t start, ptrdiff_t stop) { - int m[3] = + ptrdiff_t m[3] = { rand(start, stop), rand(start, stop), @@ -57,7 +57,7 @@ void array_base::SortQuickSwap(int start, int stop) { if (m_data[m[i+1]] < m_data[m[i]]) { - int mt = m[i+1]; + ptrdiff_t mt = m[i+1]; m[i+1] = m[i]; m[i] = mt; i = 0; @@ -67,8 +67,8 @@ void array_base::SortQuickSwap(int start, int stop) } //actual stuff T median = m_data[m[1]]; - int i0 = start; - int i1 = stop - 1; + ptrdiff_t i0 = start; + ptrdiff_t i1 = stop - 1; bool swap = false; while (i0 < i1) { diff --git a/src/lol/base/array.h b/src/lol/base/array.h index c6f9395e..fcea6c05 100644 --- a/src/lol/base/array.h +++ b/src/lol/base/array.h @@ -29,7 +29,7 @@ namespace lol { -#define INDEX_NONE -1 +static ptrdiff_t const INDEX_NONE = -1; /* * The base array type. @@ -52,14 +52,14 @@ public: m_count(0), m_reserved(0) { - Reserve((int)list.size()); + Reserve(list.size()); for (auto elem : list) Push(elem); } inline ~array_base() { - for (int i = 0; i < m_count; i++) + for (ptrdiff_t i = 0; i < m_count; i++) m_data[i].~element_t(); delete[] reinterpret_cast(m_data); } @@ -69,7 +69,7 @@ public: /* Reserve the exact number of values instead of what the other * array had reserved. Just a method for not wasting too much. */ Reserve(that.m_count); - for (int i = 0; i < that.m_count; i++) + for (ptrdiff_t i = 0; i < that.m_count; i++) new(&m_data[i]) element_t(that[i]); m_count = that.m_count; } @@ -87,9 +87,9 @@ public: * elements, then use placement new directly for the * remaining elements. */ Reserve(that.m_count); - for (int i = 0; i < m_count && i < that.m_count; i++) + for (ptrdiff_t i = 0; i < m_count && i < that.m_count; i++) m_data[i] = element_t(that[i]); - for (int i = m_count; i < that.m_count; i++) + for (ptrdiff_t i = m_count; i < that.m_count; i++) new(&m_data[i]) element_t(that[i]); } else @@ -98,11 +98,11 @@ public: * use placement new for the elements in the other array * that we do not have, and finally destroy the remaining * elements. */ - for (int i = 0; i < m_count && i < that.m_count; i++) + for (ptrdiff_t i = 0; i < m_count && i < that.m_count; i++) m_data[i] = element_t(that[i]); - for (int i = m_count; i < that.m_count; i++) + for (ptrdiff_t i = m_count; i < that.m_count; i++) new(&m_data[i]) element_t(that[i]); - for (int i = that.m_count; i < m_count; i++) + for (ptrdiff_t i = that.m_count; i < m_count; i++) m_data[i].~element_t(); } m_count = that.m_count; @@ -112,9 +112,9 @@ public: array_base& operator+=(array_base const &that) { - int todo = that.m_count; + ptrdiff_t todo = that.m_count; Reserve(m_count + todo); - for (int i = 0; i < todo; i++) + for (ptrdiff_t i = 0; i < todo; i++) new(&m_data[m_count + i]) element_t(that[i]); m_count += todo; return *this; @@ -129,16 +129,16 @@ public: return ret; } - inline element_t& operator[](int n) + inline element_t& operator[](ptrdiff_t n) { /* Allow array[0] even if size is zero so that people can * always use &array[0] to get a pointer to the data. */ ASSERT(n >= 0); - ASSERT((unsigned)n < (unsigned)m_count || (!n && !m_count)); + ASSERT(n < m_count || (!n && !m_count)); return m_data[n]; } - inline element_t const& operator[](int n) const + inline element_t const& operator[](ptrdiff_t n) const { ASSERT(n >= 0); ASSERT(n < m_count || (!n && !m_count)); @@ -203,7 +203,7 @@ public: return true; } - inline void Insert(T const &x, int pos) + inline void Insert(T const &x, ptrdiff_t pos) { ASSERT(pos >= 0); ASSERT(pos <= m_count); @@ -211,7 +211,7 @@ public: if (m_count >= m_reserved) Grow(); - for (int i = m_count; i > pos; --i) + for (ptrdiff_t i = m_count; i > pos; --i) { new (&m_data[i]) element_t(m_data[i - 1]); m_data[i - 1].~element_t(); @@ -220,7 +220,7 @@ public: ++m_count; } - inline bool InsertUnique(T const &x, int pos) + inline bool InsertUnique(T const &x, ptrdiff_t pos) { ASSERT(pos >= 0); ASSERT(pos <= m_count); @@ -232,9 +232,9 @@ public: return true; } - inline int Find(T const &x) + inline ptrdiff_t Find(T const &x) { - for (int i = 0; i < m_count; ++i) + for (ptrdiff_t i = 0; i < m_count; ++i) if (m_data[i] == x) return i; return INDEX_NONE; @@ -242,7 +242,7 @@ public: bool RemoveItem(T const &x) { - int idx = Find(x); + ptrdiff_t idx = Find(x); if (idx != INDEX_NONE) { Remove(idx); @@ -253,7 +253,7 @@ public: bool RemoveSwapItem(T const &x) { - int idx = Find(x); + ptrdiff_t idx = Find(x); if (idx != INDEX_NONE) { RemoveSwap(idx); @@ -264,8 +264,8 @@ public: bool SwapItem(T const &x1, T const &x2) { - int idx1 = Find(x1); - int idx2 = Find(x2); + ptrdiff_t idx1 = Find(x1); + ptrdiff_t idx2 = Find(x2); if (idx1 != INDEX_NONE && idx2 != INDEX_NONE) { Swap(idx1, idx2); @@ -282,7 +282,7 @@ public: return tmp; } - void Swap(int pos1, int pos2) + void Swap(ptrdiff_t pos1, ptrdiff_t pos2) { ASSERT(pos1 >= 0); ASSERT(pos2 >= 0); @@ -293,7 +293,7 @@ public: std::swap((*this)[pos1], (*this)[pos2]); } - void Remove(int pos, int todelete = 1) + void Remove(ptrdiff_t pos, ptrdiff_t todelete = 1) { ASSERT(todelete >= 0); ASSERT(pos - todelete >= -m_count - 1); @@ -301,14 +301,14 @@ public: if (pos < 0) pos = m_count + pos; - for (int i = pos; i + todelete < m_count; i++) + for (ptrdiff_t i = pos; i + todelete < m_count; i++) m_data[i] = m_data[i + todelete]; - for (int i = m_count - todelete; i < m_count; i++) + for (ptrdiff_t i = m_count - todelete; i < m_count; i++) m_data[i].~element_t(); m_count -= todelete; } - void RemoveSwap(int pos, int todelete = 1) + void RemoveSwap(ptrdiff_t pos, ptrdiff_t todelete = 1) { ASSERT(todelete >= 0); ASSERT(pos - todelete >= -m_count - 1); @@ -316,7 +316,7 @@ public: if (pos < 0) pos = m_count + pos; - for (int i = 0; i < todelete; i++) + for (ptrdiff_t i = 0; i < todelete; i++) { if (pos + i < m_count - 1 - i) m_data[pos + i] = m_data[m_count - 1 - i]; @@ -325,17 +325,17 @@ public: m_count -= todelete; } - void Resize(int count, element_t e = element_t()) + void Resize(ptrdiff_t count, element_t e = element_t()) { ASSERT(count >= 0); Reserve(count); /* Too many elements? Remove them. */ - for (int i = count; i < m_count; ++i) + for (ptrdiff_t i = count; i < m_count; ++i) m_data[i].~element_t(); /* Not enough elements? Add some. */ - for (int i = m_count; i < count; ++i) + for (ptrdiff_t i = m_count; i < count; ++i) new(&m_data[i]) element_t(e); m_count = count; @@ -346,9 +346,9 @@ public: Remove(0, m_count); } - void Reserve(int toreserve) + void Reserve(ptrdiff_t toreserve) { - if (toreserve <= (int)m_reserved) + if (toreserve <= m_reserved) return; /* This cast is not very nice, because we kill any alignment @@ -358,7 +358,7 @@ public: element_t *tmp = reinterpret_cast(reinterpret_cast (new uint8_t[sizeof(element_t) * toreserve])); ASSERT(tmp, "out of memory in array class"); - for (int i = 0; i < m_count; i++) + for (ptrdiff_t i = 0; i < m_count; i++) { new(&tmp[i]) element_t(m_data[i]); m_data[i].~element_t(); @@ -369,15 +369,15 @@ public: } void Sort(int sort); - void SortQuickSwap(int start, int stop); + void SortQuickSwap(ptrdiff_t start, ptrdiff_t stop); /* Support C++11 range-based for loops */ class ConstIterator { public: - ConstIterator(array_base const *array, int pos) + ConstIterator(array_base const *that, ptrdiff_t pos) : m_pos(pos), - m_array(array) + m_array(that) { } bool operator !=(const ConstIterator& that) const @@ -397,16 +397,16 @@ public: } private: - int m_pos; + ptrdiff_t m_pos; array_base const *m_array; }; class Iterator { public: - Iterator(array_base *array, int pos) + Iterator(array_base *that, ptrdiff_t pos) : m_pos(pos), - m_array(array) + m_array(that) { } bool operator !=(const Iterator& that) const @@ -426,13 +426,13 @@ public: } private: - int m_pos; + ptrdiff_t m_pos; array_base *m_array; }; public: - inline int Count() const { return m_count; } - inline int Bytes() const { return m_count * sizeof(element_t); } + inline ptrdiff_t Count() const { return m_count; } + inline ptrdiff_t Bytes() const { return m_count * sizeof(element_t); } protected: void Grow() @@ -441,7 +441,7 @@ protected: } element_t *m_data; - int m_count, m_reserved; + ptrdiff_t m_count, m_reserved; }; /* diff --git a/src/lol/base/map.h b/src/lol/base/map.h index 936d44a5..8de2a226 100644 --- a/src/lol/base/map.h +++ b/src/lol/base/map.h @@ -34,7 +34,7 @@ public: inline V const& operator[] (E const &key) const { /* Look for the hash in our table and return the value. */ - int i = FindIndex(key); + ptrdiff_t i = FindIndex(key); ASSERT(i >= 0, "trying to read a nonexistent key in map"); return m_array[i].m3; } @@ -44,7 +44,7 @@ public: { /* Look for the hash in our table and return the value if found. */ uint32_t hashed = ((hash const &)*this)(key); - int i = FindIndex(key, hashed); + ptrdiff_t i = FindIndex(key, hashed); if (i >= 0) return m_array[i].m3; @@ -56,7 +56,7 @@ public: template inline void Remove(E const &key) { - int i = FindIndex(key); + ptrdiff_t i = FindIndex(key); if (i >= 0) m_array.Remove(i); } @@ -70,7 +70,7 @@ public: template inline bool TryGetValue(E const &key, V& value) { - int i = FindIndex(key); + ptrdiff_t i = FindIndex(key); if (i >= 0) { value = m_array[i].m3; @@ -88,16 +88,16 @@ public: return ret; } - inline int Count() const + inline ptrdiff_t Count() const { return m_array.Count(); } private: template - inline int FindIndex(E const &key, uint32_t hashed) + inline ptrdiff_t FindIndex(E const &key, uint32_t hashed) { - for (int i = 0; i < m_array.Count(); ++i) + for (ptrdiff_t i = 0; i < m_array.Count(); ++i) if (m_array[i].m1 == hashed) if (m_array[i].m2 == key) return i; @@ -105,7 +105,7 @@ private: } template - inline int FindIndex(E const &key) + inline ptrdiff_t FindIndex(E const &key) { uint32_t hashed = ((hash const &)*this)(key); return FindIndex(key, hashed); diff --git a/src/lol/base/string.h b/src/lol/base/string.h index 3a52b27e..9a62d47b 100644 --- a/src/lol/base/string.h +++ b/src/lol/base/string.h @@ -43,11 +43,11 @@ public: { using namespace std; ASSERT(str); - Resize((int)strlen(str)); + Resize(strlen(str)); memcpy(&(*this)[0], str, Count() + 1); } - inline String(char const *str, int count) + inline String(char const *str, ptrdiff_t count) : Super() { using namespace std; @@ -62,16 +62,16 @@ public: { } - inline char &operator [](int n) + inline char &operator [](ptrdiff_t n) { /* Allow n == Count() because we might have reasonable reasons * to access that hidden null character. */ ASSERT(n >= 0); - ASSERT((unsigned)n <= (unsigned)Count()); + ASSERT(n <= Count()); return ((Super &)*this)[n]; } - inline char const &operator [](int n) const + inline char const &operator [](ptrdiff_t n) const { ASSERT(n >= 0); ASSERT(n <= Count()); @@ -90,7 +90,7 @@ public: return (*this)[Count() - 1]; } - inline int Count() const + inline ptrdiff_t Count() const { return ((Super const &)*this).Count() - 1; } @@ -108,14 +108,14 @@ public: } /* Does not initialise the newly allocated characters */ - void Resize(int count) + void Resize(ptrdiff_t count) { ASSERT(count >= 0); ((Super &)*this).Resize(count + 1); ((Super &)*this).Last() = '\0'; } - String Sub(int start, int count = -1) const + String Sub(ptrdiff_t start, ptrdiff_t count = -1) const { ASSERT(start >= 0); if (start >= Count()) @@ -125,20 +125,20 @@ public: return String(&(*this)[start], count); } - int IndexOf(char token) const + ptrdiff_t IndexOf(char token) const { using namespace std; char const *tmp = strchr(C(), token); - return tmp ? (int)(intptr_t)(tmp - C()) : INDEX_NONE; + return tmp ? (ptrdiff_t)(tmp - C()) : INDEX_NONE; } - int IndexOf(char const* token) const + ptrdiff_t IndexOf(char const* token) const { using namespace std; char const *tmp = strstr(C(), token); - return tmp ? (int)(intptr_t)(tmp - C()) : INDEX_NONE; + return tmp ? (ptrdiff_t)(tmp - C()) : INDEX_NONE; } bool Contains(String const &s) const @@ -146,15 +146,16 @@ public: return IndexOf(s.C()) != INDEX_NONE; } - int LastIndexOf(char token) const + ptrdiff_t LastIndexOf(char token) const { using namespace std; char const *tmp = strrchr(C(), token); - return tmp ? (int)(intptr_t)(tmp - C()) : INDEX_NONE; + return tmp ? (ptrdiff_t)(tmp - C()) : INDEX_NONE; } - int Replace(const char old_token, const char new_token, bool all_occurence=false) + int Replace(char const old_token, char const new_token, + bool all_occurrences = false) { using namespace std; @@ -164,7 +165,7 @@ public: { *tmp = new_token; res++; - if (!all_occurence) + if (!all_occurrences) break; } return res; @@ -173,7 +174,7 @@ public: inline String& ToLower() { char* p = C(); - for (int i = 0; i < Count(); ++i) + for (ptrdiff_t i = 0; i < Count(); ++i) if ('A' <= p[i] && p[i] <= 'Z') p[i] += 'a' - 'A'; return *this; @@ -182,18 +183,18 @@ public: inline String& ToUpper() { char* p = C(); - for (int i = 0; i < Count(); ++i) + for (ptrdiff_t i = 0; i < Count(); ++i) if ('a' <= p[i] && p[i] <= 'z') p[i] += 'A' - 'a'; return *this; } - int LastIndexOf(char const* token) const + ptrdiff_t LastIndexOf(char const* token) const { using namespace std; - int token_len = (int)strlen(token); - for (int i = Count() - token_len; i >= 0; --i) + ptrdiff_t token_len = strlen(token); + for (ptrdiff_t i = Count() - token_len; i >= 0; --i) if (strstr(C() + i, token)) return i; return -1; @@ -215,7 +216,7 @@ public: bool IsAlpha() { - for (int i = 0; i < m_count; i++) + for (ptrdiff_t i = 0; i < m_count; i++) if (m_data[i] != '\0' && (m_data[i] < '0' || '9' < m_data[i])) return false; return true; @@ -230,7 +231,7 @@ public: inline String& operator +=(String const &s) { using namespace std; - int old_count = Count(); + ptrdiff_t old_count = Count(); Resize(Count() + s.Count()); memcpy(&(*this)[old_count], &s[0], Count() - old_count); return *this; @@ -266,7 +267,7 @@ public: /* We parse the C string twice because of strlen + memcmp * but it's probably still faster than doing it by hand. */ using namespace std; - int sz_len = (int)strlen(sz); + ptrdiff_t sz_len = strlen(sz); return Count() == sz_len && memcmp(C(), sz, sz_len) == 0; } diff --git a/src/lol/math/array2d.h b/src/lol/math/array2d.h index 7d51236e..96fa7373 100644 --- a/src/lol/math/array2d.h +++ b/src/lol/math/array2d.h @@ -71,7 +71,7 @@ public: ASSERT(pos.x < m_size.x); ASSERT(pos.y < m_size.y); - int n = pos.y * m_size.x + pos.x; + ptrdiff_t n = pos.y * m_size.x + pos.x; ASSERT(n >= 0); ASSERT(n < this->m_count); @@ -85,7 +85,7 @@ public: ASSERT(pos.x < m_size.x); ASSERT(pos.y < m_size.y); - int n = pos.y * m_size.x + pos.x; + ptrdiff_t n = pos.y * m_size.x + pos.x; ASSERT(n >= 0); ASSERT(n < this->m_count); @@ -95,13 +95,13 @@ public: class Column { public: - inline Column(array2d &array, int i) + inline Column(array2d &array, ptrdiff_t i) : m_array(array), m_column(i) { } - inline element_t &operator [](int j) + inline element_t &operator [](ptrdiff_t j) { ASSERT(j >= 0); ASSERT(j < m_array.m_size.y); @@ -110,19 +110,19 @@ public: private: array2d &m_array; - int m_column; + ptrdiff_t m_column; }; class ConstColumn { public: - inline ConstColumn(array2d const &array, int i) + inline ConstColumn(array2d const &array, ptrdiff_t i) : m_array(array), m_column(i) { } - inline element_t const &operator [](int j) const + inline element_t const &operator [](ptrdiff_t j) const { ASSERT(j >= 0); ASSERT(j < m_array.m_size.y); @@ -131,18 +131,18 @@ public: private: array2d const &m_array; - int m_column; + ptrdiff_t m_column; }; /* Access addressable columns, allowing for array[i][j] syntax. */ - inline class Column operator [](int i) + inline class Column operator [](ptrdiff_t i) { ASSERT(i >= 0); ASSERT(i < m_size.x); return Column(*this, i); } - inline class ConstColumn operator [](int i) const + inline class ConstColumn operator [](ptrdiff_t i) const { ASSERT(i >= 0); ASSERT(i < m_size.x); @@ -165,8 +165,8 @@ public: public: inline element_t *Data() { return super::Data(); } inline element_t const *Data() const { return super::Data(); } - inline int Count() const { return super::Count(); } - inline int Bytes() const { return super::Bytes(); } + inline ptrdiff_t Count() const { return super::Count(); } + inline ptrdiff_t Bytes() const { return super::Bytes(); } private: ivec2 m_size; diff --git a/src/lol/math/array3d.h b/src/lol/math/array3d.h index 48917001..783314f8 100644 --- a/src/lol/math/array3d.h +++ b/src/lol/math/array3d.h @@ -77,7 +77,7 @@ public: ASSERT(pos.y < m_size.y); ASSERT(pos.z < m_size.z); - int n = (pos.z * m_size.y + pos.y) * m_size.x + pos.x; + ptrdiff_t n = (pos.z * m_size.y + pos.y) * m_size.x + pos.x; ASSERT(n >= 0); ASSERT(n < this->m_count); @@ -93,7 +93,7 @@ public: ASSERT(pos.y < m_size.y); ASSERT(pos.z < m_size.z); - int n = (pos.z * m_size.y + pos.y) * m_size.x + pos.x; + ptrdiff_t n = (pos.z * m_size.y + pos.y) * m_size.x + pos.x; ASSERT(n >= 0); ASSERT(n < this->m_count); @@ -103,7 +103,7 @@ public: class Slice { public: - inline Slice(array3d &array, int i) + inline Slice(array3d &array, ptrdiff_t i) : m_array(array), m_slice(i) { @@ -118,7 +118,7 @@ public: { } - inline element_t &operator [](int k) + inline element_t &operator [](ptrdiff_t k) { ASSERT(k >= 0); ASSERT(k < m_array.m_size.z); @@ -130,7 +130,7 @@ public: ivec2 m_line; }; - inline class Line operator [](int j) + inline class Line operator [](ptrdiff_t j) { ASSERT(j >= 0); ASSERT(j < m_array.m_size.y); @@ -139,13 +139,13 @@ public: private: array3d &m_array; - int m_slice; + ptrdiff_t m_slice; }; class ConstSlice { public: - inline ConstSlice(array3d const &array, int i) + inline ConstSlice(array3d const &array, ptrdiff_t i) : m_array(array), m_slice(i) { @@ -160,7 +160,7 @@ public: { } - inline element_t const &operator [](int k) const + inline element_t const &operator [](ptrdiff_t k) const { ASSERT(k >= 0); ASSERT(k < m_array.m_size.z); @@ -172,7 +172,7 @@ public: ivec2 m_line; }; - inline class Line operator [](int j) const + inline class Line operator [](ptrdiff_t j) const { ASSERT(j >= 0); ASSERT(j < m_array.m_size.y); @@ -181,18 +181,18 @@ public: private: array3d const &m_array; - int m_slice; + ptrdiff_t m_slice; }; /* Access addressable slices, allowing for array[i][j][k] syntax. */ - inline class Slice operator [](int i) + inline class Slice operator [](ptrdiff_t i) { ASSERT(i >= 0); ASSERT(i < m_size.x); return Slice(*this, i); } - inline class ConstSlice operator [](int i) const + inline class ConstSlice operator [](ptrdiff_t i) const { ASSERT(i >= 0); ASSERT(i < m_size.x); @@ -215,8 +215,8 @@ public: public: inline element_t *Data() { return super::Data(); } inline element_t const *Data() const { return super::Data(); } - inline int Count() const { return super::Count(); } - inline int Bytes() const { return super::Bytes(); } + inline ptrdiff_t Count() const { return super::Count(); } + inline ptrdiff_t Bytes() const { return super::Bytes(); } private: ivec3 m_size; diff --git a/src/lol/math/arraynd.h b/src/lol/math/arraynd.h index 1e0a69aa..7957489c 100644 --- a/src/lol/math/arraynd.h +++ b/src/lol/math/arraynd.h @@ -33,7 +33,7 @@ namespace lol { -template +template class arraynd_initializer { public: @@ -43,7 +43,7 @@ public: { } - void FillSizes(size_t * sizes) + void FillSizes(ptrdiff_t * sizes) { *sizes = std::max(*sizes, m_initializers.size()); @@ -51,9 +51,9 @@ public: subinitializer.FillSizes(sizes + 1); } - void FillValues(T * values, size_t * sizes, size_t accumulator) + void FillValues(T * values, ptrdiff_t * sizes, ptrdiff_t accumulator) { - int pos = 0; + ptrdiff_t pos = 0; for (auto subinitializer : m_initializers) { @@ -78,16 +78,16 @@ public: { } - void FillSizes(size_t * sizes) + void FillSizes(ptrdiff_t * sizes) { *sizes = std::max(*sizes, m_initializers.size()); } - void FillValues(T * values, size_t * sizes, size_t accumulator) + void FillValues(T * values, ptrdiff_t * sizes, ptrdiff_t accumulator) { UNUSED(sizes); - int pos = 0; + ptrdiff_t pos = 0; for (auto value : m_initializers) { @@ -102,7 +102,7 @@ private: }; -template +template class arraynd : protected array { public: @@ -113,7 +113,7 @@ public: { } - inline arraynd(vec_t sizes, element_t e = element_t()) + inline arraynd(vec_t sizes, element_t e = element_t()) : m_sizes(sizes) { FixSizes(e); @@ -129,26 +129,26 @@ public: /* Access elements directly using an ivec2, ivec3 etc. index */ inline element_t const & operator[](vec_t const &pos) const { - size_t n = pos[N - 1]; - for (int i = N - 2; i >= 0; --i) + ptrdiff_t n = pos[N - 1]; + for (ptrdiff_t i = N - 2; i >= 0; --i) n = pos[i] + m_sizes[i + 1] * n; return super::operator[](n); } - inline element_t & operator[](vec_t const &pos) + inline element_t & operator[](vec_t const &pos) { return const_cast( const_cast const&>(*this)[pos]); } /* Proxy to access slices */ - template + template class slice { public: typedef slice subslice; - inline slice(ARRAY_TYPE &array, size_t index, size_t accumulator) + inline slice(ARRAY_TYPE &array, ptrdiff_t index, ptrdiff_t accumulator) : m_array(array), m_index(index), m_accumulator(accumulator) @@ -158,7 +158,7 @@ public: /* Accessors for the const version of the proxy */ template::value> inline typename std::enable_if::type - operator[](size_t pos) const + operator[](ptrdiff_t pos) const { return subslice(m_array, m_index + pos * m_accumulator, m_accumulator * m_array.m_sizes[N - L]); @@ -166,7 +166,7 @@ public: template::value> inline typename std::enable_if::type - const & operator[](size_t pos) const + const & operator[](ptrdiff_t pos) const { return m_array.super::operator[](m_index + pos * m_accumulator); } @@ -174,7 +174,7 @@ public: /* Accessors for the non-const version of the proxy */ template::value> inline typename std::enable_if::type - operator[](size_t pos) + operator[](ptrdiff_t pos) { return subslice(m_array, m_index + pos * m_accumulator, m_accumulator * m_array.m_sizes[N - L]); @@ -182,36 +182,36 @@ public: template::value> inline typename std::enable_if::type - & operator[](size_t pos) + & operator[](ptrdiff_t pos) { return m_array.super::operator[](m_index + pos * m_accumulator); } private: ARRAY_TYPE &m_array; - size_t m_index, m_accumulator; + ptrdiff_t m_index, m_accumulator; }; /* Access addressable slices, allowing for array[i][j][...] syntax. */ - inline slice const> operator[](size_t pos) const + inline slice const> operator[](ptrdiff_t pos) const { return slice const>(*this, pos, m_sizes[0]); } - inline slice> operator[](size_t pos) + inline slice> operator[](ptrdiff_t pos) { return slice>(*this, pos, m_sizes[0]); } /* Resize the array. * FIXME: data gets scrambled; should we care? */ - inline void SetSize(vec_t sizes, element_t e = element_t()) + inline void SetSize(vec_t sizes, element_t e = element_t()) { m_sizes = sizes; FixSizes(e); } - inline vec_t GetSize() const + inline vec_t GetSize() const { return this->m_sizes; } @@ -219,13 +219,13 @@ public: public: inline element_t *Data() { return super::Data(); } inline element_t const *Data() const { return super::Data(); } - inline int Count() const { return super::Count(); } - inline int Bytes() const { return super::Bytes(); } + inline ptrdiff_t Count() const { return super::Count(); } + inline ptrdiff_t Bytes() const { return super::Bytes(); } private: inline void FixSizes(element_t e = element_t()) { - size_t total_size = 1; + ptrdiff_t total_size = 1; for (auto size : m_sizes) total_size *= size; @@ -233,7 +233,7 @@ private: this->Resize(total_size, e); } - vec_t m_sizes; + vec_t m_sizes; }; } /* namespace lol */ diff --git a/src/ticker.cpp b/src/ticker.cpp index 84d080d9..e9b59b91 100644 --- a/src/ticker.cpp +++ b/src/ticker.cpp @@ -42,10 +42,10 @@ public: ~TickerData() { ASSERT(nentities == 0, - "still %i entities in ticker\n", nentities); + "still %td entities in ticker\n", nentities); ASSERT(m_autolist.Count() == 0, - "still %i autoreleased entities\n", m_autolist.Count()); - Log::Debug("%i frames required to quit\n", frame - quitframe); + "still %ti autoreleased entities\n", m_autolist.Count()); + Log::Debug("%d frames required to quit\n", frame - quitframe); #if LOL_FEATURE_THREADS gametick.Push(0); @@ -59,7 +59,7 @@ private: /* Entity management */ array m_todolist, m_autolist; array m_list[Entity::ALLGROUP_END]; - int nentities; + ptrdiff_t nentities; /* Fixed framerate management */ int frame, recording; @@ -221,13 +221,13 @@ void TickerData::GameThreadTick() Log::Debug("-------------------------------------\n"); for (int g = 0; g < Entity::ALLGROUP_END; ++g) { - Log::Debug("%s Group %i\n", + Log::Debug("%s Group %d\n", (g < Entity::GAMEGROUP_END) ? "Game" : "Draw", g); for (int i = 0; i < data->m_list[g].Count(); ++i) { Entity *e = data->m_list[g][i]; - Log::Debug(" \\-- [%p] %s (m_ref %i, destroy %i)\n", + Log::Debug(" \\-- [%p] %s (m_ref %d, destroy %d)\n", e, e->GetName(), e->m_ref, e->m_destroy); } } @@ -290,7 +290,7 @@ void TickerData::GameThreadTick() #if !LOL_BUILD_RELEASE if (n) - Log::Error("%i entities stuck after %i frames, poked %i\n", + Log::Error("%td entities stuck after %d frames, poked %d\n", data->nentities, data->quitdelay, n); #endif diff --git a/src/tileset.h b/src/tileset.h index b84e86a1..3f5a8f4e 100644 --- a/src/tileset.h +++ b/src/tileset.h @@ -48,7 +48,7 @@ public: /* New methods */ int AddTile(ibox2 rect); void AddTile(ivec2 count); - int GetTileCount() const; + ptrdiff_t GetTileCount() const; ivec2 GetTileSize(int tileid) const; Texture * GetTexture(); diff --git a/test/meshviewer.cpp b/test/meshviewer.cpp index a7a2d186..90afc778 100644 --- a/test/meshviewer.cpp +++ b/test/meshviewer.cpp @@ -376,7 +376,7 @@ public: //Mesh Change #if NO_NACL_EM_INPUT - m_mesh_id = clamp(m_mesh_id + ((int)KeyPressed(KEY_MESH_PREV) - (int)KeyPressed(KEY_MESH_NEXT)), 0, m_meshes.Count() - 1); + m_mesh_id = clamp(m_mesh_id + ((int)KeyPressed(KEY_MESH_PREV) - (int)KeyPressed(KEY_MESH_NEXT)), 0, (int)m_meshes.Count() - 1); #endif //NO_NACL_EM_INPUT m_mesh_id1 = damp(m_mesh_id1, (float)m_mesh_id, .2f, seconds); diff --git a/test/unit/arraynd.cpp b/test/unit/arraynd.cpp index ba67a2f7..fe24dbf8 100644 --- a/test/unit/arraynd.cpp +++ b/test/unit/arraynd.cpp @@ -27,7 +27,7 @@ LOLUNIT_FIXTURE(ArrayNDTest) LOLUNIT_TEST(Array2D) { arraynd<2, int> a; - a.SetSize(vec_t(2, 2)); + a.SetSize(vec_t(2, 2)); /* Non-const accessors */ a[0][0] = 1; @@ -58,7 +58,7 @@ LOLUNIT_FIXTURE(ArrayNDTest) LOLUNIT_TEST(ArrayNDInit) { int const NDIM = 8; - vec_t size; + vec_t size; for (int i = 0; i < NDIM; ++i) size[i] = 5; diff --git a/test/unit/string.cpp b/test/unit/string.cpp index 73071e39..08365482 100644 --- a/test/unit/string.cpp +++ b/test/unit/string.cpp @@ -177,14 +177,14 @@ LOLUNIT_FIXTURE(StringTest) LOLUNIT_TEST(IndexOf) { String s1 = "Hello World"; - int i1 = s1.IndexOf('H'); - int i2 = s1.IndexOf('W'); - int i3 = s1.IndexOf('d'); - int i4 = s1.IndexOf("Hello"); - int i5 = s1.IndexOf("World"); - int i6 = s1.IndexOf("lo"); - int i7 = s1.IndexOf("Hello World"); - int i8 = s1.IndexOf("Sup' dude"); + ptrdiff_t i1 = s1.IndexOf('H'); + ptrdiff_t i2 = s1.IndexOf('W'); + ptrdiff_t i3 = s1.IndexOf('d'); + ptrdiff_t i4 = s1.IndexOf("Hello"); + ptrdiff_t i5 = s1.IndexOf("World"); + ptrdiff_t i6 = s1.IndexOf("lo"); + ptrdiff_t i7 = s1.IndexOf("Hello World"); + ptrdiff_t i8 = s1.IndexOf("Sup' dude"); LOLUNIT_ASSERT(i1 == 0); LOLUNIT_ASSERT(i2 == 6); @@ -199,15 +199,15 @@ LOLUNIT_FIXTURE(StringTest) LOLUNIT_TEST(LastIndexOf) { String s1 = "Hello World"; - int i1 = s1.LastIndexOf('H'); - int i2 = s1.LastIndexOf('W'); - int i3 = s1.LastIndexOf('d'); - int i4 = s1.LastIndexOf("Hello"); - int i5 = s1.LastIndexOf("World"); - int i6 = s1.LastIndexOf("lo"); - int i7 = s1.LastIndexOf("Hello World"); - int i8 = s1.LastIndexOf("Sup' dude"); - int i9 = s1.LastIndexOf('l'); + ptrdiff_t i1 = s1.LastIndexOf('H'); + ptrdiff_t i2 = s1.LastIndexOf('W'); + ptrdiff_t i3 = s1.LastIndexOf('d'); + ptrdiff_t i4 = s1.LastIndexOf("Hello"); + ptrdiff_t i5 = s1.LastIndexOf("World"); + ptrdiff_t i6 = s1.LastIndexOf("lo"); + ptrdiff_t i7 = s1.LastIndexOf("Hello World"); + ptrdiff_t i8 = s1.LastIndexOf("Sup' dude"); + ptrdiff_t i9 = s1.LastIndexOf('l'); LOLUNIT_ASSERT(i1 == 0); LOLUNIT_ASSERT(i2 == 6);