2015-12-07 23:27:53 +03:00
|
|
|
/*
|
|
|
|
* Vector.h
|
|
|
|
*
|
|
|
|
* Created on: 7 Dec 2015
|
|
|
|
* Author: hieu
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
2015-12-10 08:49:51 +03:00
|
|
|
#include <cassert>
|
2016-02-25 16:26:31 +03:00
|
|
|
#include <boost/functional/hash.hpp>
|
2015-12-07 23:27:53 +03:00
|
|
|
#include "MemPool.h"
|
|
|
|
|
2015-12-10 23:49:30 +03:00
|
|
|
namespace Moses2
|
|
|
|
{
|
|
|
|
|
2016-03-31 23:00:16 +03:00
|
|
|
template<typename T>
|
|
|
|
class Vector: public std::vector<T, MemPoolAllocator<T> >
|
2016-01-12 15:10:56 +03:00
|
|
|
{
|
2016-03-30 14:20:20 +03:00
|
|
|
typedef std::vector<T, MemPoolAllocator<T> > Parent;
|
2015-12-10 08:49:51 +03:00
|
|
|
|
2016-01-12 15:10:56 +03:00
|
|
|
public:
|
2016-03-31 23:00:16 +03:00
|
|
|
Vector(MemPool &pool, size_t size = 0, const T &val = T()) :
|
|
|
|
Parent(size, val, MemPoolAllocator<T>(pool))
|
2015-12-11 00:21:52 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-12-07 23:27:53 +03:00
|
|
|
protected:
|
|
|
|
};
|
|
|
|
|
2016-02-25 16:26:31 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
2016-03-31 23:00:16 +03:00
|
|
|
template<typename T>
|
2016-02-25 16:26:31 +03:00
|
|
|
class Array
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef T* iterator;
|
|
|
|
typedef const T* const_iterator;
|
|
|
|
//! iterators
|
2016-03-31 23:00:16 +03:00
|
|
|
const_iterator begin() const
|
|
|
|
{
|
|
|
|
return m_arr;
|
2016-02-25 16:26:31 +03:00
|
|
|
}
|
2016-03-31 23:00:16 +03:00
|
|
|
const_iterator end() const
|
|
|
|
{
|
|
|
|
return m_arr + m_size;
|
2016-02-25 16:26:31 +03:00
|
|
|
}
|
|
|
|
|
2016-03-31 23:00:16 +03:00
|
|
|
iterator begin()
|
|
|
|
{
|
|
|
|
return m_arr;
|
2016-02-25 16:26:31 +03:00
|
|
|
}
|
2016-03-31 23:00:16 +03:00
|
|
|
iterator end()
|
|
|
|
{
|
|
|
|
return m_arr + m_size;
|
2016-02-25 16:26:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Array(MemPool &pool, size_t size = 0, const T &val = T())
|
|
|
|
{
|
2016-03-31 23:00:16 +03:00
|
|
|
m_size = size;
|
|
|
|
m_maxSize = size;
|
|
|
|
m_arr = pool.Allocate<T>(size);
|
|
|
|
for (size_t i = 0; i < size; ++i) {
|
|
|
|
m_arr[i] = val;
|
|
|
|
}
|
2016-02-25 16:26:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t size() const
|
2016-03-31 23:00:16 +03:00
|
|
|
{
|
|
|
|
return m_size;
|
|
|
|
}
|
2016-02-25 16:26:31 +03:00
|
|
|
|
2016-03-31 23:00:16 +03:00
|
|
|
const T& operator[](size_t ind) const
|
|
|
|
{
|
|
|
|
return m_arr[ind];
|
2016-02-25 16:26:31 +03:00
|
|
|
}
|
|
|
|
|
2016-03-31 23:00:16 +03:00
|
|
|
T& operator[](size_t ind)
|
|
|
|
{
|
|
|
|
return m_arr[ind];
|
2016-02-25 16:26:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t hash() const
|
|
|
|
{
|
2016-03-31 23:00:16 +03:00
|
|
|
size_t seed = 0;
|
|
|
|
for (size_t i = 0; i < m_size; ++i) {
|
|
|
|
boost::hash_combine(seed, m_arr[i]);
|
|
|
|
}
|
|
|
|
return seed;
|
2016-02-25 16:26:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int Compare(const Array &compare) const
|
|
|
|
{
|
|
|
|
|
2016-03-31 23:00:16 +03:00
|
|
|
int cmp = memcmp(m_arr, compare.m_arr, sizeof(T) * m_size);
|
|
|
|
return cmp;
|
2016-02-25 16:26:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const Array &compare) const
|
|
|
|
{
|
2016-03-31 23:00:16 +03:00
|
|
|
int cmp = Compare(compare);
|
|
|
|
return cmp == 0;
|
2016-02-25 16:26:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void resize(size_t newSize)
|
|
|
|
{
|
2016-03-31 23:00:16 +03:00
|
|
|
assert(m_size < m_maxSize);
|
|
|
|
m_size = newSize;
|
2016-02-25 16:26:31 +03:00
|
|
|
}
|
|
|
|
protected:
|
|
|
|
size_t m_size, m_maxSize;
|
|
|
|
T *m_arr;
|
|
|
|
};
|
|
|
|
|
2015-12-10 23:49:30 +03:00
|
|
|
}
|
|
|
|
|