| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689 |
- //+------------------------------------------------------------------+
- //| MetaTrader 5 API |
- //| Copyright 2000-2019, MetaQuotes Software Corp. |
- //| http://www.metaquotes.net |
- //+------------------------------------------------------------------+
- #pragma once
- #include <new.h>
- #include <stdlib.h>
- #include "MT5APISearch.h"
- //+------------------------------------------------------------------+
- //| Dynamic array base class |
- //| For POD data types only! |
- //+------------------------------------------------------------------+
- class CMTArrayBase
- {
- protected:
- UCHAR *m_data; // array
- UINT m_data_total; // array records total
- UINT m_data_max; // array records max
- UINT m_data_width; // record size in bytes
- UINT m_data_step; // reallocation step
- public:
- CMTArrayBase(const UINT width,const UINT step);
- virtual ~CMTArrayBase();
- //--- common properties
- UINT Total(void) const { return(m_data_total); }
- UINT Width(void) const { return(m_data_width); }
- UINT Max(void) const { return(m_data_max); }
- UINT Step(void) const { return(m_data_step); }
- bool Compare(const CMTArrayBase& array) const;
- //--- global management
- void Clear(void) { m_data_total=0; }
- bool Zero(void);
- void Shutdown(void);
- void Compact(void);
- bool Assign(const CMTArrayBase& array);
- void Swap(CMTArrayBase &arr);
- bool Reserve(const UINT size);
- bool Resize(const UINT size);
- //--- add
- bool Add(const void *elem) { return Add(elem,1); }
- bool Add(const void *elem,const UINT total);
- bool Add(const CMTArrayBase& array);
- bool AddRange(const CMTArrayBase& array,const UINT from,const UINT to);
- bool AddEmpty(const UINT total);
- void* Append(void);
- bool Insert(const UINT pos,const void *elem) { return Insert(pos,elem,1); }
- bool Insert(const UINT pos,const void *elem,const UINT total);
- bool InsertEmpty(const UINT pos,const UINT total);
- void* Insert(const void *elem,SMTSearch::SortFunctionPtr sort_function);
- //--- delete
- bool Delete(const UINT pos);
- bool Delete(const void *elem);
- bool DeleteRange(const UINT from,const UINT to);
- bool Remove(const void *elem,SMTSearch::SortFunctionPtr sort_function);
- //--- modify
- bool Update(const UINT pos,const void *elem);
- bool Shift(const UINT pos,const int shift);
- bool Trim(const UINT size);
- //--- data access
- bool Next(const UINT pos,void *elem) const;
- void* Next(const void *elem) const;
- void* Prev(const void *elem) const;
- const void* At(const UINT pos) const;
- void* At(const UINT pos);
- int Position(const void* ptr) const;
- bool Range(const UINT from,const UINT to,void* data) const;
- //--- sort & search
- void Sort(SMTSearch::SortFunctionPtr sort_function);
- void* Search(const void *key,SMTSearch::SortFunctionPtr sort_function) const;
- void* SearchGreatOrEq(const void *key,SMTSearch::SortFunctionPtr sort_function) const;
- void* SearchGreater(const void *key,SMTSearch::SortFunctionPtr sort_function) const;
- void* SearchLessOrEq(const void *key,SMTSearch::SortFunctionPtr sort_function) const;
- void* SearchLess(const void *key,SMTSearch::SortFunctionPtr sort_function) const;
- void* SearchLeft(const void *key,SMTSearch::SortFunctionPtr sort_function) const;
- void* SearchRight(const void *key,SMTSearch::SortFunctionPtr sort_function) const;
- protected:
- CMTArrayBase(void):m_data(NULL),m_data_total(0),m_data_max(0),m_data_width(0),m_data_step(0){}
- bool Realloc(const UINT total);
- };
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- inline CMTArrayBase::CMTArrayBase(const UINT width,const UINT step) : m_data(NULL),m_data_total(0),
- m_data_max(0),m_data_width(width),m_data_step(step)
- {
- }
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- inline CMTArrayBase::~CMTArrayBase(void)
- {
- Shutdown();
- }
- //+------------------------------------------------------------------+
- //| Zero all elements |
- //+------------------------------------------------------------------+
- inline bool CMTArrayBase::Zero(void)
- {
- if(m_data) ZeroMemory(m_data,m_data_width*m_data_max);
- return(true);
- }
- //+------------------------------------------------------------------+
- //| Full shutdown with memory free |
- //+------------------------------------------------------------------+
- inline void CMTArrayBase::Shutdown(void)
- {
- //--- clear all
- if(m_data) { delete[] m_data; m_data=NULL; }
- //--- zero sizes
- m_data_total=m_data_max=0;
- }
- //+------------------------------------------------------------------+
- //| Arrays comparison |
- //+------------------------------------------------------------------+
- inline bool CMTArrayBase::Compare(const CMTArrayBase& array) const
- {
- //--- check total and with
- if(m_data_width!=array.m_data_width || m_data_total!=array.m_data_total)
- return(false);
- //--- check total
- if(Total())
- {
- //--- check data
- if(!m_data || !array.m_data) return(false);
- //--- check record by record
- for(UINT i=0;i<m_data_total;i++)
- if(memcmp(At(i),array.At(i),m_data_width)!=0) return(false);
- }
- //--- ok
- return(true);
- }
- //+------------------------------------------------------------------+
- //| Memory check and reallocation to store 'total' records |
- //+------------------------------------------------------------------+
- inline bool CMTArrayBase::Realloc(const UINT total)
- {
- //--- check
- if(total<1 || m_data_step<1) return(true);
- //--- check size
- if(m_data && (m_data_total+total)<=m_data_max) return(true);
- //--- calculate reallocation
- UINT add=((total/m_data_step)+1)*m_data_step;
- //--- allocate new buffer
- UCHAR *buffer=new(std::nothrow) UCHAR[(m_data_max+add)*m_data_width];
- //--- check
- if(!buffer) return(false);
- //--- previous data?
- if(m_data)
- {
- if(m_data_total>0) memcpy(buffer,m_data,m_data_width*m_data_total);
- delete[] m_data;
- }
- //--- replace
- m_data =buffer;
- m_data_max+=add;
- //--- ok
- return(true);
- }
- //+------------------------------------------------------------------+
- //| Compact allocated memory |
- //+------------------------------------------------------------------+
- inline void CMTArrayBase::Compact(void)
- {
- UINT freespace=m_data_max-m_data_total;
- UCHAR *newdata;
- //--- check
- if(!m_data || freespace<=m_data_step) return;
- //--- allocate new block
- newdata=new(std::nothrow) UCHAR[(m_data_total+m_data_step)*m_data_width];
- //--- check memory
- if(!newdata) return;
- //--- copy old data
- memcpy(newdata,m_data,m_data_total*m_data_width);
- //--- free old buffer
- delete[] m_data;
- //--- replace
- m_data =newdata;
- m_data_max=m_data_total+m_data_step;
- }
- //+------------------------------------------------------------------+
- //| Assign from 'array' and |
- //+------------------------------------------------------------------+
- inline bool CMTArrayBase::Assign(const CMTArrayBase& array)
- {
- //--- check
- if(this==&array) return(true);
- //--- check width
- if(array.m_data_width!=m_data_width) return(false);
- //--- clear self
- Clear();
- //--- check data
- if(!array.m_data || array.m_data_total<1) return(true);
- //--- add
- return(Add(array.m_data,array.m_data_total));
- }
- //+------------------------------------------------------------------+
- //| Swap arrays content |
- //+------------------------------------------------------------------+
- inline void CMTArrayBase::Swap(CMTArrayBase &arr)
- {
- UCHAR *data;
- UINT data_total;
- UINT data_max;
- //--- check
- if(this==&arr) return;
- //--- check width
- if(arr.m_data_width!=m_data_width) return;
- //--- swap them
- data =m_data;
- data_total=m_data_total;
- data_max =m_data_max;
- m_data =arr.m_data;
- m_data_total=arr.m_data_total;
- m_data_max =arr.m_data_max;
- arr.m_data =data;
- arr.m_data_total=data_total;
- arr.m_data_max =data_max;
- }
- //+------------------------------------------------------------------+
- //| Reserve free space |
- //+------------------------------------------------------------------+
- inline bool CMTArrayBase::Reserve(const UINT size)
- {
- if(size<=m_data_max) return(true);
- else return(Realloc(size-m_data_total));
- }
- //+------------------------------------------------------------------+
- //| Resize array |
- //+------------------------------------------------------------------+
- inline bool CMTArrayBase::Resize(const UINT size)
- {
- //--- check
- if(!m_data) return(false);
- if(size==m_data_total) return(true);
- //--- resize or realloc
- if(size<m_data_total) m_data_total=size;
- else
- if(size>m_data_total) return(AddEmpty(size-m_data_total));
- //--- ok
- return(true);
- }
- //+------------------------------------------------------------------+
- //| Add total records to tail |
- //+------------------------------------------------------------------+
- inline bool CMTArrayBase::Add(const void *elem,const UINT total)
- {
- //--- check ptr
- if(!elem) return(false);
- //--- check total
- if(total<1) return(true);
- //--- check and reallocate memory
- if(!Realloc(total)) return(false);
- //--- add
- memcpy((char*)m_data+(m_data_width*m_data_total),(const char*)elem,m_data_width*total);
- m_data_total+=total;
- //--- ok
- return(true);
- }
- //+------------------------------------------------------------------+
- //| Add array |
- //+------------------------------------------------------------------+
- inline bool CMTArrayBase::Add(const CMTArrayBase& array)
- {
- //--- check
- if(this==&array || !array.m_data || array.m_data_total<1) return(true);
- //--- check width
- if(array.m_data_width!=m_data_width) return(false);
- //--- add
- return(Add(array.m_data,array.m_data_total));
- }
- //+------------------------------------------------------------------+
- //| Add records [from,to] from array |
- //+------------------------------------------------------------------+
- inline bool CMTArrayBase::AddRange(const CMTArrayBase& array,const UINT from,const UINT to)
- {
- //--- check
- if(this==&array) return(true);
- //--- check width
- if(array.m_data_width!=m_data_width) return(false);
- //--- check borders
- if(from>to || to>=array.Total()) return(false);
- //--- add
- return(Add((char*)array.m_data+(from*array.m_data_width),(to-from)+1));
- }
- //+------------------------------------------------------------------+
- //| Add total uninitialized records |
- //+------------------------------------------------------------------+
- inline bool CMTArrayBase::AddEmpty(const UINT total)
- {
- if(!Realloc(total)) return(false);
- m_data_total+=total;
- return(true);
- }
- //+------------------------------------------------------------------+
- //| Add new uninitialized record and return pointer to them |
- //+------------------------------------------------------------------+
- inline void* CMTArrayBase::Append(void)
- {
- //--- check free space
- if(!Realloc(1)) return(NULL);
- //--- allocate
- if(m_data_total<m_data_max)
- {
- m_data_total++;
- return((char*)m_data+(m_data_total-1)*m_data_width);
- }
- //--- something wrong
- return(NULL);
- }
- //+------------------------------------------------------------------+
- //| Insert total records into pos position |
- //+------------------------------------------------------------------+
- inline bool CMTArrayBase::Insert(const UINT pos,const void *elem,const UINT total)
- {
- //--- check
- if(!elem || pos>m_data_total) return(false);
- //--- check free space
- if(!Realloc(total)) return(false);
- //--- move it if neccessary
- if(pos<m_data_total)
- memmove((char*)m_data+((pos+total)*m_data_width),(char*)m_data+(pos*m_data_width),(m_data_total-pos)*m_data_width);
- //--- insert
- memcpy((char*)m_data+(m_data_width*pos),(const char*)elem,m_data_width*total);
- m_data_total+=total;
- //--- ok
- return(true);
- }
- //+------------------------------------------------------------------+
- //| Insert total uninitialized records into pos position |
- //+------------------------------------------------------------------+
- inline bool CMTArrayBase::InsertEmpty(const UINT pos,const UINT total)
- {
- //--- check
- if(pos>m_data_total) return(false);
- //--- reallocate
- if(!Realloc(total)) return(false);
- //--- move it
- if(pos<m_data_total)
- memmove((char*)m_data+((pos+total)*m_data_width),(char*)m_data+(pos*m_data_width),(m_data_total-pos)*m_data_width);
- //--- correct total
- m_data_total+=total;
- //--- ok
- return(true);
- }
- //+------------------------------------------------------------------+
- //| Insert new record into sorted array |
- //| Returns NULL if record already exist or reallocation problem |
- //| Returns inserted record ptr otherwise |
- //+------------------------------------------------------------------+
- inline void* CMTArrayBase::Insert(const void *elem,SMTSearch::SortFunctionPtr sort_function)
- {
- void *result=NULL;
- //--- check
- if(!elem || !sort_function) return(result);
- //--- reallocate
- if(Realloc(1))
- if((result=SMTSearch::Insert(m_data,elem,m_data_total,m_data_width,sort_function))!=NULL)
- m_data_total++;
- //--- result
- return(result);
- }
- //+------------------------------------------------------------------+
- //| Delete record by position |
- //+------------------------------------------------------------------+
- inline bool CMTArrayBase::Delete(const UINT pos)
- {
- //--- check
- if(pos>=m_data_total || !m_data) return(false);
- //--- remove
- if((pos+1)<m_data_total)
- memmove((char*)m_data+(pos*m_data_width),(char*)m_data+((pos+1)*m_data_width),((m_data_total-pos)-1)*m_data_width);
- m_data_total--;
- //--- ok
- return(true);
- }
- //+------------------------------------------------------------------+
- //| Delete record by pointer |
- //+------------------------------------------------------------------+
- inline bool CMTArrayBase::Delete(const void *elem)
- {
- //--- check
- if(!elem || !m_data || elem<m_data || elem>=((char*)m_data+m_data_total*m_data_width)) return(false);
- //--- calculate position and delete
- return Delete(((UINT)((const char*)elem-(char*)m_data)/m_data_width));
- }
- //+------------------------------------------------------------------+
- //| Delete range [from,to] |
- //+------------------------------------------------------------------+
- inline bool CMTArrayBase::DeleteRange(const UINT from,const UINT to)
- {
- //--- check
- if(from>to || to>=m_data_total || !m_data) return(false);
- //--- delete
- if(from+1<m_data_total)
- memmove((char*)m_data+(from*m_data_width),
- (char*)m_data+((to+1)*m_data_width),((m_data_total-to)-1)*m_data_width);
- //--- correct total
- m_data_total-=((to-from)+1);
- //--- ok
- return(true);
- }
- //+------------------------------------------------------------------+
- //| Search and remove record from sorted array |
- //+------------------------------------------------------------------+
- inline bool CMTArrayBase::Remove(const void *elem,SMTSearch::SortFunctionPtr sort_function)
- {
- void *temp;
- UINT64 i;
- //--- check
- if(!elem || !sort_function || !m_data || m_data_total<1 || m_data_width<1) return(false);
- //--- search it
- if((temp=SMTSearch::Search(elem,m_data,m_data_total,m_data_width,sort_function))==NULL) return(false);
- //--- calc position
- i=((UINT64)((char*)temp-(char*)m_data))/m_data_width;
- //--- remove it
- if(i<m_data_total-1) memmove(temp,(char *)temp+m_data_width,(size_t)((m_data_total-i)-1)*m_data_width);
- m_data_total--;
- //--- ok
- return(true);
- }
- //+------------------------------------------------------------------+
- //| Update record |
- //+------------------------------------------------------------------+
- inline bool CMTArrayBase::Update(const UINT pos,const void *elem)
- {
- //--- check
- if(!elem || pos>=m_data_total || !m_data) return(false);
- //--- update
- memcpy((char*)m_data+(pos*m_data_width),(const char*)elem,m_data_width);
- //--- ok
- return(true);
- }
- //+------------------------------------------------------------------+
- //| Shift record from pos |
- //+------------------------------------------------------------------+
- inline bool CMTArrayBase::Shift(const UINT pos,const int shift)
- {
- UINT i,newpos=UINT((int)pos+shift);
- //--- check
- if(pos>=m_data_total || newpos>=m_data_total || !m_data) return(false);
- //--- we need one more record
- if(!Realloc(1)) return(false);
- //--- save current
- memcpy((char*)m_data+(m_data_total*m_data_width),(char*)m_data+(pos*m_data_width),m_data_width);
- //--- shift record by record
- if(newpos>pos)
- for(i=pos+1;i<=newpos;i++) memcpy((char*)m_data+((i-1)*m_data_width),(char*)m_data+(i*m_data_width),m_data_width);
- else
- for(i=pos;i>newpos;i--) memcpy((char*)m_data+(i*m_data_width),(char*)m_data+((i-1)*m_data_width),m_data_width);
- //--- restore current
- memcpy((char*)m_data+(newpos*m_data_width),(char*)m_data+(m_data_total*m_data_width),m_data_width);
- //--- ok
- return(true);
- }
- //+------------------------------------------------------------------+
- //| Remove size first elements |
- //+------------------------------------------------------------------+
- inline bool CMTArrayBase::Trim(const UINT size)
- {
- //--- check
- if(!m_data || size>m_data_total) return(false);
- //--- trim
- m_data_total-=size;
- memmove((char*)m_data,(char*)m_data+size*m_data_width,m_data_width*m_data_total);
- //--- ok
- return(true);
- }
- //+------------------------------------------------------------------+
- //| Next record by position |
- //+------------------------------------------------------------------+
- inline bool CMTArrayBase::Next(const UINT pos,void *elem) const
- {
- //--- check
- if(pos>=m_data_total || !elem || !m_data) return(false);
- //--- copy and return
- memcpy((char*)elem,(char*)m_data+(pos*m_data_width),m_data_width);
- return(true);
- }
- //+------------------------------------------------------------------+
- //| Next record by ptr |
- //+------------------------------------------------------------------+
- inline void* CMTArrayBase::Next(const void *elem) const
- {
- //--- check
- if(!elem || !m_data || elem<m_data || elem>=((char*)m_data+(m_data_total-1)*m_data_width)) return(NULL);
- //--- return next element
- return((char*)elem+m_data_width);
- }
- //+------------------------------------------------------------------+
- //| Prev record by ptr |
- //+------------------------------------------------------------------+
- inline void* CMTArrayBase::Prev(const void *elem) const
- {
- //--- check
- if(!elem || !m_data || elem<=m_data || elem>((char*)m_data+(m_data_total-1)*m_data_width)) return(NULL);
- //--- return next element
- return((char*)elem-m_data_width);
- }
- //+------------------------------------------------------------------+
- //| Ptr by position |
- //+------------------------------------------------------------------+
- inline const void* CMTArrayBase::At(const UINT pos) const
- {
- return((const char*)m_data+(pos*m_data_width));
- }
- //lint –restore
- //+------------------------------------------------------------------+
- //| Ptr by position |
- //+------------------------------------------------------------------+
- inline void* CMTArrayBase::At(const UINT pos)
- {
- return((char*)m_data+(pos*m_data_width));
- }
- //+------------------------------------------------------------------+
- //| Position by ptr |
- //+------------------------------------------------------------------+
- inline int CMTArrayBase::Position(const void* ptr) const
- {
- //--- check
- if(!ptr || !m_data || !m_data_width || ptr<m_data || ptr>=(char*)m_data+(m_data_total*m_data_width)) return(-1);
- //--- calc offset
- ldiv_t res=ldiv((long)((const char*)ptr-(char*)m_data),(long)m_data_width);
- //--- check ptr
- return(res.rem ? -1 : int(res.quot));
- }
- //+------------------------------------------------------------------+
- //| Receive range |
- //+------------------------------------------------------------------+
- inline bool CMTArrayBase::Range(const UINT from,const UINT to,void* data) const
- {
- //--- check
- if(from>to || from>=m_data_total || to>=m_data_total || !data || !m_data) return(false);
- //--- copy
- memcpy(data,(char*)m_data+(from*m_data_width),((to-from)+1)*m_data_width);
- return(true);
- }
- //+------------------------------------------------------------------+
- //| Sort array |
- //+------------------------------------------------------------------+
- inline void CMTArrayBase::Sort(SMTSearch::SortFunctionPtr sort_function)
- {
- if(m_data && m_data_total>0 && sort_function)
- qsort(m_data,m_data_total,m_data_width,sort_function);
- }
- //+------------------------------------------------------------------+
- //| Search by key value on sorted array |
- //+------------------------------------------------------------------+
- inline void* CMTArrayBase::Search(const void *key,SMTSearch::SortFunctionPtr sort_function) const
- {
- //--- check
- if(key && m_data && m_data_total>0 && sort_function)
- return(SMTSearch::Search(key,m_data,m_data_total,m_data_width,sort_function));
- //--- something wrong
- return(NULL);
- }
- //+------------------------------------------------------------------+
- //| Search great or equal than key value on sorted array |
- //+------------------------------------------------------------------+
- inline void* CMTArrayBase::SearchGreatOrEq(const void *key,SMTSearch::SortFunctionPtr sort_function) const
- {
- //--- check
- if(key && m_data && m_data_total>0 && sort_function)
- return(SMTSearch::SearchGreatOrEq(key,m_data,m_data_total,m_data_width,sort_function));
- //--- something wrong
- return(NULL);
- }
- //+------------------------------------------------------------------+
- //| Search great than key value on sorted array |
- //+------------------------------------------------------------------+
- inline void* CMTArrayBase::SearchGreater(const void *key,SMTSearch::SortFunctionPtr sort_function) const
- {
- //--- check
- if(key && m_data && m_data_total>0 && sort_function)
- return(SMTSearch::SearchGreater(key,m_data,m_data_total,m_data_width,sort_function));
- //--- something wrong
- return(NULL);
- }
- //+------------------------------------------------------------------+
- //| Search less or equal than key value on sorted array |
- //+------------------------------------------------------------------+
- inline void* CMTArrayBase::SearchLessOrEq(const void *key,SMTSearch::SortFunctionPtr sort_function) const
- {
- //--- check
- if(key && m_data && m_data_total>0 && sort_function)
- return(SMTSearch::SearchLessOrEq(key,m_data,m_data_total,m_data_width,sort_function));
- //--- something wrong
- return(NULL);
- }
- //+------------------------------------------------------------------+
- //| Search less than key value on sorted array |
- //+------------------------------------------------------------------+
- inline void* CMTArrayBase::SearchLess(const void *key,SMTSearch::SortFunctionPtr sort_function) const
- {
- //--- check
- if(key && m_data && m_data_total>0 && sort_function)
- return(SMTSearch::SearchLess(key,m_data,m_data_total,m_data_width,sort_function));
- //--- something wrong
- return(NULL);
- }
- //+------------------------------------------------------------------+
- //| Search first element with key equal to key value on sorted array |
- //+------------------------------------------------------------------+
- inline void* CMTArrayBase::SearchLeft(const void *key,SMTSearch::SortFunctionPtr sort_function) const
- {
- //--- check
- if(key && m_data && m_data_total>0 && sort_function)
- return(SMTSearch::SearchLeft(key,m_data,m_data_total,m_data_width,sort_function));
- //--- something wrong
- return(NULL);
- }
- //+------------------------------------------------------------------+
- //| Search last element with key equal to key value on sorted array |
- //+------------------------------------------------------------------+
- inline void* CMTArrayBase::SearchRight(const void *key,SMTSearch::SortFunctionPtr sort_function) const
- {
- //--- check
- if(key && m_data && m_data_total>0 && sort_function)
- return(SMTSearch::SearchRight(key,m_data,m_data_total,m_data_width,sort_function));
- //--- something wrong
- return(NULL);
- }
- //+------------------------------------------------------------------+
- //| Dynamic array template |
- //| For POD data types only! |
- //+------------------------------------------------------------------+
- template <class T,UINT step=16> class TMTArray : public CMTArrayBase
- {
- public:
- TMTArray() : CMTArrayBase(sizeof(T),step) {}
- virtual ~TMTArray(){}
- //--- global management
- void Swap(TMTArray<T,step> &arr) { CMTArrayBase::Swap(arr); }
- //--- add
- bool Add(const T *elem) { return CMTArrayBase::Add(elem); }
- bool Add(const T *elem,const UINT total) { return CMTArrayBase::Add(elem,total); }
- bool Add(const TMTArray<T,step>& arr) { return CMTArrayBase::Add(arr); }
- bool AddRange(const TMTArray<T,step>& arr,const UINT from,const UINT to)
- { return CMTArrayBase::AddRange(arr,from,to); }
- T* Append(void) { return(T*)CMTArrayBase::Append(); }
- bool Insert(const UINT pos,const T *elem) { return CMTArrayBase::Insert(pos,elem); }
- bool Insert(const UINT pos,const T *elem,const UINT total){ return CMTArrayBase::Insert(pos,elem,total); }
- T* Insert(const T *elem,SMTSearch::SortFunctionPtr order) { return(T*)CMTArrayBase::Insert(elem,order);}
- //--- delete
- bool Delete(const UINT pos) { return CMTArrayBase::Delete(pos); }
- bool Delete(const T *elem) { return CMTArrayBase::Delete(elem); }
- bool Remove(const T *elem,SMTSearch::SortFunctionPtr order) { return CMTArrayBase::Remove(elem,order); }
- //--- modify
- bool Update(const UINT pos,const T *elem) { return CMTArrayBase::Update(pos,elem); }
- //--- data access
- bool Next(const UINT pos,T *elem) const { return CMTArrayBase::Next(pos,elem); }
- T* Next(const T* elem) const { return(T*)CMTArrayBase::Next(elem); }
- T* Prev(const T* elem) { return(T*)CMTArrayBase::Prev(elem); }
- T* First(void) { return(T*)CMTArrayBase::At(0); }
- int Position(const T* ptr) const { return CMTArrayBase::Position(ptr); }
- bool Range(const UINT from,const UINT to,T* data) const { return CMTArrayBase::Range(from,to,data); }
- //--- search
- T* Search(const void *key,SMTSearch::SortFunctionPtr sort_function) const { return(T*)CMTArrayBase::Search(key,sort_function); }
- T* SearchGreatOrEq(const void *key,SMTSearch::SortFunctionPtr sort_function) const { return(T*)CMTArrayBase::SearchGreatOrEq(key,sort_function); }
- T* SearchGreater(const void *key,SMTSearch::SortFunctionPtr sort_function) const { return(T*)CMTArrayBase::SearchGreater(key,sort_function); }
- T* SearchLessOrEq(const void *key,SMTSearch::SortFunctionPtr sort_function) const { return(T*)CMTArrayBase::SearchLessOrEq(key,sort_function); }
- T* SearchLess(const void *key,SMTSearch::SortFunctionPtr sort_function) const { return(T*)CMTArrayBase::SearchLess(key,sort_function); }
- T* SearchLeft(const void *key,SMTSearch::SortFunctionPtr sort_function) const { return(T*)CMTArrayBase::SearchLeft(key,sort_function); }
- T* SearchRight(const void *key,SMTSearch::SortFunctionPtr sort_function) const { return(T*)CMTArrayBase::SearchRight(key,sort_function); }
- //--- operators
- const T& operator[](const UINT pos) const { return(*(T*)CMTArrayBase::At(pos)); }
- T& operator[](const UINT pos) { return(*(T*)CMTArrayBase::At(pos)); }
- bool operator==(const TMTArray<T,step>& arr) const { return(CMTArrayBase::Compare(arr)); }
- bool operator!=(const TMTArray<T,step>& arr) const { return(!CMTArrayBase::Compare(arr)); }
- TMTArray<T,step>& operator= (const TMTArray<T,step>& arr) { if(this!=&arr) Assign(arr); return(*this); }
- private:
- TMTArray(const TMTArray&) {};
- };
- //+------------------------------------------------------------------+
- //| Predefined arrays |
- //+------------------------------------------------------------------+
- typedef TMTArray<UCHAR> MTByteArray;
- typedef TMTArray<wchar_t> MTCharArray;
- typedef TMTArray<SHORT> MTShortArray;
- typedef TMTArray<USHORT> MTUShortArray;
- typedef TMTArray<int> MTIntArray;
- typedef TMTArray<UINT> MTUIntArray;
- typedef TMTArray<INT64> MTInt64Array;
- typedef TMTArray<UINT64> MTUInt64Array;
- typedef TMTArray<double> MTDoubleArray;
- //+------------------------------------------------------------------+
|