2011-04-27 11:58:34 +00:00
|
|
|
/*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Copyright 2011-2013 Blender Foundation
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
2014-12-25 01:50:24 +00:00
|
|
|
* limitations under the License.
|
2011-04-27 11:58:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __UTIL_VECTOR_H__
|
|
|
|
#define __UTIL_VECTOR_H__
|
|
|
|
|
|
|
|
/* Vector */
|
|
|
|
|
2015-06-28 09:18:48 +00:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstring>
|
2011-04-27 11:58:34 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2015-02-14 12:29:47 +00:00
|
|
|
#include "util_aligned_malloc.h"
|
2016-02-06 22:40:41 +00:00
|
|
|
#include "util_guarded_allocator.h"
|
2013-06-22 14:35:09 +00:00
|
|
|
#include "util_types.h"
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
2015-02-14 13:03:39 +00:00
|
|
|
/* Vector
|
|
|
|
*
|
|
|
|
* Own subclass-ed vestion of std::vector. Subclass is needed because:
|
|
|
|
*
|
2016-02-06 22:40:41 +00:00
|
|
|
* - Use own allocator which keeps track of used/peak memory.
|
2015-02-14 13:03:39 +00:00
|
|
|
*
|
|
|
|
* - Have method to ensure capacity is re-set to 0.
|
|
|
|
*/
|
2015-06-26 20:36:31 +00:00
|
|
|
template<typename value_type,
|
2016-02-06 22:40:41 +00:00
|
|
|
typename allocator_type = GuardedAllocator<value_type> >
|
2015-06-26 20:36:31 +00:00
|
|
|
class vector : public std::vector<value_type, allocator_type>
|
2015-02-14 13:03:39 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/* Default constructor. */
|
|
|
|
explicit vector() : std::vector<value_type, allocator_type>() { }
|
|
|
|
|
|
|
|
/* Fill constructor. */
|
|
|
|
explicit vector(size_t n, const value_type& val = value_type())
|
|
|
|
: std::vector<value_type, allocator_type>(n, val) { }
|
|
|
|
|
|
|
|
/* Range constructor. */
|
|
|
|
template <class InputIterator>
|
2015-06-28 08:51:49 +00:00
|
|
|
vector(InputIterator first, InputIterator last)
|
2015-02-14 13:03:39 +00:00
|
|
|
: std::vector<value_type, allocator_type>(first, last) { }
|
|
|
|
|
|
|
|
/* Copy constructor. */
|
|
|
|
vector(const vector &x) : std::vector<value_type, allocator_type>(x) { }
|
|
|
|
|
|
|
|
void shrink_to_fit(void)
|
|
|
|
{
|
2015-02-16 10:38:13 +00:00
|
|
|
#if __cplusplus < 201103L
|
2015-02-14 13:03:39 +00:00
|
|
|
vector<value_type>().swap(*this);
|
2015-02-16 10:38:13 +00:00
|
|
|
#else
|
|
|
|
std::vector<value_type, allocator_type>::shrink_to_fit();
|
2015-02-14 13:03:39 +00:00
|
|
|
#endif
|
2015-02-16 10:38:13 +00:00
|
|
|
}
|
2015-02-14 13:03:39 +00:00
|
|
|
|
2015-06-26 20:36:31 +00:00
|
|
|
void free_memory(void)
|
|
|
|
{
|
2015-02-14 15:49:20 +00:00
|
|
|
std::vector<value_type, allocator_type>::resize(0);
|
|
|
|
shrink_to_fit();
|
|
|
|
}
|
|
|
|
|
2015-02-14 13:03:39 +00:00
|
|
|
/* Some external API might demand working with std::vector. */
|
|
|
|
operator std::vector<value_type>()
|
|
|
|
{
|
2016-02-16 12:37:05 +00:00
|
|
|
return std::vector<value_type>(this->begin(), this->end());
|
2015-02-14 13:03:39 +00:00
|
|
|
}
|
|
|
|
};
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
/* Array
|
|
|
|
*
|
2013-06-22 14:35:09 +00:00
|
|
|
* Simplified version of vector, serving multiple purposes:
|
2011-04-27 11:58:34 +00:00
|
|
|
* - somewhat faster in that it does not clear memory on resize/alloc,
|
2013-06-22 14:35:09 +00:00
|
|
|
* this was actually showing up in profiles quite significantly. it
|
|
|
|
* also does not run any constructors/destructors
|
|
|
|
* - if this is used, we are not tempted to use inefficient operations
|
|
|
|
* - aligned allocation for SSE data types */
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2013-06-22 14:35:09 +00:00
|
|
|
template<typename T, size_t alignment = 16>
|
2011-04-27 11:58:34 +00:00
|
|
|
class array
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
array()
|
2016-02-22 15:40:32 +00:00
|
|
|
: data_(NULL),
|
|
|
|
datasize_(0),
|
|
|
|
capacity_(0)
|
|
|
|
{}
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2016-05-11 14:50:10 +00:00
|
|
|
explicit array(size_t newsize)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
if(newsize == 0) {
|
2016-02-22 15:40:32 +00:00
|
|
|
data_ = NULL;
|
|
|
|
datasize_ = 0;
|
|
|
|
capacity_ = 0;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
else {
|
2016-02-22 15:40:32 +00:00
|
|
|
data_ = mem_allocate(newsize);
|
|
|
|
datasize_ = newsize;
|
|
|
|
capacity_ = datasize_;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
array(const array& from)
|
|
|
|
{
|
2016-05-07 17:47:08 +00:00
|
|
|
if(from.datasize_ == 0) {
|
2016-02-22 15:40:32 +00:00
|
|
|
data_ = NULL;
|
|
|
|
datasize_ = 0;
|
|
|
|
capacity_ = 0;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
else {
|
2016-05-07 17:47:08 +00:00
|
|
|
data_ = mem_allocate(from.datasize_);
|
|
|
|
memcpy(data_, from.data_, from.datasize_*sizeof(T));
|
2016-02-22 15:40:32 +00:00
|
|
|
datasize_ = from.datasize_;
|
|
|
|
capacity_ = datasize_;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
2016-05-07 17:47:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
array& operator=(const array& from)
|
|
|
|
{
|
|
|
|
if(this != &from) {
|
2016-05-20 08:56:10 +00:00
|
|
|
resize(from.size());
|
|
|
|
memcpy(data_, from.data_, datasize_*sizeof(T));
|
2016-05-07 17:47:08 +00:00
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
array& operator=(const vector<T>& from)
|
|
|
|
{
|
2016-05-20 08:56:10 +00:00
|
|
|
resize(from.size());
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2016-05-07 17:47:08 +00:00
|
|
|
if(from.size() > 0) {
|
2016-02-22 15:40:32 +00:00
|
|
|
memcpy(data_, &from[0], datasize_*sizeof(T));
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
~array()
|
|
|
|
{
|
2016-02-22 15:40:32 +00:00
|
|
|
mem_free(data_, capacity_);
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
2016-05-07 17:47:37 +00:00
|
|
|
bool operator==(const array<T>& other) const
|
2016-05-07 17:47:08 +00:00
|
|
|
{
|
2016-05-20 08:56:10 +00:00
|
|
|
if(datasize_ != other.datasize_) {
|
2016-05-07 17:47:08 +00:00
|
|
|
return false;
|
2016-05-20 08:56:10 +00:00
|
|
|
}
|
2016-05-07 17:47:08 +00:00
|
|
|
|
|
|
|
return memcmp(data_, other.data_, datasize_*sizeof(T)) == 0;
|
|
|
|
}
|
|
|
|
|
2016-05-07 17:47:37 +00:00
|
|
|
void steal_data(array& from)
|
|
|
|
{
|
|
|
|
if(this != &from) {
|
|
|
|
clear();
|
|
|
|
|
|
|
|
data_ = from.data_;
|
|
|
|
datasize_ = from.datasize_;
|
|
|
|
capacity_ = from.capacity_;
|
|
|
|
|
|
|
|
from.data_ = NULL;
|
|
|
|
from.datasize_ = 0;
|
|
|
|
from.capacity_ = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-04 07:38:10 +00:00
|
|
|
T* resize(size_t newsize)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
if(newsize == 0) {
|
|
|
|
clear();
|
|
|
|
}
|
2016-05-20 08:56:10 +00:00
|
|
|
else if(newsize != datasize_) {
|
|
|
|
if(newsize > capacity_) {
|
|
|
|
T *newdata = mem_allocate(newsize);
|
|
|
|
if(newdata == NULL) {
|
|
|
|
/* Allocation failed, likely out of memory. */
|
|
|
|
clear();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else if(data_ != NULL) {
|
|
|
|
memcpy(newdata, data_, ((datasize_ < newsize)? datasize_: newsize)*sizeof(T));
|
|
|
|
mem_free(data_, capacity_);
|
|
|
|
}
|
|
|
|
data_ = newdata;
|
|
|
|
capacity_ = newsize;
|
2014-06-06 14:08:40 +00:00
|
|
|
}
|
2016-02-22 15:40:32 +00:00
|
|
|
datasize_ = newsize;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
2016-02-22 15:40:32 +00:00
|
|
|
return data_;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
{
|
2016-02-22 15:40:32 +00:00
|
|
|
if(data_ != NULL) {
|
|
|
|
mem_free(data_, capacity_);
|
|
|
|
data_ = NULL;
|
2015-09-04 07:38:10 +00:00
|
|
|
}
|
2016-02-22 15:40:32 +00:00
|
|
|
datasize_ = 0;
|
|
|
|
capacity_ = 0;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
2016-05-07 17:47:08 +00:00
|
|
|
size_t empty() const
|
|
|
|
{
|
|
|
|
return datasize_ == 0;
|
|
|
|
}
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
size_t size() const
|
|
|
|
{
|
2016-02-22 15:40:32 +00:00
|
|
|
return datasize_;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
2016-07-17 02:57:06 +00:00
|
|
|
T* data()
|
|
|
|
{
|
|
|
|
return data_;
|
|
|
|
}
|
|
|
|
|
2016-05-07 17:47:08 +00:00
|
|
|
const T* data() const
|
|
|
|
{
|
|
|
|
return data_;
|
|
|
|
}
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
T& operator[](size_t i) const
|
|
|
|
{
|
2016-02-22 15:40:32 +00:00
|
|
|
assert(i < datasize_);
|
|
|
|
return data_[i];
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
2016-05-07 17:47:08 +00:00
|
|
|
void reserve(size_t newcapacity)
|
|
|
|
{
|
2016-02-22 15:40:32 +00:00
|
|
|
if(newcapacity > capacity_) {
|
2016-03-30 08:46:56 +00:00
|
|
|
T *newdata = mem_allocate(newcapacity);
|
2016-02-22 15:40:32 +00:00
|
|
|
if(data_ != NULL) {
|
|
|
|
memcpy(newdata, data_, ((datasize_ < newcapacity)? datasize_: newcapacity)*sizeof(T));
|
|
|
|
mem_free(data_, capacity_);
|
2015-08-23 13:50:31 +00:00
|
|
|
}
|
2016-02-22 15:40:32 +00:00
|
|
|
data_ = newdata;
|
|
|
|
capacity_ = newcapacity;
|
2015-08-23 13:50:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-07 17:47:08 +00:00
|
|
|
size_t capacity() const
|
|
|
|
{
|
2016-02-22 15:40:32 +00:00
|
|
|
return capacity_;
|
|
|
|
}
|
|
|
|
|
2016-05-07 17:47:08 +00:00
|
|
|
// do not use this method unless you are sure the code is not performance critical
|
|
|
|
void push_back_slow(const T& t)
|
|
|
|
{
|
2016-05-17 20:08:34 +00:00
|
|
|
if(capacity_ == datasize_)
|
2016-05-07 17:47:08 +00:00
|
|
|
{
|
|
|
|
reserve(datasize_ == 0 ? 1 : (size_t)((datasize_ + 1) * 1.2));
|
|
|
|
}
|
|
|
|
|
|
|
|
data_[datasize_++] = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
void push_back_reserved(const T& t)
|
|
|
|
{
|
|
|
|
assert(datasize_ < capacity_);
|
|
|
|
push_back_slow(t);
|
|
|
|
}
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
protected:
|
2016-03-30 08:46:56 +00:00
|
|
|
inline T* mem_allocate(size_t N)
|
|
|
|
{
|
2016-04-20 13:49:52 +00:00
|
|
|
if(N == 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-03-30 08:46:56 +00:00
|
|
|
T *mem = (T*)util_aligned_malloc(sizeof(T)*N, alignment);
|
|
|
|
if(mem != NULL) {
|
|
|
|
util_guarded_mem_alloc(sizeof(T)*N);
|
|
|
|
}
|
2016-04-20 13:49:52 +00:00
|
|
|
else {
|
|
|
|
throw std::bad_alloc();
|
|
|
|
}
|
2016-03-30 08:46:56 +00:00
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void mem_free(T *mem, size_t N)
|
|
|
|
{
|
|
|
|
if(mem != NULL) {
|
|
|
|
util_guarded_mem_free(sizeof(T)*N);
|
|
|
|
util_aligned_free(mem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-22 15:40:32 +00:00
|
|
|
T *data_;
|
|
|
|
size_t datasize_;
|
|
|
|
size_t capacity_;
|
2011-04-27 11:58:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
CCL_NAMESPACE_END
|
|
|
|
|
|
|
|
#endif /* __UTIL_VECTOR_H__ */
|
|
|
|
|