-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
3 changed files
with
223 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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);} | ||
}; | ||
//+------------------------------------------------------------------+ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; | ||
//+------------------------------------------------------------------+ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters