1use cxx::UniquePtr;
2
3use crate::bridge::{self, *};
4use crate::graph;
5use miette::{bail, Result};
6
7pub struct GraphBuilder {
8 inner: UniquePtr<bridge::GraphBuilder>,
9}
10
11unsafe impl Send for GraphBuilder {}
12
13impl GraphBuilder {
14 pub fn new(n: u64, weighted: bool, directed: bool) -> Self {
15 Self {
16 inner: NewGraphBuilder(n, weighted, directed),
17 }
18 }
19 pub fn complete_graph(&mut self, parallel: bool) -> graph::Graph {
20 let g = GraphBuilderCompleteGraph(self.inner.pin_mut(), parallel);
21 graph::Graph { inner: g }
22 }
23 pub fn reset(&mut self, n: u64) {
24 self.inner.pin_mut().reset(n);
25 }
26 pub fn is_weighted(&self) -> bool {
27 self.inner.isWeighted()
28 }
29 pub fn is_directed(&self) -> bool {
30 self.inner.isDirected()
31 }
32 pub fn is_empty(&self) -> bool {
33 self.inner.isEmpty()
34 }
35 pub fn number_of_nodes(&self) -> u64 {
36 self.inner.numberOfNodes()
37 }
38 pub fn upper_node_id_bound(&self) -> u64 {
39 self.inner.upperNodeIdBound()
40 }
41 pub fn add_node(&mut self) -> u64 {
42 self.inner.pin_mut().addNode()
43 }
44 pub unsafe fn add_half_edge_unchecked(&mut self, u: u64, v: u64, ew: Option<f64>) {
45 self.inner.pin_mut().addHalfEdge(u, v, ew.unwrap_or(1.))
46 }
47 pub fn add_half_edge(&mut self, u: u64, v: u64, ew: Option<f64>) -> Result<()> {
48 for n in [u, v] {
49 if n >= self.upper_node_id_bound() {
50 bail!("Node out of bound: {}", n);
51 }
52 }
53 Ok(unsafe { self.add_half_edge_unchecked(u, v, ew) })
54 }
55 pub unsafe fn add_half_out_edge_unchecked(&mut self, u: u64, v: u64, ew: Option<f64>) {
56 self.inner.pin_mut().addHalfOutEdge(u, v, ew.unwrap_or(1.))
57 }
58 pub fn add_half_out_edge(&mut self, u: u64, v: u64, ew: Option<f64>) -> Result<()> {
59 for n in [u, v] {
60 if n >= self.upper_node_id_bound() {
61 bail!("Node out of bound: {}", n);
62 }
63 }
64 Ok(unsafe { self.add_half_out_edge_unchecked(u, v, ew) })
65 }
66 pub unsafe fn add_half_in_edge_unchecked(&mut self, u: u64, v: u64, ew: Option<f64>) {
67 self.inner.pin_mut().addHalfInEdge(u, v, ew.unwrap_or(1.))
68 }
69 pub fn add_half_in_edge(&mut self, u: u64, v: u64, ew: Option<f64>) -> Result<()> {
70 for n in [u, v] {
71 if n >= self.upper_node_id_bound() {
72 bail!("Node out of bound: {}", n);
73 }
74 }
75 Ok(unsafe { self.add_half_in_edge_unchecked(u, v, ew) })
76 }
77 pub unsafe fn set_weight_unchecked(&mut self, u: u64, v: u64, ew: Option<f64>) {
78 self.inner.pin_mut().setWeight(u, v, ew.unwrap_or(1.))
79 }
80 pub fn set_weight(&mut self, u: u64, v: u64, ew: Option<f64>) -> Result<()> {
81 for n in [u, v] {
82 if n >= self.upper_node_id_bound() {
83 bail!("Node out of bound: {}", n);
84 }
85 }
86 Ok(unsafe { self.set_weight_unchecked(u, v, ew) })
87 }
88 pub unsafe fn set_out_weight_unchecked(&mut self, u: u64, v: u64, ew: Option<f64>) {
89 self.inner.pin_mut().setOutWeight(u, v, ew.unwrap_or(1.))
90 }
91 pub fn set_out_weight(&mut self, u: u64, v: u64, ew: Option<f64>) -> Result<()> {
92 for n in [u, v] {
93 if n >= self.upper_node_id_bound() {
94 bail!("Node out of bound: {}", n);
95 }
96 }
97 Ok(unsafe { self.set_out_weight_unchecked(u, v, ew) })
98 }
99 pub unsafe fn set_in_weight_unchecked(&mut self, u: u64, v: u64, ew: Option<f64>) {
100 self.inner.pin_mut().setInWeight(u, v, ew.unwrap_or(1.))
101 }
102 pub fn set_in_weight(&mut self, u: u64, v: u64, ew: Option<f64>) -> Result<()> {
103 for n in [u, v] {
104 if n >= self.upper_node_id_bound() {
105 bail!("Node out of bound: {}", n);
106 }
107 }
108 Ok(unsafe { self.set_in_weight_unchecked(u, v, ew) })
109 }
110 pub unsafe fn increase_weight_unchecked(&mut self, u: u64, v: u64, ew: Option<f64>) {
111 self.inner.pin_mut().increaseWeight(u, v, ew.unwrap_or(1.))
112 }
113 pub fn increase_weight(&mut self, u: u64, v: u64, ew: Option<f64>) -> Result<()> {
114 for n in [u, v] {
115 if n >= self.upper_node_id_bound() {
116 bail!("Node out of bound: {}", n);
117 }
118 }
119 Ok(unsafe { self.increase_weight_unchecked(u, v, ew) })
120 }
121 pub unsafe fn increase_out_weight_unchecked(&mut self, u: u64, v: u64, ew: Option<f64>) {
122 self.inner
123 .pin_mut()
124 .increaseOutWeight(u, v, ew.unwrap_or(1.))
125 }
126 pub fn increase_out_weight(&mut self, u: u64, v: u64, ew: Option<f64>) -> Result<()> {
127 for n in [u, v] {
128 if n >= self.upper_node_id_bound() {
129 bail!("Node out of bound: {}", n);
130 }
131 }
132 Ok(unsafe { self.increase_out_weight_unchecked(u, v, ew) })
133 }
134 pub unsafe fn increase_in_weight_unchecked(&mut self, u: u64, v: u64, ew: Option<f64>) {
135 self.inner
136 .pin_mut()
137 .increaseInWeight(u, v, ew.unwrap_or(1.))
138 }
139 pub fn increase_in_weight(&mut self, u: u64, v: u64, ew: Option<f64>) -> Result<()> {
140 for n in [u, v] {
141 if n >= self.upper_node_id_bound() {
142 bail!("Node out of bound: {}", n);
143 }
144 }
145 Ok(unsafe { self.increase_in_weight_unchecked(u, v, ew) })
146 }
147}