-
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.
Fix #32: Implemented ArraySet and fixed Set hierarchy
- Loading branch information
Showing
4 changed files
with
125 additions
and
62 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,66 @@ | ||
//+------------------------------------------------------------------+ | ||
//| Module: Collection/ArraySet.mqh | | ||
//| This file is part of the mql4-lib project: | | ||
//| https://github.com/dingmaotu/mql4-lib | | ||
//| | | ||
//| Copyright 2015-2018 Li Ding <[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 "Vector.mqh" | ||
#include "Set.mqh" | ||
//+------------------------------------------------------------------+ | ||
//| Simple Set implementation using an vector (no repeated elements) | | ||
//+------------------------------------------------------------------+ | ||
template<typename T> | ||
class ArraySet: public Set<T> | ||
{ | ||
private: | ||
Vector<T>m_vector; | ||
public: | ||
ArraySet(bool owned=false,EqualityComparer<T>*comparer=NULL):Set<T>(owned,comparer),m_vector(owned,10,comparer){} | ||
~ArraySet() {} | ||
|
||
// CosntIterator interface | ||
ConstIterator<T>*constIterator() const {return m_vector.constIterator();} | ||
// Iterator interface | ||
Iterator<T>*iterator() {return new ArraySetIterator<T>(GetPointer(m_vector),m_owned);} | ||
|
||
// Collection interface | ||
void clear() {m_vector.clear();} | ||
int size() const {return m_vector.size();} | ||
bool add(T value) | ||
{ | ||
if(m_vector.contains(value)) return false; | ||
else return m_vector.add(value); | ||
} | ||
bool remove(const T value) {return m_vector.remove(value);} | ||
bool contains(const T value) const {return m_vector.contains(value);} | ||
}; | ||
//+------------------------------------------------------------------+ | ||
//| Iterator implementation for ArraySet | | ||
//| Overrides VectorIterator<T>.add: ensure no repeated elements can | | ||
//| be added to the ArraySet | | ||
//+------------------------------------------------------------------+ | ||
template<typename T> | ||
class ArraySetIterator: public VectorIterator<T> | ||
{ | ||
public: | ||
ArraySetIterator(Vector<T>*v,bool owned):VectorIterator<T>(v,owned) {} | ||
// you can not set a value during Set iteration | ||
virtual bool set(T value) {return false;} | ||
}; | ||
//+------------------------------------------------------------------+ |
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
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 |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
//| This file is part of the mql4-lib project: | | ||
//| https://github.com/dingmaotu/mql4-lib | | ||
//| | | ||
//| Copyright 2015-2016 Li Ding <[email protected]> | | ||
//| Copyright 2015-2018 Li Ding <[email protected]> | | ||
//| | | ||
//| Licensed under the Apache License, Version 2.0 (the "License"); | | ||
//| you may not use this file except in compliance with the License. | | ||
|
@@ -20,74 +20,71 @@ | |
//+------------------------------------------------------------------+ | ||
#property strict | ||
|
||
#include "../Lang/Array.mqh" | ||
#include "Collection.mqh" | ||
//+------------------------------------------------------------------+ | ||
//| Simple Set implementation using an array | | ||
//| The Set interface | | ||
//| A set does not allow duplicates | | ||
//+------------------------------------------------------------------+ | ||
template<typename T> | ||
class Set: public Collection<T> | ||
{ | ||
private: | ||
Array<T>m_array; | ||
public: | ||
// Iterator interface | ||
Iterator<T>*iterator() const {return new SetIterator<T>(m_array);} | ||
// for Set initial buffer set to zero | ||
Set(int buffer=0):m_array(buffer){} | ||
// Collection interface | ||
void clear() {m_array.clear();} | ||
int size() const {return m_array.size();} | ||
bool add(T value); | ||
bool remove(const T value); | ||
Set(bool owned,EqualityComparer<T>*comparer):Collection<T>(owned,comparer){} | ||
|
||
bool contains(const T value) {return m_array.index(value)>=0;} | ||
}; | ||
//+------------------------------------------------------------------+ | ||
//| Add the element if the element is not in this set | | ||
//+------------------------------------------------------------------+ | ||
template<typename T> | ||
bool Set::add(T value) | ||
{ | ||
int index=m_array.index(value); | ||
if(index>=0) | ||
return false; | ||
else | ||
virtual bool setByIntersection(const Collection<T>&left,const Collection<T>&right) | ||
{ | ||
m_array.insertAt(size(),value); | ||
return true; | ||
if(!isEmpty()) | ||
{ | ||
clear(); | ||
} | ||
|
||
for(ConstIter<T>it(left); !it.end(); it.next()) | ||
{ | ||
T value=it.current(); | ||
if(right.contains(value)) | ||
// Notice if elements of left or right are owned by their container and they are pointers | ||
// and this Set is also owned, | ||
// you should deal with the owning problem on your own | ||
add(it.current()); | ||
} | ||
return size() > 0; | ||
} | ||
} | ||
//+------------------------------------------------------------------+ | ||
//| Remove the element that is equal to value | | ||
//+------------------------------------------------------------------+ | ||
template<typename T> | ||
bool Set::remove(const T value) | ||
{ | ||
int index=m_array.index(value); | ||
if(index>=0) | ||
|
||
virtual bool setByUnion(const Collection<T>&left,const Collection<T>&right) | ||
{ | ||
m_array.removeAt(index); | ||
return true; | ||
if(!isEmpty()) | ||
{ | ||
clear(); | ||
} | ||
for(ConstIter<T>it(left); !it.end(); it.next()) | ||
{ | ||
add(it.current()); | ||
} | ||
|
||
for(ConstIter<T>it(right); !it.end(); it.next()) | ||
{ | ||
add(it.current()); | ||
} | ||
return size() > 0; | ||
} | ||
|
||
virtual bool setByComplement(const Collection<T>&left,const Collection<T>&right) | ||
{ | ||
if(!isEmpty()) | ||
{ | ||
clear(); | ||
} | ||
|
||
if(left.isEmpty()) return false; | ||
|
||
for(ConstIter<T>it(left); !it.end(); it.next()) | ||
{ | ||
T value=it.current(); | ||
if(!right.contains(value)) | ||
add(it.current()); | ||
} | ||
|
||
return size() > 0; | ||
} | ||
else | ||
return false; | ||
} | ||
//+------------------------------------------------------------------+ | ||
//| Iterator implementation for Set | | ||
//+------------------------------------------------------------------+ | ||
template<typename T> | ||
class SetIterator: public Iterator<T> | ||
{ | ||
private: | ||
int m_index; | ||
const int m_size; | ||
Array<T>*m_a; | ||
public: | ||
SetIterator(const Array<T>&v):m_index(0),m_size(v.size()),m_a((Array<T>*)GetPointer(v)) {} | ||
bool end() const {return m_index>=m_size;} | ||
void next() {if(!end()){m_index++;}} | ||
T current() const {return m_a[m_index];} | ||
bool set(T value) {m_a.set(m_index,value);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