Skip to content

Commit

Permalink
feat: Array of boxes
Browse files Browse the repository at this point in the history
  • Loading branch information
sibvic committed Sep 5, 2024
1 parent aba94cc commit d7228fc
Show file tree
Hide file tree
Showing 5 changed files with 322 additions and 39 deletions.
185 changes: 156 additions & 29 deletions snippets/Array/Array.mqh
Original file line number Diff line number Diff line change
@@ -1,49 +1,176 @@
// Array v1.0
// Array v1.3
#include <Array/IArray.mqh>
//#include <Array/LineArray.mqh>
//#include <Array/IntArray.mqh>
//#include <Array/FloatArray.mqh>
#include <Array/BoxArray.mqh>

class Array : public IArray
class Array
{
double array[];
public:
Array(int size)
//static void Unshift(IIntArray* array, int value) { if (array == NULL) { return; } array.Unshift(value); }
//static void Unshift(IFloatArray* array, double value) { if (array == NULL) { return; } array.Unshift(value); }
//static void Unshift(ILineArray* array, Line* value) { if (array == NULL) { return; } array.Unshift(value); }
static void Unshift(IBoxArray* array, Box* value) { if (array == NULL) { return; } array.Unshift(value); }

//static int Size(IIntArray* array) { if (array == NULL) { return EMPTY_VALUE;} return array.Size(); }
//static int Size(IFloatArray* array) { if (array == NULL) { return EMPTY_VALUE;} return array.Size(); }
//static int Size(ILineArray* array) { if (array == NULL) { return EMPTY_VALUE;} return array.Size(); }
static int Size(IBoxArray* array) { if (array == NULL) { return EMPTY_VALUE;} return array.Size(); }

//static int Shift(IIntArray* array) { if (array == NULL) { return EMPTY_VALUE; } return array.Shift(); }
//static double Shift(IFloatArray* array) { if (array == NULL) { return EMPTY_VALUE; } return array.Shift(); }
//static Line* Shift(ILineArray* array) { if (array == NULL) { return NULL; } return array.Shift(); }
static Box* Shift(IBoxArray* array) { if (array == NULL) { return NULL; } return array.Shift(); }

//static void Push(IIntArray* array, int value) { if (array == NULL) { return; } array.Push(value); }
//static void Push(IFloatArray* array, double value) { if (array == NULL) { return; } array.Push(value); }
//static void Push(ILineArray* array, Line* value) { if (array == NULL) { return; } array.Push(value); }
static void Push(IBoxArray* array, Box* value) { if (array == NULL) { return; } array.Push(value); }

//static int Pop(IIntArray* array) { if (array == NULL) { return EMPTY_VALUE; } return array.Pop(); }
//static double Pop(IFloatArray* array) { if (array == NULL) { return EMPTY_VALUE; } return array.Pop(); }
//static Line* Pop(ILineArray* array) { if (array == NULL) { return NULL; } return array.Pop(); }
static Box* Pop(IBoxArray* array) { if (array == NULL) { return NULL; } return array.Pop(); }

//static int Get(IIntArray* array, int index) { if (array == NULL) { return EMPTY_VALUE; } return array.Get(index); }
//static double Get(IFloatArray* array, int index) { if (array == NULL) { return EMPTY_VALUE; } return array.Get(index); }
//static Line* Get(ILineArray* array, int index) { if (array == NULL) { return NULL; } return array.Get(index); }
static Box* Get(IBoxArray* array, int index) { if (array == NULL) { return NULL; } return array.Get(index); }

//static void Set(IIntArray* array, int index, int value) { if (array == NULL) { return; } array.Set(index, value); }
//static void Set(IFloatArray* array, int index, double value) { if (array == NULL) { return; } array.Set(index, value); }
//static void Set(ILineArray* array, int index, Line* value) { if (array == NULL) { return; } array.Set(index, value); }
static void Set(IBoxArray* array, int index, Box* value) { if (array == NULL) { return; } array.Set(index, value); }

//static int Remove(IIntArray* array, int index) { if (array == NULL) { return EMPTY_VALUE; } return array.Remove(index); }
//static double Remove(IFloatArray* array, int index) { if (array == NULL) { return EMPTY_VALUE; } return array.Remove(index); }
//static Line* Remove(ILineArray* array, int index) { if (array == NULL) { return NULL; } return array.Remove(index); }
static Box* Remove(IBoxArray* array, int index) { if (array == NULL) { return NULL; } return array.Remove(index); }
/*
static int PercentRank(IIntArray* array, int index)
{
ArrayResize(array, size);
int arraySize = array.Size();
if (array == NULL || arraySize == 0 || arraySize <= index) { return EMPTY_VALUE; }
int target = array.Get(index);
if (target == EMPTY_VALUE)
{
return EMPTY_VALUE;
}
int count = 0;
for (int i = 0; i < arraySize; ++i)
{
int current = array.Get(i);
if (current != EMPTY_VALUE && target >= current)
{
count++;
}
}
return (count * 100.0) / arraySize;
}

void Unshift(double value)
static double PercentRank(IFloatArray* array, int index)
{
int size = ArraySize(array);
ArrayResize(array, size + 1);
array[size] = value;
return value;
int arraySize = array.Size();
if (array == NULL || arraySize == 0 || arraySize <= index) { return EMPTY_VALUE; }
double target = array.Get(index);
if (target == EMPTY_VALUE)
{
return EMPTY_VALUE;
}
int count = 0;
for (int i = 0; i < arraySize; ++i)
{
double current = array.Get(i);
if (current != EMPTY_VALUE && target >= current)
{
count++;
}
}
return (count * 100.0) / arraySize;
}
int Size()
static int Max(IIntArray* array)
{
return ArraySize(array);
if (array == NULL || array.Size() == 0) { return EMPTY_VALUE; }
int max = array.Get(0);
for (int i = 1; i < array.Size(); ++i)
{
int current = array.Get(i);
if (max == EMPTY_VALUE || (current != EMPTY_VALUE && max < current))
{
max = current;
}
}
return max;
}

void Push(double value)
static double Max(IFloatArray* array)
{
int size = ArraySize(array);
ArrayResize(array, size + 1);
array[size] = value;
if (array == NULL || array.Size() == 0) { return EMPTY_VALUE; }
double max = array.Get(0);
for (int i = 1; i < array.Size(); ++i)
{
double current = array.Get(i);
if (max == EMPTY_VALUE || (current != EMPTY_VALUE && max < current))
{
max = current;
}
}
return max;
}

double Pop()
static int Min(IIntArray* array)
{
int size = ArraySize(array);
double value = array[0];
for (int i = 0; i < size - 1; ++i)
if (array == NULL || array.Size() == 0) { return EMPTY_VALUE; }
int min = array.Get(0);
for (int i = 1; i < array.Size(); ++i)
{
array[i] = array[i + 1];
int current = array.Get(i);
if (min == EMPTY_VALUE || (current != EMPTY_VALUE && min > current))
{
min = current;
}
}
ArrayResize(array, size - 1);
return value;
return min;
}
static double Min(IFloatArray* array)
{
if (array == NULL || array.Size() == 0) { return EMPTY_VALUE; }
double min = array.Get(0);
for (int i = 1; i < array.Size(); ++i)
{
double current = array.Get(i);
if (min == EMPTY_VALUE || (current != EMPTY_VALUE && min > current))
{
min = current;
}
}
return min;
}
double Get(int index)
static int Sum(IIntArray* array)
{
return array[index];
if (array == NULL)
{
return 0;
}
int sum = 0;
for (int i = 0; i < array.Size(); ++i)
{
sum += array.Get(i);
}
return sum;
}
};
static double Sum(IFloatArray* array)
{
if (array == NULL)
{
return 0;
}
double sum = 0;
for (int i = 0; i < array.Size(); ++i)
{
sum += array.Get(i);
}
return sum;
}*/
};

139 changes: 139 additions & 0 deletions snippets/Array/BoxArray.mqh
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
#ifndef BoxArray_IMPL
#define BoxArray_IMPL
// Box array v1.4
#include <Array/IBoxArray.mqh>
#include <Objects/BoxesCollection.mqh>

class BoxArray : public IBoxArray
{
Box* _array[];
int _defaultSize;
Box* _defaultValue;
public:
BoxArray(int size, Box* defaultValue)
{
_defaultSize = size;
Clear();
}

~BoxArray()
{
Clear();
}

IBoxArray* Clear()
{
int size = ArraySize(_array);
int i;
for (i = 0; i < size; i++)
{
if (_array[i] != NULL)
{
BoxesCollection::Delete(_array[i]);
_array[i].Release();
}
}
ArrayResize(_array, _defaultSize);
for (i = 0; i < _defaultSize; ++i)
{
_array[i] = _defaultValue;
}
return &this;
}

void Unshift(Box* value)
{
int size = ArraySize(_array);
ArrayResize(_array, size + 1);
for (int i = size - 1; i >= 0; --i)
{
_array[i + 1] = _array[i];
}
_array[0] = value;
if (value != NULL)
{
value.AddRef();
}
}

int Size()
{
return ArraySize(_array);
}

void Push(Box* value)
{
int size = ArraySize(_array);
ArrayResize(_array, size + 1);
_array[size] = value;
if (value != NULL)
{
value.AddRef();
}
}

Box* Pop()
{
int size = ArraySize(_array);
Box* value = _array[size - 1];
ArrayResize(_array, size - 1);
if (value != NULL && value.Release() == 0)
{
return NULL;
}
return value;
}

Box* Shift()
{
return Remove(0);
}

Box* Get(int index)
{
if (index < 0 || index >= Size())
{
return NULL;
}
return _array[index];
}

void Set(int index, Box* value)
{
if (index < 0 || index >= Size())
{
return;
}
if (_array[index] != NULL)
{
_array[index].Release();
}
_array[index] = value;
if (value != NULL)
{
value.AddRef();
}
}

IBoxArray* Slice(int from, int to)
{
return NULL; //TODO;
}

Box* Remove(int index)
{
int size = ArraySize(_array);
Box* value = _array[index];
for (int i = index; i < size - 1; ++i)
{
_array[i] = _array[i + 1];
}
ArrayResize(_array, size - 1);
if (value.Release() == 0)
{
return NULL;
}
return value;
}
};
#endif
10 changes: 1 addition & 9 deletions snippets/Array/IArray.mqh
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
// Array interface v1.0

class IArray
{
public:
virtual void Unshift(double value) = 0;
virtual int Size() = 0;
virtual void Push(double value) = 0;
virtual double Pop() = 0;
virtual double Get(int index) = 0;
};
#include <Array/IBoxArray.mqh>
17 changes: 17 additions & 0 deletions snippets/Array/IBoxArray.mqh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Box array interface v1.1
#include <Objects/Box.mqh>

class IBoxArray
{
public:
virtual void Unshift(Box* value) = 0;
virtual int Size() = 0;
virtual void Push(Box* value) = 0;
virtual Box* Pop() = 0;
virtual Box* Get(int index) = 0;
virtual void Set(int index, Box* value) = 0;
virtual IBoxArray* Slice(int from, int to) = 0;
virtual IBoxArray* Clear() = 0;
virtual Box* Shift() = 0;
virtual Box* Remove(int index) = 0;
};
10 changes: 9 additions & 1 deletion snippets/Array/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,12 @@ Interface for an array.

## Array

Array implementation similar to PineScript arrays
Array implementation similar to PineScript arrays

## BoxArray

Array of boxes

## IBoxArray

Interface for an array of boxes

0 comments on commit d7228fc

Please sign in to comment.