1 // find_univpoly_ring().
4 #include "base/cl_sysdep.h"
7 #include "cln/univpoly.h"
12 #include "polynomial/cl_UP.h"
16 // Create a new univariate polynomial ring with a named variable.
18 static inline cl_heap_univpoly_ring* cl_make_univpoly_ring (const cl_ring& r, const cl_symbol& varname)
20 cl_heap_univpoly_ring* UPR = cl_make_univpoly_ring(r);
21 UPR->add_property(new cl_varname_property(cl_univpoly_varname_key,varname));
27 // The table of univariate polynomial rings with named variable.
28 // A weak hash table (cl_ring,cl_symbol) -> cl_univpoly_ring.
30 #include "base/hash/cl_rcpointer2_hashweak_rcpointer.h"
34 // An entry can be collected when the value (the ring) isn't referenced any more
35 // except from the hash table, and when the keys (the base ring and the name)
36 // are't referenced any more except from the hash table and the ring. Note that
37 // the ring contains exactly one reference to the base ring and exactly one
38 // reference to the name (on the property list).
40 static bool maygc_htentry (const cl_htentry_from_rcpointer2_to_rcpointer& entry)
42 if (!entry.key1.pointer_p() || (entry.key1.heappointer->refcount == 2))
43 if (!entry.key2.pointer_p() || (entry.key2.heappointer->refcount == 2))
44 if (!entry.val.pointer_p() || (entry.val.heappointer->refcount == 1))
49 class named_univpoly_ring_cache
51 static cl_wht_from_rcpointer2_to_rcpointer* univpoly_ring_table;
54 named_univpoly_ring_cache();
55 ~named_univpoly_ring_cache();
57 inline cl_univpoly_ring* get_univpoly_ring(const cl_ring& r, const cl_symbol& v)
59 return (cl_univpoly_ring*) univpoly_ring_table->get(r,v);
61 inline void store_univpoly_ring(const cl_univpoly_ring& R)
63 univpoly_ring_table->put(R->basering(),
64 ((cl_varname_property*)(R->get_property(cl_univpoly_varname_key)))->varname,
69 cl_wht_from_rcpointer2_to_rcpointer* named_univpoly_ring_cache::univpoly_ring_table = 0;
70 int named_univpoly_ring_cache::count = 0;
72 named_univpoly_ring_cache::named_univpoly_ring_cache()
75 univpoly_ring_table = new cl_wht_from_rcpointer2_to_rcpointer(maygc_htentry);
79 named_univpoly_ring_cache::~named_univpoly_ring_cache()
82 delete univpoly_ring_table;
85 const cl_univpoly_ring find_univpoly_ring (const cl_ring& r, const cl_symbol& varname)
87 static named_univpoly_ring_cache cache;
88 var cl_univpoly_ring* ring_in_table = cache.get_univpoly_ring(r,varname);
90 var cl_univpoly_ring R = cl_make_univpoly_ring(r,varname);
91 cache.store_univpoly_ring(R);
92 ring_in_table = cache.get_univpoly_ring(r,varname);
94 throw runtime_exception();
96 return *ring_in_table;