Skip to content

Commit 90a2d84

Browse files
authored
Update to C API 0.99.15 (breaking) (#183)
* manually update C API files * minimal updates for build/tests/docs
1 parent 58e7c5e commit 90a2d84

16 files changed

+3697
-1686
lines changed

src/trees.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,12 @@ impl Tree {
337337

338338
/// Return the `[left, right)` coordinates of the tree.
339339
pub fn interval(&self) -> (f64, f64) {
340-
unsafe { ((*self.as_ptr()).left, (*self.as_ptr()).right) }
340+
unsafe {
341+
(
342+
(*self.as_ptr()).interval.left,
343+
(*self.as_ptr()).interval.right,
344+
)
345+
}
341346
}
342347

343348
/// Return the length of the genome for which this
@@ -681,7 +686,7 @@ impl<'a> RootIterator<'a> {
681686
fn new(tree: &'a Tree) -> Self {
682687
RootIterator {
683688
current_root: None,
684-
next_root: tree.inner.left_root.into(),
689+
next_root: unsafe { ll_bindings::tsk_tree_get_left_root(tree.as_ptr()).into() },
685690
tree,
686691
}
687692
}
@@ -905,9 +910,10 @@ impl TreeSequence {
905910
/// tskit::TreeSequenceFlags::default()).unwrap();
906911
/// ```
907912
pub fn new(tables: TableCollection, flags: TreeSequenceFlags) -> Result<Self, TskitError> {
913+
let mut t = tables;
908914
let mut treeseq = Self::wrap();
909915
let rv = unsafe {
910-
ll_bindings::tsk_treeseq_init(treeseq.as_mut_ptr(), tables.as_ptr(), flags.bits())
916+
ll_bindings::tsk_treeseq_init(treeseq.as_mut_ptr(), t.as_mut_ptr(), flags.bits())
911917
};
912918
handle_tsk_return_value!(rv, treeseq)
913919
}

subprojects/kastore/kastore.c

+13-9
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99

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

1417
const char *
1518
kas_strerror(int err)
@@ -261,7 +264,7 @@ kastore_read_descriptors(kastore_t *self)
261264
if (size + KAS_HEADER_SIZE > self->file_size) {
262265
goto out;
263266
}
264-
read_buffer = malloc(size);
267+
read_buffer = (char *) malloc(size);
265268
if (read_buffer == NULL) {
266269
ret = KAS_ERR_NO_MEMORY;
267270
goto out;
@@ -386,7 +389,7 @@ kastore_read_file(kastore_t *self)
386389
assert(size > offset);
387390
size -= offset;
388391

389-
self->read_buffer = malloc(size);
392+
self->read_buffer = (char *) malloc(size);
390393
if (self->read_buffer == NULL) {
391394
ret = KAS_ERR_NO_MEMORY;
392395
goto out;
@@ -480,7 +483,7 @@ kastore_read(kastore_t *self)
480483
goto out;
481484
}
482485
if (self->num_items > 0) {
483-
self->items = calloc(self->num_items, sizeof(*self->items));
486+
self->items = (kaitem_t *) calloc(self->num_items, sizeof(*self->items));
484487
if (self->items == NULL) {
485488
ret = KAS_ERR_NO_MEMORY;
486489
goto out;
@@ -559,6 +562,7 @@ kastore_open(kastore_t *self, const char *filename, const char *mode, int flags)
559562
tmp.file = NULL;
560563
if (err != 0) {
561564
ret = KAS_ERR_IO;
565+
goto out;
562566
}
563567
}
564568
file = fopen(filename, file_mode);
@@ -690,7 +694,7 @@ kastore_get(kastore_t *self, const char *key, size_t key_len, void **array,
690694
int ret = KAS_ERR_KEY_NOT_FOUND;
691695
kaitem_t search;
692696
kaitem_t *item;
693-
search.key = malloc(key_len);
697+
search.key = (char *) malloc(key_len);
694698
search.key_len = key_len;
695699

696700
if (self->mode != KAS_READ) {
@@ -733,7 +737,7 @@ static int KAS_WARN_UNUSED
733737
kastore_gets_type(
734738
kastore_t *self, const char *key, void **array, size_t *array_len, int type)
735739
{
736-
int loaded_type;
740+
int loaded_type = -1;
737741
int ret;
738742

739743
ret = kastore_get(self, key, strlen(key), array, array_len, &loaded_type);
@@ -847,7 +851,7 @@ kastore_oput(kastore_t *self, const char *key, size_t key_len, void *array,
847851
{
848852
int ret = 0;
849853
kaitem_t *new_item;
850-
void *p;
854+
kaitem_t *p;
851855
size_t j;
852856

853857
if (self->mode != KAS_WRITE) {
@@ -864,7 +868,7 @@ kastore_oput(kastore_t *self, const char *key, size_t key_len, void *array,
864868
}
865869
/* This isn't terribly efficient, but we're not expecting large
866870
* numbers of items. */
867-
p = realloc(self->items, (self->num_items + 1) * sizeof(*self->items));
871+
p = (kaitem_t *) realloc(self->items, (self->num_items + 1) * sizeof(*self->items));
868872
if (p == NULL) {
869873
ret = KAS_ERR_NO_MEMORY;
870874
goto out;
@@ -877,7 +881,7 @@ kastore_oput(kastore_t *self, const char *key, size_t key_len, void *array,
877881
new_item->key_len = key_len;
878882
new_item->array_len = array_len;
879883
new_item->array = array;
880-
new_item->key = malloc(key_len);
884+
new_item->key = (char *) malloc(key_len);
881885
if (new_item->key == NULL) {
882886
kas_safe_free(new_item->key);
883887
ret = KAS_ERR_NO_MEMORY;

subprojects/kastore/kastore.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ extern "C" {
2424
#include <stddef.h>
2525
#include <stdio.h>
2626

27-
/** @} */
28-
2927
/**
3028
@defgroup ERROR_GROUP Error return values.
3129
@{
@@ -155,7 +153,7 @@ to the API or ABI are introduced, i.e., the addition of a new function.
155153
The library patch version. Incremented when any changes not relevant to the
156154
to the API or ABI are introduced, i.e., internal refactors of bugfixes.
157155
*/
158-
#define KAS_VERSION_PATCH 0
156+
#define KAS_VERSION_PATCH 2
159157
/** @} */
160158

161159
#define KAS_HEADER_SIZE 64

subprojects/tskit/tskit/convert.c

+14-7
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,15 @@ tsk_newick_converter_run(
5757
const tsk_tree_t *tree = self->tree;
5858
tsk_id_t *stack = self->traversal_stack;
5959
const double *time = self->tree->tree_sequence->tables->nodes.time;
60+
const tsk_flags_t *flags = self->tree->tree_sequence->tables->nodes.flags;
6061
int stack_top = 0;
6162
int label;
6263
size_t s = 0;
6364
int r;
6465
tsk_id_t u, v, w, root_parent;
6566
double branch_length;
67+
bool ms_labels = self->options & TSK_NEWICK_LEGACY_MS_LABELS;
68+
const char *label_format = ms_labels ? "%d" : "n%d";
6669

6770
if (root < 0 || root >= (tsk_id_t) self->tree->num_nodes) {
6871
ret = TSK_ERR_NODE_OUT_OF_BOUNDS;
@@ -91,15 +94,20 @@ tsk_newick_converter_run(
9194
} else {
9295
u = tree->parent[v];
9396
stack_top--;
94-
if (tree->left_child[v] == TSK_NULL) {
97+
label = -1;
98+
if (ms_labels) {
99+
if (tree->left_child[v] == TSK_NULL) {
100+
label = (int) v + 1;
101+
}
102+
} else if (flags[v] & TSK_NODE_IS_SAMPLE) {
103+
label = (int) v;
104+
}
105+
if (label != -1) {
95106
if (s >= buffer_size) {
96107
ret = TSK_ERR_BUFFER_OVERFLOW;
97108
goto out;
98109
}
99-
/* We do this for ms-compatability. This should be a configurable option
100-
* via the flags attribute */
101-
label = (int) v + 1;
102-
r = snprintf(buffer + s, buffer_size - s, "%d", label);
110+
r = snprintf(buffer + s, buffer_size - s, label_format, label);
103111
if (r < 0) {
104112
ret = TSK_ERR_IO;
105113
goto out;
@@ -154,8 +162,7 @@ tsk_newick_converter_init(tsk_newick_converter_t *self, const tsk_tree_t *tree,
154162
self->options = options;
155163
self->tree = tree;
156164
self->traversal_stack
157-
= tsk_malloc(tsk_treeseq_get_num_nodes(self->tree->tree_sequence)
158-
* sizeof(*self->traversal_stack));
165+
= tsk_malloc(tsk_tree_get_size_bound(tree) * sizeof(*self->traversal_stack));
159166
if (self->traversal_stack == NULL) {
160167
ret = TSK_ERR_NO_MEMORY;
161168
goto out;

subprojects/tskit/tskit/convert.h

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ extern "C" {
3232

3333
#include <tskit/trees.h>
3434

35+
#define TSK_NEWICK_LEGACY_MS_LABELS (1 << 0)
36+
3537
int tsk_convert_newick(const tsk_tree_t *tree, tsk_id_t root, unsigned int precision,
3638
tsk_flags_t options, size_t buffer_size, char *buffer);
3739

0 commit comments

Comments
 (0)