From: bangerth Date: Sun, 12 Feb 2012 16:53:39 +0000 (+0000) Subject: New function IndexSet::fill_binary_vector. X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b6976fd3c573010c11df1ac0ecc144d84f1bf358;p=dealii-svn.git New function IndexSet::fill_binary_vector. git-svn-id: https://svn.dealii.org/trunk@25042 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/deal.II/doc/news/changes.h b/deal.II/doc/news/changes.h index 89f9899599..8b1dbc2213 100644 --- a/deal.II/doc/news/changes.h +++ b/deal.II/doc/news/changes.h @@ -87,6 +87,11 @@ enabled due to a missing include file in file

Specific improvements

    +
  1. New: The function IndexSet::fill_binary_vector creates a numerical +representation of an IndexSet containing zeros and ones. +
    +(Wolfgang Bangerth, 2012/02/12) +
  2. New: The function IndexSet::clear resets an index set to be empty.
    (Wolfgang Bangerth, 2012/02/12) diff --git a/deal.II/include/deal.II/base/index_set.h b/deal.II/include/deal.II/base/index_set.h index 28f8310f44..c4162e414a 100644 --- a/deal.II/include/deal.II/base/index_set.h +++ b/deal.II/include/deal.II/base/index_set.h @@ -265,6 +265,29 @@ class IndexSet */ void fill_index_vector(std::vector & indices) const; + /** + * Fill the given vector with either + * zero or one elements, providing + * a binary representation of this + * index set. The given vector is + * assumed to already have the correct + * size. + * + * The given argument is filled with + * integer values zero and one, using + * vector.operator[]. Thus, + * any object that has such an operator + * can be used as long as it allows + * conversion of integers zero and one to + * elements of the vector. Specifically, + * this is the case for classes Vector, + * BlockVector, but also + * std::vector@, + * std::vector@, and + * std::vector@. + */ + template + void fill_binary_vector (Vector &vector) const; /** * Outputs a text representation of this @@ -955,6 +978,31 @@ IndexSet::operator != (const IndexSet &is) const return ranges != is.ranges; } + + +template +void +IndexSet::fill_binary_vector (Vector &vector) const +{ + Assert (vector.size() == size(), + ExcDimensionMismatch (vector.size(), size())); + + compress(); + // first fill all elements of the vector + // with zeroes. + std::fill (vector.begin(), vector.end(), 0); + + // then write ones into the elements whose + // indices are contained in the index set + for (std::vector::iterator it = ranges.begin(); + it != ranges.end(); + ++it) + for (unsigned int i=it->begin; iend; ++i) + vector[i] = 1; +} + + + template inline void diff --git a/tests/base/index_set_22.cc b/tests/base/index_set_22.cc new file mode 100644 index 0000000000..9ae96cb130 --- /dev/null +++ b/tests/base/index_set_22.cc @@ -0,0 +1,60 @@ +//----------------------------------------------------------------------------- +// $Id$ +// Version: $Name$ +// +// Copyright (C) 2010, 2012 by the deal.II authors +// +// This file is subject to QPL and may not be distributed +// without copyright and license information. Please refer +// to the file deal.II/doc/license.html for the text and +// further information on this license. +// +//----------------------------------------------------------------------------- + +// test IndexSet::fill_binary_vector + +#include "../tests.h" +#include +#include +#include +#include +#include + +#include + + +void test () +{ + IndexSet is1 (100); + + // randomly add 90 elements to each + // set, some of which may be + // repetitions of previous ones + for (unsigned int i=0; i<9*is1.size()/10; ++i) + is1.add_index (rand() % is1.size()); + + std::vector zeros_and_ones(is1.size()); + is1.fill_binary_vector (zeros_and_ones); + + deallog << "Original index set: " << std::endl; + is1.print(deallog); + + for (unsigned int i=0; i