permutations.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /**
  2. * This file is part of Gomu.
  3. *
  4. * Copyright 2016 by Jean Fromentin <jean.fromentin@math.cnrs.fr>
  5. *
  6. * Gomu is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Gomu is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Gomu. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef PERMUTATIONS_HPP
  20. #define PERMUTATIONS_HPP
  21. #include <cassert>
  22. #include <cstdint>
  23. #include <iostream>
  24. #include "smmintrin.h"
  25. #include "coxeter.hpp"
  26. using namespace std;
  27. typedef int8_t int8;
  28. typedef uint32_t uint32;
  29. typedef uint64_t uint64;
  30. typedef int64_t int64;
  31. static const __m128i id128=_mm_setr_epi8(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);
  32. static const __m128i shmask=_mm_setr_epi8(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,15);
  33. void swap(int8& a,int8& b);
  34. uint64 fact(uint64 n);
  35. template<CoxeterType T> class Permutation{
  36. public:
  37. uint64 n;
  38. static Permutation Ptemp;
  39. union{
  40. __m128i m128;
  41. int8 win[16];
  42. };
  43. Permutation(uint64=0);
  44. void setInverse(Permutation&) const;
  45. const Permutation& getInverse() const;
  46. uint64 descentInteger() const;
  47. int cmp(const Permutation&) const;
  48. string display() const;
  49. template<CoxeterType U> friend ostream& operator<<(ostream&,const Permutation<U>&);
  50. };
  51. template<CoxeterType T> class PermutationEnumerator;
  52. template<> class PermutationEnumerator<CoxeterA>{
  53. protected:
  54. Permutation<CoxeterA> sigma;
  55. uint64 n;
  56. public:
  57. PermutationEnumerator(uint64);
  58. void reset();
  59. bool next();
  60. size_t size() const;
  61. const Permutation<CoxeterA>& get() const;
  62. int cmp(const PermutationEnumerator<CoxeterA>& E) const;
  63. string display() const;
  64. friend ostream& operator<<(ostream&,const PermutationEnumerator<CoxeterA>&);
  65. };
  66. template<> class PermutationEnumerator<CoxeterB>:public PermutationEnumerator<CoxeterA>{
  67. protected:
  68. Permutation<CoxeterB> sigmaB;
  69. uint64 sign;
  70. public:
  71. PermutationEnumerator(uint64);
  72. void reset();
  73. bool next();
  74. size_t size() const;
  75. const Permutation<CoxeterB>& get() const;
  76. int cmp(const PermutationEnumerator<CoxeterB>& E) const;
  77. string display() const;
  78. friend ostream& operator<<(ostream&,const PermutationEnumerator<CoxeterB>&);
  79. };
  80. template<> class PermutationEnumerator<CoxeterD>:public PermutationEnumerator<CoxeterA>{
  81. protected:
  82. Permutation<CoxeterD> sigmaD;
  83. uint64 sign;
  84. public:
  85. PermutationEnumerator(uint64);
  86. void reset();
  87. bool next();
  88. size_t size() const;
  89. const Permutation<CoxeterD>& get() const;
  90. int cmp(const PermutationEnumerator<CoxeterD>& E) const;
  91. string display() const;
  92. friend ostream& operator<<(ostream&,const PermutationEnumerator<CoxeterD>&);
  93. };
  94. typedef Permutation<CoxeterA> PermutationA;
  95. typedef Permutation<CoxeterB> PermutationB;
  96. typedef Permutation<CoxeterD> PermutationD;
  97. inline void
  98. swap(int8& a,int8& b){
  99. int8 t=a;
  100. a=b;
  101. b=t;
  102. }
  103. inline uint64
  104. fact(uint64 n){
  105. if(n<2) return 1;
  106. return n*fact(n-1);
  107. }
  108. //***************
  109. //* Permutation *
  110. //***************
  111. template<CoxeterType T> Permutation<T> Permutation<T>::Ptemp;
  112. template<CoxeterType T> inline
  113. Permutation<T>::Permutation(uint64 r):n(r),m128(id128){
  114. }
  115. template<CoxeterType T> void
  116. Permutation<T>::setInverse(Permutation<T>& inv) const{
  117. inv.n=n;
  118. for(uint64 i=1;i<=n;++i){
  119. int8 v=win[i];
  120. if(v>0) inv.win[v]=i;
  121. else inv.win[-v]=-i;
  122. }
  123. }
  124. template<CoxeterType T> const Permutation<T>&
  125. Permutation<T>::getInverse() const{
  126. setInverse(Ptemp);
  127. return Ptemp;
  128. }
  129. template<> inline uint64
  130. Permutation<CoxeterA>::descentInteger() const{
  131. __m128i temp=_mm_shuffle_epi8(m128,shmask);
  132. temp=_mm_cmpgt_epi8(m128,temp);
  133. return _mm_movemask_epi8(temp);
  134. }
  135. template<> inline uint64
  136. Permutation<CoxeterB>::descentInteger() const{
  137. __m128i temp=_mm_shuffle_epi8(m128,shmask);
  138. temp=_mm_cmpgt_epi8(m128,temp);
  139. return _mm_movemask_epi8(temp);
  140. }
  141. template<> inline uint64
  142. Permutation<CoxeterD>::descentInteger() const{
  143. __m128i temp=_mm_shuffle_epi8(m128,shmask);
  144. temp=_mm_cmpgt_epi8(m128,temp);
  145. uint64 res=_mm_movemask_epi8(temp);
  146. if(win[1]+win[2]<0) res|=1L;
  147. else res&=(~1L);
  148. return res;
  149. }
  150. template<CoxeterType T> int
  151. Permutation<T>::cmp(const Permutation& P) const{
  152. if(n!=P.n){
  153. if(n<P.n) return 1;
  154. return -1;
  155. }
  156. for(uint64 i=1;i<=n;++i){
  157. if(win[i]!=P.win[i]){
  158. if(win[i]<P.win[i]) return 1;
  159. return -1;
  160. }
  161. }
  162. return 0;
  163. }
  164. template<CoxeterType T> string
  165. Permutation<T>::display() const{
  166. string str;
  167. if(n==0) return "()";
  168. str="(\033[34m"+to_string((int64)win[1])+"\033[0m";
  169. for(uint64 i=2;i<=n;++i){
  170. str+=",\033[34m"+to_string((int64)win[i])+"\033[0m";
  171. }
  172. str+=')';
  173. return str;
  174. }
  175. template<CoxeterType T> inline ostream&
  176. operator<<(ostream& os,const Permutation<T>& sigma){
  177. return os<<sigma.display();
  178. }
  179. //
  180. // PermutationEnumerator
  181. //
  182. inline size_t
  183. PermutationEnumerator<CoxeterA>::size() const{
  184. return fact(n);
  185. }
  186. inline size_t
  187. PermutationEnumerator<CoxeterB>::size() const{
  188. return (1<<n)*fact(n);
  189. }
  190. inline size_t
  191. PermutationEnumerator<CoxeterD>::size() const{
  192. if(n==0) return 1;
  193. return (1<<(n-1))*fact(n);
  194. }
  195. inline string
  196. PermutationEnumerator<CoxeterA>::display() const{
  197. return "Enumerator for permutations of type A and rank <= "+to_string(n);
  198. }
  199. inline string
  200. PermutationEnumerator<CoxeterB>::display() const{
  201. return "Enumerator for permutations of type B and rank <= "+to_string(n);
  202. }
  203. inline string
  204. PermutationEnumerator<CoxeterD>::display() const{
  205. return "Enumerator for permutations of type D and rank <= "+to_string(n);
  206. }
  207. template<CoxeterType T> inline ostream&
  208. operator<<(ostream& os,const PermutationEnumerator<T>& E){
  209. return os<<E.display();
  210. }
  211. #endif