source: git/factory/DegreePattern.h @ e4fe2b

jengelh-datetimespielwiese
Last change on this file since e4fe2b was e4fe2b, checked in by Oleksandr Motsak <motsak@…>, 11 years ago
FIX: Fixed huge BUG in cf_gmp.h CHG: starting to cleanup factory
  • Property mode set to 100644
File size: 5.0 KB
Line 
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 * @internal @version \$Id$
11 *
12 **/
13/*****************************************************************************/
14#ifndef DEGREE_PATTERN_H
15#define DEGREE_PATTERN_H
16
17// #include "config.h"
18
19#include "cf_assert.h"
20
21#include "canonicalform.h"
22#include "cf_iter.h"
23#include "templates/ftmpl_functions.h"
24#include "gfops.h"
25
26/** @class DegreePattern DegreePattern.h "factory/DegreePattern.h"
27 *
28 * DegreePattern provides a functionality to create, intersect and refine
29 * degree patterns.
30 *
31 *
32 */
33class DegreePattern
34{
35private:
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");
54    if( m_data->m_pattern != NULL )
55      delete[] m_data->m_pattern;
56    m_data->m_pattern = NULL;
57
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();
69
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
84public:
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  ///
96  /// @return @a operator[] returns the element at @a index
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  ///
108  /// @return @a operator[] sets the element at @a index
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
123                ): m_data( degPat.m_data )
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                );
132
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
150  ~DegreePattern ()
151  {
152    ASSERT( m_data !=  NULL, "non-null pointer expected"  );
153    if( (--m_data->m_refCounter) < 1 )
154      release();
155  }
156
157  /// find an element @a x
158  ///
159  /// @return @a find returns the index + 1 of @a x, if @a x is an element of
160  ///         the degree pattern, 0 otherwise
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  };
169
170  /// intersect two degree patterns
171  void intersect (const DegreePattern& degPat ///< [in] some degree pattern
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
175  /// some entry @a b such that @a a+b= d. Elements which do not satisfy this
176  /// relation are removed.
177  void refine ();
178};
179
180#endif
181/* DEGREE_PATTERN_H */
182
Note: See TracBrowser for help on using the repository browser.