From: Martin Kronbichler Date: Wed, 11 Aug 2010 13:34:23 +0000 (+0000) Subject: Improve documentation of nth_index_in_set and index_within_set by giving the variable... X-Git-Tag: v8.0.0~5739 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d705bcfa6b689c84f27124f23437cbe5ea2d6023;p=dealii.git Improve documentation of nth_index_in_set and index_within_set by giving the variables intuitive names: local_index and global_index. Also implemented one TODO regarding the binary search. git-svn-id: https://svn.dealii.org/trunk@21643 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/deal.II/base/include/base/index_set.h b/deal.II/base/include/base/index_set.h index 3a3bfc07f4..3dd45a5d7e 100644 --- a/deal.II/base/include/base/index_set.h +++ b/deal.II/base/include/base/index_set.h @@ -130,25 +130,25 @@ class IndexSet unsigned int n_elements () const; /** - * Return the nth index stored in - * this index set. @p n obviously - * needs to be less than - * n_elements(). + * Return the global index of the local + * index with number @p local_index + * stored in this index set. @p + * local_index obviously needs to be less + * than n_elements(). */ - unsigned int nth_index_in_set (const unsigned int n) const; + unsigned int nth_index_in_set (const unsigned int local_index) const; /** - * Return the how-manyth element - * of this set (counted in - * ascending order) @p n is. @p n - * needs to be less than the - * size(). This function throws - * an exception if the index @p n - * is not actually a member of - * this index set, i.e. if - * is_element(n) is false. + * Return the how-manyth element of this + * set (counted in ascending order) @p + * global_index is. @p global_index needs + * to be less than the size(). This + * function throws an exception if the + * index @p global_index is not actually + * a member of this index set, i.e. if + * is_element(global_index) is false. */ - unsigned int index_within_set (const unsigned int n) const; + unsigned int index_within_set (const unsigned int global_index) const; /** * Each index set can be @@ -264,8 +264,8 @@ class IndexSet * write() function. */ void read(std::istream & in); - - + + #ifdef DEAL_II_USE_TRILINOS /** * Given an MPI communicator, @@ -328,7 +328,7 @@ class IndexSet const bool overlapping = false) const; #endif - + /** * Determine an estimate for the memory * consumption (in bytes) of this @@ -336,6 +336,10 @@ class IndexSet */ unsigned int memory_consumption () const; + DeclException1 (ExcIndexNotPresent, int, + << "The global index " << arg1 + << " is not an element of this set."); + private: /** * A type that denotes the half @@ -378,6 +382,13 @@ class IndexSet return x.end < y.end; } + static bool nth_index_compare (const IndexSet::Range & x, + const IndexSet::Range & y) + { + return (x.nth_index_in_set+(x.end-x.begin) < + y.nth_index_in_set+(y.end-y.begin)); + } + friend inline bool operator== (const Range &range_1, const Range &range_2) @@ -391,7 +402,7 @@ class IndexSet { return sizeof(Range); } - + }; /** @@ -663,17 +674,23 @@ IndexSet::nth_index_in_set (const unsigned int n) const { Assert (n < n_elements(), ExcIndexRange (n, 0, n_elements())); -//TODO: this could be done more efficiently by using a binary search - for (std::vector::const_iterator p = ranges.begin(); - p != ranges.end(); ++p) + // find out which chunk the local index n + // belongs to by using a binary search. the + // comparator is based on the end of the + // ranges + Range r (0,0); + r.nth_index_in_set = n; + std::vector::const_iterator p = std::lower_bound(ranges.begin(), + ranges.end(), + r, + Range::nth_index_compare); + if (p != ranges.end()) + return p->begin + (n-p->nth_index_in_set); + else { - Assert (n >= p->nth_index_in_set, ExcInternalError()); - if (n < p->nth_index_in_set + (p->end-p->begin)) - return p->begin + (n-p->nth_index_in_set); + Assert (false, ExcInternalError()); + return numbers::invalid_unsigned_int; } - - Assert (false, ExcInternalError()); - return numbers::invalid_unsigned_int; } @@ -682,8 +699,7 @@ inline unsigned int IndexSet::index_within_set (const unsigned int n) const { - Assert (is_element(n) == true, - ExcMessage ("Given number is not an element of this set.")); + Assert (is_element(n) == true, ExcIndexNotPresent (n)); Assert (n < size(), ExcIndexRange (n, 0, size())); Range r(n, n);