GetFEM  5.4.2
bgeot_permutations.h
1 /* -*- c++ -*- (enables emacs c++ mode) */
2 /*===========================================================================
3 
4  Copyright (C) 2004-2020 Julien Pommier
5 
6  This file is a part of GetFEM
7 
8  GetFEM is free software; you can redistribute it and/or modify it
9  under the terms of the GNU Lesser General Public License as published
10  by the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version along with the GCC Runtime Library
12  Exception either version 3.1 or (at your option) any later version.
13  This program is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16  License and GCC Runtime Library Exception for more details.
17  You should have received a copy of the GNU Lesser General Public License
18  along with this program; if not, write to the Free Software Foundation,
19  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20 
21  As a special exception, you may use this file as it is a part of a free
22  software library without restriction. Specifically, if other files
23  instantiate templates or use macros or inline functions from this file,
24  or you compile this file and link it with other files to produce an
25  executable, this file does not by itself cause the resulting executable
26  to be covered by the GNU Lesser General Public License. This exception
27  does not however invalidate any other reasons why the executable file
28  might be covered by the GNU Lesser General Public License.
29 
30 ===========================================================================*/
31 #include <vector>
32 #include "bgeot_config.h"
33 
34 namespace bgeot {
35  /**
36  generation of permutations, and ranking/unranking of these.
37 
38  based on algorithms detailed in "Ranking and Unranking Permutations in linear time", W. Myrvold, F. Ruskey
39  ( http://www.csr.uvic.ca/~fruskey/Publications/RankPerm.html )
40  note that this is not lexigraphical order, and to_rank(0) != {0,1,2,3,...} (it is {1,2,3,...,n,0})
41 
42  however, the reset(), finished(), and ++ operator are based on the lexicagraphical ordering
43  */
44  class permutation : public std::vector<dim_type> {
45  size_type remaining;
46  public:
47  permutation(size_type n) : std::vector<dim_type>(n) { reset(); }
48  size_type nb_permutations() { return permutation::nb_permutations(size()); }
49  static size_type nb_permutations(size_type n)
50  { size_type f=1; for (; n>1; --n) f *= n; return f; }
51  void reset() {
52  remaining = 1;
53  for (size_type i=0; i < size(); ++i)
54  { (*this)[i] = dim_type(i); remaining *= (i+1); }
55  }
56  permutation& to_rank(size_type r);
57  permutation inversed() const {
58  permutation pinv(*this);
59  for (size_type i=0; i < size(); ++i) pinv[(*this)[i]] = dim_type(i);
60  return pinv;
61  }
62  size_type rank() const;
63  bool finished() const { return remaining == 0; }
64  /* increment in lexicographical order (not the best, but it is simple) */
65  const permutation &operator ++();
66  template < typename CONT1, typename CONT2 > void apply_to(const CONT1& src, CONT2& dest)
67  { for (size_type i=0; i < size(); ++i) dest[i] = src[(*this)[i]]; }
68  };
69  inline permutation& permutation::to_rank(size_type r) {
70  reset();
71  for (size_type n = size(); n; --n) {
72  std::swap((*this)[n-1], (*this)[r % n]);
73  r /= n;
74  }
75  return (*this);
76  }
77  inline size_type permutation::rank() const {
78  permutation p(*this);
79  permutation pinv(p.inversed());
80  size_type mul=1, r=0;
81  for (size_type n=size(); n>1; --n) {
82  dim_type s = p[n-1];
83  std::swap(p[n-1], p[pinv[n-1]]);
84  std::swap(pinv[s],pinv[n-1]);
85  r += s*mul; mul*=n;
86  }
87  return r;
88  }
89  inline const permutation &permutation::operator ++() {
90  if (--remaining == 0) return (*this);
91  size_type i = size()-2, j=size()-1;
92  while ((*this)[i] > (*this)[i+1]) i--;
93  while ((*this)[i] > (*this)[j]) j--;
94  std::swap((*this)[i], (*this)[j]);
95  for (size_type r = size()-1, s=i+1; r>s; --r, ++s) std::swap((*this)[r],(*this)[s]);
96  return (*this);
97  }
98 }
bgeot::size_type
size_t size_type
used as the common size type in the library
Definition: bgeot_poly.h:49
bgeot::permutation
generation of permutations, and ranking/unranking of these.
Definition: bgeot_permutations.h:44
bgeot
Basic Geometric Tools.
Definition: bgeot_convex_ref.cc:27
bgeot_config.h
defines and typedefs for namespace bgeot

Rabisu Mirror Service We provide mirrors to support Open source communities. Our mirror server is located in Istanbul/Turkey region.

Please do not hesitate to contact mirror@rabisu.com for new open source mirror submissions.