00001
00002
00003
00004
00005
00006
00007 #ifndef SYNAPS_MPOL_TINYEXP_H
00008 #define SYNAPS_MPOL_TINYEXP_H
00009
00010 #include <iostream>
00011 #include <cstdlib>
00012 #include <string>
00013 #include <synaps/init.h>
00014
00015
00016 __BEGIN_NAMESPACE_SYNAPS
00017
00019 template<char X, int N, class E=char>
00020 struct tinyexp {
00021
00022 typedef E exponent_t;
00023 typedef int degree_t;
00024 typedef tinyexp<X,N,E> self_t;
00025
00026
00027 tinyexp() {tab = (E *)malloc(sizeof(E)*N);}
00028 tinyexp(const tinyexp<X,N,E> & d)
00029 {
00030 tab = (E *)malloc(sizeof(E)*N);
00031 memcpy(tab,d.tab,sizeof(E)*N);
00032 }
00033 tinyexp(int s, E *t){
00034 tab = (E *)malloc(sizeof(E)*N);
00035 for(int i=0;i<s;i++) tab[i]=t[i];
00036 }
00037
00038
00039 ~tinyexp(){if (tab) delete [] tab;}
00040
00041 E operator[] (int i) const
00042 {
00043 if(i<0) cout <<"("<<*this<<"["<<i<<"])";
00044 return (i<N &&i>=0?tab[i]:0); }
00045
00046
00047 tinyexp & operator = (const tinyexp & A)
00048 {
00049 memcpy(tab,A.tab,sizeof(E)*N);
00050 return *this;
00051 }
00052 self_t & operator+= (const tinyexp<X,N,E> & B)
00053 {
00054 for (int i = 0; i < N; ++i) tab[i] += B.tab[i];
00055 return *this;
00056 }
00057
00058 void reserve(int d) {}
00059
00060 self_t setExponent(int i, int d)
00061 {
00062 tab[i]=d; return *this;
00063 }
00064
00065 friend bool operator == (const tinyexp & A, const tinyexp & B)
00066 {
00067 return memcmp(A.tab,B.tab,sizeof(E)*N) == 0;
00068 }
00069
00070
00071 E *tab;
00072 };
00073
00074 template<char X,int N,class E> inline
00075 typename tinyexp<X,N,E>::degree_t Degree(const tinyexp<X,N,E> & t)
00076 {
00077 typename tinyexp<X,N,E>::degree_t d(0);
00078 for (int i = 0; i < N; ++i) d+=t[i];
00079 return(d);
00080 }
00081
00082 template<char X,int N,class E>
00083 ostream & operator << (ostream & os, const tinyexp<X,N,E> & t) {
00084 for (int i = 0; i < N; ++i)
00085 {
00086 if (t.tab[i] == 1)
00087 os <<X<< i;
00088 else if (t.tab[i] == 0);
00089 else
00090 os <<X << i << '^' <<(int)(t[i]);
00091 }
00092 return os;
00093 }
00094
00095 template<char X,class E>
00096 void SDegree (tinyexp<X,N,E> & A, int i, int d)
00097 {
00098 if (i < 0 && i>= N); else A.tab[i] = d;
00099 }
00100
00101 template<char X,int N, class E>
00102 int lvar (const tinyexp<X,N,E> & A) {
00103 int i=N-1;
00104 while(i>=0 && A[i]==0) i--;
00105 return i;
00106 }
00107
00108 template<char X,int N,class E>
00109 void erase (tinyexp<X,N,E> & A) {
00110 free(A.tab);
00111 A.tab = NULL;
00112 }
00113
00114
00115
00116 template<char X,int N,class E>
00117 void add (tinyexp<X,N,E>& r, const tinyexp<X,N,E>& A, const tinyexp<X,N,E>& B)
00118 {
00119 for (int i=0; i < N; ++i) r.tab[i] = A.tab[i]+B.tab[i];
00120 }
00121
00122
00123 __END_NAMESPACE_SYNAPS
00124
00125 #endif // SYNAPS_MPOL_TINYEXP_H
00126