gatelib  2.1
g_cont_alg.h
1 #pragma once
2 
3 #include "g_common_def.h"
4 #include <sstream>
5 
6 namespace g
7 {
8 namespace cont
9 {
10 
11 template <class T> class gstr;
12 
13 namespace alg
14 {
15 
16 template < class IT1 , class IT2 > inline size_t copy ( IT1 aFrom , IT1 aTo , IT2 aDestination )
17 {
18  size_t result=0;
19 
20  for ( ; aFrom != aTo && aFrom.isIn(); result++ , aDestination++ , aFrom++ )
21  {
22  aDestination.underlying() = aFrom.underlying();
23  }
24 
25  return result;
26 }
27 
28 template < class IT1 , class IT2 > inline size_t copy ( IT1 aFrom , IT2 aDestination )
29 {
30  size_t result=0;
31 
32  for ( ; aFrom.isIn(); result++ , aDestination++ , aFrom++ )
33  {
34  aDestination.underlying() = aFrom.underlying();
35  }
36 
37  return result;
38 }
39 
40 template < class T , class IT2 > inline size_t copy ( const T* aVectorP , size_t aSize , IT2 aDestination )
41 {
42  for ( int i = 0 ; i < (int)aSize ; i++ )
43  {
44  aDestination.underlying() = aVectorP[i];
45  }
46 
47  return aSize;
48 }
49 
50 template < class IT1 , class IT2 > inline size_t copy_reverse ( IT1 aFrom , IT1 aTo , IT2 aDestination )
51 {
52  size_t result=0;
53 
54  for ( ; aFrom != aTo ; result++ , aDestination++ , aFrom-- )
55  {
56  aDestination.underlying() = aFrom.underlying();
57  }
58 
59  return result;
60 }
61 
62 template < class T , class IT1 > inline IT1 search ( IT1 aFrom , IT1 aTo , T aValue )
63 {
64  for ( ; aFrom.isIn() ; aFrom++ )
65  {
66  if ( aFrom.underlying() == aValue )
67  {
68  return aFrom;
69  }
70 
71  if ( aFrom == aTo )
72  {
73  return IT1();//search failed
74  }
75  }
76 
77  return IT1();
78 }
79 
80 template < class T , class IT1 > inline IT1 search ( IT1 aFrom , T aValue )
81 {
82  for ( ; aFrom.isIn() ; aFrom++ )
83  {
84  if ( aFrom.underlying() == aValue )
85  {
86  return aFrom;
87  }
88  }
89 
90  return IT1();
91 }
92 
93 template < class R, class T >
94  inline gstr<T> get_cont_string ( const R& aCont , const T* aSeparator = (const T*)gstr<T>::space() )
95 {
96  typename R::ItConst_t it(aCont);
97 
98  std::basic_stringstream<T> output;
99 
100  if(it.isIn())
101  {
102  output << *it;
103 
104  for( ++it ; it.isIn() ; it++ )
105  {
106  output << aSeparator << *it;
107  }
108  }
109 
110  return output.str().c_str();
111 }
112 
113 //tries to remove the first occurence of aItem returns true if it succeeds.
114 template < class T , class R > inline bool try_remove ( R& aRecipient , T aItem )
115 {
116  typename R::It_t it = search( aRecipient.getIterator(head) , aRecipient.getIterator(tail) , aItem );
117 
118  if ( it.isIn())
119  {
120  aRecipient.remove(it);
121 
122  return true;
123  }
124  else
125  {
126  return false;
127  }
128 }
129 
130 //removes all occurences item equal to a item, returns the number of 'remove' instances.
131 template < class T , class R > inline size_t remove_all ( R& aRecipient , T aItem )
132 {
133  typename R::It_t it = search( aRecipient.getIterator(head) , aItem );
134  size_t result = 0;//number of removed items
135 
136  while ( it.isIn() )
137  {
138  aRecipient.remove(it);
139  result++;
140  it = search ( it , aItem );
141  }
142 
143  return result;
144 }
145 
146 }//namespace alg
147 }//namespace cont
148 }//namespace g
149 
150 
Definition: g.mthread.ThreadSimpleEvent.h:5
Definition: g_cont_alg.h:11