biomcmc-lib  0.1
low level library for phylogenetic analysis
bipartition.h
Go to the documentation of this file.
1 /*
2  * This file is part of biomcmc-lib, a low-level library for phylogenomic analysis.
3  * Copyright (C) 2019-today Leonardo de Oliveira Martins [ leomrtns at gmail.com; http://www.leomartins.org ]
4  *
5  * biomcmc is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
6  * License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later
7  * version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
10  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11  * details (file "COPYING" or http://www.gnu.org/copyleft/gpl.html).
12  */
13 
19 #ifndef _biomcmc_bipartition_h_
20 #define _biomcmc_bipartition_h_
21 
22 
23 #include "lowlevel.h"
24 
25 typedef struct bipartition_struct* bipartition;
26 typedef struct bipsize_struct* bipsize;
27 typedef bipartition* tripartition; /* just a vector of size 3 */
28 
31 {
33  uint64_t *bs;
35  int n_ones;
37  bipsize n;
40 };
41 
43 {
45  uint64_t mask;
47  int ints, bits, original_size;
50 };
51 
55 bipartition new_bipartition (int size);
57 bipsize new_bipsize (int size);
59 bipartition new_bipartition_copy_from (const bipartition from);
61 bipartition new_bipartition_from_bipsize (bipsize n);
63 void del_bipartition (bipartition bip);
65 void del_bipsize (bipsize n);
67 void bipsize_resize (bipsize n, int nbits);
69 void bipartition_initialize (bipartition bip, int position);
71 void bipartition_zero (bipartition bip);
73 void bipartition_set (bipartition bip, int position);
74 void bipartition_set_lowlevel (bipartition bip, int i, int j);
76 void bipartition_unset (bipartition bip, int position);
77 void bipartition_unset_lowlevel (bipartition bip, int i, int j);
79 void bipartition_copy (bipartition to, const bipartition from);
82 void bipartition_OR (bipartition result, const bipartition b1, const bipartition b2, bool update_count);
85 void bipartition_AND (bipartition result, const bipartition b1, const bipartition b2, bool update_count);
87 void bipartition_ANDNOT (bipartition result, const bipartition b1, const bipartition b2, bool update_count);
90 void bipartition_XOR (bipartition result, const bipartition b1, const bipartition b2, bool update_count);
93 void bipartition_XORNOT (bipartition result, const bipartition b1, const bipartition b2, bool update_count);
95 void bipartition_NOT (bipartition result, const bipartition bip);
98 void bipartition_count_n_ones (const bipartition bip);
100 void bipartition_to_int_vector (const bipartition b, int *id, int vecsize);
102 bool bipartition_is_equal (const bipartition b1, const bipartition b2);
104 bool bipartition_is_equal_bothsides (const bipartition b1, const bipartition b2);
106 int compare_bipartitions_increasing (const void *a1, const void *a2);
108 int compare_bipartitions_decreasing (const void *a1, const void *a2);
110 bool bipartition_is_larger (const bipartition b1, const bipartition b2);
112 void bipartition_flip_to_smaller_set (bipartition bip);
114 bool bipartition_is_bit_set (const bipartition bip, int position);
116 bool bipartition_contains_bits (const bipartition b1, const bipartition b2);
118 void bipartition_print_to_stdout (const bipartition b1);
120 void bipartition_replace_bit_in_vector (bipartition *bvec, int n_b, int to, int from, bool reduce);
122 void bipartition_resize_vector (bipartition *bvec, int n_b);
123 
125 tripartition new_tripartition (int nleaves);
127 void del_tripartition (tripartition trip);
129 void store_tripartition_from_bipartitions (tripartition tri, bipartition b1, bipartition b2);
136 
137 
138 #endif
bipsize new_bipsize(int size)
Create a new bipsize, which controls some bipartition sizes.
Definition: bipartition.c:36
bool bipartition_is_equal_bothsides(const bipartition b1, const bipartition b2)
Compare if two bipartitions represent the same splits (or they are equal or one is the complement of ...
Definition: bipartition.c:269
void bipartition_unset(bipartition bip, int position)
simply unset the bit at "position" (set to zero), irrespective of other bits
Definition: bipartition.c:152
Definition: bipartition.h:42
void bipartition_initialize(bipartition bip, int position)
set all bits to zero except the one at position-th bit
Definition: bipartition.c:118
bipartition new_bipartition_from_bipsize(bipsize n)
create new bipartition that will share bipsize – useful for bipartition vectors
Definition: bipartition.c:69
void bipartition_OR(bipartition result, const bipartition b1, const bipartition b2, bool update_count)
Binary logical OR ("|") between b1 and b2, where update_count should be true if you need to know the ...
Definition: bipartition.c:174
bipartition new_bipartition_copy_from(const bipartition from)
Create a new bipartition (allocate memory) and initialize it from another bipartition.
Definition: bipartition.c:52
Definition: lowlevel.h:71
void bipartition_XORNOT(bipartition result, const bipartition b1, const bipartition b2, bool update_count)
Binary logical eXclusive OR ("^") between b1 and complement of b2 (that is, NOT b2: b1 ^ ~b2)...
Definition: bipartition.c:224
void bipsize_resize(bipsize n, int nbits)
update the valid number of bits and mask – e.g. when replacing subtrees by leaves in reduced trees ...
Definition: bipartition.c:108
void bipartition_count_n_ones(const bipartition bip)
Count the number of active bits (equal to one). Used by bipartition_AND() and bipartition_XOR() when ...
Definition: bipartition.c:243
int compare_bipartitions_increasing(const void *a1, const void *a2)
Bipartitions comparison, to be used by sort() since returns integer and uses (void) ...
Definition: bipartition.c:283
bipartition new_bipartition(int size)
create a new bipartition (bitstring) capable of storing an arbitrary number of bits and initialize it...
Definition: bipartition.c:19
void bipartition_set(bipartition bip, int position)
simply set the bit at "position" to one, irrespective of other bits
Definition: bipartition.c:138
int n_ones
Counter (number of "one"s)
Definition: bipartition.h:35
void del_tripartition(tripartition trip)
free tripartition space (just 3 bipartitions)
Definition: bipartition.c:413
bipsize n
number of bits (leaves), vector size and mask
Definition: bipartition.h:37
Lowest level header file. Header file for lowlevel.c.
void bipartition_print_to_stdout(const bipartition b1)
Print to screen a bit representation of the bipartition (with number of ones at the end) ...
Definition: bipartition.c:354
bool bipartition_is_equal(const bipartition b1, const bipartition b2)
Compare equality of two bipartitions.
Definition: bipartition.c:257
void del_bipsize(bipsize n)
free memory allocated by bipsize
Definition: bipartition.c:99
int ref_counter
How many times this struct is being referenced.
Definition: bipartition.h:49
void store_tripartition_from_bipartitions(tripartition tri, bipartition b1, bipartition b2)
from node, create tripartition from node->left and node->right (assuming bipartitions were not 'flipp...
Definition: bipartition.c:421
int compare_bipartitions_decreasing(const void *a1, const void *a2)
Bipartitions comparison, to be used by sort() since returns integer and uses (void) ...
Definition: bipartition.c:297
uint64_t mask
mask to make sure we consider only active positions (of last bitstring)
Definition: bipartition.h:45
void bipartition_to_int_vector(const bipartition b, int *id, int vecsize)
fill vector id[] with positions of set bits, up to vecsize bits set
Definition: bipartition.c:347
void bipartition_AND(bipartition result, const bipartition b1, const bipartition b2, bool update_count)
Binary logical AND ("&") between b1 and b2, update_count should be set to false only if you really don't ...
Definition: bipartition.c:184
bool bipartition_contains_bits(const bipartition b1, const bipartition b2)
Check if first bipartition contains all elements of second bipartition (b2 is a subset of b1) ...
Definition: bipartition.c:338
bool bipartition_is_bit_set(const bipartition bip, int position)
Check if position-th bit is equal to one or not.
Definition: bipartition.c:331
void bipartition_zero(bipartition bip)
set all bits to zero
Definition: bipartition.c:130
bool bipartition_is_larger(const bipartition b1, const bipartition b2)
Compare sizes of two bipartitions, by number of active bits with ties broken by actual bitstrings...
Definition: bipartition.c:303
void sort_tripartition(tripartition tri)
sort order of bipartitions s.t. smallest is first
Definition: bipartition.c:430
void bipartition_ANDNOT(bipartition result, const bipartition b1, const bipartition b2, bool update_count)
Binary logical AND ("&") between b1 and ~b2 (NOT b2), that is, apply mask b1 on the inverse of b2...
Definition: bipartition.c:194
bool tripartition_is_equal(tripartition tp1, tripartition tp2)
assuming tripartitions are ordered, check if nodes (represented by tripartitions) are the same ...
Definition: bipartition.c:465
int align_tripartitions(tripartition tp1, tripartition tp2, hungarian h)
match bipartitions between two nodes and return optimal score (min disagreement)
Definition: bipartition.c:450
uint64_t * bs
Representation of a bipartition by a vector of integers (bitstrings).
Definition: bipartition.h:33
void bipartition_NOT(bipartition result, const bipartition bip)
Unary complement ("~") of bipartition. Use with caution, since there is no mask for unused padded bit...
Definition: bipartition.c:234
int ref_counter
How many times this struct is being referenced.
Definition: bipartition.h:39
tripartition new_tripartition(int nleaves)
tripartition of a node (a vector with 3 bipartitions, that should not be 'flipped' to smaller set...
Definition: bipartition.c:402
void bipartition_replace_bit_in_vector(bipartition *bvec, int n_b, int to, int from, bool reduce)
replace bit info, copying 'from' one position 'to' another; bool "update" indicates if afterwards siz...
Definition: bipartition.c:366
int ints
Vector size and total number of elements (n_ints = n_bits/(8*sizeof(long long)) +1).
Definition: bipartition.h:47
Bit-string representation of splits.
Definition: bipartition.h:30
void del_bipartition(bipartition bip)
free memory allocated by bipartition
Definition: bipartition.c:87
void bipartition_copy(bipartition to, const bipartition from)
Copy contents from one bipartition to another.
Definition: bipartition.c:166
void bipartition_flip_to_smaller_set(bipartition bip)
invert ones and zeroes in loco when necessary to assure bipartition has more zeroes than ones ...
Definition: bipartition.c:315
void bipartition_resize_vector(bipartition *bvec, int n_b)
apply mask to last element (useful after manipulations) and count number of bits
Definition: bipartition.c:393
void bipartition_XOR(bipartition result, const bipartition b1, const bipartition b2, bool update_count)
Binary logical eXclusive OR ("^") between b1 and b2, update_count should be set to false only if you ...
Definition: bipartition.c:214