Skip to main content

Module vector

Module vector 

Source
Expand description

Owned igraph vectors that behave like Rust slices.

igraph stores sequences in its own vector types (igraph_vector_t for reals, igraph_vector_int_t for integers, …). The types below are those very C structs, enriched with Rusty behaviour:

  • they own their buffer and destroy it on Drop;
  • they Deref to a Rust slice, so every slice method (len, iter, sort, indexing, contains, …) works with zero copies;
  • they convert from and to Vec, slices and iterators;
  • they implement Clone, Debug, PartialEq and Default.
Rust aliasC typeelement
Vectorigraph_vector_tf64
VectorIntigraph_vector_int_ti64
VectorBooligraph_vector_bool_tbool
VectorCharigraph_vector_char_tc_char
VectorComplexigraph_vector_complex_tigraph_complex_t

Borrowed, read-only views over Rust slices (no copy at all) are created with view, e.g. Vector::view; they are what the safe wrappers use to hand weights and other inputs to igraph.

Most wrappers in this crate return plain Vecs, so these types matter mostly when calling raw FFI functions, or to use igraph’s vector algorithms (below) on any slice. Lists of vectors are in list, matrices in matrix, sparse matrices in linalg (SparseMat), and misc has more numeric helpers, e.g. misc::running_mean and misc::power_law_fit.

This module covers igraph_vector_pmt.h (and the non-templated parts of igraph_vector.h and igraph_complex.h). Besides everything slices offer, the vectors provide igraph’s own algorithms:

GroupMethodsTypes
editinginsert, remove, swap_remove, remove_section, extend_from_slice, push, pop, truncate, resize, clearall
reorderingshuffle (igraph RNG), permute, select, move_interval, get_interval, searchall
memoryreserve, capacity, shrink_to_fitall
ordersort, reverse_sort, sort_ind, min, max, which_min, which_max, minmax, which_minmax, binsearch, contains_sortedreal, int, char
comparisonlex_cmp, colex_cmp, all_l, all_g, all_le, all_ge, maxdifference, is_in_interval, any_smallerreal, int, char
sorted setsintersect_sorted, difference_sorted, intersection_size_sorted, difference_and_intersection_sorted, filter_smallerreal, int, char
arithmeticsum, prod, cumsum, add_constant, scale, add, sub, mul, div, absreal, int, complex
floating pointall_almost_e, zapsmall, floor, round, is_nan, is_any_nan, is_all_finitereal (complex)
complexfrom_parts, from_polar, real, imag, realimag; igraph_complex_t has + - * /, abs, arg, exp, ln, sqrt, pow, trigonometry, …complex

Integer arithmetic is done in Rust with wrapping semantics (signed overflow in the C implementation would be undefined behaviour); real and complex arithmetic uses igraph’s functions. All index arguments are validated before reaching igraph, which does not check them.

use igraph::prelude::*;

let scores = Vector::from([0.3, 0.9, 0.1, 0.5]);
let ranking = scores.sort_ind(Order::Descending);
assert_eq!(ranking, vec![1, 3, 0, 2]);
assert_eq!(scores.which_max(), Some(1));
assert_eq!(scores.select(&[1, 3]).unwrap(), vec![0.9, 0.5]);

let a = VectorInt::from([1, 3, 5, 7]);
assert_eq!(a.intersect_sorted(&[3, 4, 5]), vec![3, 5]);
assert_eq!(a.binsearch(4), Err(2));
use igraph::prelude::*;

let mut v: VectorInt = (0..5).collect();
v.push(10);
v[0] = -1;
assert_eq!(v.len(), 6);
assert_eq!(v.iter().sum::<i64>(), 19);
assert_eq!(Vec::from(v), vec![-1, 1, 2, 3, 4, 10]);

let weights = [0.5, 1.5];
let view = Vector::view(&weights);
assert_eq!(&view[..], &weights[..]);

Structs§

View
A read-only view of a Rust slice as an igraph vector, without copying.

Functions§

format_real
Formats a real number the way igraph does in its text output (igraph_real_snprintf): %g-like, with NaN, Inf and -Inf for the special values.
format_real_precise
Formats a real number with 15 significant digits (igraph_real_snprintf_precise), as used by igraph’s writers.

Type Aliases§

Vector
Owned vector of reals (igraph_vector_t), see the module docs.
VectorBool
Owned vector of booleans (igraph_vector_bool_t), see the module docs.
VectorChar
Owned vector of chars (igraph_vector_char_t), see the module docs.
VectorComplex
Owned vector of complex numbers (igraph_vector_complex_t), see the module docs.
VectorInt
Owned vector of integers (igraph_vector_int_t), see the module docs.