Chemical Data Processing Library C++ API - Version 1.1.1
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
CDPL::Util::IndirectArray::getElementsReverseBegin
ConstReverseElementIterator getElementsReverseBegin() const
Returns a constant iterator over the pointed-to objects that points to the beginning of the reversed ...
Definition: IndirectArray.hpp:504
CDPL::Util::IndirectArray::getElementsReverseBegin
ReverseElementIterator getElementsReverseBegin()
Returns a mutable iterator over the pointed-to objects that points to the beginning of the reversed a...
Definition: IndirectArray.hpp:511
CDPL::Util::IndirectArray::insertElements
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
CDPL::Util::Array::removeElements
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
CDPL::Util::Array::ReverseElementIterator
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
CDPL::Util::IndirectArray::insertElements
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
CDPL::Util::IndirectArray::getElement
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
CDPL::Util::IndirectArray::getElementsReverseEnd
ReverseElementIterator getElementsReverseEnd()
Returns a mutable iterator over the pointed-to objects that points to the end of the reversed array.
Definition: IndirectArray.hpp:525
CDPL::Util::IndirectArray::getLastElement
const ValueType & getLastElement() const
Returns a const reference to the object pointed to by the last element of the array.
Definition: IndirectArray.hpp:429
CDPL::Util::IndirectArray::end
ElementIterator end()
Returns a mutable iterator over the pointed-to objects that points to the end of the array.
Definition: IndirectArray.hpp:497
CDPL::Util::IndirectArray::IndirectArray
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
CDPL::Util::IndirectArray::insertElement
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
CDPL::Util::IndirectArray::insertElement
ElementIterator insertElement(const ElementIterator &it, const PointerType &ptr)
Inserts a new element before the location specified by the iterator it.
Definition: IndirectArray.hpp:388
CDPL::Util::IndirectArray::~IndirectArray
virtual ~IndirectArray()
Virtual destructor.
Definition: IndirectArray.hpp:151
CDPL::Util::IndirectArray::begin
ElementIterator begin()
Returns a mutable iterator over the pointed-to objects that points to the beginning of the array.
Definition: IndirectArray.hpp:483
CDPL::Util::IndirectArray::getElementsBegin
ConstElementIterator getElementsBegin() const
Returns a constant iterator over the pointed-to objects that points to the beginning of the array.
Definition: IndirectArray.hpp:448
CDPL::Util::IndirectArray::insertElements
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
CDPL::Util::IndirectArray
A dynamic array class for the storage of object pointers with an indirected query interface.
Definition: IndirectArray.hpp:68
CDPL::Util::Array
A dynamic array class providing amortized constant time access to arbitrary elements.
Definition: Array.hpp:92
CDPL::Util::IndirectArray::SharedPointer
std::shared_ptr< IndirectArray > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated IndirectArray instances.
Definition: IndirectArray.hpp:90
CDPL::Util::NullCheckDereferencer
An unary functor for the dereferenciation of pointers with null pointer checking.
Definition: Dereferencer.hpp:76
CDPL::Util::Dereferencer
An unary functor for the dereferenciation of pointers without null pointer checking.
Definition: Dereferencer.hpp:52
CDPL::Util::IndirectArray::getElementsEnd
ElementIterator getElementsEnd()
Returns a mutable iterator over the pointed-to objects that points to the end of the array.
Definition: IndirectArray.hpp:469
CDPL::Util::IndirectArray::removeElement
ElementIterator removeElement(const ElementIterator &it)
Removes the element at the position specified by the iterator it.
Definition: IndirectArray.hpp:410
CDPL::Util::Array::ConstReverseElementIterator
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
CDPL::Util::IndirectArray::BaseType
Array< PointerType > BaseType
The type of the public Array<PointerType> base class storing the pointer elements.
Definition: IndirectArray.hpp:79
CDPL::Util::Array::insertElements
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
Array.hpp
Definition of the class CDPL::Util::Array.
CDPL::Util::IndirectArray::ConstElementIterator
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
CDPL::Util::IndirectArray::removeElement
void removeElement(std::size_t idx)
Removes the element at the position specified by the index idx.
Definition: Array.hpp:823
CDPL::Util::IndirectArray::IndirectArray
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
CDPL::Util::IndirectArray::getElementsBegin
ElementIterator getElementsBegin()
Returns a mutable iterator over the pointed-to objects that points to the beginning of the array.
Definition: IndirectArray.hpp:455
CDPL::Util::IndirectArray::getElementsEnd
ConstElementIterator getElementsEnd() const
Returns a constant iterator over the pointed-to objects that points to the end of the array.
Definition: IndirectArray.hpp:462
CDPL::Util::Array::ConstElementIterator
StorageType::const_iterator ConstElementIterator
A constant random access iterator used to iterate over the elements of the array.
Definition: Array.hpp:125
CDPL::Util::IndirectArray::getLastElement
ValueType & getLastElement()
Returns a non-const reference to the object pointed to by the last element of the array.
Definition: IndirectArray.hpp:441
CDPL::Util::IndirectArray::IndirectArray
IndirectArray()
Creates an empty array.
Definition: IndirectArray.hpp:126
CDPL::Util::IndirectArray::removeElements
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
CDPL::Util::IndirectArray::ElementType
ValueType ElementType
The type of the object that is obtained after dereferenciation of a stored pointer element.
Definition: IndirectArray.hpp:85
CDPL::Util::IndirectArray::ReverseElementIterator
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
CDPL
The namespace of the Chemical Data Processing Library.
CDPL::Util::IndirectArray::ConstReverseElementIterator
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
CDPL::Util::IndirectArray::getElement
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
CDPL::Util::IndirectArray::operator[]
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
CDPL::Util::IndirectArray::end
ConstElementIterator end() const
Returns a constant iterator over the pointed-to objects that points to the end of the array.
Definition: IndirectArray.hpp:490
CDPL::Util::IndirectArray::getElementsReverseEnd
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
CDPL::Util::IndirectArray::getFirstElement
ValueType & getFirstElement()
Returns a non-const reference to the object pointed to by the first element of the array.
Definition: IndirectArray.hpp:435
CDPL::Util::IndirectArray::ElementIterator
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
CDPL::Util::IndirectArray::begin
ConstElementIterator begin() const
Returns a constant iterator over the pointed-to objects that points to the beginning of the array.
Definition: IndirectArray.hpp:476
Dereferencer.hpp
Definition of the classes CDPL::Util::Dereferencer and CDPL::Util::NullCheckDereferencer.
CDPL::Util::Array::insertElement
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
CDPL::Util::IndirectArray::operator[]
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
CDPL::Util::Array::ElementIterator
StorageType::iterator ElementIterator
A mutable random access iterator used to iterate over the elements of the array.
Definition: Array.hpp:137
CDPL::Util::Array::removeElement
void removeElement(std::size_t idx)
Removes the element at the position specified by the index idx.
Definition: Array.hpp:823
CDPL::Util::IndirectArray::removeElements
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
CDPL::Util::IndirectArray::getFirstElement
const ValueType & getFirstElement() const
Returns a const reference to the object pointed to by the first element of the array.
Definition: IndirectArray.hpp:423