Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data structures update #77

Merged
merged 21 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/poly.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ typedef struct lp_interval_struct lp_interval_t;
typedef struct lp_feasibility_set_struct lp_feasibility_set_t;
typedef struct lp_polynomial_hash_set_struct lp_polynomial_hash_set_t;
typedef struct lp_polynomial_vector_struct lp_polynomial_vector_t;
typedef struct lp_polynomial_heap_struct lp_polynomial_heap_t;

/** Enable a given tag for tracing */
void lp_trace_enable(const char* tag);
Expand Down
33 changes: 29 additions & 4 deletions include/polynomial_hash_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,55 @@ struct lp_polynomial_hash_set_struct {
size_t data_size;
/** Number of set elements */
size_t size;
/** Treshold for resize */
/** Threshold for resize */
size_t resize_threshold;
/** Has the set been closed */
int closed;
};

/** Allocate and construct a new hash set */
lp_polynomial_hash_set_t* lp_polynomial_hash_set_new(void);

/** Destructs and deletes the hash set */
void lp_polynomial_hash_set_delete(lp_polynomial_hash_set_t* set);

/** Construct a new set */
void lp_polynomial_hash_set_construct(lp_polynomial_hash_set_t* set);

/** Destruct the set */
void lp_polynomial_hash_set_destruct(lp_polynomial_hash_set_t* set);

/** Returns true if empty */
int lp_polynomial_hash_set_is_empty(lp_polynomial_hash_set_t* set);
int lp_polynomial_hash_set_is_empty(const lp_polynomial_hash_set_t* set);

/** Returns the number of elements */
size_t lp_polynomial_hash_set_size(const lp_polynomial_hash_set_t* set);

/** Check whether p is in set. The set must not be closed). */
int lp_polynomial_hash_set_contains(lp_polynomial_hash_set_t* set, const lp_polynomial_t* p);
int lp_polynomial_hash_set_contains(const lp_polynomial_hash_set_t* set, const lp_polynomial_t* p);

/** Add polynomial p to set. Returns true if p was added (not already in the set). */
/** Add polynomial p to the set. Returns true if p was added (not already in the set). */
int lp_polynomial_hash_set_insert(lp_polynomial_hash_set_t* set, const lp_polynomial_t* p);

/** Add polynomial p to the set. Returns true if p was added (not already in the set) and p becomes a 0 polynomial.
* Returns false if p was found in the set and p remained unchanged. */
int lp_polynomial_hash_set_insert_move(lp_polynomial_hash_set_t* set, lp_polynomial_t* p);

/** Add all polynomials from the vector to the hash map. Returns the number of inserted polynomials */
int lp_polynomial_hash_set_insert_vector(lp_polynomial_hash_set_t* set, const lp_polynomial_vector_t* v);

/** Add polynomial p to set. Returns true if p was removed (was in the set). */
int lp_polynomial_hash_set_remove(lp_polynomial_hash_set_t* set, const lp_polynomial_t* p);

/** Removes all elements from set that are not in other */
void lp_polynomial_hash_set_intersect(lp_polynomial_hash_set_t* set, const lp_polynomial_hash_set_t* other);

/** Close the set: compact the data so that all elements get stored in data[0..size]. No addition after close! */
void lp_polynomial_hash_set_close(lp_polynomial_hash_set_t* set);

/** Returns one element at index. Hashset must be closed. */
const lp_polynomial_t* lp_polynomial_hash_set_at(const lp_polynomial_hash_set_t* set, size_t n);

/** Clear the set. */
void lp_polynomial_hash_set_clear(lp_polynomial_hash_set_t* set);

Expand Down
88 changes: 88 additions & 0 deletions include/polynomial_heap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* Copyright 2015, SRI International.
*
* This file is part of LibPoly.
*
* LibPoly is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LibPoly is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with LibPoly. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include "poly.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef int (*lp_polynomial_heap_compare_f)(const lp_polynomial_t* A, const lp_polynomial_t* B);

struct lp_polynomial_heap_struct {
/** The data */
lp_polynomial_t** data;
/** Size of the data */
size_t data_size;
/** Number of set elements */
size_t size;
/** The compare function */
lp_polynomial_heap_compare_f cmp;
};

/** Allocates a new heap and constructs it */
lp_polynomial_heap_t* lp_polynomial_heap_new(lp_polynomial_heap_compare_f cmp);

/** Destructs a heap and frees the memory */
void lp_polynomial_heap_delete(lp_polynomial_heap_t* heap);

/** Construct a new heap */
void lp_polynomial_heap_construct(lp_polynomial_heap_t* heap, lp_polynomial_heap_compare_f cmp);

/** Destruct the heap */
void lp_polynomial_heap_destruct(lp_polynomial_heap_t* heap);

/** Returns true if empty */
int lp_polynomial_heap_is_empty(const lp_polynomial_heap_t* heap);

/** Returns the number of elements */
size_t lp_polynomial_heap_size(const lp_polynomial_heap_t* heap);

/** Add polynomial p to the heap. */
void lp_polynomial_heap_push(lp_polynomial_heap_t* heap, const lp_polynomial_t* p);

/** Add polynomial p to the heap. Moves the content of p and p becomes a 0 polynomial. */
ahmed-irfan marked this conversation as resolved.
Show resolved Hide resolved
void lp_polynomial_heap_push_move(lp_polynomial_heap_t* heap, lp_polynomial_t* p);

/** Add all polynomials from the vector to the heap */
void lp_polynomial_heap_push_vector(lp_polynomial_heap_t* heap, const lp_polynomial_vector_t* v);

/** Removes and returns the top element of the heap, returns NULL if the heap is empty */
lp_polynomial_t* lp_polynomial_heap_pop(lp_polynomial_heap_t* heap);

/** Removes an element from the heap. Returns number of removed polynomials. */
int lp_polynomial_heap_remove(lp_polynomial_heap_t* heap, const lp_polynomial_t *p);

/** Returns the top element without removing it. */
const lp_polynomial_t* lp_polynomial_heap_peek(const lp_polynomial_heap_t* heap);

/** Returns one element */
const lp_polynomial_t* lp_polynomial_heap_at(const lp_polynomial_heap_t* heap, size_t n);

/** Clear the heap. */
void lp_polynomial_heap_clear(lp_polynomial_heap_t* heap);

/** Prints the heap */
void lp_polynomial_heap_print(lp_polynomial_heap_t* heap, FILE *);

#ifdef __cplusplus
} /* close extern "C" { */
#endif
18 changes: 17 additions & 1 deletion include/polynomial_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,22 @@ extern "C" {
/** Allocate and construct a new vector */
lp_polynomial_vector_t* lp_polynomial_vector_new(const lp_polynomial_context_t* ctx);

/* Delete the vector */
/** Allocate and construct a new vector, copies all elements from v */
lp_polynomial_vector_t* lp_polynomial_vector_copy(const lp_polynomial_vector_t *v);

/** Delete the vector */
void lp_polynomial_vector_delete(lp_polynomial_vector_t* v);

/** Swap two vectors */
void lp_polynomial_vector_swap(lp_polynomial_vector_t *v1, lp_polynomial_vector_t *v2);

/** Add to back (makes a copy, should be in the context of the vector) */
void lp_polynomial_vector_push_back(lp_polynomial_vector_t* v, const lp_polynomial_t* p);

/** Add to back by move. More efficient than copying, p becomes a (constructed) zero-polynomial
* and must be in the context of the vector. */
void lp_polynomial_vector_push_back_move(lp_polynomial_vector_t* v, lp_polynomial_t* p);

/** Reset the vector to 0 elements */
void lp_polynomial_vector_reset(lp_polynomial_vector_t* v);

Expand All @@ -43,6 +53,12 @@ size_t lp_polynomial_vector_size(const lp_polynomial_vector_t* v);
/** Returns the polynomial at i (newly constructed each time) */
lp_polynomial_t* lp_polynomial_vector_at(const lp_polynomial_vector_t* v, size_t i);

/** Returns the context of the polynomial vector */
const lp_polynomial_context_t* lp_polynomial_vector_get_context(const lp_polynomial_vector_t *v);

/** Prints the polynomial vector to the given stream. */
void lp_polynomial_vector_print(const lp_polynomial_vector_t* v, FILE* out);

#ifdef __cplusplus
} /* close extern "C" { */
#endif
6 changes: 6 additions & 0 deletions include/upolynomial.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ int lp_upolynomial_print(const lp_upolynomial_t* p, FILE* out);
*/
char* lp_upolynomial_to_string(const lp_upolynomial_t* p);

/**
* Converts the upolynomial to a polynomial in variable var.
* ctx must have the same ring than upolynomial.
*/
lp_polynomial_t* lp_upolynomial_to_polynomial(const lp_upolynomial_t* p, const lp_polynomial_context_t* ctx, lp_variable_t var);

/**
* Returns true if this is a zero polynomial
*/
Expand Down
16 changes: 16 additions & 0 deletions include/variable_order.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ void lp_variable_order_detach(lp_variable_order_t* var_order);
*/
int lp_variable_order_cmp(const lp_variable_order_t* var_order, lp_variable_t x, lp_variable_t y);

/** returns the bigger of the two variables wrt. the order */
static inline
lp_variable_t lp_variable_order_max(const lp_variable_order_t* var_order, lp_variable_t x, lp_variable_t y) {
if (x == lp_variable_null) return y;
if (y == lp_variable_null) return x;
return lp_variable_order_cmp(var_order, x, y) < 0 ? x : y;
}

/** returns the smaller of the two variables wrt. the order */
static inline
lp_variable_t lp_variable_order_min(const lp_variable_order_t* var_order, lp_variable_t x, lp_variable_t y) {
if (x == lp_variable_null) return y;
if (y == lp_variable_null) return x;
return lp_variable_order_cmp(var_order, x, y) > 0 ? x : y;
}

/** Get the size of the order */
size_t lp_variable_order_size(const lp_variable_order_t* var_order);

Expand Down
2 changes: 1 addition & 1 deletion python/polypyPolynomial3.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ PyMethodDef Polynomial_methods[] = {
{"coefficients", (PyCFunction)Polynomial_coefficients, METH_NOARGS, "Returns a dictionary from degrees to coefficients"},
{"reductum", (PyCFunction)Polynomial_reductum, METH_VARARGS, "Returns the reductum of the polynomial"},
{"sgn", (PyCFunction)Polynomial_sgn, METH_VARARGS, "Returns the sign of the polynomials in the given model"},
{"sgn_check", (PyCFunction)Polynomial_sgn_check, METH_VARARGS, "Returns true if the sign of the polynomail respects the sign condition."},
{"sgn_check", (PyCFunction)Polynomial_sgn_check, METH_VARARGS, "Returns true if the sign of the polynomial respects the sign condition."},
{"rem", (PyCFunction)Polynomial_rem, METH_VARARGS, "Returns the remainder of current and given polynomial"},
{"prem", (PyCFunction)Polynomial_prem, METH_VARARGS, "Returns the pseudo remainder of current and given polynomial"},
{"sprem", (PyCFunction)Polynomial_sprem, METH_VARARGS, "Returns the sparse pseudo remainder of current and given polynomial"},
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ set(poly_SOURCES
polynomial/polynomial_context.c
polynomial/feasibility_set.c
polynomial/polynomial_hash_set.c
polynomial/polynomial_heap.c
polynomial/polynomial_vector.c
poly.c
)
Expand Down
Loading
Loading