]> www.ginac.de Git - cln.git/blob - src/polynomial/elem/cl_UP_named.cc
959f08ba6caa2ef741cea814e7ff003870045455
[cln.git] / src / polynomial / elem / cl_UP_named.cc
1 // cl_find_univpoly_ring().
2
3 // General includes.
4 #include "cl_sysdep.h"
5
6 CL_PROVIDE(cl_UP_named)
7
8 // Specification.
9 #include "cl_univpoly.h"
10
11
12 // Implementation.
13
14 #include "cl_UP.h"
15
16 // Create a new univariate polynomial ring with a named variable.
17
18 static inline cl_heap_univpoly_ring* cl_make_univpoly_ring (const cl_ring& r, const cl_symbol& varname)
19 {
20         cl_heap_univpoly_ring* UPR = cl_make_univpoly_ring(r);
21         UPR->add_property(new cl_varname_property(cl_univpoly_varname_key,varname));
22         return UPR;
23 }
24
25
26 // The table of univariate polynomial rings with named variable.
27 // A weak hash table (cl_ring,cl_symbol) -> cl_univpoly_ring.
28
29 #include "cl_rcpointer2_hashweak_rcpointer.h"
30
31 // An entry can be collected when the value (the ring) isn't referenced any more
32 // except from the hash table, and when the keys (the base ring and the name)
33 // are't referenced any more except from the hash table and the ring. Note that
34 // the ring contains exactly one reference to the base ring and exactly one
35 // reference to the name (on the property list).
36
37 static cl_boolean maygc_htentry (const cl_htentry_from_rcpointer2_to_rcpointer& entry)
38 {
39         if (!entry.key1.pointer_p() || (entry.key1.heappointer->refcount == 2))
40                 if (!entry.key2.pointer_p() || (entry.key2.heappointer->refcount == 2))
41                         if (!entry.val.pointer_p() || (entry.val.heappointer->refcount == 1))
42                                 return cl_true;
43         return cl_false;
44 }
45
46 static const cl_wht_from_rcpointer2_to_rcpointer univpoly_ring_table = cl_wht_from_rcpointer2_to_rcpointer(maygc_htentry);
47
48 static inline cl_univpoly_ring* get_univpoly_ring (const cl_ring& r, const cl_symbol& v)
49 {
50         return (cl_univpoly_ring*) univpoly_ring_table.get(r,v);
51 }
52
53 static inline void store_univpoly_ring (const cl_univpoly_ring& R)
54 {
55         univpoly_ring_table.put(R->basering(), ((cl_varname_property*)(R->get_property(cl_univpoly_varname_key)))->varname,
56                                 R);
57 }
58
59
60 const cl_univpoly_ring cl_find_univpoly_ring (const cl_ring& r, const cl_symbol& varname)
61 {
62         var cl_univpoly_ring* ring_in_table = get_univpoly_ring(r,varname);
63         if (!ring_in_table) {
64                 var cl_univpoly_ring R = cl_make_univpoly_ring(r,varname);
65                 store_univpoly_ring(R);
66                 ring_in_table = get_univpoly_ring(r,varname);
67                 if (!ring_in_table)
68                         cl_abort();
69         }
70         return *ring_in_table;
71 }
72
73 CL_PROVIDE_END(cl_UP_named)