Skip to content

Commit

Permalink
Fix #32: Implemented ArraySet and fixed Set hierarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
dingmaotu committed Jul 28, 2018
1 parent e5cb0ae commit 81c2854
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 62 deletions.
66 changes: 66 additions & 0 deletions Collection/ArraySet.mqh
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;}
};
//+------------------------------------------------------------------+
6 changes: 3 additions & 3 deletions Collection/HashSet.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

#include "HashEntries.mqh"
#include "HashSlots.mqh"
#include "Collection.mqh"
#include "Set.mqh"
//+------------------------------------------------------------------+
//| storage for actual entries |
//+------------------------------------------------------------------+
Expand Down Expand Up @@ -61,7 +61,7 @@ public:
//| Set based on open addressing hash table |
//+------------------------------------------------------------------+
template<typename T>
class HashSet: public Collection<T>
class HashSet: public Set<T>
{
private:
HashSetEntries<T>m_entries;
Expand All @@ -72,7 +72,7 @@ public:
//--- by default the HashSet do not own its elements
//--- if the hash elements are pointers and the real owner wants to
//--- transfer the ownership to this collection, then she need to explicitly `new HashSet(NULL,true)`
HashSet(EqualityComparer<T>*comparer=NULL,bool owned=false):Collection<T>(owned,comparer),m_slots(m_comparer,GetPointer(m_entries)) {}
HashSet(EqualityComparer<T>*comparer=NULL,bool owned=false):Set<T>(owned,comparer),m_slots(m_comparer,GetPointer(m_entries)) {}
~HashSet()
{
if(m_owned)
Expand Down
113 changes: 55 additions & 58 deletions Collection/Set.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand All @@ -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;}
};
//+------------------------------------------------------------------+
2 changes: 1 addition & 1 deletion Collection/Vector.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public:
void next() {if(!end()){m_index++;}}
T current() const {if(removed()) return NULL; else return m_vector[m_index];}

bool set(T value) {if(removed()) return false; m_vector.set(m_index,value);return true;}
virtual bool set(T value) {if(removed()) return false; m_vector.set(m_index,value); return true;}
bool remove()
{
if(end()) return false;
Expand Down

0 comments on commit 81c2854

Please sign in to comment.