#include <gp-bnb/partition.hpp>

partition::partition(graph* g) : supergraph_(g) {
    // Assigns all vertices to none
    for (unsigned int i = 0; i < g->num_vertices(); i++) {
        vertex_assignments_[i] = none;
    }
    // Initializes vertex counting map
    vertices_[sg_a] = 0;
    vertices_[sg_b] = 0;
    vertices_[none] = g->num_vertices();
}

unsigned int partition::num_vertices(subgraph sg) {
    return vertices_[sg];
}

void partition::assign_vertex(vertex_id v, subgraph sg) {
    // Returns if v is already assigned to a subgraph
    if (vertex_assignments_[v-1] != none) {
        return;
    }
    // Increments current objectives
    for (auto const& target : supergraph_->get_adjacency(v)) {
        if (vertex_assignments_[target-1] == -sg) {
            current_objective_++;    
        }
    }
    // Assigns vertex to subgraph
    vertex_assignments_[v-1] = sg;
    vertices_[sg]++;
    vertices_[none]--;
}

void partition::unassign_vertex(vertex_id v) {
    subgraph sg = vertex_assignments_[v-1];
    // Returns if v is not assigned to a subgraph
    if (sg == none) {
        return;
    }
    // Decrements current objectives
    for (auto const& target : supergraph_->get_adjacency(v)) {
        if (vertex_assignments_[target-1] == -sg) {
            current_objective_--;
        }
    }
    // Reverts assignment to subgraph 
    vertex_assignments_[v-1] = none;
    vertices_[sg]--;
    vertices_[none]++;
}