#define Heap_h
#include "Vec.h"
+#include <algorithm>
#include "string.h"
#include <limits>
Heap(const Comp& c) : lt(c) { }
Heap(const Heap<Comp>& other) : lt(other.lt) {
heap.growTo(other.heap.size());
- memcpy(heap.getData(), other.heap.getData(), sizeof(uint32_t)*other.heap.size());
+ std::copy(other.heap.getData(), other.heap.getDataEnd(), heap.getData());
indices.growTo(other.indices.size());
- memcpy(indices.getData(), other.indices.getData(), sizeof(uint32_t)*other.indices.size());
+ std::copy(other.indices.getData(), other.indices.getDataEnd(), indices.getData());
}
void operator=(const Heap<Comp>& other)
if (other.heap.size() > heap.size())
heap.growTo(other.heap.size());
else
- heap.shrink(other.heap.size()-heap.size());
- memcpy(heap.getData(), other.heap.getData(), heap.size()*sizeof(uint32_t));
+ heap.shrink(heap.size()-other.heap.size());
+ std::copy(other.heap.getData(), other.heap.getDataEnd(), heap.getData());
if (other.indices.size() > indices.size())
indices.growTo(other.indices.size());
else
- indices.shrink(other.indices.size()-indices.size());
- memcpy(indices.getData(), other.indices.getData(), indices.size()*sizeof(uint32_t));
+ indices.shrink(indices.size() - other.indices.size());
+ std::copy(other.indices.getData(), other.indices.getDataEnd(), indices.getData());
}
uint32_t size () const { return heap.size(); }
void capacity (uint32_t size) { grow(size); }
// Stack interface:
- void reserve(uint32_t res) { grow(res);}
+ void reserve(uint32_t res) { if (cap < res) {cap = res; data = (T*)realloc(data, cap * sizeof(T));}}
void push (void) { if (sz == cap) { cap = imax(2, (cap*3+1)>>1); data = (T*)realloc(data, cap * sizeof(T)); } new (&data[sz]) T(); sz++; }
void push (const T& elem) { if (sz == cap) { cap = imax(2, (cap*3+1)>>1); data = (T*)realloc(data, cap * sizeof(T)); } data[sz++] = elem; }
void push_ (const T& elem) { assert(sz < cap); data[sz++] = elem; }