Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cArraySortString.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cArraySortString_H
7 #define _INC_cArraySortString_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "cArraySortRef.h"
13 #include "cArrayString.h"
14 #include "cUnitTestDecl.h"
15 
16 namespace Gray
17 {
18  UNITTEST2_PREDEF(cArraySortString);
19 
20  template< typename _TYPE_CH = TCHAR >
21  class GRAYCORE_LINK cArraySortString : public cArraySorted < cStringT<_TYPE_CH>, cStringT<_TYPE_CH>, const _TYPE_CH* >
22  {
25 
26  public:
28  typedef cStringT<_TYPE_CH> STR_t; // alias for container
29  typedef typename SUPER_t::REF_t REF_t;
30  typedef typename SUPER_t::KEY_t KEY_t;
31 
32  public:
34  {
35  }
36  virtual COMPARE_t CompareKey(KEY_t pszID1, REF_t sID2) const override
37  {
38  ASSERT(pszID1 != nullptr);
39  return StrT::CmpI<_TYPE_CH>(pszID1, sID2);
40  }
41  virtual COMPARE_t CompareData(REF_t sID1, REF_t sID2) const noexcept override
42  {
43  return StrT::CmpI<_TYPE_CH>(sID1, sID2);
44  }
45 
46  ITERATE_t AddStr(const _TYPE_CH* pszStr)
47  {
48  return this->Add(STR_t(pszStr));
49  }
50 
51  ITERATE_t FindKeyRoot(const _TYPE_CH* pszRoot)
52  {
57 
58  StrLen_t iStrLen = StrT::Len(pszRoot);
59  ITERATE_t iHigh = this->GetSize() - 1;
60  ITERATE_t iLow = 0;
61  while (iLow <= iHigh)
62  {
63  ITERATE_t i = (iHigh + iLow) / 2;
64  STR_t sCur = this->GetAt(i);
65  COMPARE_t iCompare = StrT::CmpIN<_TYPE_CH>(pszRoot, sCur, iStrLen);
66  if (iCompare == COMPARE_Equal)
67  return i; // pszRoot is a parent of
68  if (iCompare > 0)
69  {
70  iLow = i + 1;
71  }
72  else
73  {
74  iHigh = i - 1;
75  }
76  }
77  return k_ITERATE_BAD;
78  }
79 
80  ITERATE_t FindKeyDerived(const _TYPE_CH* pszDerived)
81  {
86 
87  ITERATE_t iHigh = this->GetSize() - 1;
88  ITERATE_t iLow = 0;
89  while (iLow <= iHigh)
90  {
91  ITERATE_t i = (iHigh + iLow) / 2;
92  STR_t sCur = this->GetAt(i);
93  COMPARE_t iCompare = StrT::CmpIN<_TYPE_CH>(pszDerived, sCur, sCur.GetLength());
94  if (iCompare == COMPARE_Equal)
95  return(i); // pszDerived is a child of sCur (derived from root sCur)
96  if (iCompare > 0)
97  {
98  iLow = i + 1;
99  }
100  else
101  {
102  iHigh = i - 1;
103  }
104  }
105  return k_ITERATE_BAD;
106  }
107 
109  };
110 
113 
114 }
115 
116 #endif
#define GRAYCORE_LINK
Definition: GrayCore.h:47
#define ASSERT(exp)
Definition: cDebugAssert.h:87
#define UNITTEST2_PREDEF(x)
Definition: cUnitTestDecl.h:19
#define UNITTEST_FRIEND(n)
Define this in the class body to be unit tested. Allow the unit test to access private/protected stuf...
Definition: cUnitTestDecl.h:17
StrLen_t GetLength() const noexcept
Definition: cString.h:183
Definition: cArraySortString.h:22
SUPER_t::KEY_t KEY_t
Definition: cArraySortString.h:30
virtual ~cArraySortString()
Definition: cArraySortString.h:33
cStringT< _TYPE_CH > STR_t
Definition: cArraySortString.h:28
ITERATE_t FindKeyRoot(const _TYPE_CH *pszRoot)
Definition: cArraySortString.h:51
SUPER_t::REF_t REF_t
Definition: cArraySortString.h:29
virtual COMPARE_t CompareData(REF_t sID1, REF_t sID2) const noexcept override
Definition: cArraySortString.h:41
ITERATE_t FindKeyDerived(const _TYPE_CH *pszDerived)
Definition: cArraySortString.h:80
cArraySorted< cStringT< _TYPE_CH >, cStringT< _TYPE_CH >, const _TYPE_CH * > SUPER_t
Definition: cArraySortString.h:27
virtual COMPARE_t CompareKey(KEY_t pszID1, REF_t sID2) const override
Definition: cArraySortString.h:36
ITERATE_t AddStr(const _TYPE_CH *pszStr)
Definition: cArraySortString.h:46
Definition: cArraySort.h:24
ARG_TYPE REF_t
How to refer to this? value or ref or pointer?
Definition: cArray.h:449
Definition: cString.h:381
< The main namespace for all Core functions.
Definition: GrayCore.cpp:14
int COMPARE_t
result of compare. 0=same, 1=a>b, -1=a<b
Definition: cValT.h:17
int StrLen_t
the length of a string in chars (bytes for UTF8, wchar_t for UNICODE). or offset in characters....
Definition: StrConst.h:32
int ITERATE_t
like size_t but signed
Definition: Index.h:28
const ITERATE_t k_ITERATE_BAD
Definition: Index.h:30
cArraySortString< char > cArraySortStringA
Definition: cArraySortString.h:111
@ COMPARE_Equal
VARCMP_EQ.
Definition: cValT.h:23
cArraySortString< wchar_t > cArraySortStringW
Definition: cArraySortString.h:112
static StrLen_t Len(const TYPE *pszStr) noexcept