File: permxx.h

package info (click to toggle)
flint 2.5.2-19
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 30,308 kB
  • sloc: ansic: 289,367; cpp: 11,210; python: 1,280; sh: 649; makefile: 283
file content (114 lines) | stat: -rw-r--r-- 3,520 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*=============================================================================

    This file is part of FLINT.

    FLINT is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    FLINT is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with FLINT; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

=============================================================================*/
/******************************************************************************

    Copyright (C) 2013 Tom Bachmann

******************************************************************************/

#ifndef PERMXX_H
#define PERMXX_H

#include "perm.h"

#include "flintxx/frandxx.h"
#include "flintxx/mp.h"

namespace flint {
class permxx
{
private:
    slong* data;
    slong sz;

public:
    permxx(slong n) {data = _perm_init(n);sz = n;}
    ~permxx() {_perm_clear(data);}

    permxx(const permxx& o)
    {
        sz = o.size();
        data = _perm_init(sz);
        _perm_set(data, o._data(), sz);
    }
    permxx& operator=(const permxx& o)
    {
        sz = o.size();
        _perm_set(_data(), o._data(), sz);
        return *this;
    }

    bool operator==(const permxx& o)
        {return size() == o.size() && _perm_equal(_data(), o._data(), size());}
    bool operator!=(const permxx& o) {return !(*this == o);}

    static permxx one(slong n) {return permxx(n);}
    static permxx randtest(slong n, frandxx& state)
        {permxx res(n);res.set_randtest(state);return res;}

    void set_one() {_perm_set_one(_data(), size());}
    int set_randtest(frandxx& state)
        {return _perm_randtest(_data(), size(), state._data());}

    slong* _data() {return data;}
    const slong* _data() const {return data;}
    slong size() const {return sz;}

    slong& operator[](slong idx) {return data[idx];}
    slong operator[](slong idx) const {return data[idx];}

    int parity() const {return _perm_parity(_data(), size());}

    permxx operator*(const permxx& o) const
    {
        permxx res(o.size());
        _perm_compose(res._data(), _data(), o._data(), size());
        return res;
    }
    permxx& operator*=(const permxx& o)
    {
        _perm_compose(_data(), _data(), o._data(), size());
        return *this;
    }

    void set_inv(const permxx& o) {_perm_inv(_data(), o._data(), o.size());}
    permxx inv() const {permxx res(size());res.set_inv(*this);return res;}
};

inline permxx compose(const permxx& p1, const permxx& p2) {return p1*p2;}
inline int parity(const permxx& p) {return p.parity();}
inline permxx inv(const permxx& o) {return o.inv();}

inline slong* maybe_perm_data(permxx* p) {return p ? p->_data() : 0;}
inline slong* maybe_perm_data(int zero) {return 0;}

namespace traits {
template<class T> struct is_maybe_perm
    : mp::or_<mp::equal_types<T, permxx*>, mp::equal_types<T, int> > { };
template<class T> struct is_permxx : mp::equal_types<T, permxx> { };
} // traits

inline int print(const permxx& p)
{
    return _perm_print(p._data(), p.size());
}
} // flint

#endif