Chemical Data Processing Library C++ API - Version 1.2.0
IndirectArray.hpp
Go to the documentation of this file.
1 /*
2  * IndirectArray.hpp
3  *
4  * This file is part of the Chemical Data Processing Toolkit
5  *
6  * Copyright (C) 2003 Thomas Seidel <thomas.seidel@univie.ac.at>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this library; see the file COPYING. If not, write to
20  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23 
29 #ifndef CDPL_UTIL_INDIRECTARRAY_HPP
30 #define CDPL_UTIL_INDIRECTARRAY_HPP
31 
32 #include <type_traits>
33 
34 #include <boost/iterator/transform_iterator.hpp>
35 
37 #include "CDPL/Util/Array.hpp"
38 
39 
40 namespace CDPL
41 {
42 
43  namespace Util
44  {
45 
66  template <typename ValueType, typename PointerType = typename ValueType::SharedPointer, bool NullPointerCheck = true>
67  class IndirectArray : public Array<PointerType>
68  {
69 
70  typedef typename std::conditional<NullPointerCheck,
72  Dereferencer<PointerType, ValueType&> >::type DerefFunc;
73 
74  public:
80 
85  typedef ValueType ElementType;
86 
90  typedef std::shared_ptr<IndirectArray> SharedPointer;
91 
96  typedef boost::transform_iterator<DerefFunc, typename BaseType::ConstElementIterator,
97  const ValueType&>
99 
104  typedef boost::transform_iterator<DerefFunc, typename BaseType::ConstReverseElementIterator,
105  const ValueType&>
107 
112  typedef boost::transform_iterator<DerefFunc, typename BaseType::ElementIterator,
113  ValueType&>
115 
120  typedef boost::transform_iterator<DerefFunc, typename BaseType::ReverseElementIterator,
121  ValueType&>
127  BaseType() {}
128 
134  IndirectArray(std::size_t num_elem, const PointerType& ptr = PointerType()):
135  BaseType(num_elem, ptr) {}
136 
143  template <typename InputIter>
144  IndirectArray(const InputIter& first, const InputIter& last):
145  BaseType(first, last)
146  {}
147 
151  virtual ~IndirectArray() {}
152 
154 
161  ElementIterator insertElement(const ElementIterator& it, const PointerType& ptr);
162 
164 
172  void insertElements(const ElementIterator& it, std::size_t num_elem, const PointerType& ptr);
173 
182  template <typename InputIter>
183  void insertElements(const ElementIterator& it, const InputIter& first, const InputIter& last);
184 
186 
195 
197 
208 
216  const ValueType& getFirstElement() const;
217 
225  const ValueType& getLastElement() const;
226 
234  ValueType& getFirstElement();
235 
243  ValueType& getLastElement();
244 
251 
258 
265 
272 
279 
286 
293 
300 
307 
314 
321 
328 
340  const ValueType& getElement(std::size_t idx) const;
341 
353  ValueType& getElement(std::size_t idx);
354 
363  const ValueType& operator[](std::size_t idx) const;
364 
373  ValueType& operator[](std::size_t idx);
374 
375  private:
376  virtual const char* getClassName() const;
377 
378  DerefFunc dereferencer;
379  };
380  } // namespace Util
381 } // namespace CDPL
382 
383 
384 // Implementation of IndirectArray<>
385 
386 template <typename ValueType, typename PointerType, bool NullPointerCheck>
389 {
390  return ElementIterator(Array<PointerType>::insertElement(it.base(), ptr));
391 }
392 
393 template <typename ValueType, typename PointerType, bool NullPointerCheck>
395  const PointerType& ptr)
396 {
397  Array<PointerType>::insertElements(it.base(), n, ptr);
398 }
399 
400 template <typename ValueType, typename PointerType, bool NullPointerCheck>
401 template <typename InputIter>
403  const InputIter& last)
404 {
405  Array<PointerType>::insertElements(it.base(), first, last);
406 }
407 
408 template <typename ValueType, typename PointerType, bool NullPointerCheck>
411 {
413 }
414 
415 template <typename ValueType, typename PointerType, bool NullPointerCheck>
418 {
419  return ElementIterator(Array<PointerType>::removeElements(first.base(), last.base()));
420 }
421 
422 template <typename ValueType, typename PointerType, bool NullPointerCheck>
424 {
425  return dereferencer(Array<PointerType>::getFirstElement());
426 }
427 
428 template <typename ValueType, typename PointerType, bool NullPointerCheck>
430 {
431  return dereferencer(Array<PointerType>::getLastElement());
432 }
433 
434 template <typename ValueType, typename PointerType, bool NullPointerCheck>
436 {
437  return dereferencer(Array<PointerType>::getFirstElement());
438 }
439 
440 template <typename ValueType, typename PointerType, bool NullPointerCheck>
442 {
443  return dereferencer(Array<PointerType>::getLastElement());
444 }
445 
446 template <typename ValueType, typename PointerType, bool NullPointerCheck>
449 {
451 }
452 
453 template <typename ValueType, typename PointerType, bool NullPointerCheck>
456 {
458 }
459 
460 template <typename ValueType, typename PointerType, bool NullPointerCheck>
463 {
465 }
466 
467 template <typename ValueType, typename PointerType, bool NullPointerCheck>
470 {
472 }
473 
474 template <typename ValueType, typename PointerType, bool NullPointerCheck>
477 {
479 }
480 
481 template <typename ValueType, typename PointerType, bool NullPointerCheck>
484 {
486 }
487 
488 template <typename ValueType, typename PointerType, bool NullPointerCheck>
491 {
493 }
494 
495 template <typename ValueType, typename PointerType, bool NullPointerCheck>
498 {
500 }
501 
502 template <typename ValueType, typename PointerType, bool NullPointerCheck>
505 {
507 }
508 
509 template <typename ValueType, typename PointerType, bool NullPointerCheck>
512 {
514 }
515 
516 template <typename ValueType, typename PointerType, bool NullPointerCheck>
519 {
521 }
522 
523 template <typename ValueType, typename PointerType, bool NullPointerCheck>
526 {
528 }
529 
530 template <typename ValueType, typename PointerType, bool NullPointerCheck>
532 {
533  return dereferencer(Array<PointerType>::getElement(idx));
534 }
535 
536 template <typename ValueType, typename PointerType, bool NullPointerCheck>
538 {
539  return dereferencer(Array<PointerType>::getElement(idx));
540 }
541 
542 template <typename ValueType, typename PointerType, bool NullPointerCheck>
544 {
545  return dereferencer(Array<PointerType>::operator[](idx));
546 }
547 
548 template <typename ValueType, typename PointerType, bool NullPointerCheck>
550 {
551  return dereferencer(Array<PointerType>::operator[](idx));
552 }
553 
554 template <typename ValueType, typename PointerType, bool NullPointerCheck>
556 {
557  return "IndirectArray";
558 }
559 
560 #endif // CDPL_UTIL_INDIRECTARRAY_HPP
Definition of the class CDPL::Util::Array.
Definition of the classes CDPL::Util::Dereferencer and CDPL::Util::NullCheckDereferencer.
A dynamic array class providing amortized constant time access to arbitrary elements.
Definition: Array.hpp:92
StorageType::const_iterator ConstElementIterator
A constant random access iterator used to iterate over the elements of the array.
Definition: Array.hpp:125
StorageType::const_reverse_iterator ConstReverseElementIterator
A constant random access iterator used to iterate over the elements of the array in reverse order.
Definition: Array.hpp:131
StorageType::reverse_iterator ReverseElementIterator
A mutable random access iterator used to iterate over the elements of the array in reverse order.
Definition: Array.hpp:143
StorageType::iterator ElementIterator
A mutable random access iterator used to iterate over the elements of the array.
Definition: Array.hpp:137
void removeElement(std::size_t idx)
Removes the element at the position specified by the index idx.
Definition: Array.hpp:823
void insertElement(std::size_t idx, const ValueType &value=ValueType())
Inserts a new element before the location specified by the index idx.
Definition: Array.hpp:763
void insertElements(std::size_t idx, std::size_t num_elem, const ValueType &value=ValueType())
Inserts num_elem copies of value before the location specified by the index idx.
Definition: Array.hpp:780
ElementIterator removeElements(const ElementIterator &first, const ElementIterator &last)
Removes the elements pointed to by the iterators in the range [first, last).
Definition: Array.hpp:839
A dynamic array class for the storage of object pointers with an indirected query interface.
Definition: IndirectArray.hpp:68
ElementIterator removeElement(const ElementIterator &it)
Removes the element at the position specified by the iterator it.
Definition: IndirectArray.hpp:410
const ValueType & getLastElement() const
Returns a const reference to the object pointed to by the last element of the array.
Definition: IndirectArray.hpp:429
ValueType & getElement(std::size_t idx)
Returns a non-const reference to the object pointed-to by the pointer element at index idx.
Definition: IndirectArray.hpp:537
const ValueType & getFirstElement() const
Returns a const reference to the object pointed to by the first element of the array.
Definition: IndirectArray.hpp:423
ElementIterator begin()
Returns a mutable iterator over the pointed-to objects that points to the beginning of the array.
Definition: IndirectArray.hpp:483
IndirectArray(std::size_t num_elem, const PointerType &ptr=PointerType())
Creates and initializes the array with num_elem copies of ptr.
Definition: IndirectArray.hpp:134
ValueType & operator[](std::size_t idx)
Returns a non-const reference to the object pointed-to by the pointer element at index idx.
Definition: IndirectArray.hpp:549
ConstElementIterator getElementsBegin() const
Returns a constant iterator over the pointed-to objects that points to the beginning of the array.
Definition: IndirectArray.hpp:448
ConstElementIterator getElementsEnd() const
Returns a constant iterator over the pointed-to objects that points to the end of the array.
Definition: IndirectArray.hpp:462
boost::transform_iterator< DerefFunc, typename BaseType::ReverseElementIterator, ValueType & > ReverseElementIterator
A mutable random access iterator used to iterate over the pointed-to objects in reverse order.
Definition: IndirectArray.hpp:122
ConstReverseElementIterator getElementsReverseBegin() const
Returns a constant iterator over the pointed-to objects that points to the beginning of the reversed ...
Definition: IndirectArray.hpp:504
const ValueType & operator[](std::size_t idx) const
Returns a const reference to the object pointed-to by the pointer element at index idx.
Definition: IndirectArray.hpp:543
ElementIterator getElementsEnd()
Returns a mutable iterator over the pointed-to objects that points to the end of the array.
Definition: IndirectArray.hpp:469
ElementIterator end()
Returns a mutable iterator over the pointed-to objects that points to the end of the array.
Definition: IndirectArray.hpp:497
ReverseElementIterator getElementsReverseBegin()
Returns a mutable iterator over the pointed-to objects that points to the beginning of the reversed a...
Definition: IndirectArray.hpp:511
const ValueType & getElement(std::size_t idx) const
Returns a const reference to the object pointed-to by the pointer element at index idx.
Definition: IndirectArray.hpp:531
IndirectArray(const InputIter &first, const InputIter &last)
Creates and initializes the array with copies of the pointers in the range [first,...
Definition: IndirectArray.hpp:144
void removeElement(std::size_t idx)
Removes the element at the position specified by the index idx.
Definition: Array.hpp:823
Array< PointerType > BaseType
The type of the public Array<PointerType> base class storing the pointer elements.
Definition: IndirectArray.hpp:79
ValueType & getFirstElement()
Returns a non-const reference to the object pointed to by the first element of the array.
Definition: IndirectArray.hpp:435
virtual ~IndirectArray()
Virtual destructor.
Definition: IndirectArray.hpp:151
ReverseElementIterator getElementsReverseEnd()
Returns a mutable iterator over the pointed-to objects that points to the end of the reversed array.
Definition: IndirectArray.hpp:525
IndirectArray()
Creates an empty array.
Definition: IndirectArray.hpp:126
void insertElements(const ElementIterator &it, std::size_t num_elem, const PointerType &ptr)
Inserts num_elem copies of ptr before the location specified by the iterator it.
Definition: IndirectArray.hpp:394
ElementIterator insertElement(const ElementIterator &it, const PointerType &ptr)
Inserts a new element before the location specified by the iterator it.
Definition: IndirectArray.hpp:388
boost::transform_iterator< DerefFunc, typename BaseType::ConstElementIterator, const ValueType & > ConstElementIterator
A constant random access iterator used to iterate over the pointed-to objects.
Definition: IndirectArray.hpp:98
ValueType & getLastElement()
Returns a non-const reference to the object pointed to by the last element of the array.
Definition: IndirectArray.hpp:441
ElementIterator getElementsBegin()
Returns a mutable iterator over the pointed-to objects that points to the beginning of the array.
Definition: IndirectArray.hpp:455
boost::transform_iterator< DerefFunc, typename BaseType::ConstReverseElementIterator, const ValueType & > ConstReverseElementIterator
A constant random access iterator used to iterate over the pointed-to objects in reverse order.
Definition: IndirectArray.hpp:106
std::shared_ptr< IndirectArray > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated IndirectArray instances.
Definition: IndirectArray.hpp:90
void insertElement(std::size_t idx, const ValueType &value=ValueType())
Inserts a new element before the location specified by the index idx.
Definition: Array.hpp:763
void insertElements(std::size_t idx, std::size_t num_elem, const ValueType &value=ValueType())
Inserts num_elem copies of value before the location specified by the index idx.
Definition: Array.hpp:780
ElementIterator removeElements(const ElementIterator &first, const ElementIterator &last)
Removes the elements pointed to by the iterators in the range [first, last).
Definition: Array.hpp:839
ValueType ElementType
The type of the object that is obtained after dereferenciation of a stored pointer element.
Definition: IndirectArray.hpp:85
boost::transform_iterator< DerefFunc, typename BaseType::ElementIterator, ValueType & > ElementIterator
A mutable random access iterator used to iterate over the pointed-to objects.
Definition: IndirectArray.hpp:114
ConstReverseElementIterator getElementsReverseEnd() const
Returns a constant iterator over the pointed-to objects that points to the end of the reversed array.
Definition: IndirectArray.hpp:518
ElementIterator removeElements(const ElementIterator &first, const ElementIterator &last)
Removes the elements pointed to by the iterators in the range [first, last).
Definition: IndirectArray.hpp:417
ConstElementIterator end() const
Returns a constant iterator over the pointed-to objects that points to the end of the array.
Definition: IndirectArray.hpp:490
void insertElements(const ElementIterator &it, const InputIter &first, const InputIter &last)
Inserts the range of pointers [first, last) before the location specified by the iterator it.
Definition: IndirectArray.hpp:402
ConstElementIterator begin() const
Returns a constant iterator over the pointed-to objects that points to the beginning of the array.
Definition: IndirectArray.hpp:476
The namespace of the Chemical Data Processing Library.
An unary functor for the dereferenciation of pointers without null pointer checking.
Definition: Dereferencer.hpp:52
An unary functor for the dereferenciation of pointers with null pointer checking.
Definition: Dereferencer.hpp:76