1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
// Copyright 1996 Michael E. Stillman
// This should probably be done by:
// (a) making a type const FreeModule, that FreeModule, and res_poly
// both can inherit from: but this is a bit of a kludge...
// (b) making a vector type with a next and coeff field, that
// is then inherited by vecterm, resterm.
// Redefine:
// const FreeModule
// routines that should be implemented in this class:
// add_to, compare, get_ring, remove
// vecterm *
// fields of this structure type should include:
// next, coeff
// GEOHEAP_SIZE: defined in style.hpp
// heap_size: defined in object.cpp
class vecHeap
{
const FreeModule *F; // Our elements will be vectors in here
const Ring *K; // The coefficient ring
vecterm *heap[GEOHEAP_SIZE];
int top_of_heap;
int mLead; // set after a call to get_lead_term.
// set negative after each call to add,
// or remove_lead_term
public:
vecHeap(const FreeModule *F);
~vecHeap();
void add(vecterm *p);
const vecterm *get_lead_term(); // Returns NULL if none.
vecterm *remove_lead_term(); // Returns NULL if none.
const FreeModule *get_target() const { return F; }
vecterm *value(); // Returns the linearized value, and resets the vecHeap.
vecterm *debug_list(int i)
{
return heap[i];
} // DO NOT USE, except for debugging purposes!
vecterm *current_value()
const; // Adds up all the elements and returns this value
// Mainly used for debugging.
};
inline vecHeap::vecHeap(const FreeModule *FF)
: F(FF), K(FF->get_ring()), top_of_heap(-1), mLead(-1)
{
// set K
int i;
for (i = 0; i < GEOHEAP_SIZE; i++) heap[i] = NULL;
}
inline vecHeap::~vecHeap()
{
// The user of this class must insure that all 'vecterm's
// have been removed first. Thus, we don't need to
// do anything here.
}
inline void vecHeap::add(vecterm *p)
{
mLead = -1;
int len = K->n_nonzero_terms(p);
int i = 0;
while (len >= heap_size[i]) i++;
K->add_vec_to(heap[i], p);
len = K->n_nonzero_terms(heap[i]);
p = NULL;
while (len >= heap_size[i])
{
i++;
K->add_vec_to(heap[i], heap[i - 1]);
len = K->n_nonzero_terms(heap[i]);
heap[i - 1] = NULL;
}
if (i > top_of_heap) top_of_heap = i;
}
static int compare(const vecterm *t, const vecterm *s)
{
int cmp = t->comp - s->comp;
if (cmp < 0) return LT;
if (cmp > 0) return GT;
return EQ;
}
inline const vecterm *vecHeap::get_lead_term()
{
int lead_so_far = -1;
for (int i = 0; i <= top_of_heap; i++)
{
if (heap[i] == NULL) continue;
if (lead_so_far < 0)
{
lead_so_far = i;
continue;
}
int cmp = compare(heap[lead_so_far], heap[i]);
if (cmp == GT) continue;
if (cmp == LT)
{
lead_so_far = i;
continue;
}
// At this point we have equality
K->add_to(heap[lead_so_far]->coeff, heap[i]->coeff);
vecterm *tmp = heap[i];
heap[i] = tmp->next;
tmp->next = NULL;
K->remove_vec(tmp);
if (K->is_zero(heap[lead_so_far]->coeff))
{
// Remove, and start over
tmp = heap[lead_so_far];
heap[lead_so_far] = tmp->next;
tmp->next = NULL;
K->remove_vec(tmp);
lead_so_far = -1;
i = -1;
}
}
mLead = lead_so_far;
if (lead_so_far < 0) return NULL;
vecterm *result = heap[lead_so_far];
return result;
}
inline vecterm *vecHeap::remove_lead_term()
{
if (mLead < 0) get_lead_term();
if (mLead < 0) return NULL;
vecterm *result = heap[mLead];
heap[mLead] = result->next;
result->next = NULL;
mLead = -1;
return result;
}
inline vecterm *vecHeap::value()
{
vecterm *result = NULL;
for (int i = 0; i <= top_of_heap; i++)
{
if (heap[i] == NULL) continue;
K->add_vec_to(result, heap[i]);
heap[i] = NULL;
}
top_of_heap = -1;
return result;
}
inline vecterm *vecHeap::current_value() const
{
vecterm *result = NULL;
for (int i = 0; i <= top_of_heap; i++)
{
if (heap[i] == NULL) continue;
vecterm *tmp = K->copy_vec(heap[i]);
K->add_vec_to(result, tmp);
}
return result;
}
// Local Variables:
// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
// indent-tabs-mode: nil
// End:
|