Skip to content

Commit

Permalink
sorting capabilities (#31)
Browse files Browse the repository at this point in the history
* lang: add Array::removeBatch method

New method allows to remove several indices in a row.

* collection: add SortComparer class

New class allows to perform sorting on arrays with regard to element precedence.

* collection: add SortedArray.mqh

New SortedArray heir of Collection class provides sorted array
implementation with standardized interface.

Methods utilize binary search which makes this collection more efficient
in some circumstances.

Possible applications may include percentile computations,
orders manipulation and so on.
  • Loading branch information
yerden authored and dingmaotu committed Apr 2, 2018
1 parent 7dec5fc commit 897edfd
Show file tree
Hide file tree
Showing 3 changed files with 223 additions and 0 deletions.
49 changes: 49 additions & 0 deletions Collection/SortComparer.mqh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//+------------------------------------------------------------------+
//| Module: Collection/SortComparer.mqh |
//| This file is part of the mql4-lib project: |
//| https://github.com/dingmaotu/mql4-lib |
//| |
//| Copyright 2017 Li Ding <[email protected]> |
//| Copyright 2017 Yerden Zhumabekov <[email protected]> |
//| |
//| 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 |
//| |
//| http://www.apache.org/licenses/LICENSE-2.0 |
//| |
//| 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 limitations under the License. |
//+------------------------------------------------------------------+
#include "../Lang/Hash.mqh"
#include "EqualityComparer.mqh"
#property strict
//+------------------------------------------------------------------+
//| Sort comparer is useful for sorted sets, arrays etc. |
//+------------------------------------------------------------------+
template<typename T>
class SortComparer: public EqualityComparer<T>
{
// Sort comparison:
// >0 if left>right
// <0 if left<right
// ==0 if left==right
public:
virtual int compare(const T left,const T right) const=0;
virtual bool equals(const T left,const T right) const override {return compare(left,right)==0;}
};
//+------------------------------------------------------------------+
//| Generic sort comparer for conventional comparable types |
//+------------------------------------------------------------------+
template<typename T>
class GenericSortComparer: public SortComparer<T>
{
public:
virtual int compare(const T left,const T right) const override {return left<right?-1:left>right;}
virtual int hash(const T value) const override {return Hash(value);}
};
//+------------------------------------------------------------------+
170 changes: 170 additions & 0 deletions Collection/SortedArray.mqh
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
//+------------------------------------------------------------------+
//| Module: Collection/SortedArray.mqh |
//| This file is part of the mql4-lib project: |
//| https://github.com/dingmaotu/mql4-lib |
//| |
//| Copyright 2015-2016 Li Ding <[email protected]> |
//| Copyright 2018 Yerden Zhumabekov <[email protected] |
//| |
//| 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 |
//| |
//| http://www.apache.org/licenses/LICENSE-2.0 |
//| |
//| 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 limitations under the License. |
//+------------------------------------------------------------------+
#property strict

#include "../Lang/Array.mqh"
#include "SortComparer.mqh"
#include "Collection.mqh"
//+------------------------------------------------------------------+
//| Simple Array-based implementation of sorted sets |
//+------------------------------------------------------------------+
template<typename T>
class SortedArray: public Collection<T>
{
private:
SortComparer<T>*m_sorter;
Array<T>m_array;
bool m_unique;
bool bsearch(const T value,int &id) const;
public:
// owned: flag restricts destructor to delete array elements
// unique: flag allows add()-ing of identical elements
// sorter: array sorting implementation
SortedArray(bool owned=true,bool unique=true,SortComparer<T>*sorter=NULL):
Collection(owned,sorter),
m_unique(unique),
m_sorter(sorter==NULL?new GenericSortComparer<T>():sorter){}
~SortedArray() {SafeDelete(m_sorter);}

// ConstIterator interface
ConstIterator<T>*constIterator() const {return new ConstSortedArrayIterator<T>(GetPointer(this));}
// Iterator interface
Iterator<T>*iterator() {return new SortedArrayIterator<T>(GetPointer(this),m_owned);}

// Collection interface
void clear() {m_array.clear();}
bool add(T value);
bool remove(const T value);
int size() const {return m_array.size();}
bool contains(const T value) const {return index(value)>=0;}

// SortedArray specific
T operator[](const int index) const {return m_array[index];}
// return index of a matched element, or <0 if not contained
int index(const T value) const {int id=0;return bsearch(value,id)?id:-1;}
int removeIndices(const int &removed[])
{
if(m_owned)
for(int i=0; i<ArraySize(removed); i++)
SafeDelete(m_array[removed[i]]);
return m_array.removeBatch(removed);
}
};
//+------------------------------------------------------------------+
//| Find value in set. Return true if found. |
//| Set &id to element index if found or the appropriate |
//| position for insertion. |
//+------------------------------------------------------------------+
template<typename T>
bool SortedArray::bsearch(const T value,int &id) const
{
int cmp=1,l=0,u=m_array.size();

while(l<u)
{
int idx=l+(u-l)/2;
cmp=m_sorter.compare(value,m_array[idx]);
if(cmp<0) u=idx;
else if(cmp>0) l=idx+1;
else u=l=idx;
}

id=u;
return cmp==0;
}
//+------------------------------------------------------------------+
//| Add value to sorted array |
//+------------------------------------------------------------------+
template<typename T>
bool SortedArray::add(T value)
{
int id;
bool res=!bsearch(value,id);
res=res || !m_unique;
if(res) m_array.insertAt(id,value);
return res;
}
//+------------------------------------------------------------------+
//| Remove value from sorted array |
//+------------------------------------------------------------------+
template<typename T>
bool SortedArray::remove(const T value)
{
int id;
if(!bsearch(value,id))
return false;
T ovalue=m_array[id];
m_array.removeAt(id);
if(m_owned) SafeDelete(ovalue);
return true;
}
//+------------------------------------------------------------------+
//| ConstIterator implementation for SortedArray |
//+------------------------------------------------------------------+
template<typename T>
class ConstSortedArrayIterator: public ConstIterator<T>
{
private:
int m_index;
const int m_size;
const SortedArray<T>*m_sortedarray;
public:
ConstSortedArrayIterator(const SortedArray<T>*v):m_index(0),m_size(v.size()),m_sortedarray(v) {}
bool end() const {return m_index>=m_size;}
void next() {if(!end()){m_index++;}}
T current() const {return m_sortedarray[m_index];}
};
//+------------------------------------------------------------------+
//| Iterator implementation for SortedArray |
//+------------------------------------------------------------------+
template<typename T>
class SortedArrayIterator: public Iterator<T>
{
private:
int m_index;
const int m_size;
SortedArray<T>*m_sortedarray;
int m_removed[];
protected:
bool removed() const
{
int s=ArraySize(m_removed);
return (s>0 && m_removed[s-1]==m_index);
}
public:
SortedArrayIterator(SortedArray<T>*v,bool owned):m_index(0),m_size(v.size()),m_sortedarray(v){}
~SortedArrayIterator() {if(ArraySize(m_removed)>0) m_sortedarray.removeIndices(m_removed);}
bool end() const {return m_index>=m_size;}
void next() {if(!end()){m_index++;}}
T current() const {if(removed()) return NULL; else return m_sortedarray[m_index];}

bool set(T value) {return false;} // unapplicable
bool remove()
{
if(end()||removed()) return false;
int s=ArraySize(m_removed);
ArrayResize(m_removed,s+1,5);
m_removed[s]=m_index;
return true;
}
};
//+------------------------------------------------------------------+
4 changes: 4 additions & 0 deletions Lang/Array.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
#property strict
//+------------------------------------------------------------------+
//| Generic array insert |
//| Inserts value at index into array. |
//| Array expands to contain new element but only in case no holes |
//| appear in new array. |
//+------------------------------------------------------------------+
template<typename T>
void ArrayInsert(T &array[],int index,T value,int extraBuffer=10)
Expand Down Expand Up @@ -200,6 +203,7 @@ public:
void insertAt(int index,T value) {ArrayInsert(m_array,index,value,m_extraBuffer);}
void removeAt(int index) {ArrayDelete(m_array,index);}
int removeAll(const T value) {return ArrayDeleteAll(m_array,value);}
int removeBatch(const int &removed[]) {return ArrayBatchRemove(m_array,removed);}

int index(const T value) const;
};
Expand Down

0 comments on commit 897edfd

Please sign in to comment.