1 // find_univpoly_ring().
7 #include "cln/univpoly.h"
14 // The table of univariate polynomial rings without named variable.
15 // A weak hash table cl_ring -> cl_univpoly_ring.
16 // (It could also be a weak hashuniq table cl_ring -> cl_univpoly_ring.)
18 #include "cl_rcpointer_hashweak_rcpointer.h"
22 // An entry can be collected when the value (the ring) isn't referenced any more
23 // except from the hash table, and when the key (the base ring) isn't referenced
24 // any more except from the hash table and the ring. Note that the ring contains
25 // exactly one reference to the base ring.
27 static bool maygc_htentry (const cl_htentry_from_rcpointer_to_rcpointer& entry)
29 if (!entry.key.pointer_p() || (entry.key.heappointer->refcount == 2))
30 if (!entry.val.pointer_p() || (entry.val.heappointer->refcount == 1))
35 class univpoly_ring_cache
37 static cl_wht_from_rcpointer_to_rcpointer* univpoly_ring_table;
40 inline cl_univpoly_ring* get_univpoly_ring(const cl_ring& r)
42 return (cl_univpoly_ring*) univpoly_ring_table->get(r);
44 inline void store_univpoly_ring(const cl_univpoly_ring& R)
46 univpoly_ring_table->put(R->basering(), R);
48 univpoly_ring_cache();
49 ~univpoly_ring_cache();
52 cl_wht_from_rcpointer_to_rcpointer* univpoly_ring_cache::univpoly_ring_table = 0;
53 int univpoly_ring_cache::count = 0;
55 univpoly_ring_cache::univpoly_ring_cache()
58 univpoly_ring_table = new cl_wht_from_rcpointer_to_rcpointer(maygc_htentry);
61 univpoly_ring_cache::~univpoly_ring_cache()
64 delete univpoly_ring_table;
67 const cl_univpoly_ring find_univpoly_ring (const cl_ring& r)
69 static univpoly_ring_cache cache;
70 var cl_univpoly_ring* ring_in_table = cache.get_univpoly_ring(r);
72 var cl_univpoly_ring R = cl_make_univpoly_ring(r);
73 cache.store_univpoly_ring(R);
74 ring_in_table = cache.get_univpoly_ring(r);
76 throw runtime_exception();
78 return *ring_in_table;