Procházet zdrojové kódy

base: add lowercase versions for most array methods.

undefined
Sam Hocevar před 10 roky
rodič
revize
72a7abbac1
2 změnil soubory, kde provedl 47 přidání a 31 odebrání
  1. +12
    -10
      src/lol/algorithm/sort.h
  2. +35
    -21
      src/lol/base/array.h

+ 12
- 10
src/lol/algorithm/sort.h Zobrazit soubor

@@ -1,12 +1,14 @@
// //
// Lol Engine
// Lol Engine
// //
// Copyright: (c) 2010-2014 Sam Hocevar <sam@hocevar.net>
// (c) 2013-2014 Benjamin "Touky" Huet <huet.benjamin@gmail.com>
// This program is free software; you can redistribute it and/or
// modify it under the terms of the Do What The Fuck You Want To
// Public License, Version 2, as published by Sam Hocevar. See
// http://www.wtfpl.net/ for more details.
// Copyright © 2010—2015 Sam Hocevar <sam@hocevar.net>
// © 2013—2015 Benjamin "Touky" Huet <huet.benjamin@gmail.com>
//
// This library is free software. It comes without any warranty, to
// the extent permitted by applicable law. You can redistribute it
// and/or modify it under the terms of the Do What the Fuck You Want
// to Public License, Version 2, as published by the WTFPL Task Force.
// See http://www.wtfpl.net/ for more details.
// //


#pragma once #pragma once
@@ -80,7 +82,7 @@ void array_base<T, ARRAY>::SortQuickSwap(ptrdiff_t start, ptrdiff_t stop)
T median = m_data[m[1]]; T median = m_data[m[1]];
ptrdiff_t i0 = start; ptrdiff_t i0 = start;
ptrdiff_t i1 = stop - 1; ptrdiff_t i1 = stop - 1;
bool swap = false;
bool b_swap = false;
while (i0 < i1) while (i0 < i1)
{ {
if (!(m_data[i0] < median) && m_data[i1] < median) if (!(m_data[i0] < median) && m_data[i1] < median)
@@ -88,7 +90,7 @@ void array_base<T, ARRAY>::SortQuickSwap(ptrdiff_t start, ptrdiff_t stop)
Swap(i0, i1); Swap(i0, i1);
i0++; i0++;
i1--; i1--;
swap = true;
b_swap = true;
} }
else else
{ {
@@ -98,7 +100,7 @@ void array_base<T, ARRAY>::SortQuickSwap(ptrdiff_t start, ptrdiff_t stop)
i1--; i1--;
} }
} }
if (stop - start == 1 || !swap)
if (stop - start == 1 || !b_swap)
return; return;
if (start < i0) if (start < i0)
SortQuickSwap(start, i0); SortQuickSwap(start, i0);


+ 35
- 21
src/lol/base/array.h Zobrazit soubor

@@ -1,12 +1,14 @@
// //
// Lol Engine
// Lol Engine
// //
// Copyright: (c) 2010-2015 Sam Hocevar <sam@hocevar.net>
// (c) 2013-2014 Benjamin "Touky" Huet <huet.benjamin@gmail.com>
// This program is free software; you can redistribute it and/or
// modify it under the terms of the Do What The Fuck You Want To
// Public License, Version 2, as published by Sam Hocevar. See
// http://www.wtfpl.net/ for more details.
// Copyright © 2010—2015 Sam Hocevar <sam@hocevar.net>
// © 2013—2015 Benjamin "Touky" Huet <huet.benjamin@gmail.com>
//
// This library is free software. It comes without any warranty, to
// the extent permitted by applicable law. You can redistribute it
// and/or modify it under the terms of the Do What the Fuck You Want
// to Public License, Version 2, as published by the WTFPL Task Force.
// See http://www.wtfpl.net/ for more details.
// //


#pragma once #pragma once
@@ -54,7 +56,7 @@ public:
{ {
Reserve(list.size()); Reserve(list.size());
for (auto elem : list) for (auto elem : list)
Push(elem);
push(elem);
} }


inline ~array_base() inline ~array_base()
@@ -191,17 +193,17 @@ public:
return *this; return *this;
} }


inline void Push(T const &x)
inline void push(T const &x)
{ {
*this << x; *this << x;
} }


inline bool PushUnique(T const &x)
inline bool push_unique(T const &x)
{ {
if (Find(x) != INDEX_NONE) if (Find(x) != INDEX_NONE)
return false; return false;


Push(x);
push(x);
return true; return true;
} }


@@ -249,7 +251,7 @@ public:
ptrdiff_t idx = Find(x); ptrdiff_t idx = Find(x);
if (idx != INDEX_NONE) if (idx != INDEX_NONE)
{ {
Remove(idx);
remove(idx);
return true; return true;
} }
return false; return false;
@@ -272,21 +274,21 @@ public:
ptrdiff_t idx2 = Find(x2); ptrdiff_t idx2 = Find(x2);
if (idx1 != INDEX_NONE && idx2 != INDEX_NONE) if (idx1 != INDEX_NONE && idx2 != INDEX_NONE)
{ {
Swap(idx1, idx2);
swap(idx1, idx2);
return true; return true;
} }
return false; return false;
} }


inline T Pop()
inline T pop()
{ {
ASSERT(m_count > 0); ASSERT(m_count > 0);
element_t tmp = Last(); element_t tmp = Last();
Remove(m_count - 1, 1);
remove(m_count - 1, 1);
return tmp; return tmp;
} }


void Swap(ptrdiff_t pos1, ptrdiff_t pos2)
void swap(ptrdiff_t pos1, ptrdiff_t pos2)
{ {
ASSERT(pos1 >= 0 && pos1 <= m_count, ASSERT(pos1 >= 0 && pos1 <= m_count,
"cannot swap index %lld in array of size %lld", "cannot swap index %lld in array of size %lld",
@@ -299,7 +301,7 @@ public:
std::swap((*this)[pos1], (*this)[pos2]); std::swap((*this)[pos1], (*this)[pos2]);
} }


void Remove(ptrdiff_t pos, ptrdiff_t todelete = 1)
void remove(ptrdiff_t pos, ptrdiff_t todelete = 1)
{ {
ASSERT(todelete >= 0); ASSERT(todelete >= 0);
ASSERT(pos - todelete >= -m_count - 1 && pos + todelete <= m_count, ASSERT(pos - todelete >= -m_count - 1 && pos + todelete <= m_count,
@@ -353,9 +355,9 @@ public:
m_count = count; m_count = count;
} }


inline void Empty()
inline void empty()
{ {
Remove(0, m_count);
remove(0, m_count);
} }


void Reserve(ptrdiff_t toreserve) void Reserve(ptrdiff_t toreserve)
@@ -384,6 +386,14 @@ public:
void Sort(int sort); void Sort(int sort);
void SortQuickSwap(ptrdiff_t start, ptrdiff_t stop); void SortQuickSwap(ptrdiff_t start, ptrdiff_t stop);


/* TODO: remove these legacy functions one day */
inline void Push(T const &x) { push(x); }
inline bool PushUnique(T const &x) { return push_unique(x); }
inline T Pop() { return pop(); }
inline void Swap(ptrdiff_t pos1, ptrdiff_t pos2) { return swap(pos1, pos2); }
inline void Remove(ptrdiff_t pos, ptrdiff_t todelete = 1) { return remove(pos, todelete); }
inline void Empty() { empty(); }

/* Support C++11 range-based for loops */ /* Support C++11 range-based for loops */
class ConstIterator class ConstIterator
{ {
@@ -480,7 +490,7 @@ public:
#endif #endif


public: public:
inline void Push(T... args)
inline void push(T... args)
{ {
if (this->m_count >= this->m_reserved) if (this->m_count >= this->m_reserved)
{ {
@@ -495,7 +505,7 @@ public:
++this->m_count; ++this->m_count;
} }


inline void Insert(ptrdiff_t pos, T... args)
inline void insert(ptrdiff_t pos, T... args)
{ {
ASSERT(pos >= 0 && pos <= this->m_count, ASSERT(pos >= 0 && pos <= this->m_count,
"cannot insert at index %lld in array of size %lld", "cannot insert at index %lld in array of size %lld",
@@ -512,6 +522,10 @@ public:
new (&this->m_data[pos]) tuple<T...>({ args... }); new (&this->m_data[pos]) tuple<T...>({ args... });
++this->m_count; ++this->m_count;
} }

/* TODO: remove these legacy functions one day */
inline void Push(T... args) { push(args...); }
inline void Insert(ptrdiff_t pos, T... args) { insert(pos, args...); }
}; };


template<typename T> template<typename T>


Načítá se…
Zrušit
Uložit