patterncppMinor
Custom ArrayList class
Viewed 0 times
customarraylistclass
Problem
Please review my custom
It is automatically created on the heap with an optional seed value which determines how much space to give it upon being recreated after filling up. It comes with a refine function which cuts off the garbage values at any time and it can also be reseeded. Seed default is 10 new array spots for each recreation.
```
#ifndef MEMORYLIST_H
#define MEMORYLIST_H
//Class is designed to be a basic array list off the stack
template class MemoryList {
private:
Heap *array; //ArrayList on Heap
int length; //Max Length of ArrayList
int counter; //Current Position on ArrayList
int seed; //amount to add by when limit is breached
//Increase list if needed
inline void spaceCheck() {
if (counter == length) {
int i;
Heap *temp = new Heap[length];
for (i = 0; i = place + 1; --i) {
array[i] = array[i - 1];
}
}
public:
MemoryList(int seed = 10) {
this->seed = seed;
length = seed;
counter = 0;
array = new Heap[length];
}
//just shows what value is stored in what position
Heap showValue(int position) {
return array[position];
}
//tack a value to the end
void newValue(Heap variable){
spaceCheck();
array[counter] = variable;
++counter;
}
//tack a value anywhere you want and array will self adjust
void insertValue(int position, Heap variable) {
spaceCheck();
shiftRight(position);
array[position] = variable;
++counter;
}
//delete any value you'd like and array will self adjust
void deleteValue(int position) {
for (int i = position; i seed = seed;
}
//is item in array?
boo
ArrayList class I constructed from scratch and add any suggestions or constructive criticisms. The point of this template class is give the user a lightweight ArrayList with optimal control over how it operates.It is automatically created on the heap with an optional seed value which determines how much space to give it upon being recreated after filling up. It comes with a refine function which cuts off the garbage values at any time and it can also be reseeded. Seed default is 10 new array spots for each recreation.
```
#ifndef MEMORYLIST_H
#define MEMORYLIST_H
//Class is designed to be a basic array list off the stack
template class MemoryList {
private:
Heap *array; //ArrayList on Heap
int length; //Max Length of ArrayList
int counter; //Current Position on ArrayList
int seed; //amount to add by when limit is breached
//Increase list if needed
inline void spaceCheck() {
if (counter == length) {
int i;
Heap *temp = new Heap[length];
for (i = 0; i = place + 1; --i) {
array[i] = array[i - 1];
}
}
public:
MemoryList(int seed = 10) {
this->seed = seed;
length = seed;
counter = 0;
array = new Heap[length];
}
//just shows what value is stored in what position
Heap showValue(int position) {
return array[position];
}
//tack a value to the end
void newValue(Heap variable){
spaceCheck();
array[counter] = variable;
++counter;
}
//tack a value anywhere you want and array will self adjust
void insertValue(int position, Heap variable) {
spaceCheck();
shiftRight(position);
array[position] = variable;
++counter;
}
//delete any value you'd like and array will self adjust
void deleteValue(int position) {
for (int i = position; i seed = seed;
}
//is item in array?
boo
Solution
A couple of things jump out:
Use
Use exponential, not linear, reallocations. Doubling each time is the best way. This allows
Use
size_t for the all allocation sizes/indices. I have had real-world bugs because of this in third-party libraries.Use exponential, not linear, reallocations. Doubling each time is the best way. This allows
.newValue to append in \$O(1)\$ amortized time, as opposed to the current \$O(n)\$.Context
StackExchange Code Review Q#96077, answer score: 6
Revisions (0)
No revisions yet.