]>
git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/Array.cpp
a2ec27b101be5ba3c834e1b1f754586a0d336bc0
2 * This file is defining a kind of tool for simulating std::vector
3 * for using wx on OS which are not supporting the STL
4 * @author foldink (foldink@gmail.com)
5 * @date 26-February-2010
11 #include "Scintilla.h"
12 #include "Selection.h"
15 SelectionRange
& ArrayIterator::operator*()
17 if ( m_idx
>= m_parent
->size() )
18 throw "Error access out of bounds";
20 return (*m_parent
)[m_idx
];
23 ArrayIterator
ArrayIterator::operator++(int)
25 ArrayIterator
ret(*this);
30 ArrayIterator
& ArrayIterator::operator++()
36 ArrayIterator
ArrayIterator::operator--(int)
39 throw "Error access out of bounds";
41 ArrayIterator
ret(*this);
46 ArrayIterator
& ArrayIterator::operator--()
55 ArrayIterator
ArrayIterator::operator+(size_t idx
)
57 ArrayIterator
ret(*this);
62 ArrayIterator
ArrayIterator::operator-(size_t idx
)
65 throw "Error access out of bounds";
67 ArrayIterator
ret(*this);
72 ArrayIterator
& ArrayIterator::operator+=(size_t idx
)
78 ArrayIterator
& ArrayIterator::operator-=(size_t idx
)
81 throw "Error access out of bounds";
87 bool ArrayIterator::operator!=(const ArrayIterator
& rhs
)
89 if ( m_parent
== rhs
.m_parent
&& m_idx
== rhs
.m_idx
)
95 bool ArrayIterator::operator==(const ArrayIterator
& rhs
)
97 if ( m_parent
== rhs
.m_parent
&& m_idx
== rhs
.m_idx
)
103 Array::Array( size_t len
):
110 m_data
= new SelectionRange
[len
];
114 Array::Array( size_t len
, const SelectionRange
& val
):
121 m_data
= new SelectionRange
[len
];
124 for ( size_t i
= 0; i
< len
; ++i
)
128 Array::Array( const Array
& rhs
):
132 if ( rhs
.m_size
== 0 )
135 m_data
= new SelectionRange
[rhs
.m_size
];
137 memcpy( m_data
, rhs
.m_data
, m_size
*sizeof(SelectionRange
) );
146 Array
& Array::operator=( const Array
& rhs
)
154 if ( rhs
.m_size
== 0 )
157 m_data
= new SelectionRange
[rhs
.m_size
];
159 memcpy( m_data
, rhs
.m_data
, m_size
*sizeof(SelectionRange
) );
164 void Array::reserve( size_t len
)
166 if ( len
<= m_size
|| len
== 0 )
169 SelectionRange
* data
= 0L;
170 data
= new SelectionRange
[len
];
173 memcpy( data
, m_data
, m_size
*sizeof(SelectionRange
) );
180 void Array::push_back( const SelectionRange
& val
)
183 m_data
= new SelectionRange
[1];
189 SelectionRange
* data
= 0L;
190 data
= new SelectionRange
[m_size
+1];
191 memcpy( data
, m_data
, m_size
*sizeof(SelectionRange
) );
193 *(m_data
+m_size
) = val
;
197 void Array::pop_back( )
200 throw "Error access out of bounds";
203 SelectionRange
* data
= 0L;
206 data
= new SelectionRange
[m_size
];
207 memcpy( data
, m_data
, m_size
*sizeof(SelectionRange
) );
214 void Array::insert( ArrayIterator it
, SelectionRange
& val
)
216 size_t idx
= it
.GetIdx();
217 SelectionRange
* data
= 0L;
220 m_data
= new SelectionRange
[m_size
];
221 } else if ( idx
>= m_size
) {
222 data
= new SelectionRange
[idx
+1];
223 memcpy( data
, m_data
, m_size
*sizeof(SelectionRange
) );
227 } else if ( idx
== 0 ) {
228 data
= new SelectionRange
[m_size
+1];
229 memcpy( data
+ 1 , m_data
, m_size
*sizeof(SelectionRange
) );
234 data
= new SelectionRange
[m_size
+1];
235 memcpy( data
, m_data
, idx
*sizeof(SelectionRange
) );
236 memcpy( data
+ idx
+ 1 , m_data
+ idx
, (m_size
-idx
)*sizeof(SelectionRange
) );
242 *(m_data
+ idx
) = val
;
245 void Array::erase( ArrayIterator it
)
247 size_t idx
= it
.GetIdx();
248 if (!m_data
|| idx
>= m_size
)
249 throw "Error access out of bounds";
251 SelectionRange
* data
= 0L;
256 } else if ( idx
== 0) {
257 data
= new SelectionRange
[m_size
-1];
258 memcpy( data
, m_data
+ 1 , (m_size
-1)*sizeof(SelectionRange
) );
263 data
= new SelectionRange
[m_size
-1];
264 memcpy( data
, m_data
, (idx
)*sizeof(SelectionRange
) );
265 memcpy( data
+ idx
, m_data
+ idx
+ 1 , (m_size
-idx
-1)*sizeof(SelectionRange
) );
282 void Array::resize( size_t n
, SelectionRange val
)
284 if ( (n
== 0 && !m_data
) || n
<= m_size
)
293 SelectionRange
* data
= 0L;
294 data
= new SelectionRange
[n
];
295 memcpy( data
, m_data
, m_size
*sizeof(SelectionRange
) );
299 for ( size_t i
= m_size
; i
< n
; ++i
)
300 *( m_data
+ i
) = val
;
305 bool Array::operator==(const Array
& rhs
)
307 if ( m_size
!= rhs
.m_size
)
310 if ( m_size
== 0 && rhs
.m_size
== 0 )
313 for ( size_t i
= 0 ; i
< m_size
; ++i
)
314 if ( ! ((*(m_data
+i
)) == (*(rhs
.m_data
+i
))) )
320 ArrayIterator
Array::begin()
322 ArrayIterator
ret(this,0);
326 ArrayIterator
Array::end()
328 ArrayIterator
ret(this,m_size
);
333 int partition(Array
&array
, int top
, int bottom
)
335 SelectionRange x
= array
[top
];
342 } while (x
< array
[j
]);
346 } while (array
[j
] < x
);
354 return j
; // returns middle subscript
357 void quicksort(Array
&num
, int top
, int bottom
)
359 // top = subscript of beginning of array
360 // bottom = subscript of end of array
364 middle
= partition(num
, top
, bottom
);
365 quicksort(num
, top
, middle
); // sort first section
366 quicksort(num
, middle
+1, bottom
); // sort second section
371 void ArraySort( ArrayIterator start
, ArrayIterator finish
)
373 if ( start
.m_parent
!= finish
.m_parent
)
374 throw "Invalid iterators parent mismatch";
376 if ( start
.m_idx
>= finish
.m_idx
-1 )
377 throw "Invalid iterators are refering to bad values";
379 quicksort( *(start
.m_parent
) , start
.m_idx
, finish
.m_idx
-1 );