1 | /*****************************************************************************\ |
---|
2 | * Computer Algebra System SINGULAR |
---|
3 | \*****************************************************************************/ |
---|
4 | /** @file DegreePattern.h |
---|
5 | * |
---|
6 | * This file provides a class to handle degree patterns. |
---|
7 | * |
---|
8 | * @author Martin Lee |
---|
9 | * |
---|
10 | **/ |
---|
11 | /*****************************************************************************/ |
---|
12 | #ifndef DEGREE_PATTERN_H |
---|
13 | #define DEGREE_PATTERN_H |
---|
14 | |
---|
15 | // #include "config.h" |
---|
16 | |
---|
17 | #include "cf_assert.h" |
---|
18 | |
---|
19 | #include "canonicalform.h" |
---|
20 | #include "cf_iter.h" |
---|
21 | #include "templates/ftmpl_functions.h" |
---|
22 | #include "gfops.h" |
---|
23 | |
---|
24 | /** @class DegreePattern DegreePattern.h "factory/DegreePattern.h" |
---|
25 | * |
---|
26 | * DegreePattern provides a functionality to create, intersect and refine |
---|
27 | * degree patterns. |
---|
28 | * |
---|
29 | * |
---|
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"); |
---|
52 | if( m_data->m_pattern != NULL ) |
---|
53 | delete[] m_data->m_pattern; |
---|
54 | m_data->m_pattern = NULL; |
---|
55 | |
---|
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(); |
---|
67 | |
---|
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 | /// |
---|
94 | /// @return @a operator[] returns the element at @a index |
---|
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 | /// |
---|
106 | /// @return @a operator[] sets the element at @a index |
---|
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 |
---|
121 | ): m_data( degPat.m_data ) |
---|
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 | ); |
---|
130 | |
---|
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 |
---|
148 | ~DegreePattern () |
---|
149 | { |
---|
150 | ASSERT( m_data != NULL, "non-null pointer expected" ); |
---|
151 | if( (--m_data->m_refCounter) < 1 ) |
---|
152 | release(); |
---|
153 | } |
---|
154 | |
---|
155 | /// find an element @a x |
---|
156 | /// |
---|
157 | /// @return @a find returns the index + 1 of @a x, if @a x is an element of |
---|
158 | /// the degree pattern, 0 otherwise |
---|
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 | }; |
---|
167 | |
---|
168 | /// intersect two degree patterns |
---|
169 | void intersect (const DegreePattern& degPat ///< [in] some degree pattern |
---|
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 |
---|
173 | /// some entry @a b such that @a a+b= d. Elements which do not satisfy this |
---|
174 | /// relation are removed. |
---|
175 | void refine (); |
---|
176 | }; |
---|
177 | |
---|
178 | #endif |
---|
179 | /* DEGREE_PATTERN_H */ |
---|
180 | |
---|