text:
/* -*- mode: C -*-  */
/* vim:set ts=4 sw=4 sts=4 et: */
/*
   IGraph library.
   Copyright (C) 2007-2012  Gabor Csardi <csardi.gabor@gmail.com>
   334 Harvard street, Cambridge, MA 02139 USA

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   02110-1301 USA

*/

#include "igraph_adjlist.h"
#include "igraph_community.h"
#include "igraph_dqueue.h"
#include "igraph_interface.h"
#include "igraph_interrupt_internal.h"
#include "igraph_memory.h"
#include "igraph_random.h"
#include "igraph_stack.h"
#include "igraph_constructors.h"

/* Move nodes in order to improve the quality of a partition.
 *
 * This function considers each node and greedily moves it to a neighboring
 * community that maximizes the improvement in the quality of a partition.
 *
 * The nodes are examined in a queue, and initially all nodes are put in the
 * queue in a random order. Nodes are popped from the queue when they are
 * examined, and only neighbors of nodes that are moved (which are not part of
 * the cluster the node was moved to) are pushed to the queue again.
 *
 * The \c membership vector is used as the starting point to move around nodes,
 * and is updated in-place.
 *
 */
static int igraph_i_community_leiden_fastmovenodes(
        const igraph_t *graph,
        const igraph_inclist_t *edges_per_node,
        const igraph_vector_t *edge_weights, const igraph_vector_t *node_weights,
        const igraph_real_t resolution_parameter,
        igraph_integer_t *nb_clusters,
        igraph_vector_t *membership) {

    igraph_dqueue_t unstable_nodes;
    igraph_real_t max_diff = 0.0, diff = 0.0;
    igraph_integer_t n = igraph_vcount(graph);
    igraph_vector_bool_t neighbor_cluster_added, node_is_stable;
    igraph_vector_t node_order, cluster_weights, edge_weights_per_cluster, neighbor_clusters;
    igraph_vector_int_t nb_nodes_per_cluster;
    igraph_stack_t empty_clusters;
    long int i, j, c, nb_neigh_clusters;

    /* Initialize queue of unstable nodes and whether node is stable. Only
     * unstable nodes are in the queue. */
    IGRAPH_CHECK(igraph_vector_bool_init(&node_is_stable, n));
    IGRAPH_FINALLY(igraph_vector_bool_destroy, &node_is_stable);

    IGRAPH_CHECK(igraph_dqueue_init(&unstable_nodes, n));
    IGRAPH_FINALLY(igraph_dqueue_destroy, &unstable_nodes);

    /* Shuffle nodes */
    IGRAPH_CHECK(igraph_vector_init_seq(&node_order, 0, n - 1));
    IGRAPH_FINALLY(igraph_vector_destroy, &node_order);
    IGRAPH_CHECK(igraph_vector_shuffle(&node_order));

    /* Add to the queue */
    for (i = 0; i < n; i++) {
        igraph_dqueue_push(&unstable_nodes, (long int)VECTOR(node_order)[i]);
    }

    /* Initialize cluster weights and nb nodes */
    IGRAPH_CHECK(igraph_vector_init(&cluster_weights, n));
    IGRAPH_FINALLY(igraph_vector_destroy, &cluster_weights);
    IGRAPH_CHECK(igraph_vector_int_init(&nb_nodes_per_cluster, n));
    IGRAPH_FINALLY(igraph_vector_int_destroy, &nb_nodes_per_cluster);
    for (i = 0; i < n; i++) {
        c = (long int)VECTOR(*membership)[i];
        VECTOR(cluster_weights)[c] += VECTOR(*node_weights)[i];
        VECTOR(nb_nodes_per_cluster)[c] += 1;
    }

    /* Initialize empty clusters */
    IGRAPH_CHECK(igraph_stack_init(&empty_clusters, n));
    IGRAPH_FINALLY(igraph_stack_destroy, &empty_clusters);
    for (c = 0; c < n; c++)
        if (VECTOR(nb_nodes_per_cluster)[c] == 0) {
            igraph_stack_push(&empty_clusters, c);
        }

    /* Initialize vectors to be used in calculating differences */
    IGRAPH_CHECK(igraph_vector_init(&edge_weights_per_cluster, n));
    IGRAPH_FINALLY(igraph_vector_destroy, &edge_weights_per_cluster);

    /* Initialize neighboring cluster */
    IGRAPH_CHECK(igraph_vector_bool_init(&neighbor_cluster_added, n));
    IGRAPH_FINALLY(igraph_vector_bool_destroy, &neighbor_cluster_added);
    IGRAPH_CHECK(igraph_vector_init(&neighbor_clusters, n));
    IGRAPH_FINALLY(igraph_vector_destroy, &neighbor_clusters);

    /* Iterate while the queue is not empty */
    j = 0;
    while (!igraph_dqueue_empty(&unstable_nodes)) {
        long int v = (long int)igraph_dqueue_pop(&unstable_nodes);
        long int best_cluster, current_cluster = VECTOR(*membership)[v];
        long int degree, i;
        igraph_vector_int_t *edges;

        /* Remove node from current cluster */
        VECTOR(cluster_weights)[current_cluster] -= VECTOR(*node_weights)[v];
        VECTOR(nb_nodes_per_cluster)[current_cluster]--;
        if (VECTOR(nb_nodes_per_cluster)[current_cluster] == 0) {
            IGRAPH_CHECK(igraph_stack_push(&empty_clusters, current_cluster));
        }

        /* Find out neighboring clusters */
        c = (long int)igraph_stack_top(&empty_clusters);
        VECTOR(neighbor_clusters)[0] = c;
        VECTOR(neighbor_cluster_added)[c] = 1;
        nb_neigh_clusters = 1;

        /* Determine the edge weight to each neighboring cluster */
        edges = igraph_inclist_get(edges_per_node, v);
        degree = igraph_vector_int_size(edges);
        for (i = 0; i < degree; i++) {
            long int e = VECTOR(*edges)[i];
            long int u = (long int)IGRAPH_OTHER(graph, e, v);
            if (u != v) {
                c = VECTOR(*membership)[u];
                if (!VECTOR(neighbor_cluster_added)[c]) {
                    VECTOR(neighbor_cluster_added)[c] = 1;
                    VECTOR(neighbor_clusters)[nb_neigh_clusters++] = c;
                }
                VECTOR(edge_weights_per_cluster)[c] += VECTOR(*edge_weights)[e];
            }
        }

        /* Calculate maximum diff */
        best_cluster = current_cluster;
        max_diff = VECTOR(edge_weights_per_cluster)[current_cluster] - VECTOR(*node_weights)[v] * VECTOR(cluster_weights)[current_cluster] * resolution_parameter;
        for (i = 0; i < nb_neigh_clusters; i++) {
            c = VECTOR(neighbor_clusters)[i];
            diff = VECTOR(edge_weights_per_cluster)[c] - VECTOR(*node_weights)[v] * VECTOR(cluster_weights)[c] * resolution_parameter;
            if (diff > max_diff) {
                best_cluster = c;
                max_diff = diff;
            }
            VECTOR(edge_weights_per_cluster)[c] = 0.0;
            VECTOR(neighbor_cluster_added)[c] = 0;
        }

        /* Move node to best cluster */
        VECTOR(cluster_weights)[best_cluster] += VECTOR(*node_weights)[v];
        VECTOR(nb_nodes_per_cluster)[best_cluster]++;
        if (best_cluster == igraph_stack_top(&empty_clusters)) {
            igraph_stack_pop(&empty_clusters);
        }

        /* Mark node as stable */
        VECTOR(node_is_stable)[v] = 1;

        /* Add stable neighbours that are not part of the new cluster to the queue */
        if (best_cluster != current_cluster) {
            VECTOR(*membership)[v] = best_cluster;

            for (i = 0; i < degree; i++) {
                long int e = VECTOR(*edges)[i];
                long int u = (long int)IGRAPH_OTHER(graph, e, v);
                if (VECTOR(node_is_stable)[u] && VECTOR(*membership)[u] != best_cluster) {
                    IGRAPH_CHECK(igraph_dqueue_push(&unstable_nodes, u));
                    VECTOR(node_is_stable)[u] = 0;
                }
            }
        }

        j++;
        if (j > 10000) {
            IGRAPH_ALLOW_INTERRUPTION();
            j = 0;
        }
    }

    IGRAPH_CHECK(igraph_reindex_membership(membership, NULL, nb_clusters));

    igraph_vector_destroy(&neighbor_clusters);
    igraph_vector_bool_destroy(&neighbor_cluster_added);
    igraph_vector_destroy(&edge_weights_per_cluster);
    igraph_stack_destroy(&empty_clusters);
    igraph_vector_int_destroy(&nb_nodes_per_cluster);
    igraph_vector_destroy(&cluster_weights);
    igraph_vector_destroy(&node_order);
    igraph_dqueue_destroy(&unstable_nodes);
    igraph_vector_bool_destroy(&node_is_stable);

    IGRAPH_FINALLY_CLEAN(9);

    return IGRAPH_SUCCESS;
}

/* Clean a refined membership vector.
 *
 * This function examines all nodes in \c node_subset and updates \c
 * refined_membership to ensure that the clusters are numbered consecutively,
 * starting from \c nb_refined_clusters. The \c nb_refined_clusters is also
 * updated itself. If C is the initial \c nb_refined_clusters and C' the
 * resulting \c nb_refined_clusters, then nodes in \c node_subset are numbered
 * C, C + 1, ..., C' - 1.
 */
static int igraph_i_community_leiden_clean_refined_membership(
        const igraph_vector_t* node_subset,
        igraph_vector_t *refined_membership,
        igraph_integer_t* nb_refined_clusters) {
    long int i, n = igraph_vector_size(node_subset);
    igraph_vector_t new_cluster;

    IGRAPH_CHECK(igraph_vector_init(&new_cluster, n));
    IGRAPH_FINALLY(igraph_vector_destroy, &new_cluster);

    /* Clean clusters. We will store the new cluster + 1 so that cluster == 0
     * indicates that no membership was assigned yet. */
    *nb_refined_clusters += 1;
    for (i = 0; i < n; i++) {
        long int v = (long int)VECTOR(*node_subset)[i];
        long int c = (long int)VECTOR(*refined_membership)[v];
        if (VECTOR(new_cluster)[c] == 0) {
            VECTOR(new_cluster)[c] = (igraph_real_t)(*nb_refined_clusters);
            *nb_refined_clusters += 1;
        }
    }

    /* Assign new cluster */
    for (i = 0; i < n; i++) {
        long int v = (long int)VECTOR(*node_subset)[i];
        long int c = (long int)VECTOR(*refined_membership)[v];
        VECTOR(*refined_membership)[v] = VECTOR(new_cluster)[c] - 1;
    }
    /* We used the cluster + 1, so correct */
    *nb_refined_clusters -= 1;

    igraph_vector_destroy(&new_cluster);

    IGRAPH_FINALLY_CLEAN(1);

    return IGRAPH_SUCCESS;
}

/* Merge nodes for a subset of the nodes. This is used to refine a partition.
 *
 * The nodes included in \c node_subset are assumed to be the nodes i for which
 * membership[i] = cluster_subset.
 *
 * All nodes in \c node_subset are initialized to a singleton partition in \c
 * refined_membership. Only singleton clusters can be merged if they are
 * sufficiently well connected to the current subgraph induced by \c
 * node_subset.
 *
 * We only examine each node once. Instead of greedily choosing the maximum
 * possible cluster to merge with, the cluster is chosen randomly among all
 * possibilities that do not decrease the quality of the partition. The
 * probability of choosing a certain cluster is proportional to exp(diff/beta).
 * For beta to 0 this converges to selecting a cluster with the maximum
 * improvement. For beta to infinity this converges to a uniform distribution
 * among all eligible clusters.
 *
 * The \c refined_membership is updated for node in \c node_subset. The number
 * of refined clusters, \c nb_refined_clusters is used to set the actual refined
 * cluster membership and is updated after this routine. Within each cluster
 * (i.e. for a given \c node_subset), the refined membership is initially simply
 * set to 0, ..., n - 1 (for n nodes in \c node_subset). However, for each \c
 * node_subset the refined membership should of course be unique. Hence, after
 * merging, the refined membership starts with \c nb_refined_clusters, which is
 * also updated to ensure that the resulting \c nb_refined_clusters counts all
 * refined clusters that have already been processed. See
 * igraph_i_community_leiden_clean_refined_membership for more information about
 * this aspect.
 */
static int igraph_i_community_leiden_mergenodes(
        const igraph_t *graph,
        const igraph_inclist_t *edges_per_node,
        const igraph_vector_t *edge_weights, const igraph_vector_t *node_weights,
        const igraph_vector_t *node_subset,
        const igraph_vector_t *membership,
        const igraph_integer_t cluster_subset,
        const igraph_real_t resolution_parameter,
        const igraph_real_t beta,
        igraph_integer_t *nb_refined_clusters,
        igraph_vector_t *refined_membership) {
    igraph_vector_t node_order;
    igraph_vector_bool_t non_singleton_cluster, neighbor_cluster_added;
    igraph_real_t max_diff, total_cum_trans_diff, diff = 0.0, total_node_weight = 0.0;
    igraph_integer_t n = igraph_vector_size(node_subset);
    igraph_vector_t cluster_weights, cum_trans_diff, edge_weights_per_cluster, external_edge_weight_per_cluster_in_subset, neighbor_clusters;
    igraph_vector_int_t *edges, nb_nodes_per_cluster;
    long int i, j, degree, nb_neigh_clusters;

    /* Initialize cluster weights */
    IGRAPH_CHECK(igraph_vector_init(&cluster_weights, n));
    IGRAPH_FINALLY(igraph_vector_destroy, &cluster_weights);

    /* Initialize number of nodes per cluster */
    IGRAPH_CHECK(igraph_vector_int_init(&nb_nodes_per_cluster, n));
    IGRAPH_FINALLY(igraph_vector_int_destroy, &nb_nodes_per_cluster);

    /* Initialize external edge weight per cluster in subset */
    IGRAPH_CHECK(igraph_vector_init(&external_edge_weight_per_cluster_in_subset, n));
    IGRAPH_FINALLY(igraph_vector_destroy, &external_edge_weight_per_cluster_in_subset);

    /* Initialize administration for a singleton partition */
    for (i = 0; i < n; i++) {
        long int v = (long int)VECTOR(*node_subset)[i];
        VECTOR(*refined_membership)[v] = i;
        VECTOR(cluster_weights)[i] += VECTOR(*node_weights)[v];
        VECTOR(nb_nodes_per_cluster)[i] += 1;
        total_node_weight += VECTOR(*node_weights)[v];

        /* Find out neighboring clusters */
        edges = igraph_inclist_get(edges_per_node, v);
        degree = igraph_vector_int_size(edges);
        for (j = 0; j < degree; j++) {
            long int e = VECTOR(*edges)[j];
            long int u = (long int)IGRAPH_OTHER(graph, e, v);
            if (u != v && VECTOR(*membership)[u] == cluster_subset) {
                VECTOR(external_edge_weight_per_cluster_in_subset)[i] += VECTOR(*edge_weights)[e];
            }
        }
    }

    /* Shuffle nodes */
    IGRAPH_CHECK(igraph_vector_copy(&node_order, node_subset));
    IGRAPH_FINALLY(igraph_vector_destroy, &node_order);
    IGRAPH_CHECK(igraph_vector_shuffle(&node_order));

    /* Initialize non singleton clusters */
    IGRAPH_CHECK(igraph_vector_bool_init(&non_singleton_cluster, n));
    IGRAPH_FINALLY(igraph_vector_bool_destroy, &non_singleton_cluster);

    /* Initialize vectors to be used in calculating differences */
    IGRAPH_CHECK(igraph_vector_init(&edge_weights_per_cluster, n));
    IGRAPH_FINALLY(igraph_vector_destroy, &edge_weights_per_cluster);

    /* Initialize neighboring cluster */
    IGRAPH_CHECK(igraph_vector_bool_init(&neighbor_cluster_added, n));
    IGRAPH_FINALLY(igraph_vector_bool_destroy, &neighbor_cluster_added);
    IGRAPH_CHECK(igraph_vector_init(&neighbor_clusters, n));
    IGRAPH_FINALLY(igraph_vector_destroy, &neighbor_clusters);

    /* Initialize cumulative transformed difference */
    IGRAPH_CHECK(igraph_vector_init(&cum_trans_diff, n));
    IGRAPH_FINALLY(igraph_vector_destroy, &cum_trans_diff);

    RNG_BEGIN();

    for (i = 0; i < n; i++) {
        long int v = (long int)VECTOR(node_order)[i];
        long int chosen_cluster, best_cluster, current_cluster = (long int)VECTOR(*refined_membership)[v];

        if (!VECTOR(non_singleton_cluster)[current_cluster] &&
            (VECTOR(external_edge_weight_per_cluster_in_subset)[current_cluster] >=
             VECTOR(cluster_weights)[current_cluster] * (total_node_weight - VECTOR(cluster_weights)[current_cluster]) * resolution_parameter)) {
            /* Remove node from current cluster, which is then a singleton by
             * definition. */
            VECTOR(cluster_weights)[current_cluster] = 0.0;
            VECTOR(nb_nodes_per_cluster)[current_cluster] = 0;

            /* Find out neighboring clusters */
            edges = igraph_inclist_get(edges_per_node, v);
            degree = igraph_vector_int_size(edges);

            /* Also add current cluster to ensure it can be chosen. */
            VECTOR(neighbor_clusters)[0] = current_cluster;
            VECTOR(neighbor_cluster_added)[current_cluster] = 1;
            nb_neigh_clusters = 1;
            for (j = 0; j < degree; j++) {
                long int e = (long int)VECTOR(*edges)[j];
                long int u = (long int)IGRAPH_OTHER(graph, e, v);
                if (u != v && VECTOR(*membership)[u] == cluster_subset) {
                    long int c = VECTOR(*refined_membership)[u];
                    if (!VECTOR(neighbor_cluster_added)[c]) {
                        VECTOR(neighbor_cluster_added)[c] = 1;
                        VECTOR(neighbor_clusters)[nb_neigh_clusters++] = c;
                    }
                    VECTOR(edge_weights_per_cluster)[c] += VECTOR(*edge_weights)[e];
                }
            }

            /* Calculate diffs */
            best_cluster = current_cluster;
            max_diff = 0.0;
            total_cum_trans_diff = 0.0;
            for (j = 0; j < nb_neigh_clusters; j++) {
                long int c = (long int)VECTOR(neighbor_clusters)[j];
                if (VECTOR(external_edge_weight_per_cluster_in_subset)[c] >= VECTOR(cluster_weights)[c] * (total_node_weight - VECTOR(cluster_weights)[c]) * resolution_parameter) {
                    diff = VECTOR(edge_weights_per_cluster)[c] - VECTOR(*node_weights)[v] * VECTOR(cluster_weights)[c] * resolution_parameter;

                    if (diff > max_diff) {
                        best_cluster = c;
                        max_diff = diff;
                    }

                    /* Calculate the transformed difference for sampling */
                    if (diff >= 0) {
                        total_cum_trans_diff += exp(diff / beta);
                    }

                }

                VECTOR(cum_trans_diff)[j] = total_cum_trans_diff;
                VECTOR(edge_weights_per_cluster)[c] = 0.0;
                VECTOR(neighbor_cluster_added)[c] = 0;
            }

            /* Determine the neighboring cluster to which the currently selected node
             * will be moved.
             */
            if (total_cum_trans_diff < IGRAPH_INFINITY) {
                igraph_real_t r = igraph_rng_get_unif(igraph_rng_default(), 0, total_cum_trans_diff);
                long int chosen_idx;
                igraph_vector_binsearch_slice(&cum_trans_diff, r, &chosen_idx, 0, nb_neigh_clusters);
                chosen_cluster = VECTOR(neighbor_clusters)[chosen_idx];
            } else {
                chosen_cluster = best_cluster;
            }

            /* Move node to randomly chosen cluster */
            VECTOR(cluster_weights)[chosen_cluster] += VECTOR(*node_weights)[v];
            VECTOR(nb_nodes_per_cluster)[chosen_cluster]++;

            for (j = 0; j < degree; j++) {
                long int e = (long int)VECTOR(*edges)[j];
                long int u = (long int)IGRAPH_OTHER(graph, e, v);
                if (VECTOR(*membership)[u] == cluster_subset) {
                    if (VECTOR(*refined_membership)[u] == chosen_cluster) {
                        VECTOR(external_edge_weight_per_cluster_in_subset)[chosen_cluster] -= VECTOR(*edge_weights)[e];
                    } else {
                        VECTOR(external_edge_weight_per_cluster_in_subset)[chosen_cluster] += VECTOR(*edge_weights)[e];
                    }
                }
            }

            /* Set cluster  */
            if (chosen_cluster != current_cluster) {
                VECTOR(*refined_membership)[v] = chosen_cluster;

                VECTOR(non_singleton_cluster)[chosen_cluster] = 1;
            }
        } /* end if singleton and may be merged */
    }

    RNG_END();

    IGRAPH_CHECK(igraph_i_community_leiden_clean_refined_membership(node_subset, refined_membership, nb_refined_clusters));

    igraph_vector_destroy(&cum_trans_diff);
    igraph_vector_destroy(&neighbor_clusters);
    igraph_vector_bool_destroy(&neighbor_cluster_added);
    igraph_vector_destroy(&edge_weights_per_cluster);
    igraph_vector_bool_destroy(&non_singleton_cluster);
    igraph_vector_destroy(&node_order);
    igraph_vector_destroy(&external_edge_weight_per_cluster_in_subset);
    igraph_vector_int_destroy(&nb_nodes_per_cluster);
    igraph_vector_destroy(&cluster_weights);

    IGRAPH_FINALLY_CLEAN(9);

    return IGRAPH_SUCCESS;
}

/* Create clusters out of a membership vector.
 *
 * The cluster pointer vector should be initialized for all entries of the
 * membership vector, no range checking is performed. If a vector for a cluster
 * does not yet exist it will be created and initialized. If a vector for a
 * cluster already does exist it will not be emptied on first use. Hence, it
 * should be ensured that all clusters are always properly empty (or
 * non-existing) before calling this function.
 */
static int igraph_i_community_get_clusters(const igraph_vector_t *membership, igraph_vector_ptr_t *clusters) {
    long int i, c, n = igraph_vector_size(membership);
    igraph_vector_t *cluster;
    for (i = 0; i < n; i++) {
        /* Get cluster for node i */
        c = VECTOR(*membership)[i];
        cluster = (igraph_vector_t*)VECTOR(*clusters)[c];

        /* No cluster vector exists yet, so we create a new one */
        if (!cluster) {
            cluster = igraph_Calloc(1, igraph_vector_t);
            if (cluster == 0) {
                IGRAPH_ERROR("Cannot allocate memory for assigning cluster", IGRAPH_ENOMEM);
            }
            IGRAPH_CHECK(igraph_vector_init(cluster, 0));
            VECTOR(*clusters)[c] = cluster;
        }

        /* Add node i to cluster vector */
        IGRAPH_CHECK(igraph_vector_push_back(cluster, i));
    }

    return IGRAPH_SUCCESS;
}

/* Aggregate the graph based on the \c refined membership while setting the
 * membership of each aggregated node according to the \c membership.
 *
 * Technically speaking we have that
 * aggregated_membership[refined_membership[v]] = membership[v] for each node v.
 *
 * The new aggregated graph is returned in \c aggregated_graph. This graph
 * object should not yet be initialized, `igraph_create` is called on it, and
 * responsibility for destroying the object lies with the calling method
 *
 * The remaining results, aggregated_edge_weights, aggregate_node_weights and
 * aggregated_membership are all expected to be initialized.
 *
 */
static int igraph_i_community_leiden_aggregate(
    const igraph_t *graph, const igraph_inclist_t *edges_per_node, const igraph_vector_t *edge_weights, const igraph_vector_t *node_weights,
    const igraph_vector_t *membership, const igraph_vector_t *refined_membership, const igraph_integer_t nb_refined_clusters,
    igraph_t *aggregated_graph, igraph_vector_t *aggregated_edge_weights, igraph_vector_t *aggregated_node_weights, igraph_vector_t *aggregated_membership) {
    igraph_vector_t aggregated_edges, edge_weight_to_cluster;
    igraph_vector_ptr_t refined_clusters;
    igraph_vector_int_t *incident_edges;
    igraph_vector_t neighbor_clusters;
    igraph_vector_bool_t neighbor_cluster_added;
    long int i, j, c, degree, nb_neigh_clusters;

    /* Get refined clusters */
    IGRAPH_CHECK(igraph_vector_ptr_init(&refined_clusters, nb_refined_clusters));
    IGRAPH_VECTOR_PTR_SET_ITEM_DESTRUCTOR(&refined_clusters, igraph_vector_destroy);
    IGRAPH_FINALLY(igraph_vector_ptr_destroy_all, &refined_clusters);
    IGRAPH_CHECK(igraph_i_community_get_clusters(refined_membership, &refined_clusters));

    /* Initialize new edges */
    IGRAPH_CHECK(igraph_vector_init(&aggregated_edges, 0));
    IGRAPH_FINALLY(igraph_vector_destroy, &aggregated_edges);

    /* We clear the aggregated edge weights, we will push each new edge weight */
    igraph_vector_clear(aggregated_edge_weights);
    /* Simply resize the aggregated node weights and membership, they can be set
     * directly */
    IGRAPH_CHECK(igraph_vector_resize(aggregated_node_weights, nb_refined_clusters));
    IGRAPH_CHECK(igraph_vector_resize(aggregated_membership, nb_refined_clusters));

    IGRAPH_CHECK(igraph_vector_init(&edge_weight_to_cluster, nb_refined_clusters));
    IGRAPH_FINALLY(igraph_vector_destroy, &edge_weight_to_cluster);

    /* Initialize neighboring cluster */
    IGRAPH_CHECK(igraph_vector_bool_init(&neighbor_cluster_added, nb_refined_clusters));
    IGRAPH_FINALLY(igraph_vector_bool_destroy, &neighbor_cluster_added);
    IGRAPH_CHECK(igraph_vector_init(&neighbor_clusters, nb_refined_clusters));
    IGRAPH_FINALLY(igraph_vector_destroy, &neighbor_clusters);

    /* Check per cluster */
    for (c = 0; c < nb_refined_clusters; c++) {
        igraph_vector_t* refined_cluster = (igraph_vector_t*)VECTOR(refined_clusters)[c];
        long int n_c = igraph_vector_size(refined_cluster);
        long int v = -1;

        /* Calculate the total edge weight to other clusters */
        VECTOR(*aggregated_node_weights)[c] = 0.0;
        nb_neigh_clusters = 0;
        for (i = 0; i < n_c; i++) {
            v = (long int)VECTOR(*refined_cluster)[i];
            incident_edges = igraph_inclist_get(edges_per_node, v);
            degree = igraph_vector_int_size(incident_edges);

            for (j = 0; j < degree; j++) {
                long int e = VECTOR(*incident_edges)[j];
                long int u = (long int)IGRAPH_OTHER(graph, e, v);
                long int c2 = VECTOR(*refined_membership)[u];

                if (c2 > c) {
                    if (!VECTOR(neighbor_cluster_added)[c2]) {
                        VECTOR(neighbor_cluster_added)[c2] = 1;
                        VECTOR(neighbor_clusters)[nb_neigh_clusters++] = c2;
                    }
                    VECTOR(edge_weight_to_cluster)[c2] += VECTOR(*edge_weights)[e];
                }
            }

            VECTOR(*aggregated_node_weights)[c] += VECTOR(*node_weights)[v];
        }

        /* Add actual edges from this cluster to the other clusters */
        for (i = 0; i < nb_neigh_clusters; i++) {
            long int c2 = VECTOR(neighbor_clusters)[i];

            /* Add edge */
            IGRAPH_CHECK(igraph_vector_push_back(&aggregated_edges, c));
            IGRAPH_CHECK(igraph_vector_push_back(&aggregated_edges, c2));

            /* Add edge weight */
            IGRAPH_CHECK(igraph_vector_push_back(aggregated_edge_weights, VECTOR(edge_weight_to_cluster)[c2]));

            VECTOR(edge_weight_to_cluster)[c2] = 0.0;
            VECTOR(neighbor_cluster_added)[c2] = 0;
        }

        VECTOR(*aggregated_membership)[c] = VECTOR(*membership)[v];

    }

    igraph_vector_destroy(&neighbor_clusters);
    igraph_vector_bool_destroy(&neighbor_cluster_added);
    igraph_vector_destroy(&edge_weight_to_cluster);
    igraph_vector_ptr_destroy_all(&refined_clusters);

    IGRAPH_FINALLY_CLEAN(4);

    igraph_destroy(aggregated_graph);
    IGRAPH_CHECK(igraph_create(aggregated_graph, &aggregated_edges, nb_refined_clusters,
                               IGRAPH_UNDIRECTED));

    igraph_vector_destroy(&aggregated_edges);

    IGRAPH_FINALLY_CLEAN(1);

    return IGRAPH_SUCCESS;
}

/* Calculate the quality of the partition.
 *
 * The quality is defined as
 *
 * 1 / 2m sum_ij (A_ij - gamma n_i n_j)d(s_i, s_j)
 *
 * where m is the total edge weight, A_ij is the weight of edge (i, j), gamma is
 * the so-called resolution parameter, n_i is the node weight of node i, s_i is
 * the cluster of node i and d(x, y) = 1 if and only if x = y and 0 otherwise.
 *
 * Note that by setting n_i = k_i the degree of node i and dividing gamma by 2m,
 * we effectively optimize modularity. By setting n_i = 1 we optimize the
 * Constant Potts Model.
 *
 * This can be represented as a sum over clusters as
 *
 * 1 / 2m sum_c (e_c - gamma N_c^2)
 *
 * where e_c = sum_ij A_ij d(s_i, c)d(s_j, c) is (twice) the internal edge
 * weight in cluster c and N_c = sum_i n_i d(s_i, c) is the sum of the node
 * weights inside cluster c. This is how the quality is calculated in practice.
 *
 */
static int igraph_i_community_leiden_quality(
        const igraph_t *graph, const igraph_vector_t *edge_weights, const igraph_vector_t *node_weights,
        const igraph_vector_t *membership, const igraph_integer_t nb_comms, const igraph_real_t resolution_parameter,
        igraph_real_t *quality) {
    igraph_vector_t cluster_weights;
    igraph_real_t total_edge_weight = 0.0;
    igraph_eit_t eit;
    long int i, c, n = igraph_vcount(graph);;

    *quality = 0.0;

    /* Create the edgelist */
    IGRAPH_CHECK(igraph_eit_create(graph, igraph_ess_all(IGRAPH_EDGEORDER_ID), &eit));
    IGRAPH_FINALLY(igraph_eit_destroy, &eit);
    i = 0;
    while (!IGRAPH_EIT_END(eit)) {
        igraph_integer_t e = IGRAPH_EIT_GET(eit), from, to;
        IGRAPH_CHECK(igraph_edge(graph, e, &from, &to));
        total_edge_weight += VECTOR(*edge_weights)[e];
        /* We add the internal edge weights */
        if (VECTOR(*membership)[(long int) from] == VECTOR(*membership)[(long int) to]) {
            *quality += 2 * VECTOR(*edge_weights)[e];
        }
        IGRAPH_EIT_NEXT(eit);
    }
    igraph_eit_destroy(&eit);
    IGRAPH_FINALLY_CLEAN(1);

    /* Initialize cluster weights and nb nodes */
    IGRAPH_CHECK(igraph_vector_init(&cluster_weights, n));
    IGRAPH_FINALLY(igraph_vector_destroy, &cluster_weights);
    for (i = 0; i < n; i++) {
        c = VECTOR(*membership)[i];
        VECTOR(cluster_weights)[c] += VECTOR(*node_weights)[i];
    }

    /* We subtract gamma * N_c^2 */
    for (c = 0; c < nb_comms; c++) {
        *quality -= resolution_parameter * VECTOR(cluster_weights)[c] * VECTOR(cluster_weights)[c];
    }

    igraph_vector_destroy(&cluster_weights);
    IGRAPH_FINALLY_CLEAN(1);

    /* We normalise by 2m */
    *quality /= (2.0 * total_edge_weight);

    return IGRAPH_SUCCESS;
}

/* This is the core of the Leiden algorithm and relies on subroutines to
 * perform the three different phases: (1) local moving of nodes, (2)
 * refinement of the partition and (3) aggregation of the network based on the
 * refined partition, using the non-refined partition to create an initial
 * partition for the aggregate network.
 */
int igraph_i_community_leiden(const igraph_t *graph,
                              igraph_vector_t *edge_weights, igraph_vector_t *node_weights,
                              const igraph_real_t resolution_parameter, const igraph_real_t beta,
                              igraph_vector_t *membership, igraph_integer_t *nb_clusters, igraph_real_t *quality) {
    igraph_integer_t nb_refined_clusters;
    long int i, c, n = igraph_vcount(graph);
    igraph_t aggregated_graph, *i_graph;
    igraph_vector_t aggregated_edge_weights, aggregated_node_weights, aggregated_membership;
    igraph_vector_t *i_edge_weights, *i_node_weights, *i_membership;
    igraph_vector_t tmp_edge_weights, tmp_node_weights, tmp_membership;
    igraph_vector_t refined_membership;
    igraph_vector_int_t aggregate_node;
    igraph_vector_ptr_t clusters;
    igraph_inclist_t edges_per_node;
    igraph_bool_t continue_clustering;
    igraph_integer_t level = 0;

    /* Initialize temporary weights and membership to be used in aggregation */
    IGRAPH_CHECK(igraph_vector_init(&tmp_edge_weights, 0));
    IGRAPH_FINALLY(igraph_vector_destroy, &tmp_edge_weights);
    IGRAPH_CHECK(igraph_vector_init(&tmp_node_weights, 0));
    IGRAPH_FINALLY(igraph_vector_destroy, &tmp_node_weights);
    IGRAPH_CHECK(igraph_vector_init(&tmp_membership, 0));
    IGRAPH_FINALLY(igraph_vector_destroy, &tmp_membership);

    /* Initialize clusters */
    IGRAPH_CHECK(igraph_vector_ptr_init(&clusters, n));
    IGRAPH_VECTOR_PTR_SET_ITEM_DESTRUCTOR(&clusters, igraph_vector_destroy);
    IGRAPH_FINALLY(igraph_vector_ptr_destroy_all, &clusters);

    /* Initialize aggregate nodes, which initially is identical to simply the
     * nodes in the graph. */
    IGRAPH_CHECK(igraph_vector_int_init(&aggregate_node, n));
    IGRAPH_FINALLY(igraph_vector_int_destroy, &aggregate_node);
    for (i = 0; i < n; i++) {
        VECTOR(aggregate_node)[i] = i;
    }

    /* Initialize refined membership */
    IGRAPH_CHECK(igraph_vector_init(&refined_membership, 0));
    IGRAPH_FINALLY(igraph_vector_destroy, &refined_membership);

    /* Initialize aggregated graph */
    IGRAPH_CHECK(igraph_empty(&aggregated_graph, 0, IGRAPH_UNDIRECTED));
    IGRAPH_FINALLY(igraph_destroy, &aggregated_graph);

    /* Initialize aggregated edge weights */
    IGRAPH_CHECK(igraph_vector_init(&aggregated_edge_weights, 0));
    IGRAPH_FINALLY(igraph_vector_destroy, &aggregated_edge_weights);

    /* Initialize aggregated node weights */
    IGRAPH_CHECK(igraph_vector_init(&aggregated_node_weights, 0));
    IGRAPH_FINALLY(igraph_vector_destroy, &aggregated_node_weights);

    /* Initialize aggregated membership */
    IGRAPH_CHECK(igraph_vector_init(&aggregated_membership, 0));
    IGRAPH_FINALLY(igraph_vector_destroy, &aggregated_membership);

    /* Set actual graph, weights and membership to be used. */
    i_graph = (igraph_t*)graph;
    i_edge_weights = edge_weights;
    i_node_weights = node_weights;
    i_membership = membership;

    /* Clean membership and count number of *clusters */

    IGRAPH_CHECK(igraph_reindex_membership(i_membership, NULL, nb_clusters));

    if (*nb_clusters > n) {
        IGRAPH_ERROR("Too many communities in membership vector", IGRAPH_EINVAL);
    }

    do {

        /* Get incidence list for fast iteration */
        IGRAPH_CHECK(igraph_inclist_init( i_graph, &edges_per_node, IGRAPH_ALL));
        IGRAPH_FINALLY(igraph_inclist_destroy, &edges_per_node);

        /* Move around the nodes in order to increase the quality */
        IGRAPH_CHECK(igraph_i_community_leiden_fastmovenodes(i_graph,
                     &edges_per_node,
                     i_edge_weights, i_node_weights,
                     resolution_parameter,
                     nb_clusters,
                     i_membership));

        /* We only continue clustering if not all clusters are represented by a
         * single node yet
         */
        continue_clustering = (*nb_clusters < igraph_vcount(i_graph));

        if (continue_clustering) {
            /* Set original membership */
            if (level > 0) {
                for (i = 0; i < n; i++) {
                    long int v_aggregate = VECTOR(aggregate_node)[i];
                    VECTOR(*membership)[i] = VECTOR(*i_membership)[v_aggregate];
                }
            }

            /* Get node sets for each cluster. */
            IGRAPH_CHECK(igraph_i_community_get_clusters(i_membership, &clusters));

            /* Ensure refined membership is correct size */
            IGRAPH_CHECK(igraph_vector_resize(&refined_membership, igraph_vcount(i_graph)));

            /* Refine each cluster */
            nb_refined_clusters = 0;
            for (c = 0; c < *nb_clusters; c++) {
                igraph_vector_t* cluster = (igraph_vector_t*)VECTOR(clusters)[c];
                IGRAPH_CHECK(igraph_i_community_leiden_mergenodes(i_graph,
                             &edges_per_node,
                             i_edge_weights, i_node_weights,
                             cluster, i_membership, c,
                             resolution_parameter, beta,
                             &nb_refined_clusters, &refined_membership));
                /* Empty cluster */
                igraph_vector_clear(cluster);
            }

            /* If refinement didn't aggregate anything, we aggregate on the basis of
             * the actual clustering */
            if (nb_refined_clusters >= igraph_vcount(i_graph)) {
                igraph_vector_update(&refined_membership, i_membership);
                nb_refined_clusters = *nb_clusters;
            }

            /* Keep track of aggregate node. */
            for (i = 0; i < n; i++) {
                /* Current aggregate node */
                igraph_integer_t v_aggregate = VECTOR(aggregate_node)[i];
                /* New aggregate node */
                VECTOR(aggregate_node)[i] = (igraph_integer_t)VECTOR(refined_membership)[v_aggregate];
            }

            IGRAPH_CHECK(igraph_i_community_leiden_aggregate(
                             i_graph, &edges_per_node, i_edge_weights, i_node_weights,
                             i_membership, &refined_membership, nb_refined_clusters,
                             &aggregated_graph, &tmp_edge_weights, &tmp_node_weights, &tmp_membership));

            /* On the lowest level, the actual graph and node and edge weights and
             * membership are used. On higher levels, we will use the aggregated graph
             * and associated vectors.
             */
            if (level == 0) {
                /* Set actual graph, weights and membership to be used. */
                i_graph = &aggregated_graph;
                i_edge_weights = &aggregated_edge_weights;
                i_node_weights = &aggregated_node_weights;
                i_membership = &aggregated_membership;
            }

            /* Update the aggregated administration. */
            IGRAPH_CHECK(igraph_vector_update(i_edge_weights, &tmp_edge_weights));
            IGRAPH_CHECK(igraph_vector_update(i_node_weights, &tmp_node_weights));
            IGRAPH_CHECK(igraph_vector_update(i_membership, &tmp_membership));

            level += 1;
        }

        /* We are done iterating, so we destroy the incidence list */
        igraph_inclist_destroy(&edges_per_node);
        IGRAPH_FINALLY_CLEAN(1);
    } while (continue_clustering);

    /* Free aggregated graph and associated vectors */
    igraph_vector_destroy(&aggregated_membership);
    igraph_vector_destroy(&aggregated_node_weights);
    igraph_vector_destroy(&aggregated_edge_weights);
    igraph_destroy(&aggregated_graph);
    IGRAPH_FINALLY_CLEAN(4);

    /* Free remaining memory */
    igraph_vector_destroy(&refined_membership);
    igraph_vector_int_destroy(&aggregate_node);
    igraph_vector_ptr_destroy_all(&clusters);
    igraph_vector_destroy(&tmp_membership);
    igraph_vector_destroy(&tmp_node_weights);
    igraph_vector_destroy(&tmp_edge_weights);
    IGRAPH_FINALLY_CLEAN(6);

    /* Calculate quality */
    if (quality) {
        IGRAPH_CHECK(igraph_i_community_leiden_quality(graph, edge_weights, node_weights, membership, *nb_clusters, resolution_parameter, quality));
    }

    return IGRAPH_SUCCESS;
}

/**
 * \ingroup communities
 * \function igraph_community_leiden
 * \brief Finding community structure using the Leiden algorithm.
 *
 * This function implements the Leiden algorithm for finding community
 * structure, see Traag, V. A., Waltman, L., & van Eck, N. J. (2019). From
 * Louvain to Leiden: guaranteeing well-connected communities. Scientific
 * reports, 9(1), 5233. http://dx.doi.org/10.1038/s41598-019-41695-z
 *
 * </para><para>
 * It is similar to the multilevel algorithm, often called the Louvain
 * algorithm, but it is faster and yields higher quality solutions. It can
 * optimize both modularity and the Constant Potts Model, which does not suffer
 * from the resolution-limit (see preprint http://arxiv.org/abs/1104.3083).
 *
 * </para><para>
 * The Leiden algorithm consists of three phases: (1) local moving of nodes,
 * (2) refinement of the partition and (3) aggregation of the network based on
 * the refined partition, using the non-refined partition to create an initial
 * partition for the aggregate network. In the local move procedure in the
 * Leiden algorithm, only nodes whose neighborhood has changed are visited. The
 * refinement is done by restarting from a singleton partition within each
 * cluster and gradually merging the subclusters. When aggregating, a single
 * cluster may then be represented by several nodes (which are the subclusters
 * identified in the refinement).
 *
 * </para><para>
 * The Leiden algorithm provides several guarantees. The Leiden algorithm is
 * typically iterated: the output of one iteration is used as the input for the
 * next iteration. At each iteration all clusters are guaranteed to be
 * connected and well-separated. After an iteration in which nothing has
 * changed, all nodes and some parts are guaranteed to be locally optimally
 * assigned. Finally, asymptotically, all subsets of all clusters are
 * guaranteed to be locally optimally assigned. For more details, please see
 * Traag, Waltman & van Eck (2019).
 *
 * </para><para>
 * The objective function being optimized is
 *
 * </para><para>
 * 1 / 2m sum_ij (A_ij - gamma n_i n_j)d(s_i, s_j)
 *
 * </para><para>
 * where m is the total edge weight, A_ij is the weight of edge (i, j), gamma is
 * the so-called resolution parameter, n_i is the node weight of node i, s_i is
 * the cluster of node i and d(x, y) = 1 if and only if x = y and 0 otherwise.
 * By setting n_i = k_i, the degree of node i, and dividing gamma by 2m, you
 * effectively obtain an expression for modularity. Hence, the standard
 * modularity will be optimized when you supply the degrees as \c node_weights
 * and by supplying as a resolution parameter 1.0/(2*m), with m the number of
 * edges.
 *
 * \param graph The input graph. It must be an undirected graph.
 * \param edge_weights Numeric vector containing edge weights. If \c NULL, every edge
 *    has equal weight of 1. The weights need not be non-negative.
 * \param node_weights Numeric vector containing node weights.
 * \param resolution_parameter The resolution parameter used, which is
 *    represented by gamma in the objective function mentioned in the
 *    documentation.
 * \param beta The randomness used in the refinement step when merging. A small
 *    amount of randomness (\c beta = 0.01) typically works well.
 * \param start Start from membership vector. If this is true, the optimization
 *    will start from the provided membership vector. If this is false, the
 *    optimization will start from a singleton partition.
 * \param membership The membership vector. This is both used as the initial
 *    membership from which optimisation starts and is updated in place. It
 *    must hence be properly initialized. When finding clusters from scratch it
 *    is typically started using a singleton clustering. This can be achieved
 *    using \c igraph_vector_init_seq.
 * \param nb_clusters The number of clusters contained in \c membership. Must
 *    not be a \c NULL pointer.
 * \param quality The quality of the partition, in terms of the objective
 *    function as included in the documentation. If \c NULL the quality will
 *    not be calculated.
 * \return Error code.
 *
 * Time complexity: near linear on sparse graphs.
 *
 * \example examples/simple/igraph_community_leiden.c
 */
int igraph_community_leiden(const igraph_t *graph,
                            const igraph_vector_t *edge_weights, const igraph_vector_t *node_weights,
                            const igraph_real_t resolution_parameter, const igraph_real_t beta, const igraph_bool_t start,
                            igraph_vector_t *membership, igraph_integer_t *nb_clusters, igraph_real_t *quality) {
    igraph_vector_t *i_edge_weights, *i_node_weights;
    igraph_integer_t n = igraph_vcount(graph);

    if (start) {
        if (!membership) {
            IGRAPH_ERROR("Cannot start optimization if membership is missing", IGRAPH_EINVAL);
        }

        if (igraph_vector_size(membership) != n) {
            IGRAPH_ERROR("Initial membership length does not equal the number of vertices", IGRAPH_EINVAL);
        }
    } else {
        int i;
        if (!membership)
            IGRAPH_ERROR("Membership vector should be supplied and initialized, "
                         "even when not starting optimization from it", IGRAPH_EINVAL);

        igraph_vector_resize(membership, n);
        for (i = 0; i < n; i++) {
            VECTOR(*membership)[i] = i;
        }
    }

    if (igraph_is_directed(graph)) {
        IGRAPH_ERROR("Leiden algorithm is only implemented for undirected graphs", IGRAPH_EINVAL);
    }

    /* Check edge weights to possibly use default */
    if (!edge_weights) {
        i_edge_weights = igraph_Calloc(1, igraph_vector_t);
        if (i_edge_weights == 0) {
            IGRAPH_ERROR("Leiden algorithm failed, could not allocate memory for edge weights", IGRAPH_ENOMEM);
        }
        IGRAPH_FINALLY(igraph_free, i_edge_weights);
        IGRAPH_CHECK(igraph_vector_init(i_edge_weights, igraph_ecount(graph)));
        IGRAPH_FINALLY(igraph_vector_destroy, i_edge_weights);
        igraph_vector_fill(i_edge_weights, 1);
    } else {
        i_edge_weights = (igraph_vector_t*)edge_weights;
    }

    /* Check edge weights to possibly use default */
    if (!node_weights) {
        i_node_weights = igraph_Calloc(1, igraph_vector_t);
        if (i_node_weights == 0) {
            IGRAPH_ERROR("Leiden algorithm failed, could not allocate memory for node weights", IGRAPH_ENOMEM);
        }
        IGRAPH_FINALLY(igraph_free, i_node_weights);
        IGRAPH_CHECK(igraph_vector_init(i_node_weights, n));
        IGRAPH_FINALLY(igraph_vector_destroy, i_node_weights);
        igraph_vector_fill(i_node_weights, 1);
    } else {
        i_node_weights = (igraph_vector_t*)node_weights;
    }

    /* Perform actual Leiden algorithm */
    IGRAPH_CHECK(igraph_i_community_leiden(graph, i_edge_weights, i_node_weights,
                                           resolution_parameter, beta,
                                           membership, nb_clusters, quality));

    if (!edge_weights) {
        igraph_vector_destroy(i_edge_weights);
        igraph_Free(i_edge_weights);
        IGRAPH_FINALLY_CLEAN(2);
    }

    if (!node_weights) {
        igraph_vector_destroy(i_node_weights);
        igraph_Free(i_node_weights);
        IGRAPH_FINALLY_CLEAN(2);
    }

    return IGRAPH_SUCCESS;
}


text:
package com.temple.geo.service;

import java.io.Serializable;

import com.temple.service.ServiceException;
import com.temple.view.LocaleBundle;

/**
 *
 * @author flominou
 */
public class GeoServiceException extends ServiceException {

	private static final long serialVersionUID = 1L;

	protected GeoServiceException(Throwable cause) {
		super(cause);
	}

	protected GeoServiceException(Serializable[] parameters) {
		super(parameters);
	}

	protected GeoServiceException(Serializable[] parameters, Throwable cause) {
		super(parameters, cause);
	}

	protected GeoServiceException(LocaleBundle bundle, Throwable cause) {
		super(bundle, cause);
	}

	protected GeoServiceException(LocaleBundle bundle, Serializable[] parameters) {
		super(bundle, parameters);
	}

	protected GeoServiceException(LocaleBundle bundle, Serializable[] parameters, Throwable cause) {
		super(bundle, parameters, cause);
	}

}

redpajama_set_name: RedPajamaGithub


text:
"""``ovshelpers.py``

`Helpers functions for OVS test suites`

"""

import time

from . import loggers

# create logger for module
mod_logger = loggers.module_logger(name=__name__)

def set_ovs_config_with_controller(ports, switch, controller):
    """Set OVS configuration on Switch.

    Args:
        ports(dict):  Links information
        switch(SwitchGeneral):  switch
        controller(OvsControllerGeneralMixin):  Controller

    Returns:
        None

    """
    switch.ui.create_ovs_bridge(bridge_name="spp0")
    controller_ip = "tcp:%s:%s" % (controller.ipaddr, controller.cport, )
    switch.ui.create_ovs_bridge_controller("spp0", controller_ip)
    for port in ports:
        if port[0] != "tg1":
            for link_id in ports[port]:
                try:
                    switch.ui.get_table_ovs_ports()[ports[port][link_id]]
                except Exception:
                    switch.ui.create_ovs_port(int(ports[port][link_id]), bridge_name="spp0")
    # Wait until configuration is applied and connection to Controller is established
    time.sleep(6)

def set_ovs_complex_config_with_controller(ports, switch, sw_id, controller):
    """Set OVS configuration on Switch.

    Args:
        ports(dict):  Links information
        switch(SwitchGeneral):  switch
        sw_id(int):  id of switch in complex setup on which OVS is configured
        controller(OvsControllerGeneralMixin):  Controller

    Returns:
        None

    """
    switch.ui.create_ovs_bridge(bridge_name="spp0")
    controller_ip = "tcp:%s:%s" % (controller.ipaddr, str(controller.cport), )
    switch.ui.create_ovs_bridge_controller("spp0", controller_ip)
    for port in ports:
        if port[0] != "tg1" and (port[0] == "sw%d" % (sw_id, )):
            for link_id in ports[port]:
                try:
                    switch.ui.get_table_ovs_ports()[ports[port][link_id]]
                except Exception:
                    switch.ui.create_ovs_port(int(ports[port][link_id]), bridge_name="spp0")
    # Wait until configuration is applied and connection to Controller is established
    time.sleep(6)

def add_flow_via_controller(qualifiers, action, controller, name=None, dpid=None):
    """Add flow to OVS bridge via Controller.

    Args:
        qualifiers(str):  flow qualifiers
        action(str):  flow actions
        controller(OvsControllerGeneralMixin):  Controller
        name(str):  flow name
        dpid(bool):  Get process ID

    Returns:
        None

    """
    flow_command = "flow_add"
    flow = "%s %s" % (qualifiers, action, )
    if not name:
        name = 'flow1'
    if controller.name == 'nox':
        controller.setprop(flow_command, [flow_command, flow])
    elif controller.name == 'floodlight':
        if not dpid:
            dpid = controller.getprop('get_dpid', '')
            mod_logger.info('Switch dpid: %s' % (dpid, ))
        controller.setprop(flow_command, [dpid, flow, name])
    elif controller.name == 'oftest':
        controller.setprop(flow_command, [flow])

def delete_flow_via_controller(qualifiers, controller, name=None):
    """Delete flow from OVS bridge via Controller.

    Args:
        qualifiers(str):  flow qualifiers
        controller(OvsControllerGeneralMixin):  Controller
        name(str):  flow name

    Returns:
        None

    """
    flow_command = "flow_delete"
    flow = qualifiers
    if not name:
        name = 'flow1'
    if controller.name == 'nox':
        controller.setprop(flow_command, [flow_command, flow])
    elif controller.name == 'floodlight':
        controller.setprop(flow_command, ['flow_delete', name])
    elif controller.name == 'oftest':
        controller.setprop(flow_command, [flow])

def create_packet_definition(packet_to_send):
    """Create packet definition to send.

    Args:
        packet_to_send(dict):  dictionary with specified packet type and fields with values

    Returns:
        tuple(dict): packet_definition

    """
    source_mac = "00:00:00:00:00:01"
    destination_mac = "00:00:00:00:00:02"
    source_ip = "10.10.10.1"
    destination_ip = "10.10.10.2"
    source_ip6 = 'fe80::214:f2ff:fe07:af0'
    destination_ip6 = 'ff02::1'
    sport = 1
    dport = 2
    tos = 4
    if packet_to_send["type"] == "ip":
        packet_definition = ({"Ether": {"dst": destination_mac, "src": source_mac, "type": 0x0800}},
                             {"IP": {"dst": destination_ip, "src": source_ip, "tos": tos}},
                             {"TCP": {}})
    elif packet_to_send["type"] == "tagged_ip":
        packet_definition = ({"Ether": {"dst": destination_mac, "src": source_mac, "type": 0x8100}},
                             {"Dot1Q": {"vlan": packet_to_send["vlan"],
                                        "prio": packet_to_send["priority"]}},
                             {"IP": {"dst": destination_ip, "src": source_ip, "tos": tos}})
    elif packet_to_send["type"] == "tcp":
        packet_definition = ({"Ether": {"dst": destination_mac, "src": source_mac, "type": 0x0800}},
                             {"IP": {"dst": destination_ip, "src": source_ip, "tos": tos}},
                             {"TCP": {"sport": sport, "dport": dport}})
    elif packet_to_send["type"] == "udp":
        packet_definition = ({"Ether": {"dst": destination_mac, "src": source_mac, "type": 0x0800}},
                             {"IP": {"dst": destination_ip, "src": source_ip, "tos": tos}},
                             {"UDP": {"sport": sport, "dport": dport}})
    elif packet_to_send["type"] == "double_tagged_ip":
        packet_definition = ({"Ether": {"dst": destination_mac, "src": source_mac, "type": 0x8100}},
                             {"Dot1Q": {"vlan": packet_to_send["outer_vlan"], "type": 0x8100,
                                        "prio": packet_to_send["outer_priority"]}},
                             {"Dot1Q": {"vlan": packet_to_send["inner_vlan"], "type": 0x0800,
                                        "prio": packet_to_send["inner_priority"]}},
                             {"IP": {"dst": destination_ip, "src": source_ip, "tos": tos}})
    elif packet_to_send["type"] == "arp":
        packet_definition = (
            {"Ether": {"src": source_mac, "dst": 'FF:FF:FF:FF:FF:FF', "type": 0x0806}},
            {"ARP": {"op": 1, "hwsrc": source_mac,
                     "psrc": source_ip, "pdst": destination_ip}},)
    elif packet_to_send["type"] == "arp_reply_tagged":
        packet_definition = ({"Ether": {"src": source_mac, "dst": destination_mac, "type": 0x8100}},
                             {"Dot1Q": {"vlan": 2}},
                             {"ARP": {"op": 2, "hwsrc": source_mac, "hwdst": destination_mac,
                                      "pdst": destination_ip, "psrc": source_ip}}, )
    elif packet_to_send["type"] == "icmp":
        packet_definition = ({"Ether": {"dst": destination_mac, "src": source_mac, "type": 0x0800}},
                             {"IP": {"dst": destination_ip, "src": source_ip, "proto": 1}},
                             {"ICMP": {"type": 8, "code": 0}})
    elif packet_to_send["type"] == "ipv6":
        packet_definition = ({"Ether": {"dst": destination_mac, "src": source_mac, "type": 0x86dd}},
                             {"IPv6": {"dst": destination_ip6, "src": source_ip6, "version": 6,
                                       "hlim": 255, "plen": 64, "tc": 225}})
    elif packet_to_send["type"] == "tcp6":
        packet_definition = ({"Ether": {"dst": destination_mac, "src": source_mac, "type": 0x86dd}},
                             {"IPv6": {"dst": destination_ip6, "src": source_ip6, "version": 6,
                                       "hlim": 255, "tc": 224, "nh": 6}},
                             {"TCP": {"sport": sport, "dport": dport}})
    elif packet_to_send["type"] == "udp6":
        packet_definition = ({"Ether": {"dst": destination_mac, "src": source_mac, "type": 0x86dd}},
                             {"IPv6": {"dst": destination_ip6, "src": source_ip6, "version": 6,
                                       "hlim": 255, "tc": 224, "nh": 17}},
                             {"UDP": {"sport": sport, "dport": dport}})
    elif packet_to_send["type"] == "icmp6":
        packet_definition = ({"Ether": {"dst": destination_mac, "src": source_mac, "type": 0x86dd}},
                             {"IPv6": {"dst": destination_ip6, "src": source_ip6, "version": 6,
                                       "hlim": 255, "tc": 224, "nh": 1}},
                             {"ICMP": {"type": 8, "code": 0}})
    return packet_definition

def set_ovs_test_preconditions(ports, test_preconditions, switch, controller):
    """Set test preconditions, e.g. flows.

    Args:
        ports(dict):  ports
        test_preconditions(tuple):  test preconditions
        switch(SwitchGeneral):  switch
        controller(OvsControllerGeneralMixin):  Controller

    Returns:
        None

    """
    if isinstance(test_preconditions, tuple):
        for i, v in enumerate(test_preconditions):
            name = "flow%d" % (i + 1, )
            # configure actions string (if output port is in actions)
            if "output" in test_preconditions[i][1]:
                action = test_preconditions[i][1].split(",")
                for j, k in enumerate(action):
                    if "output" in action[j]:
                        port_id = int(action[j][action[j].find(":") + 1:])
                        action[j] = "output:%d" % (int(ports[('sw1', 'tg1')][port_id], ))
                action_string = ",".join(action)
                test_preconditions[i][1] = action_string
            if "enqueue" in test_preconditions[i][1]:
                action = test_preconditions[i][1].split(",")
                for j, l in enumerate(action):
                    if "enqueue" in action[j]:
                        port_id = int(action[j].split(":")[1])
                        action[j] = "enqueue:%d:%s" % (
                            int(ports[('sw1', 'tg1')][port_id]), action[j].split(":")[2], )
                action_string = ",".join(action)
                test_preconditions[i][1] = action_string
            if "in_port" in test_preconditions[i][0]:
                qualifiers = test_preconditions[i][0].split(",")
                for j, g in enumerate(qualifiers):
                    if "in_port" in qualifiers[j]:
                        port_id = int(qualifiers[j][qualifiers[j].find("=") + 1:])
                        qualifiers[j] = "in_port=%d" % (int(ports[('sw1', 'tg1')][port_id], ))
                qualifiers_string = ",".join(qualifiers)
                test_preconditions[i][0] = qualifiers_string
                add_flow_via_controller(test_preconditions[i][0], test_preconditions[i][1],
                                        controller, name)
            else:
                add_flow_via_controller(test_preconditions[i][0], test_preconditions[i][1],
                                        controller, name)
        # Wait until Controller sends flow to Switch"
        time.sleep(5)

def set_ovs_complex_test_preconditions(ports, test_preconditions, switch, controller, dpid=None):
    """Set test preconditions, e.g. flows.

    Args:
        ports(dict):  ports
        test_preconditions(tuple):  test preconditions
        switch(SwitchGeneral):  switch
        controller(OvsControllerGeneralMixin):  Controller
        dpid(bool):  Get process ID

    Returns:
        None

    """
    if isinstance(test_preconditions, tuple):
        for i, v in enumerate(test_preconditions):
            name = "flow%d" % (i + 1, )
            # configure actions string (if output port is in actions)
            if "output" in test_preconditions[i][1]:
                action = test_preconditions[i][1].split(",")
                for j, k in enumerate(action):
                    if "output" in action[j]:
                        action[j] = "output:%d" % (int(
                            ports[(action[j].split(":")[1], action[j].split(":")[2])][
                                int(action[j].split(":")[3])]), )
                action_string = ",".join(action)
                test_preconditions[i][1] = action_string
            if "enqueue" in test_preconditions[i][1]:
                action = test_preconditions[i][1].split(",")
                for j, l in enumerate(action):
                    if "enqueue" in action[j]:
                        action[j] = "enqueue:%d:%s" % \
                                    (int(ports[(action[j].split(":")[1], action[j].split(":")[2])][
                                        int(action[j].split(":")[3])]),
                                     action[j].split(":")[4])
                action_string = ",".join(action)
                test_preconditions[i][1] = action_string
            if "in_port" in test_preconditions[i][0]:
                qualifiers = test_preconditions[i][0].split(",")
                for j, g in enumerate(qualifiers):
                    if "in_port" in qualifiers[j]:
                        port_string = qualifiers[j].split("=")[1]
                        qualifiers[j] = "in_port=%d" % (int(
                            ports[(port_string.split(":")[0], port_string.split(":")[1])][
                                int(port_string.split(":")[2])]), )
                qualifiers_string = ",".join(qualifiers)
                test_preconditions[i][0] = qualifiers_string
                add_flow_via_controller(test_preconditions[i][0], test_preconditions[i][1],
                                        controller, name=name, dpid=dpid)
            else:
                add_flow_via_controller(test_preconditions[i][0], test_preconditions[i][1],
                                        controller, name=name, dpid=dpid)
        # Wait until Controller sends flow to Switch"
        time.sleep(5)


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace GodaddyWrapper.Responses
{
    public class CloudApplicationResponse
    {
        public string Name { get; set; }
        public string CreatedAt { get; set; }
        public int MinimumRamMb { get; set; }
        public List<CloudApplicationLinkResponse> Links { get; set; }
        public string Status { get; set; }
        public List<CloudApplicationInputFieldResponse> Inputs { get; set; }
        public string ApplicationId { get; set; }
        public string Description { get; set; }
        public string ModifiedAt { get; set; }
        public string Version { get; set; }
        public string ChangelogUrl { get; set; }
        public List<string> Categories { get; set; }
        public List<string> Ports { get; set; }
        public List<string> ImageIds { get; set; }

    }
}


text:
package main

import (
	"fmt"
	"github.com/cloudfoundry/gosigar"
	"os"
)

const output_format = "%-15s %4s %4s %5s %4s %-15s\n"

func formatSize(size uint64) string {
	return sigar.FormatSize(size * 1024)
}

func main() {
	fslist := sigar.FileSystemList{}
	fslist.Get()

	fmt.Fprintf(os.Stdout, output_format,
		"Filesystem", "Size", "Used", "Avail", "Use%", "Mounted on")

	for _, fs := range fslist.List {
		dir_name := fs.DirName

		usage := sigar.FileSystemUsage{}

		usage.Get(dir_name)

		fmt.Fprintf(os.Stdout, output_format,
			fs.DevName,
			formatSize(usage.Total),
			formatSize(usage.Used),
			formatSize(usage.Avail),
			sigar.FormatPercent(usage.UsePercent()),
			dir_name)
	}
}


text:
This is the repo for the first chatbot **TalkToMeBot**.

##Repos for other bot/chatbot team projects:##

The repo for **Botivator** a motivational bot can be found here: https://github.com/TheBot-Lab/Botivatora motivational bot

The repo for **DocBert** the file manager bot, can be found here: https://github.com/TheBot-Lab/Chingu-BotLab-docBert

The purpose of this project is the learn more about chat bots, build something fun, and explore the variety of places we can use them. Chingu cross-cohort collaboration is welcomed! Or, if you know of someone outside of the cohorts that may be interested or has experience with chat bots, linguistics or AI, feel free to tell them about this project.

Kornil from the Red-Pandas designed the fun robot icons for the project.
Check out his repo here: https://github.com/Kornil/Red-Panda-FCC-Favicon

For more info about the cohorts: https://tropicalchancer.github.io/projectus/


text:
from copy import deepcopy
from itertools import count
from math import sqrt

import numpy as np
from scipy import linalg

from .tree import dir_tree_find
from .tag import find_tag
from .constants import FIFF
from .pick import pick_types
from .write import (write_int, write_float, write_string, write_name_list,
                    write_float_matrix, end_block, start_block)
from ..utils import logger, verbose, warn, deprecated
from ..externals.six import string_types

class Projection(dict):
    """Projection vector

    A basic class to proj a meaningful print for projection vectors.
    """
    def __repr__(self):
        s = "%s" % self['desc']
        s += ", active : %s" % self['active']
        s += ", n_channels : %s" % self['data']['ncol']
        return "<Projection  |  %s>" % s

class ProjMixin(object):
    """Mixin class for Raw, Evoked, Epochs

    Notes
    -----
    This mixin adds a proj attribute as a property to data containers.
    It is True if at least one proj is present and all of them are active.
    The projs might not be applied yet if data are not preloaded. In
    this case it's the _projector attribute that does the job.
    If a private _data attribute is present then the projs applied
    to it are the ones marked as active.

    A proj parameter passed in constructor of raw or epochs calls
    apply_proj and hence after the .proj attribute is True.

    As soon as you've applied the projs it will stay active in the
    remaining pipeline.

    The suggested pipeline is proj=True in epochs (it's cheaper than for raw).

    When you use delayed SSP in Epochs, projs are applied when you call
    get_data() method. They are not applied to the evoked._data unless you call
    apply_proj(). The reason is that you want to reject with projs although
    it's not stored in proj mode.
    """
    @property
    def proj(self):
        return (len(self.info['projs']) > 0 and
                all(p['active'] for p in self.info['projs']))

    @verbose
    def add_proj(self, projs, remove_existing=False, verbose=None):
        """Add SSP projection vectors

        Parameters
        ----------
        projs : list
            List with projection vectors.
        remove_existing : bool
            Remove the projection vectors currently in the file.
        verbose : bool, str, int, or None
            If not None, override default verbose level (see mne.verbose).

        Returns
        -------
        self : instance of Raw | Epochs | Evoked
            The data container.
        """
        if isinstance(projs, Projection):
            projs = [projs]

        if (not isinstance(projs, list) and
                not all(isinstance(p, Projection) for p in projs)):
            raise ValueError('Only projs can be added. You supplied '
                             'something else.')

        # mark proj as inactive, as they have not been applied
        projs = deactivate_proj(projs, copy=True, verbose=self.verbose)
        if remove_existing:
            # we cannot remove the proj if they are active
            if any(p['active'] for p in self.info['projs']):
                raise ValueError('Cannot remove projectors that have '
                                 'already been applied')
            self.info['projs'] = projs
        else:
            self.info['projs'].extend(projs)
        # We don't want to add projectors that are activated again.
        self.info['projs'] = _uniquify_projs(self.info['projs'],
                                             check_active=False, sort=False)
        return self

    @deprecated('This function is deprecated and will be removed in 0.14. '
                'Use set_eeg_reference() instead.')
    def add_eeg_average_proj(self):
        """Add an average EEG reference projector if one does not exist."""
        if _needs_eeg_average_ref_proj(self.info):
            # Don't set as active, since we haven't applied it
            eeg_proj = make_eeg_average_ref_proj(self.info, activate=False)
            self.add_proj(eeg_proj)
        elif self.info.get('custom_ref_applied', False):
            raise RuntimeError('Cannot add an average EEG reference '
                               'projection since a custom reference has been '
                               'applied to the data earlier.')

        return self

    def apply_proj(self):
        """Apply the signal space projection (SSP) operators to the data.

        Notes
        -----
        Once the projectors have been applied, they can no longer be
        removed. It is usually not recommended to apply the projectors at
        too early stages, as they are applied automatically later on
        (e.g. when computing inverse solutions).
        Hint: using the copy method individual projection vectors
        can be tested without affecting the original data.
        With evoked data, consider the following example::

            projs_a = mne.read_proj('proj_a.fif')
            projs_b = mne.read_proj('proj_b.fif')
            # add the first, copy, apply and see ...
            evoked.add_proj(a).copy().apply_proj().plot()
            # add the second, copy, apply and see ...
            evoked.add_proj(b).copy().apply_proj().plot()
            # drop the first and see again
            evoked.copy().del_proj(0).apply_proj().plot()
            evoked.apply_proj()  # finally keep both

        Returns
        -------
        self : instance of Raw | Epochs | Evoked
            The instance.
        """
        from ..epochs import _BaseEpochs
        from .base import _BaseRaw
        if self.info['projs'] is None or len(self.info['projs']) == 0:
            logger.info('No projector specified for this dataset. '
                        'Please consider the method self.add_proj.')
            return self

        # Exit delayed mode if you apply proj
        if isinstance(self, _BaseEpochs) and self._do_delayed_proj:
            logger.info('Leaving delayed SSP mode.')
            self._do_delayed_proj = False

        if all(p['active'] for p in self.info['projs']):
            logger.info('Projections have already been applied. '
                        'Setting proj attribute to True.')
            return self

        _projector, info = setup_proj(deepcopy(self.info), activate=True,
                                      verbose=self.verbose)
        # let's not raise a RuntimeError here, otherwise interactive plotting
        if _projector is None:  # won't be fun.
            logger.info('The projections don\'t apply to these data.'
                        ' Doing nothing.')
            return self
        self._projector, self.info = _projector, info
        if isinstance(self, _BaseRaw):
            if self.preload:
                self._data = np.dot(self._projector, self._data)
        elif isinstance(self, _BaseEpochs):
            if self.preload:
                for ii, e in enumerate(self._data):
                    self._data[ii] = self._project_epoch(e)
            else:
                self.load_data()  # will automatically apply
        else:  # Evoked
            self.data = np.dot(self._projector, self.data)
        logger.info('SSP projectors applied...')
        return self

    def del_proj(self, idx):
        """Remove SSP projection vector

        Note: The projection vector can only be removed if it is inactive
              (has not been applied to the data).

        Parameters
        ----------
        idx : int
            Index of the projector to remove.

        Returns
        -------
        self : instance of Raw | Epochs | Evoked
        """
        if self.info['projs'][idx]['active']:
            raise ValueError('Cannot remove projectors that have already '
                             'been applied')

        self.info['projs'].pop(idx)

        return self

    def plot_projs_topomap(self, ch_type=None, layout=None, axes=None):
        """Plot SSP vector

        Parameters
        ----------
        ch_type : 'mag' | 'grad' | 'planar1' | 'planar2' | 'eeg' | None | List
            The channel type to plot. For 'grad', the gradiometers are collec-
            ted in pairs and the RMS for each pair is plotted. If None
            (default), it will return all channel types present. If a list of
            ch_types is provided, it will return multiple figures.
        layout : None | Layout | List of Layouts
            Layout instance specifying sensor positions (does not need to
            be specified for Neuromag data). If possible, the correct
            layout file is inferred from the data; if no appropriate layout
            file was found, the layout is automatically generated from the
            sensor locations. Or a list of Layout if projections
            are from different sensor types.
        axes : instance of Axes | list | None
            The axes to plot to. If list, the list must be a list of Axes of
            the same length as the number of projectors. If instance of Axes,
            there must be only one projector. Defaults to None.

        Returns
        -------
        fig : instance of matplotlib figure
            Figure distributing one image per channel across sensor topography.
        """
        if self.info['projs'] is not None or len(self.info['projs']) != 0:
            from ..viz.topomap import plot_projs_topomap
            from ..channels.layout import find_layout
            if layout is None:
                layout = []
                if ch_type is None:
                    ch_type = [ch for ch in ['meg', 'eeg'] if ch in self]
                elif isinstance(ch_type, string_types):
                    ch_type = [ch_type]
                for ch in ch_type:
                    if ch in self:
                        layout.append(find_layout(self.info, ch, exclude=[]))
                    else:
                        warn('Channel type %s is not found in info.' % ch)
            fig = plot_projs_topomap(self.info['projs'], layout, axes=axes)
        else:
            raise ValueError("Info is missing projs. Nothing to plot.")

        return fig

def _proj_equal(a, b, check_active=True):
    """ Test if two projectors are equal """

    equal = ((a['active'] == b['active'] or not check_active) and
             a['kind'] == b['kind'] and
             a['desc'] == b['desc'] and
             a['data']['col_names'] == b['data']['col_names'] and
             a['data']['row_names'] == b['data']['row_names'] and
             a['data']['ncol'] == b['data']['ncol'] and
             a['data']['nrow'] == b['data']['nrow'] and
             np.all(a['data']['data'] == b['data']['data']))
    return equal

@verbose
def _read_proj(fid, node, verbose=None):
    """Read spatial projections from a FIF file.

    Parameters
    ----------
    fid : file
        The file descriptor of the open file.
    node : tree node
        The node of the tree where to look.
    verbose : bool, str, int, or None
        If not None, override default verbose level (see mne.verbose).

    Returns
    -------
    projs: dict
        The list of projections.
    """
    projs = list()

    #   Locate the projection data
    nodes = dir_tree_find(node, FIFF.FIFFB_PROJ)
    if len(nodes) == 0:
        return projs

    tag = find_tag(fid, nodes[0], FIFF.FIFF_NCHAN)
    if tag is not None:
        global_nchan = int(tag.data)

    items = dir_tree_find(nodes[0], FIFF.FIFFB_PROJ_ITEM)
    for item in items:
        #   Find all desired tags in one item
        tag = find_tag(fid, item, FIFF.FIFF_NCHAN)
        if tag is not None:
            nchan = int(tag.data)
        else:
            nchan = global_nchan

        tag = find_tag(fid, item, FIFF.FIFF_DESCRIPTION)
        if tag is not None:
            desc = tag.data
        else:
            tag = find_tag(fid, item, FIFF.FIFF_NAME)
            if tag is not None:
                desc = tag.data
            else:
                raise ValueError('Projection item description missing')

        # XXX : is this useful ?
        # tag = find_tag(fid, item, FIFF.FIFF_PROJ_ITEM_CH_NAME_LIST)
        # if tag is not None:
        #     namelist = tag.data
        # else:
        #     raise ValueError('Projection item channel list missing')

        tag = find_tag(fid, item, FIFF.FIFF_PROJ_ITEM_KIND)
        if tag is not None:
            kind = int(tag.data)
        else:
            raise ValueError('Projection item kind missing')

        tag = find_tag(fid, item, FIFF.FIFF_PROJ_ITEM_NVEC)
        if tag is not None:
            nvec = int(tag.data)
        else:
            raise ValueError('Number of projection vectors not specified')

        tag = find_tag(fid, item, FIFF.FIFF_PROJ_ITEM_CH_NAME_LIST)
        if tag is not None:
            names = tag.data.split(':')
        else:
            raise ValueError('Projection item channel list missing')

        tag = find_tag(fid, item, FIFF.FIFF_PROJ_ITEM_VECTORS)
        if tag is not None:
            data = tag.data
        else:
            raise ValueError('Projection item data missing')

        tag = find_tag(fid, item, FIFF.FIFF_MNE_PROJ_ITEM_ACTIVE)
        if tag is not None:
            active = bool(tag.data)
        else:
            active = False

        tag = find_tag(fid, item, FIFF.FIFF_MNE_ICA_PCA_EXPLAINED_VAR)
        if tag is not None:
            explained_var = tag.data
        else:
            explained_var = None

        # handle the case when data is transposed for some reason
        if data.shape[0] == len(names) and data.shape[1] == nvec:
            data = data.T

        if data.shape[1] != len(names):
            raise ValueError('Number of channel names does not match the '
                             'size of data matrix')

        #   Use exactly the same fields in data as in a named matrix
        one = Projection(kind=kind, active=active, desc=desc,
                         data=dict(nrow=nvec, ncol=nchan, row_names=None,
                                   col_names=names, data=data),
                         explained_var=explained_var)

        projs.append(one)

    if len(projs) > 0:
        logger.info('    Read a total of %d projection items:' % len(projs))
        for k in range(len(projs)):
            if projs[k]['active']:
                misc = 'active'
            else:
                misc = ' idle'
            logger.info('        %s (%d x %d) %s'
                        % (projs[k]['desc'], projs[k]['data']['nrow'],
                           projs[k]['data']['ncol'], misc))

    return projs

###############################################################################
# Write

def _write_proj(fid, projs):
    """Write a projection operator to a file.

    Parameters
    ----------
    fid : file
        The file descriptor of the open file.
    projs : dict
        The projection operator.
    """
    if len(projs) == 0:
        return
    start_block(fid, FIFF.FIFFB_PROJ)

    for proj in projs:
        start_block(fid, FIFF.FIFFB_PROJ_ITEM)
        write_int(fid, FIFF.FIFF_NCHAN, proj['data']['ncol'])
        write_name_list(fid, FIFF.FIFF_PROJ_ITEM_CH_NAME_LIST,
                        proj['data']['col_names'])
        write_string(fid, FIFF.FIFF_NAME, proj['desc'])
        write_int(fid, FIFF.FIFF_PROJ_ITEM_KIND, proj['kind'])
        if proj['kind'] == FIFF.FIFFV_PROJ_ITEM_FIELD:
            write_float(fid, FIFF.FIFF_PROJ_ITEM_TIME, 0.0)

        write_int(fid, FIFF.FIFF_PROJ_ITEM_NVEC, proj['data']['nrow'])
        write_int(fid, FIFF.FIFF_MNE_PROJ_ITEM_ACTIVE, proj['active'])
        write_float_matrix(fid, FIFF.FIFF_PROJ_ITEM_VECTORS,
                           proj['data']['data'])
        if proj['explained_var'] is not None:
            write_float(fid, FIFF.FIFF_MNE_ICA_PCA_EXPLAINED_VAR,
                        proj['explained_var'])
        end_block(fid, FIFF.FIFFB_PROJ_ITEM)

    end_block(fid, FIFF.FIFFB_PROJ)

###############################################################################
# Utils
def make_projector(projs, ch_names, bads=(), include_active=True):
    """Create an SSP operator from SSP projection vectors

    Parameters
    ----------
    projs : list
        List of projection vectors.
    ch_names : list of str
        List of channels to include in the projection matrix.
    bads : list of str
        Some bad channels to exclude. If bad channels were marked
        in the raw file when projs were calculated using mne-python,
        they should not need to be included here as they will
        have been automatically omitted from the projectors.
    include_active : bool
        Also include projectors that are already active.

    Returns
    -------
    proj : array of shape [n_channels, n_channels]
        The projection operator to apply to the data.
    nproj : int
        How many items in the projector.
    U : array
        The orthogonal basis of the projection vectors (optional).
    """
    return _make_projector(projs, ch_names, bads, include_active)

def _make_projector(projs, ch_names, bads=(), include_active=True,
                    inplace=False):
    """Helper to subselect projs based on ch_names and bads

    Use inplace=True mode to modify ``projs`` inplace so that no
    warning will be raised next time projectors are constructed with
    the given inputs. If inplace=True, no meaningful data are returned.
    """
    nchan = len(ch_names)
    if nchan == 0:
        raise ValueError('No channel names specified')

    default_return = (np.eye(nchan, nchan), 0, [])

    #   Check trivial cases first
    if projs is None:
        return default_return

    nvec = 0
    nproj = 0
    for p in projs:
        if not p['active'] or include_active:
            nproj += 1
            nvec += p['data']['nrow']

    if nproj == 0:
        return default_return

    #   Pick the appropriate entries
    vecs = np.zeros((nchan, nvec))
    nvec = 0
    nonzero = 0
    for k, p in enumerate(projs):
        if not p['active'] or include_active:
            if (len(p['data']['col_names']) !=
                    len(np.unique(p['data']['col_names']))):
                raise ValueError('Channel name list in projection item %d'
                                 ' contains duplicate items' % k)

            # Get the two selection vectors to pick correct elements from
            # the projection vectors omitting bad channels
            sel = []
            vecsel = []
            for c, name in enumerate(ch_names):
                if name in p['data']['col_names'] and name not in bads:
                    sel.append(c)
                    vecsel.append(p['data']['col_names'].index(name))

            # If there is something to pick, pickit
            nrow = p['data']['nrow']
            this_vecs = vecs[:, nvec:nvec + nrow]
            if len(sel) > 0:
                this_vecs[sel] = p['data']['data'][:, vecsel].T

            # Rescale for better detection of small singular values
            for v in range(p['data']['nrow']):
                psize = sqrt(np.sum(this_vecs[:, v] * this_vecs[:, v]))
                if psize > 0:
                    orig_n = p['data']['data'].shape[1]
                    if len(vecsel) < 0.9 * orig_n and not inplace:
                        warn('Projection vector "%s" has magnitude %0.2f '
                             '(should be unity), applying projector with '
                             '%s/%s of the original channels available may '
                             'be dangerous, consider recomputing and adding '
                             'projection vectors for channels that are '
                             'eventually used. If this is intentional, '
                             'consider using info.normalize_proj()'
                             % (p['desc'], psize, len(vecsel), orig_n))
                    this_vecs[:, v] /= psize
                    nonzero += 1
            # If doing "inplace" mode, "fix" the projectors to only operate
            # on this subset of channels.
            if inplace:
                p['data']['data'] = this_vecs[sel].T
                p['data']['col_names'] = [p['data']['col_names'][ii]
                                          for ii in vecsel]
            nvec += p['data']['nrow']

    #   Check whether all of the vectors are exactly zero
    if nonzero == 0 or inplace:
        return default_return

    # Reorthogonalize the vectors
    U, S, V = linalg.svd(vecs[:, :nvec], full_matrices=False)

    # Throw away the linearly dependent guys
    nproj = np.sum((S / S[0]) > 1e-2)
    U = U[:, :nproj]

    # Here is the celebrated result
    proj = np.eye(nchan, nchan) - np.dot(U, U.T)

    return proj, nproj, U

def _normalize_proj(info):
    """Helper to normalize proj after subselection to avoid warnings

    This is really only useful for tests, and might not be needed
    eventually if we change or improve our handling of projectors
    with picks.
    """
    # Here we do info.get b/c info can actually be a noise cov
    _make_projector(info['projs'], info.get('ch_names', info.get('names')),
                    info['bads'], include_active=True, inplace=True)

def make_projector_info(info, include_active=True):
    """Make an SSP operator using the measurement info

    Calls make_projector on good channels.

    Parameters
    ----------
    info : dict
        Measurement info.
    include_active : bool
        Also include projectors that are already active.

    Returns
    -------
    proj : array of shape [n_channels, n_channels]
        The projection operator to apply to the data.
    nproj : int
        How many items in the projector.
    """
    proj, nproj, _ = make_projector(info['projs'], info['ch_names'],
                                    info['bads'], include_active)
    return proj, nproj

@verbose
def activate_proj(projs, copy=True, verbose=None):
    """Set all projections to active

    Useful before passing them to make_projector.

    Parameters
    ----------
    projs : list
        The projectors.
    copy : bool
        Modify projs in place or operate on a copy.
    verbose : bool, str, int, or None
        If not None, override default verbose level (see mne.verbose).

    Returns
    -------
    projs : list
        The projectors.
    """
    if copy:
        projs = deepcopy(projs)

    #   Activate the projection items
    for proj in projs:
        proj['active'] = True

    logger.info('%d projection items activated' % len(projs))

    return projs

@verbose
def deactivate_proj(projs, copy=True, verbose=None):
    """Set all projections to inactive

    Useful before saving raw data without projectors applied.

    Parameters
    ----------
    projs : list
        The projectors.
    copy : bool
        Modify projs in place or operate on a copy.
    verbose : bool, str, int, or None
        If not None, override default verbose level (see mne.verbose).

    Returns
    -------
    projs : list
        The projectors.
    """
    if copy:
        projs = deepcopy(projs)

    #   Deactivate the projection items
    for proj in projs:
        proj['active'] = False

    logger.info('%d projection items deactivated' % len(projs))

    return projs

@verbose
def make_eeg_average_ref_proj(info, activate=True, verbose=None):
    """Create an EEG average reference SSP projection vector

    Parameters
    ----------
    info : dict
        Measurement info.
    activate : bool
        If True projections are activated.
    verbose : bool, str, int, or None
        If not None, override default verbose level (see mne.verbose).

    Returns
    -------
    eeg_proj: instance of Projection
        The SSP/PCA projector.
    """
    if info.get('custom_ref_applied', False):
        raise RuntimeError('Cannot add an average EEG reference projection '
                           'since a custom reference has been applied to the '
                           'data earlier.')

    logger.info("Adding average EEG reference projection.")
    eeg_sel = pick_types(info, meg=False, eeg=True, ref_meg=False,
                         exclude='bads')
    ch_names = info['ch_names']
    eeg_names = [ch_names[k] for k in eeg_sel]
    n_eeg = len(eeg_sel)
    if n_eeg == 0:
        raise ValueError('Cannot create EEG average reference projector '
                         '(no EEG data found)')
    vec = np.ones((1, n_eeg))
    vec /= n_eeg
    explained_var = None
    eeg_proj_data = dict(col_names=eeg_names, row_names=None,
                         data=vec, nrow=1, ncol=n_eeg)
    eeg_proj = Projection(active=activate, data=eeg_proj_data,
                          desc='Average EEG reference',
                          kind=FIFF.FIFFV_MNE_PROJ_ITEM_EEG_AVREF,
                          explained_var=explained_var)
    return eeg_proj

def _has_eeg_average_ref_proj(projs, check_active=False):
    """Determine if a list of projectors has an average EEG ref

    Optionally, set check_active=True to additionally check if the CAR
    has already been applied.
    """
    for proj in projs:
        if (proj['desc'] == 'Average EEG reference' or
                proj['kind'] == FIFF.FIFFV_MNE_PROJ_ITEM_EEG_AVREF):
            if not check_active or proj['active']:
                return True
    return False

def _needs_eeg_average_ref_proj(info):
    """Determine if the EEG needs an averge EEG reference

    This returns True if no custom reference has been applied and no average
    reference projection is present in the list of projections.
    """
    eeg_sel = pick_types(info, meg=False, eeg=True, ref_meg=False,
                         exclude='bads')
    return (len(eeg_sel) > 0 and
            not info['custom_ref_applied'] and
            not _has_eeg_average_ref_proj(info['projs']))

@verbose
def setup_proj(info, add_eeg_ref=True, activate=True, verbose=None):
    """Set up projection for Raw and Epochs

    Parameters
    ----------
    info : dict
        The measurement info.
    add_eeg_ref : bool
        If True, an EEG average reference will be added (unless one
        already exists).
    activate : bool
        If True projections are activated.
    verbose : bool, str, int, or None
        If not None, override default verbose level (see mne.verbose).

    Returns
    -------
    projector : array of shape [n_channels, n_channels]
        The projection operator to apply to the data.
    info : dict
        The modified measurement info (Warning: info is modified inplace).
    """
    # Add EEG ref reference proj if necessary
    if add_eeg_ref and _needs_eeg_average_ref_proj(info):
        eeg_proj = make_eeg_average_ref_proj(info, activate=activate)
        info['projs'].append(eeg_proj)

    # Create the projector
    projector, nproj = make_projector_info(info)
    if nproj == 0:
        if verbose:
            logger.info('The projection vectors do not apply to these '
                        'channels')
        projector = None
    else:
        logger.info('Created an SSP operator (subspace dimension = %d)'
                    % nproj)

    # The projection items have been activated
    if activate:
        info['projs'] = activate_proj(info['projs'], copy=False)

    return projector, info

def _uniquify_projs(projs, check_active=True, sort=True):
    """Aux function"""
    final_projs = []
    for proj in projs:  # flatten
        if not any(_proj_equal(p, proj, check_active) for p in final_projs):
            final_projs.append(proj)

    my_count = count(len(final_projs))

    def sorter(x):
        """sort in a nice way"""
        digits = [s for s in x['desc'] if s.isdigit()]
        if digits:
            sort_idx = int(digits[-1])
        else:
            sort_idx = next(my_count)
        return (sort_idx, x['desc'])

    return sorted(final_projs, key=sorter) if sort else final_projs


text:
/* bdata.h */

#ifndef _BDATA_H
#define _BDATA_H

#define PROBE_VERSION 4

/* GET_VIDEO defines how much video information to retrieve
 *
 *	0 = none
 *	1 = mode info
 *	2 = VGA adapter info
 *	3 = VESA checks, too
 */
#define BD_GET_VIDEO 3

/* maximun number of floppy drives to check for 2 or 4 */
#define BD_MAX_FLOPPY 2

/* maximum number of hard drives to check for 2 to 16  */
#define BD_MAX_HARD 16

#endif	/* _BDATA_H  */


text:
namespace Azure.ResourceManager.Network.Models
{
    /// <summary> An IPSec parameters for a virtual network gateway P2S connection. </summary>
    public partial class VpnClientIPsecParameters
    {
        /// <summary> Initializes a new instance of VpnClientIPsecParameters. </summary>
        /// <param name="saLifeTimeSeconds"> The IPSec Security Association (also called Quick Mode or Phase 2 SA) lifetime in seconds for P2S client. </param>
        /// <param name="saDataSizeKilobytes"> The IPSec Security Association (also called Quick Mode or Phase 2 SA) payload size in KB for P2S client.. </param>
        /// <param name="ipsecEncryption"> The IPSec encryption algorithm (IKE phase 1). </param>
        /// <param name="ipsecIntegrity"> The IPSec integrity algorithm (IKE phase 1). </param>
        /// <param name="ikeEncryption"> The IKE encryption algorithm (IKE phase 2). </param>
        /// <param name="ikeIntegrity"> The IKE integrity algorithm (IKE phase 2). </param>
        /// <param name="dhGroup"> The DH Group used in IKE Phase 1 for initial SA. </param>
        /// <param name="pfsGroup"> The Pfs Group used in IKE Phase 2 for new child SA. </param>
        public VpnClientIPsecParameters(int saLifeTimeSeconds, int saDataSizeKilobytes, IpsecEncryption ipsecEncryption, IpsecIntegrity ipsecIntegrity, IkeEncryption ikeEncryption, IkeIntegrity ikeIntegrity, DhGroup dhGroup, PfsGroup pfsGroup)
        {
            SaLifeTimeSeconds = saLifeTimeSeconds;
            SaDataSizeKilobytes = saDataSizeKilobytes;
            IpsecEncryption = ipsecEncryption;
            IpsecIntegrity = ipsecIntegrity;
            IkeEncryption = ikeEncryption;
            IkeIntegrity = ikeIntegrity;
            DhGroup = dhGroup;
            PfsGroup = pfsGroup;
        }

        /// <summary> The IPSec Security Association (also called Quick Mode or Phase 2 SA) lifetime in seconds for P2S client. </summary>
        public int SaLifeTimeSeconds { get; set; }
        /// <summary> The IPSec Security Association (also called Quick Mode or Phase 2 SA) payload size in KB for P2S client.. </summary>
        public int SaDataSizeKilobytes { get; set; }
        /// <summary> The IPSec encryption algorithm (IKE phase 1). </summary>
        public IpsecEncryption IpsecEncryption { get; set; }
        /// <summary> The IPSec integrity algorithm (IKE phase 1). </summary>
        public IpsecIntegrity IpsecIntegrity { get; set; }
        /// <summary> The IKE encryption algorithm (IKE phase 2). </summary>
        public IkeEncryption IkeEncryption { get; set; }
        /// <summary> The IKE integrity algorithm (IKE phase 2). </summary>
        public IkeIntegrity IkeIntegrity { get; set; }
        /// <summary> The DH Group used in IKE Phase 1 for initial SA. </summary>
        public DhGroup DhGroup { get; set; }
        /// <summary> The Pfs Group used in IKE Phase 2 for new child SA. </summary>
        public PfsGroup PfsGroup { get; set; }
    }
}


text:
function main(splash)
  assert(splash:go("https://news.ycombinator.com/"))
  splash:runjs([[
    document.querySelector('table')
            .style.backgroundColor = "#fff";
  ]])
  return {png=splash:png()}
end


text:
define(["exports", "module", "react", "classnames"], function (exports, module, _react, _classnames) {
  "use strict";

  var _interopRequire = function (obj) { return obj && obj.__esModule ? obj["default"] : obj; };

  var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

  var React = _interopRequire(_react);

  var classNames = _interopRequire(_classnames);

  var Jumbotron = React.createClass({
    displayName: "Jumbotron",

    render: function render() {
      return React.createElement(
        "div",
        _extends({}, this.props, { className: classNames(this.props.className, "jumbotron") }),
        this.props.children
      );
    }
  });

  module.exports = Jumbotron;
});