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