[7bf145] | 1 | /*****************************************************************************\ |
---|
[806c18] | 2 | * Computer Algebra System SINGULAR |
---|
[7bf145] | 3 | \*****************************************************************************/ |
---|
| 4 | /** @file DegreePattern.h |
---|
[806c18] | 5 | * |
---|
| 6 | * This file provides a class to handle degree patterns. |
---|
[7bf145] | 7 | * |
---|
| 8 | * @author Martin Lee |
---|
| 9 | * |
---|
| 10 | **/ |
---|
| 11 | /*****************************************************************************/ |
---|
| 12 | #ifndef DEGREE_PATTERN_H |
---|
| 13 | #define DEGREE_PATTERN_H |
---|
| 14 | |
---|
[e4fe2b] | 15 | // #include "config.h" |
---|
[7bf145] | 16 | |
---|
[650f2d8] | 17 | #include "cf_assert.h" |
---|
[7bf145] | 18 | |
---|
| 19 | #include "canonicalform.h" |
---|
| 20 | #include "cf_iter.h" |
---|
[6db552] | 21 | #include "templates/ftmpl_functions.h" |
---|
[7bf145] | 22 | #include "gfops.h" |
---|
| 23 | |
---|
| 24 | /** @class DegreePattern DegreePattern.h "factory/DegreePattern.h" |
---|
[806c18] | 25 | * |
---|
[7bf145] | 26 | * DegreePattern provides a functionality to create, intersect and refine |
---|
| 27 | * degree patterns. |
---|
| 28 | * |
---|
[806c18] | 29 | * |
---|
[7bf145] | 30 | */ |
---|
| 31 | class DegreePattern |
---|
| 32 | { |
---|
| 33 | private: |
---|
| 34 | struct Pattern |
---|
| 35 | { |
---|
| 36 | int m_refCounter; ///< reference counter |
---|
| 37 | int m_length; ///< length of m_pattern |
---|
| 38 | int* m_pattern; ///< some array containing the degree pattern |
---|
| 39 | |
---|
| 40 | /// construct a Pattern from an int |
---|
| 41 | Pattern(int n): m_refCounter(1), m_length(n), m_pattern( new int[n]) {}; |
---|
| 42 | /// default contructor |
---|
| 43 | Pattern(): m_refCounter(1), m_length(0), m_pattern(NULL) {}; |
---|
| 44 | |
---|
| 45 | }* m_data; |
---|
| 46 | |
---|
| 47 | /// clear m_data |
---|
| 48 | void release() |
---|
| 49 | { |
---|
| 50 | ASSERT ( m_data != NULL, "non-null pointer expected"); |
---|
| 51 | ASSERT ( m_data->m_refCounter == 0, "ref count of 0 expected"); |
---|
[806c18] | 52 | if( m_data->m_pattern != NULL ) |
---|
[7bf145] | 53 | delete[] m_data->m_pattern; |
---|
| 54 | m_data->m_pattern = NULL; |
---|
[806c18] | 55 | |
---|
[7bf145] | 56 | delete m_data; |
---|
| 57 | m_data = NULL; |
---|
| 58 | } |
---|
| 59 | /// initialise a DegreePattern |
---|
| 60 | void init( int n ) |
---|
| 61 | { |
---|
| 62 | ASSERT ( m_data != NULL, "non-null pointer expected" ); |
---|
| 63 | ASSERT( m_data->m_refCounter > 0, "ref count > 0 expected" ); |
---|
| 64 | |
---|
| 65 | if( (--m_data->m_refCounter) < 1 ) |
---|
| 66 | release(); |
---|
[806c18] | 67 | |
---|
[7bf145] | 68 | m_data = new Pattern(n); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | /// getter |
---|
| 72 | /// |
---|
| 73 | /// @return @a getPattern returns a degree pattern |
---|
| 74 | inline int* getPattern() const |
---|
| 75 | { |
---|
| 76 | ASSERT( m_data != NULL, "non-null pointer expected" ); |
---|
| 77 | ASSERT( m_data->m_pattern != NULL, "non-null pointer expected" ); |
---|
| 78 | return m_data->m_pattern; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | |
---|
| 82 | public: |
---|
| 83 | /// getter |
---|
| 84 | /// |
---|
| 85 | /// @return @a getLength returns the length of the degree pattern |
---|
| 86 | inline int getLength() const |
---|
| 87 | { |
---|
| 88 | ASSERT( m_data != NULL, "non-null pointer expected" ); |
---|
| 89 | return m_data->m_length; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | /// operator [] |
---|
| 93 | /// |
---|
[806c18] | 94 | /// @return @a operator[] returns the element at @a index |
---|
[7bf145] | 95 | inline int operator[] (const int index ///< [in] some int >= 0, < getLength() |
---|
| 96 | ) const |
---|
| 97 | { |
---|
| 98 | ASSERT( m_data != NULL, "non-null pointer expected" ); |
---|
| 99 | ASSERT( index >= 0 && index < getLength(), "bad index" ); |
---|
| 100 | ASSERT( getPattern() != NULL, "non-null pointer expected" ); |
---|
| 101 | return getPattern()[index]; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | /// operator [] |
---|
| 105 | /// |
---|
[806c18] | 106 | /// @return @a operator[] sets the element at @a index |
---|
[7bf145] | 107 | inline int& operator[] (const int index ///< [in] some int >= 0, < getLength() |
---|
| 108 | ) |
---|
| 109 | { |
---|
| 110 | ASSERT( m_data != NULL, "non-null pointer expected" ); |
---|
| 111 | ASSERT( index >= 0 && index < getLength(), "bad index" ); |
---|
| 112 | ASSERT( getPattern() != NULL, "non-null pointer expected" ); |
---|
| 113 | return getPattern()[index]; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | /// default constructor |
---|
| 117 | DegreePattern(): m_data( new Pattern() ){} |
---|
| 118 | |
---|
| 119 | /// copy constructor |
---|
| 120 | DegreePattern (const DegreePattern& degPat ///< [in] some degree pattern |
---|
[806c18] | 121 | ): m_data( degPat.m_data ) |
---|
[7bf145] | 122 | { |
---|
| 123 | ASSERT( degPat.m_data != NULL, "non-null pointer expected" ); |
---|
| 124 | m_data->m_refCounter++; |
---|
| 125 | }; |
---|
| 126 | |
---|
| 127 | /// construct a degree pattern from a list of (univariate) polys |
---|
| 128 | DegreePattern (const CFList& l ///< [in] some list of (univariate) polys |
---|
| 129 | ); |
---|
[806c18] | 130 | |
---|
[7bf145] | 131 | /// assignment |
---|
| 132 | DegreePattern& operator= (const DegreePattern& degPat ///< [in] some degree |
---|
| 133 | ///< pattern |
---|
| 134 | ) |
---|
| 135 | { |
---|
| 136 | ASSERT( m_data != NULL, "non-null pointer expected" ); |
---|
| 137 | ASSERT( degPat.m_data != NULL, "non-null pointer expected" ); |
---|
| 138 | if( m_data != degPat.m_data ) |
---|
| 139 | { |
---|
| 140 | m_data = degPat.m_data; |
---|
| 141 | m_data->m_refCounter++; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | return *this; |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | /// destructor |
---|
[806c18] | 148 | ~DegreePattern () |
---|
[7bf145] | 149 | { |
---|
| 150 | ASSERT( m_data != NULL, "non-null pointer expected" ); |
---|
[806c18] | 151 | if( (--m_data->m_refCounter) < 1 ) |
---|
[7bf145] | 152 | release(); |
---|
| 153 | } |
---|
| 154 | |
---|
[806c18] | 155 | /// find an element @a x |
---|
[7bf145] | 156 | /// |
---|
[806c18] | 157 | /// @return @a find returns the index + 1 of @a x, if @a x is an element of |
---|
[7bf145] | 158 | /// the degree pattern, 0 otherwise |
---|
[d52c12] | 159 | int find (const int x ///< [in] some int |
---|
| 160 | ) const |
---|
| 161 | { |
---|
| 162 | if (getLength() == 0) return 0; |
---|
| 163 | for (int i= 0; i < getLength(); i++) |
---|
| 164 | if ((*this)[i] == x) return i + 1; |
---|
| 165 | return 0; |
---|
| 166 | }; |
---|
[806c18] | 167 | |
---|
| 168 | /// intersect two degree patterns |
---|
[d52c12] | 169 | void intersect (const DegreePattern& degPat ///< [in] some degree pattern |
---|
[7bf145] | 170 | ); |
---|
| 171 | /// Refine a degree pattern. Assumes that (*this)[0]:= @a d is the degree |
---|
| 172 | /// of the poly to be factored. Now for every other entry @a a there should be |
---|
[806c18] | 173 | /// some entry @a b such that @a a+b= d. Elements which do not satisfy this |
---|
[7bf145] | 174 | /// relation are removed. |
---|
[d52c12] | 175 | void refine (); |
---|
[7bf145] | 176 | }; |
---|
| 177 | |
---|
| 178 | #endif |
---|
| 179 | /* DEGREE_PATTERN_H */ |
---|
| 180 | |
---|