Add assign to vector and static_vector
This commit is contained in:
parent
9dc8489693
commit
c05d7e47f4
|
@ -15,6 +15,9 @@ rem @echo off
|
||||||
@call :testh opp_vector.cpp
|
@call :testh opp_vector.cpp
|
||||||
@if %errorlevel% neq 0 goto :error
|
@if %errorlevel% neq 0 goto :error
|
||||||
|
|
||||||
|
@call :testh opp_static_vector.cpp
|
||||||
|
@if %errorlevel% neq 0 goto :error
|
||||||
|
|
||||||
@call :testh opp_vector_string.cpp
|
@call :testh opp_vector_string.cpp
|
||||||
@if %errorlevel% neq 0 goto :error
|
@if %errorlevel% neq 0 goto :error
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,78 @@
|
||||||
|
#include <opp/static_vector.h>
|
||||||
|
#include <opp/algorithm.h>
|
||||||
|
#include <assert.h>
|
||||||
|
#include <opp/iostream.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
opp::static_vector<int, 20> a;
|
||||||
|
|
||||||
|
for(int i=0; i<10; i++)
|
||||||
|
a.push_back(i);
|
||||||
|
|
||||||
|
int s = 0;
|
||||||
|
for(int i=0; i<a.size(); i++)
|
||||||
|
s += a[i];
|
||||||
|
|
||||||
|
assert(s == 45);
|
||||||
|
|
||||||
|
for(int i=0; i<5; i++)
|
||||||
|
a.erase(i);
|
||||||
|
|
||||||
|
s = 0;
|
||||||
|
for(int i=0; i<a.size(); i++)
|
||||||
|
s += a[i];
|
||||||
|
|
||||||
|
assert(s == 1 + 3 + 5 + 7 + 9);
|
||||||
|
|
||||||
|
opp::static_vector<int, 100> v;
|
||||||
|
|
||||||
|
for(int i=0; i<10; i++)
|
||||||
|
v.push_back(i);
|
||||||
|
|
||||||
|
assert(v.size() == 10);
|
||||||
|
v.insert(0, 20);
|
||||||
|
assert(v.size() == 11);
|
||||||
|
v.insert(6, 21);
|
||||||
|
assert(v.size() == 12);
|
||||||
|
v.insert(12, 22);
|
||||||
|
|
||||||
|
int * fi = opp::find(v.begin(), v.end(), 21);
|
||||||
|
|
||||||
|
fi = v.insert(fi, 30);
|
||||||
|
fi = v.insert(fi, 31);
|
||||||
|
fi = v.insert(fi, 32);
|
||||||
|
|
||||||
|
assert(v.size() == 16);
|
||||||
|
assert(v[0] == 20);
|
||||||
|
assert(v[15] == 22);
|
||||||
|
assert(v[8] == 32);
|
||||||
|
|
||||||
|
fi = opp::find(v.begin(), v.end(), 32);
|
||||||
|
|
||||||
|
for(int i=0; i<30; i++)
|
||||||
|
{
|
||||||
|
fi = v.insert(fi, i + 40);
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(v.size() == 46);
|
||||||
|
assert(v[28] == 60);
|
||||||
|
|
||||||
|
v.erase(10, 10);
|
||||||
|
|
||||||
|
for(int i : v)
|
||||||
|
opp::cout << i << ", ";
|
||||||
|
opp::cout << "\n";
|
||||||
|
|
||||||
|
assert(v.size() == 36);
|
||||||
|
assert(v[18] == 60);
|
||||||
|
|
||||||
|
v.assign(42, 11);
|
||||||
|
|
||||||
|
assert(v.size() == 42);
|
||||||
|
assert(v[0] == 11);
|
||||||
|
assert(v[15] == 11);
|
||||||
|
assert(v[41] == 11);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -64,5 +64,15 @@ int main(void)
|
||||||
opp::cout << i << ", ";
|
opp::cout << i << ", ";
|
||||||
opp::cout << "\n";
|
opp::cout << "\n";
|
||||||
|
|
||||||
|
assert(v.size() == 36);
|
||||||
|
assert(v[18] == 60);
|
||||||
|
|
||||||
|
v.assign(42, 11);
|
||||||
|
|
||||||
|
assert(v.size() == 42);
|
||||||
|
assert(v[0] == 11);
|
||||||
|
assert(v[15] == 11);
|
||||||
|
assert(v[41] == 11);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -173,6 +173,8 @@ public:
|
||||||
((T*)_space)[_size].~T();
|
((T*)_space)[_size].~T();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void assign(size_t count, const T & t);
|
||||||
|
|
||||||
void insert(size_t at, const T & t);
|
void insert(size_t at, const T & t);
|
||||||
|
|
||||||
void erase(size_t at, size_t n = 1);
|
void erase(size_t at, size_t n = 1);
|
||||||
|
@ -229,6 +231,17 @@ void static_vector<T, N>::emplace_back(const P&... p)
|
||||||
new ((T*)_space + _size++)T(p...);
|
new ((T*)_space + _size++)T(p...);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <class T, int N>
|
||||||
|
void static_vector<T, N>::assign(size_t count, const T & t)
|
||||||
|
{
|
||||||
|
T * data = (T*)_space;
|
||||||
|
for(size_t i=0; i<_size; i++)
|
||||||
|
data[i].~T();
|
||||||
|
for(size_t i=0; i<count; i++)
|
||||||
|
new (data + i)T(t);
|
||||||
|
_size = count;
|
||||||
|
}
|
||||||
|
|
||||||
template <class T, int N>
|
template <class T, int N>
|
||||||
void static_vector<T, N>::insert(size_t at, const T & t)
|
void static_vector<T, N>::insert(size_t at, const T & t)
|
||||||
{
|
{
|
||||||
|
|
|
@ -192,6 +192,8 @@ public:
|
||||||
_data[_size].~T();
|
_data[_size].~T();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void assign(size_t count, const T & t);
|
||||||
|
|
||||||
void insert(size_t at, const T & t);
|
void insert(size_t at, const T & t);
|
||||||
|
|
||||||
void erase(size_t at, size_t n = 1);
|
void erase(size_t at, size_t n = 1);
|
||||||
|
@ -296,6 +298,21 @@ void vector<T>::emplace_back(const P&... p)
|
||||||
new (add_back())T(p...);
|
new (add_back())T(p...);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
void vector<T>::assign(size_t count, const T & t)
|
||||||
|
{
|
||||||
|
for(size_t i=0; i<_size; i++)
|
||||||
|
_data[i].~T();
|
||||||
|
if (count > _capacity)
|
||||||
|
{
|
||||||
|
_size = 0;
|
||||||
|
reserve(count);
|
||||||
|
}
|
||||||
|
for(size_t i=0; i<count; i++)
|
||||||
|
new (_data + i)T(t);
|
||||||
|
_size = count;
|
||||||
|
}
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
void vector<T>::insert(size_t at, const T & t)
|
void vector<T>::insert(size_t at, const T & t)
|
||||||
{
|
{
|
||||||
|
|
|
@ -11271,7 +11271,10 @@ Declaration* Parser::ParseTemplateExpansion(Declaration* tmpld, Declaration* exp
|
||||||
if (mindist == 0)
|
if (mindist == 0)
|
||||||
return etdec->mBase;
|
return etdec->mBase;
|
||||||
else if (mindist == NOOVERLOAD)
|
else if (mindist == NOOVERLOAD)
|
||||||
mErrors->Error(tdec->mLocation, EERR_TEMPLATE_PARAMS, "No matching template parameters");
|
{
|
||||||
|
mErrors->Error(FullLocation(tmpld->mLocation), EERR_TEMPLATE_PARAMS, "No matching template parameters", tmpld->mQualIdent);
|
||||||
|
return tdec;
|
||||||
|
}
|
||||||
else
|
else
|
||||||
tmpld = etdec;
|
tmpld = etdec;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue