LRU Cache C++ implementation issue - c++

I was doing an exercise in an online judge:
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.
get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
set(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.
I basically use std::list and std::unordered_map and works good in small input case. But OJ give a Time Limit Exceeded on the input: cache size is 2048 and 20000+ get & set operations.
Time Limit Exceeded version:
class LRUCache {
public:
LRUCache(int capacity):cacheSize(capacity) {
}
int get(int key) {
auto it = mapping.find(key);
if(it == mapping.end())
return -1;
itemList.splice(itemList.begin(),itemList,it->second);
//mapping[key] == it->second still holds
return it->second->second;
}
void set(int key, int value) {
auto it = mapping.find(key);
if(it != mapping.end()) {
itemList.splice(itemList.begin(),itemList,it->second);
it->second->second = value;
} else {
itemList.push_front(make_pair(key,value));
mapping.insert(make_pair(key,itemList.begin()));
}
if(itemList.size() > cacheSize) {
mapping.erase(itemList.back().first);
itemList.pop_back();
}
}
private:
int cacheSize;
list<pair<int,int> > itemList;
unordered_map<int,list<pair<int,int> >::iterator> mapping;
};
Then I thought why not erase element before insert one, so I modify set function and OJ accept!
Accept version:
void set(int key, int value) {
auto it = mapping.find(key);
if(it != mapping.end()) {
itemList.splice(itemList.begin(),itemList,it->second);
it->second->second = value;
} else {
if(itemList.size() == cacheSize) {
mapping.erase(itemList.back().first);
itemList.pop_back();
}
itemList.push_front(make_pair(key,value));
mapping.insert(make_pair(key,itemList.begin()));
}
}
I wonder what makes such a different?

The reason is that the OJ you use uses a C++ compiler which has std::list::size with linear complexity. In C++11 they require it to be constant, but in C++98 it could be up to linear, and many implementations actually have it linear.
See complexity here on the C++98 tab: http://www.cplusplus.com/reference/list/list/size/
I located the OJ that you were using, and managed to get TLE with your code, but managed to get it accepted with a small modification, which just tracks the size of the list instead of calling size()
class LRUCache {
public:
LRUCache(int capacity):cacheSize(capacity) {
listSize = 0;
}
int get(int key) {
auto it = mapping.find(key);
if(it == mapping.end())
return -1;
itemList.splice(itemList.begin(),itemList,it->second);
//mapping[key] == it->second still holds
return it->second->second;
}
void set(int key, int value) {
auto it = mapping.find(key);
if(it != mapping.end()) {
itemList.splice(itemList.begin(),itemList,it->second);
it->second->second = value;
} else {
itemList.push_front(make_pair(key,value));
++ listSize;
mapping.insert(make_pair(key,itemList.begin()));
}
if(listSize > cacheSize) {
mapping.erase(itemList.back().first);
-- listSize;
itemList.pop_back();
}
}
private:
int cacheSize;
int listSize;
list<pair<int,int> > itemList;
unordered_map<int,list<pair<int,int> >::iterator> mapping;
};

Related

Deleting data while looping through a list

I'm having trouble figuring out how to delete an item from a list.
Please note that I would like to perform the deletion from the advance() function. This code is just boiled down from my actual project, to try to isolate the error.
#include <iostream>
#include <list>
#include <iterator>
#include <algorithm>
using namespace std;
const int SCT_OSC_FILLED = 11;
class OrderInfo {
private:
std::string id;
public:
OrderInfo(std::string a, int aStatusCode);
std::string key();
int statusCode;
};
OrderInfo::OrderInfo(std::string a, int aStatusCode) {
id = a;
statusCode = aStatusCode;
}
std::string OrderInfo::key() {
return id;
}
std::list <OrderInfo> MasterOrders;
void testList();
void add(OrderInfo ordInfo);
void advance(OrderInfo ordInfo, std::list <OrderInfo> ::iterator& orderIter);
void testList() {
OrderInfo o1("1", 15);
OrderInfo o2("2", 16);
OrderInfo o3("3", SCT_OSC_FILLED);
OrderInfo o4("4", 17);
OrderInfo o5("5", SCT_OSC_FILLED);
OrderInfo o6("6", 18);
add(o1);
add(o1);
add(o2);
add(o3);
add(o4);
add(o5);
add(o6);
for (auto v : MasterOrders)
std::cout << v.key() << "\n";
}
void add(OrderInfo ordInfo) {
// Add to MasterOrders (if not already in list)
bool alreadyInList = false;
std::list <OrderInfo> ::iterator orderIter = MasterOrders.begin();
while (orderIter != MasterOrders.end())
{
OrderInfo oi = *orderIter;
alreadyInList = ordInfo.key() == oi.key();
if (alreadyInList) break;
advance(ordInfo, orderIter);
}
if (!alreadyInList) MasterOrders.push_front(ordInfo);
}
void advance(OrderInfo ordInfo, std::list <OrderInfo> ::iterator& orderIter) {
bool iterate = true;
if (ordInfo.statusCode == SCT_OSC_FILLED) {
orderIter = MasterOrders.erase(orderIter++); // https://stackoverflow.com/a/5265561/270143
iterate = false;
}
if (iterate) orderIter++;
}
int main()
{
testList();
return 0;
}
update: I forgot to state the actual goal
my goal is to just delete SCT_OSC_FILLED ordInfos from within the advance() method (that part is important) and leave the rest. My actual project code does more than what is shown, these names of functions are just made up for this example.. there is more code in them not directly related to manipulating the list (but related to processing OrderInfo) in my actual project. My goal is to leave one copy of o1 in the list as well as o2, o4 and o6 - removing o3 and o5 because they have an SCT_OSC_FILLED OrderInfo.statusCode
So the problem is nothing to do with deletion from a list. Your logic is simply wrong given the stated goal.
You want to delete all SCT_OSC_FILLED items from the list when adding an item but the code you write deletes all items from the list when you add an item with SCT_OSC_FILLED. You are simply testing the wrong thing.
Change this
void advance(OrderInfo ordInfo, std::list <OrderInfo> ::iterator& orderIter) {
bool iterate = true;
if (ordInfo.statusCode == SCT_OSC_FILLED) {
orderIter = MasterOrders.erase(orderIter++);
iterate = false;
}
if (iterate) orderIter++;
}
to this
void advance(OrderInfo ordInfo, std::list <OrderInfo> ::iterator& orderIter) {
bool iterate = true;
if (orderIter->statusCode == SCT_OSC_FILLED) {
orderIter = MasterOrders.erase(orderIter);
iterate = false;
}
if (iterate) orderIter++;
}
Once you make that change you can see that the ordInfo parameter is unused. Add bit more cleanup and you end up with this much simpler function
void advance(std::list <OrderInfo> ::iterator& orderIter) {
if (orderIter->statusCode == SCT_OSC_FILLED) {
orderIter = MasterOrders.erase(orderIter);
}
else {
orderIter++;
}
}
According to cppreference.com, "References and iterators to the erased elements are invalidated." You should get an iterator to the next element, before deleting the current element.
In the same page, cppreference gives an example:
// Erase all even numbers (C++11 and later)
for (std::list<int>::iterator it = c.begin(); it != c.end(); ) {
if (*it % 2 == 0) {
it = c.erase(it);
} else {
++it;
}
}
erase returns an iterator to the next element (end() if the removed element was the last), so "it = c.erase(it);" makes "it" to point to the next element, and there is no need for incrementing the iterator (with ++).
So you could have something like:
void advance(OrderInfo ordInfo, std::list <OrderInfo> ::iterator& orderIter) {
if (ordInfo.statusCode == SCT_OSC_FILLED) {
orderIter = MasterOrders.erase(orderIter);
} else {
orderIter++;
}
}

Allocating with new and storing in LRU buffer, deletion does not happen?

I am implementing a simple cache of unordered maps allocating them with the new keyword, and I would like for the objects to be deleted when they are evicted from the cache. For reference, this is the LRU cache implementation I am using: https://github.com/facebook/hhvm/blob/master/hphp/util/concurrent-lru-cache.h
I am testing my program on Linux, 1 thread. The code is the following:
HPHP::ConcurrentLRUCache<std::string, std::unordered_map<int, int>*> my_cache(10);
for (int i = 0; i < 1000000; i++) {
auto map = new std::unordered_map<int, int>(1000);
for (int j = 0; j < 1000; j++)
map->insert(std::make_pair(j, j));
my_cache.insert(std::to_string(i), map);
}
template <class TKey, class TValue, class THash>
bool ConcurrentLRUCache<TKey, TValue, THash>::
insert(const TKey& key, const TValue& value) {
// Insert into the CHM
ListNode* node = new ListNode(key);
HashMapAccessor hashAccessor;
HashMapValuePair hashMapValue(key, HashMapValue(value, node));
if (!m_map.insert(hashAccessor, hashMapValue)) {
delete node;
return false;
}
// Evict if necessary, now that we know the hashmap insertion was successful.
size_t size = m_size.load();
bool evictionDone = false;
if (size >= m_maxSize) {
evict();
evictionDone = true;
}
std::unique_lock<ListMutex> lock(m_listMutex);
pushFront(node);
lock.unlock();
if (!evictionDone) {
size = m_size++;
}
if (size > m_maxSize) {
if (m_size.compare_exchange_strong(size, size - 1)) {
evict();
}
}
return true;
}
template <class TKey, class TValue, class THash>
void ConcurrentLRUCache<TKey, TValue, THash>::
evict() {
std::unique_lock<ListMutex> lock(m_listMutex);
ListNode* moribund = m_tail.m_prev;
if (moribund == &m_head) {
// List is empty, can't evict
return;
}
delink(moribund);
lock.unlock();
HashMapAccessor hashAccessor;
if (!m_map.find(hashAccessor, moribund->m_key)) {
// Presumably unreachable
return;
}
m_map.erase(hashAccessor);
delete moribund;
}
When inserting a map, if the cache is full, the last recently used element should get evicted. However, the map itself does not get deleted and the memory usage peaks. What am I doing wrong?
Note: the maps should be retrieved during the execution of my program, so they should be accessible from the cache.

Game inventory system

I am trying to build an inventory system in C++ for a game that I am working on. However, there is a bug in the inventory system when I call Inventory::AddItem(Item i), no item gets added and that slot still stays blank. Currently, I handle the inventory through std::vector<Item>, where Item is a struct containing the type, if it is stackable, the maximum number of blocks in a stack, the current number of blocks in the stack, and a couple of objects for animation. Moreover, I automatically fill the inventory in with 40 slots of air blocks, which have the ID of INVENTORY_EMTPY_SLOT_ID.
Here is the code:
typedef struct item {
int type; // this is whether the block is a foreground of background tile
int id; // use this for the id of objects
bool stackable; // true indicates that the block can be stacked
int max_num; // maximum number of blocks in a stack
int num; // the current number of blocks in the stack
Animation* use_animation; // the animation of the block or item when it is being used
Animation* other_animation; // secondary animation of item in case it is necessary
} Item;
How I initialize empty slots:
for (size_t x = 0; x < INVENTORY_MAX_SLOTS; x++) {
Item i = {0, INVENTORY_EMPTY_SLOT_ID, true, 1, 1, NULL, NULL};
this->items.push_back(i);
}
Adding items
/*********BUG HERE:******************/
void Inventory::AddItem(Item item) {
// find all indexes with the same item.id
std::vector<size_t> indexes_w_same_item;
for (size_t i = 0; i < this->items.size(); i++) {
if (this->items[i].id == item.id) {
indexes_w_same_item.push_back(i);
}
}
// find the next empty slot
int next_empty_slot = -1;
for (size_t i = 0; i < this->items.size(); i++) {
if (this->items[i].id == INVENTORY_EMPTY_SLOT_ID) {
next_empty_slot = i;
}
}
// go through all the indexes with the same item.id
// and see if at least one of them is empty.
// if one is empty and has sufficient capacity,
// add the item and return. if it isn't, keep moving forward
for (size_t x = 0; x < indexes_w_same_item.size(); x++) {
if (item.id == this->items[indexes_w_same_item[x]].id) {
if (this->items[indexes_w_same_item[x]].num + item.num <= this->items[indexes_w_same_item[x]].max_num) {
this->items[indexes_w_same_item[x]].num += item.num;
return;
}
}
}
// if there is an empty slot, make a new stack
if (next_empty_slot >= 0) {
this->items[next_empty_slot].id = item.id;
this->items[next_empty_slot].max_num = item.max_num;
// clamp item.num so it doesn't exceed item.max_num
if (item.max_num > item.num) {
this->items[next_empty_slot].num = item.num;
} else {
this->items[next_empty_slot].num = item.max_num;
}
}
}
I know you have found the error, but there are many issues in your code that lead to this error, and I wanted to help you understand how to write better code, so next time it will be easier for you to find it (and maybe even avoid it!).
You should divide the logic into as small pieces as possible - modularity is a key for more clear and clean code, which was helping you to understand the error much faster.
Instead of making a clear flow, you made two distinct flows on and off. The code is much clearer when you exhaust one possible flow, and only then start the other (look at the functions add_item_to_existing_stack_if_possible and add_item_to_new_stack_if_possible.
Your variables/functions/classes names must represent what they are standing for, it wasn't the case with the original code! Look at the Item struct now - it is much clearer what each member represents, without comments! (personally, I am not using comments at all)
C++ is not C with classes - things like typedef should not appear in your code, you should use operator<< to std::cout instead of printf and so on.
Make sure you add const specifiers as possible, it can help find many mistakes on compile time (and make your program run faster).
Performance related - you should pass objects as references as much as possible, it is much faster to pass an uint64 (memory location) than copy your entire Item object.
#include <vector>
#include <array>
#include <iostream>
struct Animation;
struct Item {
int type;
int id;
bool is_stackable;
int max_num_blocks_in_stack;
int curr_num_of_blocks_in_stack;
Animation* used_animation; // if it is non nullable, you should consider to use it without a pointer (possibly a reference)
Animation* secondary_animation; // nullable - can be a pointer or std::optional
};
class Inventory
{
public:
bool add_item(Item&& item);
private:
bool is_slot_empty(size_t idx) const { return items[idx].id == INVENTORY_EMPTY_SLOT_ID; }
std::vector<size_t> find_indexes_of(const Item& item) const;
size_t find_next_empty_slot() const;
bool add_item_to_existing_stack_if_possible(const Item& item);
bool add_item_to_new_stack_if_possible(Item&& item);
void print() const;
static constexpr size_t MAX_INV_SIZE = 40; // can transform into a class template!
std::array<Item, MAX_INV_SIZE> items;
static constexpr int INVENTORY_EMPTY_SLOT_ID = -1;
};
std::vector<size_t> Inventory::find_indexes_of(const Item& item) const
{
std::vector<size_t> indexes{};
for (size_t idx = 0; idx < MAX_INV_SIZE; ++idx)
{
if (items[idx].id == item.id)
{
indexes.push_back(idx);
}
}
return indexes;
}
size_t Inventory::find_next_empty_slot() const
{
for (size_t idx = 0; idx < MAX_INV_SIZE; ++idx)
{
if (is_slot_empty(idx))
{
return idx;
}
}
return MAX_INV_SIZE; // invalid value!
}
void Inventory::print() const
{
for (size_t i = 0; i < MAX_INV_SIZE; ++i)
{
if (this->items[i].id != INVENTORY_EMPTY_SLOT_ID)
{
std::cout << "Inventory slot: " << i << "\n"
<< "Item ID: " << items[i].id << "\n"
<< "Item Num: " << items[i].curr_num_of_blocks_in_stack << "\n"
<< "Item Max Num: " << items[i].max_num_blocks_in_stack << std::endl;
//<< "Item Texture: " << textures[items[i].id] << std::endl;
}
}
}
bool Inventory::add_item_to_existing_stack_if_possible(const Item& item)
{
auto indexes_with_same_item = find_indexes_of(item);
for (auto idx : indexes_with_same_item)
{
if (item.id == items[idx].id)
{
if (items[idx].curr_num_of_blocks_in_stack + item.curr_num_of_blocks_in_stack <=
items[idx].max_num_blocks_in_stack)
{
items[idx].curr_num_of_blocks_in_stack += item.curr_num_of_blocks_in_stack;
return true;
}
}
}
return false;
}
bool Inventory::add_item_to_new_stack_if_possible(Item&& item)
{
size_t next_empty_slot = find_next_empty_slot();
if (next_empty_slot >= 0)
{
this->items[next_empty_slot] = std::move(item);
return true;
}
return false;
}
bool Inventory::add_item(Item&& item)
{
bool was_possible_to_add_to_existing_stack = add_item_to_existing_stack_if_possible(item);
if (!was_possible_to_add_to_existing_stack)
{
return add_item_to_new_stack_if_possible(std::move(item));
}
return false;
}
Okay, I figured it out. There must be a break at the end of the second for loop, where it looks for the next empty slot, otherwise, it will detect the next empty slot as the last slot in the inventory, assuming that you are adding the first item in the inventory. Therefore, that item did not show up in the hopbar.
Here is the correct solution:
void Inventory::AddItem(Item item) {
// find all indexes with the same item.id
std::vector<size_t> indexes_w_same_item;
for (size_t i = 0; i < this->items.size(); i++) {
if (this->items[i].id == item.id) {
indexes_w_same_item.push_back(i);
}
}
// find the next empty slot
int next_empty_slot = -1;
for (size_t i = 0; i < this->items.size(); i++) {
if (this->items[i].id == INVENTORY_EMPTY_SLOT_ID) {
next_empty_slot = i;
break;
}
}
// go through all the indexes with the same item.id
// and see if at least one of them is empty.
// if one is empty and has sufficient capacity,
// add the item and return. if it isn't, keep moving forward
for (size_t x = 0; x < indexes_w_same_item.size(); x++) {
if (item.id == this->items[indexes_w_same_item[x]].id) {
if (this->items[indexes_w_same_item[x]].num + item.num <= this->items[indexes_w_same_item[x]].max_num) {
this->items[indexes_w_same_item[x]].num += item.num;
return;
}
}
}
// if there is an empty slot, make a new stack
if (next_empty_slot >= 0) {
this->items[next_empty_slot] = item;
}
for (size_t i = 0; i < INVENTORY_MAX_SLOTS; i++) {
if (this->items[i].id != '.') {
printf("\nInventory slot: %d\n", i);
printf("Item ID: %c\n", this->items[i].id);
printf("Item Num: %d\n", this->items[i].num);
printf("Item Max Num: %d\n", this->items[i].max_num);
printf("Item Texture: %x\n", this->textures[this->items[i].id]);
}
}
return;
}

custom hash table implementation - memory error while mapping strings to integers

I am practicing the implementation of the hash map in C++. My goal is to ultimately map words to a pair of integers that correspond to their line and column in a text file. I took the hash map implementation from here and build upon that. The code works fine when I pass words with only one letter. However, when I have a word with more than one letter, the code compiles on Visual Studio, but at runtime throughs read access violation at this line:
HashNode<K, V> *entry = table[hashValue];
(within the insert member function). I thought there might be some tweaks that I should consider when using strings in a temple structure that I might not be aware of; however, I couldn't really find itout after hours of searching the web. Any ideas on how to fix this is greatly appreciated.
#include <string>
#include <iostream>
#include <tuple>
#include <vector>
using namespace std;
#define TABLE_SIZE 1028
template <typename K, typename V>
class HashNode {
public:
HashNode(const K &key, const V &value) :
key(key), value(value), next(NULL) {
}
K getKey() const {
return key;
}
V getValue() const {
return value;
}
void setValue(V value) {
HashNode::value = value;
}
HashNode *getNext() const {
return next;
}
void setNext(HashNode *next) {
HashNode::next = next;
}
private:
// key-value pair
K key;
V value;
// next bucket with the same key
HashNode *next;
};
template <typename K, typename V, typename F = KeyHash<K>>
class HashMap {
public:
HashMap() {
// construct zero initialized hash table of size
table = new HashNode<K, V> * [TABLE_SIZE]();
}
~HashMap() {
// destroy all buckets one by one
for (int i = 0; i < TABLE_SIZE; ++i) {
HashNode<K, V> *entry = table[i];
while (entry != NULL) {
HashNode<K, V> *prev = entry;
entry = entry->getNext();
delete prev;
}
table[i] = NULL;
}
// destroy the hash table
delete[] table;
}
void get(const K &key, vector<V> &value) {
unsigned long hashValue = hashFunc(key);
HashNode<K, V> *entry = table[hashValue];
while (entry != NULL) {
if (entry->getKey() == key) {
value.push_back(entry->getValue());
//return true;
}
entry = entry->getNext();
}
//return false;
}
void insert(const K &key, const V &value) {
unsigned long hashValue = hashFunc(key);
HashNode<K, V> *prev = NULL;
HashNode<K, V> *entry = table[hashValue];
while (entry != NULL && entry->getKey() == key) {
prev = entry;
entry = entry->getNext();
}
if (entry == NULL) {
entry = new HashNode<K, V>(key, value);
if (prev == NULL) {
// insert as first bucket
table[hashValue] = entry;
}
else {
prev->setNext(entry);
}
}
else {
// just update the value
entry->setValue(value);
}
}
void remove(const K &key) {
unsigned long hashValue = hashFunc(key);
HashNode<K, V> *prev = NULL;
HashNode<K, V> *entry = table[hashValue];
while (entry != NULL && entry->getKey() != key) {
prev = entry;
entry = entry->getNext();
}
if (entry == NULL) {
// key not found
return;
}
else {
if (prev == NULL) {
// remove first bucket of the list
table[hashValue] = entry->getNext();
}
else {
prev->setNext(entry->getNext());
}
delete entry;
}
}
private:
// hash table
HashNode<K, V> **table;
F hashFunc;
};
int main()
{
struct MyKeyHash
{
unsigned long operator()(const string & s) const
{
int hash = 7;
for (int i = 0; i < s.length(); i++)
{
hash = hash * 31 + s[i];
}
return hash;
}
};
HashMap<string, tuple<int, int>, MyKeyHash> hmap;
hmap.insert("BB", make_pair(3, 3));
hmap.insert("A", make_pair(1, 2));
hmap.insert("A", make_pair(4, 2));
vector<tuple<int, int>> value;
hmap.get("B", value);
for (auto it : value)
{
cout << get<0>(it) << ", " << get<1>(it) << endl;
}
}
unsigned long hashValue = hashFunc(key);
//...
table[hashValue]
The hashValue is returned from the function
unsigned long operator()(const string & s) const
{
int hash = 7;
for (int i = 0; i < s.length(); i++)
{
hash = hash * 31 + s[i];
}
return hash;
}
which can return arbitrarily large values (in the range of int). But table is an array of length TABLE_SIZE (1028). If the output happens to be larger than that, you are accessing it out-of-bounds.
The way the function is written, this is more likely to happen for longer input strings.
You probably meant
unsigned long hashValue = hashFunc(key)%TABLE_SIZE;
Also note that your hash function overflows, causing undefined behavior (because you are using signed integers), if the string is long enough. You should be using unsigned long instead of int, matching the return type and being unsigned.
You're missing an important step for using the hash map. You've calculated the hash, and you have a table to store things in, but you can't use the hash directly as an index into the table. It needs to be reduced modulo the size of the table to keep the subscript valid.
In this case, after computing the hash (calling the hashFunc member), you need to include
hashValue = hashValue % TABLE_SIZE;
You'll want to eventually add code to determine when you need to grow the size of the hash table, which will require TABLE_SIZE to be a member of the HashMap.

Extending std::vector as an dynamic array with NULL objects

As the title says, I try to extend the std::vector class in that way if I erase an element, the value of the position is not erased but actually set to NULL (providing a gap).
template<typename T>
class FVector : public std::vector<T> {
typedef std::vector<T> Base;
protected:
/** The number of elements in the vector */
size_t elementCount;
/**
* The index of the last element. This field is actually vector's length.
* For example when you have a vector like this ["a","b","c"] then the
* elementCount would be 3 and lastIndex would be 2 (because indexes are
* zero-based). But if you erased two first elements,
* leaving [null, null, "c" ] then elementCount=1 (because there is only
* one element in the vector!) however lastIndex would still remain 2.
* After you erase "c" lastIndex would be set to -1 (so it's enough to
* add 1 to lastIndex to determine vector's length.
*/
int lastIndex;
private:
/**
* Returns the index of the last not-null element in the vector,
* starting from position position downwards.
*
* #param position the position from which counting is to be begun.
* #return last element before (or on) index <code>position</code>
*/
int FindLastIndex(int position) {
int nLastItem = position;
if (position < 0) {
return -1;
}
for (; nLastItem >= 0; nLastItem--) {
if (Base::operator[](nLastItem) != NULL) {
break;
}
}
return (nLastItem);
}
public:
FVector(const T & value = T())
: elementCount(0), lastIndex(-1) {
}
FVector(int initialCapacity, const T & value = T())
: elementCount(0), lastIndex(-1),
std::vector<T>(initialCapacity, value) {
}
size_t Capacity() const {
return Base::size();
}
size_t Size() const {
return elementCount;
}
int LastIndex() const {
return lastIndex;
}
void AddElement(const T& obj) {
Base::push_back(obj);
elementCount++;
lastIndex++;
}
T & ElementAt(int index) {
if (index > lastIndex) {
// error
}
return Base::at(index);
}
void EraseElementAt(int index) throw() {
if (index > lastIndex) {
std::stringstream ss;
ss << index << " > " << lastIndex;
throw ArrayIndexOutOfBoundsException(ss.str());
}
if (Base::operator[](index) != NULL) {
elementCount--;
T v = Base::at(index);
delete v;
Base::at(index) = NULL;
if (index == lastIndex) {
lastIndex = FindLastIndex(lastIndex - 1);
}
}
}
};
It is not working like I expect. When I call the erase() method on an element the element
is not set to NULL.
For example:
class Int {
int i;
public:
Int(int v): i(v) { };
~Int() { }
};
//...
FVector<Int *> * v = new FVector<Int *>();
v->AddElement(new Int(1));
v->AddElement(new Int(3));
v->AddElement(new Int(5));
v->EraseElementAt(0);
v->EraseElementAt(2);
// ...
delete v;
will result in
[null, 3]
but I expect it to be
[null, 3, null]
Well, I do not know if that is possible what I try to achieve. I thought taking the std::vector class, which is a dynamic array (so why should I write my own array class then) give me all the basics I need to implement such thing.
Can anybody shed some light on that, I think I have some implementation issues here.
Thanks for your help!
In your EraseElementAt you have this:
if (index == lastIndex) {
lastIndex = FindLastIndex(lastIndex - 1);
}
If you happen to erase the last element in the vector (which you did) it will shorten the vector (decrement lastIndex). It seems like you want your vector to not do this - rather you are wanting the vector to null but not shorten. Maybe just take this out.