Skip to content

C API 0.99.15 #183

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

Merged
merged 1 commit into from
Dec 7, 2021
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
12 changes: 9 additions & 3 deletions src/trees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,12 @@ impl Tree {

/// Return the `[left, right)` coordinates of the tree.
pub fn interval(&self) -> (f64, f64) {
unsafe { ((*self.as_ptr()).left, (*self.as_ptr()).right) }
unsafe {
(
(*self.as_ptr()).interval.left,
(*self.as_ptr()).interval.right,
)
}
}

/// Return the length of the genome for which this
Expand Down Expand Up @@ -681,7 +686,7 @@ impl<'a> RootIterator<'a> {
fn new(tree: &'a Tree) -> Self {
RootIterator {
current_root: None,
next_root: tree.inner.left_root.into(),
next_root: unsafe { ll_bindings::tsk_tree_get_left_root(tree.as_ptr()).into() },
tree,
}
}
Expand Down Expand Up @@ -905,9 +910,10 @@ impl TreeSequence {
/// tskit::TreeSequenceFlags::default()).unwrap();
/// ```
pub fn new(tables: TableCollection, flags: TreeSequenceFlags) -> Result<Self, TskitError> {
let mut t = tables;
let mut treeseq = Self::wrap();
let rv = unsafe {
ll_bindings::tsk_treeseq_init(treeseq.as_mut_ptr(), tables.as_ptr(), flags.bits())
ll_bindings::tsk_treeseq_init(treeseq.as_mut_ptr(), t.as_mut_ptr(), flags.bits())
};
handle_tsk_return_value!(rv, treeseq)
}
Expand Down
22 changes: 13 additions & 9 deletions subprojects/kastore/kastore.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@

/* Private flag used to indicate when we have opened the file ourselves
* and need to free it. */
#define OWN_FILE (1 << 31)
/* Note: we use 1<<14 to keep this flag at the end of the flag space,
* and this is the highest bit that can be guaranteed to fit into
* an int. */
#define OWN_FILE (1 << 14)

const char *
kas_strerror(int err)
Expand Down Expand Up @@ -261,7 +264,7 @@ kastore_read_descriptors(kastore_t *self)
if (size + KAS_HEADER_SIZE > self->file_size) {
goto out;
}
read_buffer = malloc(size);
read_buffer = (char *) malloc(size);
if (read_buffer == NULL) {
ret = KAS_ERR_NO_MEMORY;
goto out;
Expand Down Expand Up @@ -386,7 +389,7 @@ kastore_read_file(kastore_t *self)
assert(size > offset);
size -= offset;

self->read_buffer = malloc(size);
self->read_buffer = (char *) malloc(size);
if (self->read_buffer == NULL) {
ret = KAS_ERR_NO_MEMORY;
goto out;
Expand Down Expand Up @@ -480,7 +483,7 @@ kastore_read(kastore_t *self)
goto out;
}
if (self->num_items > 0) {
self->items = calloc(self->num_items, sizeof(*self->items));
self->items = (kaitem_t *) calloc(self->num_items, sizeof(*self->items));
if (self->items == NULL) {
ret = KAS_ERR_NO_MEMORY;
goto out;
Expand Down Expand Up @@ -559,6 +562,7 @@ kastore_open(kastore_t *self, const char *filename, const char *mode, int flags)
tmp.file = NULL;
if (err != 0) {
ret = KAS_ERR_IO;
goto out;
}
}
file = fopen(filename, file_mode);
Expand Down Expand Up @@ -690,7 +694,7 @@ kastore_get(kastore_t *self, const char *key, size_t key_len, void **array,
int ret = KAS_ERR_KEY_NOT_FOUND;
kaitem_t search;
kaitem_t *item;
search.key = malloc(key_len);
search.key = (char *) malloc(key_len);
search.key_len = key_len;

if (self->mode != KAS_READ) {
Expand Down Expand Up @@ -733,7 +737,7 @@ static int KAS_WARN_UNUSED
kastore_gets_type(
kastore_t *self, const char *key, void **array, size_t *array_len, int type)
{
int loaded_type;
int loaded_type = -1;
int ret;

ret = kastore_get(self, key, strlen(key), array, array_len, &loaded_type);
Expand Down Expand Up @@ -847,7 +851,7 @@ kastore_oput(kastore_t *self, const char *key, size_t key_len, void *array,
{
int ret = 0;
kaitem_t *new_item;
void *p;
kaitem_t *p;
size_t j;

if (self->mode != KAS_WRITE) {
Expand All @@ -864,7 +868,7 @@ kastore_oput(kastore_t *self, const char *key, size_t key_len, void *array,
}
/* This isn't terribly efficient, but we're not expecting large
* numbers of items. */
p = realloc(self->items, (self->num_items + 1) * sizeof(*self->items));
p = (kaitem_t *) realloc(self->items, (self->num_items + 1) * sizeof(*self->items));
if (p == NULL) {
ret = KAS_ERR_NO_MEMORY;
goto out;
Expand All @@ -877,7 +881,7 @@ kastore_oput(kastore_t *self, const char *key, size_t key_len, void *array,
new_item->key_len = key_len;
new_item->array_len = array_len;
new_item->array = array;
new_item->key = malloc(key_len);
new_item->key = (char *) malloc(key_len);
if (new_item->key == NULL) {
kas_safe_free(new_item->key);
ret = KAS_ERR_NO_MEMORY;
Expand Down
4 changes: 1 addition & 3 deletions subprojects/kastore/kastore.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ extern "C" {
#include <stddef.h>
#include <stdio.h>

/** @} */

/**
@defgroup ERROR_GROUP Error return values.
@{
Expand Down Expand Up @@ -155,7 +153,7 @@ to the API or ABI are introduced, i.e., the addition of a new function.
The library patch version. Incremented when any changes not relevant to the
to the API or ABI are introduced, i.e., internal refactors of bugfixes.
*/
#define KAS_VERSION_PATCH 0
#define KAS_VERSION_PATCH 2
/** @} */

#define KAS_HEADER_SIZE 64
Expand Down
21 changes: 14 additions & 7 deletions subprojects/tskit/tskit/convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,15 @@ tsk_newick_converter_run(
const tsk_tree_t *tree = self->tree;
tsk_id_t *stack = self->traversal_stack;
const double *time = self->tree->tree_sequence->tables->nodes.time;
const tsk_flags_t *flags = self->tree->tree_sequence->tables->nodes.flags;
int stack_top = 0;
int label;
size_t s = 0;
int r;
tsk_id_t u, v, w, root_parent;
double branch_length;
bool ms_labels = self->options & TSK_NEWICK_LEGACY_MS_LABELS;
const char *label_format = ms_labels ? "%d" : "n%d";

if (root < 0 || root >= (tsk_id_t) self->tree->num_nodes) {
ret = TSK_ERR_NODE_OUT_OF_BOUNDS;
Expand Down Expand Up @@ -91,15 +94,20 @@ tsk_newick_converter_run(
} else {
u = tree->parent[v];
stack_top--;
if (tree->left_child[v] == TSK_NULL) {
label = -1;
if (ms_labels) {
if (tree->left_child[v] == TSK_NULL) {
label = (int) v + 1;
}
} else if (flags[v] & TSK_NODE_IS_SAMPLE) {
label = (int) v;
}
if (label != -1) {
if (s >= buffer_size) {
ret = TSK_ERR_BUFFER_OVERFLOW;
goto out;
}
/* We do this for ms-compatability. This should be a configurable option
* via the flags attribute */
label = (int) v + 1;
r = snprintf(buffer + s, buffer_size - s, "%d", label);
r = snprintf(buffer + s, buffer_size - s, label_format, label);
if (r < 0) {
ret = TSK_ERR_IO;
goto out;
Expand Down Expand Up @@ -154,8 +162,7 @@ tsk_newick_converter_init(tsk_newick_converter_t *self, const tsk_tree_t *tree,
self->options = options;
self->tree = tree;
self->traversal_stack
= tsk_malloc(tsk_treeseq_get_num_nodes(self->tree->tree_sequence)
* sizeof(*self->traversal_stack));
= tsk_malloc(tsk_tree_get_size_bound(tree) * sizeof(*self->traversal_stack));
if (self->traversal_stack == NULL) {
ret = TSK_ERR_NO_MEMORY;
goto out;
Expand Down
2 changes: 2 additions & 0 deletions subprojects/tskit/tskit/convert.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ extern "C" {

#include <tskit/trees.h>

#define TSK_NEWICK_LEGACY_MS_LABELS (1 << 0)

int tsk_convert_newick(const tsk_tree_t *tree, tsk_id_t root, unsigned int precision,
tsk_flags_t options, size_t buffer_size, char *buffer);

Expand Down
Loading