10ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Ceres Solver - A fast non-linear least squares minimizer
20ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Copyright 2010, 2011, 2012 Google Inc. All rights reserved.
30ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// http://code.google.com/p/ceres-solver/
40ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
50ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Redistribution and use in source and binary forms, with or without
60ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// modification, are permitted provided that the following conditions are met:
70ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
80ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Redistributions of source code must retain the above copyright notice,
90ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   this list of conditions and the following disclaimer.
100ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Redistributions in binary form must reproduce the above copyright notice,
110ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   this list of conditions and the following disclaimer in the documentation
120ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   and/or other materials provided with the distribution.
130ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Neither the name of Google Inc. nor the names of its contributors may be
140ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   used to endorse or promote products derived from this software without
150ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   specific prior written permission.
160ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
170ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
180ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
190ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
200ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
210ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
220ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
230ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
240ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
250ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
260ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
270ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// POSSIBILITY OF SUCH DAMAGE.
280ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
290ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Author: keir@google.com (Keir Mierle)
300ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
310ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#ifndef CERES_INTERNAL_STL_UTIL_H_
320ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#define CERES_INTERNAL_STL_UTIL_H_
330ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
341d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling#include <algorithm>
351d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling
360ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongnamespace ceres {
370ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
380ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// STLDeleteContainerPointers()
390ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//  For a range within a container of pointers, calls delete
400ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//  (non-array version) on these pointers.
410ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// NOTE: for these three functions, we could just implement a DeleteObject
420ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// functor and then call for_each() on the range and functor, but this
430ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// requires us to pull in all of algorithm.h, which seems expensive.
440ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// For hash_[multi]set, it is important that this deletes behind the iterator
450ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// because the hash_set may call the hash function on the iterator when it is
460ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// advanced, which could result in the hash function trying to deference a
470ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// stale pointer.
480ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongtemplate <class ForwardIterator>
490ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongvoid STLDeleteContainerPointers(ForwardIterator begin,
500ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong                                ForwardIterator end) {
510ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  while (begin != end) {
520ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    ForwardIterator temp = begin;
530ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    ++begin;
540ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    delete *temp;
550ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  }
560ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}
570ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
581d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling// Variant of STLDeleteContainerPointers which allows the container to
591d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling// contain duplicates.
601d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberlingtemplate <class ForwardIterator>
611d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberlingvoid STLDeleteUniqueContainerPointers(ForwardIterator begin,
621d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling                                      ForwardIterator end) {
631d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling  sort(begin, end);
641d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling  ForwardIterator new_end = unique(begin, end);
651d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling  while (begin != new_end) {
661d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling    ForwardIterator temp = begin;
671d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling    ++begin;
681d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling    delete *temp;
691d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling  }
701d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling}
711d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling
720ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// STLDeleteElements() deletes all the elements in an STL container and clears
730ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// the container.  This function is suitable for use with a vector, set,
740ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// hash_set, or any other STL container which defines sensible begin(), end(),
750ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// and clear() methods.
760ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
770ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// If container is NULL, this function is a no-op.
780ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
790ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// As an alternative to calling STLDeleteElements() directly, consider
800ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ElementDeleter (defined below), which ensures that your container's elements
810ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// are deleted when the ElementDeleter goes out of scope.
820ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongtemplate <class T>
830ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongvoid STLDeleteElements(T *container) {
840ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  if (!container) return;
850ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  STLDeleteContainerPointers(container->begin(), container->end());
860ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  container->clear();
870ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}
880ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
890ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}  // namespace ceres
900ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
910ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#endif  // CERES_INTERNAL_STL_UTIL_H_
92