]> www.ginac.de Git - cln.git/blob - src/polynomial/elem/cl_UP_unnamed.cc
a37a99289ed079992ae0bc34b43307b022ad8523
[cln.git] / src / polynomial / elem / cl_UP_unnamed.cc
1 // cl_find_univpoly_ring().
2
3 // General includes.
4 #include "cl_sysdep.h"
5
6 CL_PROVIDE(cl_UP_unnamed)
7
8 // Specification.
9 #include "cl_univpoly.h"
10
11
12 // Implementation.
13
14 #include "cl_UP.h"
15
16 // The table of univariate polynomial rings without named variable.
17 // A weak hash table cl_ring -> cl_univpoly_ring.
18 // (It could also be a weak hashuniq table cl_ring -> cl_univpoly_ring.)
19
20 #include "cl_rcpointer_hashweak_rcpointer.h"
21
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.
26
27 static cl_boolean maygc_htentry (const cl_htentry_from_rcpointer_to_rcpointer& entry)
28 {
29         if (!entry.key.pointer_p() || (entry.key.heappointer->refcount == 2))
30                 if (!entry.val.pointer_p() || (entry.val.heappointer->refcount == 1))
31                         return cl_true;
32         return cl_false;
33 }
34
35 static const cl_wht_from_rcpointer_to_rcpointer univpoly_ring_table = cl_wht_from_rcpointer_to_rcpointer(maygc_htentry);
36
37 static inline cl_univpoly_ring* get_univpoly_ring (const cl_ring& r)
38 {
39         return (cl_univpoly_ring*) univpoly_ring_table.get(r);
40 }
41
42 static inline void store_univpoly_ring (const cl_univpoly_ring& R)
43 {
44         univpoly_ring_table.put(R->basering(),R);
45 }
46
47
48 const cl_univpoly_ring cl_find_univpoly_ring (const cl_ring& r)
49 {
50         var cl_univpoly_ring* ring_in_table = get_univpoly_ring(r);
51         if (!ring_in_table) {
52                 var cl_univpoly_ring R = cl_make_univpoly_ring(r);
53                 store_univpoly_ring(R);
54                 ring_in_table = get_univpoly_ring(r);
55                 if (!ring_in_table)
56                         cl_abort();
57         }
58         return *ring_in_table;
59 }
60
61 CL_PROVIDE_END(cl_UP_unnamed)