text:
#include <cassert>
#include <cmath>

#include "State.h"

using namespace std;

// FIXME: shouldn't need T, not really using suffstats here
State::State(const MatrixD &data,
    const vector<string> &GLOBAL_COL_DATATYPES,
    const vector<int> &GLOBAL_COL_MULTINOMIAL_COUNTS,
    const vector<int> &global_row_indices,
    const vector<int> &global_col_indices,
    const map<int, CM_Hypers> &HYPERS_M,
    const vector<vector<int> > &column_partition,
    const std::map<int, std::set<int> > &col_ensure_dep,
    const std::map<int, std::set<int> > &col_ensure_ind,
    double COLUMN_CRP_ALPHA,
    const vector<vector<vector<int> > > &row_partition_v,
    const vector<double> &row_crp_alpha_v,
    const vector<double> &ROW_CRP_ALPHA_GRID,
    const vector<double> &COLUMN_CRP_ALPHA_GRID,
    const vector<double> &S_GRID,
    const vector<double> &MU_GRID,
    int N_GRID, int SEED, int CT_KERNEL) : rng(SEED)
{
    assert(CT_KERNEL == 1 || CT_KERNEL == 0);
    ct_kernel = CT_KERNEL;
    column_crp_score = 0;
    data_score = 0;
    column_dependencies = col_ensure_dep;
    column_independencies = col_ensure_ind;
    num_cols_effective = get_vector_num_blocks(
        global_col_indices, column_dependencies);
    global_col_datatypes = construct_lookup_map(global_col_indices,
            GLOBAL_COL_DATATYPES);
    global_col_multinomial_counts = construct_lookup_map(global_col_indices,
            GLOBAL_COL_MULTINOMIAL_COUNTS);
    // construct grids
    construct_base_hyper_grids(data, N_GRID, ROW_CRP_ALPHA_GRID,
        COLUMN_CRP_ALPHA_GRID);
    construct_column_hyper_grids(data, global_col_indices, GLOBAL_COL_DATATYPES,
        S_GRID, MU_GRID);
    // actually build the state
    hypers_m = HYPERS_M;
    column_crp_alpha = COLUMN_CRP_ALPHA;
    init_views(data,
        global_row_indices, global_col_indices,
        column_partition, row_partition_v,
        row_crp_alpha_v);
}

State::State(const MatrixD &data,
    const vector<string> &GLOBAL_COL_DATATYPES,
    const vector<int> &GLOBAL_COL_MULTINOMIAL_COUNTS,
    const vector<int> &global_row_indices,
    const vector<int> &global_col_indices,
    const string &col_initialization,
    string row_initialization,
    const vector<double> &ROW_CRP_ALPHA_GRID,
    const vector<double> &COLUMN_CRP_ALPHA_GRID,
    const vector<double> &S_GRID,
    const vector<double> &MU_GRID,
    int N_GRID, int SEED, int CT_KERNEL) : rng(SEED)
{
    assert(CT_KERNEL == 1 || CT_KERNEL == 0);
    ct_kernel = CT_KERNEL;
    column_crp_score = 0;
    data_score = 0;
    if (row_initialization == "") {
        row_initialization = col_initialization;
    }
    num_cols_effective = global_col_indices.size();
    global_col_datatypes = construct_lookup_map(global_col_indices,
            GLOBAL_COL_DATATYPES);
    global_col_multinomial_counts = construct_lookup_map(global_col_indices,
            GLOBAL_COL_MULTINOMIAL_COUNTS);
    // construct grids
    construct_base_hyper_grids(data, N_GRID, ROW_CRP_ALPHA_GRID,
        COLUMN_CRP_ALPHA_GRID);
    construct_column_hyper_grids(data, global_col_indices, GLOBAL_COL_DATATYPES,
        S_GRID, MU_GRID);
    //
    init_column_hypers(global_col_indices);
    column_crp_alpha = sample_column_crp_alpha();
    vector<vector<int> > column_partition = generate_col_partition(
            global_col_indices,
            col_initialization);
    vector<double> row_crp_alpha_v = sample_row_crp_alphas(
            column_partition.size());
    vector<vector<vector<int> > > row_partition_v = generate_row_partitions(
            global_row_indices,
            row_crp_alpha_v, row_initialization);
    init_views(data,
        global_row_indices, global_col_indices,
        column_partition, row_partition_v,
        row_crp_alpha_v);
}

State::~State()
{
    remove_all();
}

int State::get_num_cols() const
{
    return view_lookup.size();
}

int State::get_num_cols_effective() const
{
    return num_cols_effective;
}

int State::get_num_views() const
{
    return views.size();
}

vector<int> State::get_view_counts() const
{
    vector<int> view_counts;
    vector<View *>::const_iterator it;
    for (it = views.begin(); it != views.end(); ++it) {
        View &v = (**it);
        int view_num_cols = v.get_num_cols();
        view_counts.push_back(view_num_cols);
    }
    return view_counts;
}

double State::insert_row(const vector<double> &row_data,
    int matching_row_idx,
    int row_idx)
{
    bool append_row = (row_idx == -1);
    if (append_row) {
        row_idx = (int)(**views.begin()).cluster_lookup.size();
    }
    vector<View *>::const_iterator it;
    double score_delta = 0;
    for (it = views.begin(); it != views.end(); ++it) {
        View &v = **it;
        vector<int> global_col_indices = v.get_global_col_indices();
        // append lookup if needed
        if (append_row) {
            Cluster &new_cluster = v.get_new_cluster();
            vector<double> data_subset = extract_columns(row_data, global_col_indices);
            score_delta += v.insert_row(data_subset, new_cluster, matching_row_idx);
            // FIXME: row crp to score_delta?
        } else {
            vector<double> data_subset = extract_columns(row_data, global_col_indices);
            score_delta += v.insert_row(data_subset, matching_row_idx, row_idx);
        }
    }
    return score_delta;
}

double State::insert_feature(int feature_idx,
    const vector<double> &feature_data,
    View &which_view)
{
    string col_datatype = global_col_datatypes[feature_idx];
    CM_Hypers &hypers = hypers_m[feature_idx];
    double crp_logp_delta, data_logp_delta;
    double score_delta = calc_feature_view_predictive_logp(feature_data,
            col_datatype,
            which_view,
            crp_logp_delta,
            data_logp_delta,
            hypers,
            feature_idx);
    vector<int> data_global_row_indices = create_sequence(feature_data.size());
    which_view.insert_col(feature_data,
        data_global_row_indices, feature_idx,
        hypers);
    view_lookup[feature_idx] = &which_view;
    column_crp_score += crp_logp_delta;
    data_score += data_logp_delta;
    return score_delta;
}

double State::sample_insert_feature(int feature_idx,
    const vector<double> &feature_data,
    View &singleton_view)
{
    string col_datatype = global_col_datatypes[feature_idx];
    vector<double> unorm_logps = calc_feature_view_predictive_logps(feature_data,
            feature_idx);
    double rand_u = draw_rand_u();
    int draw = numerics::draw_sample_unnormalized(unorm_logps, rand_u);
    View &which_view = get_view(draw);
    double score_delta = insert_feature(feature_idx, feature_data, which_view);
    remove_if_empty(singleton_view);
    return score_delta;
}

double State::sample_insert_feature_block(
    const vector<int> &feature_idxs,
    const vector<vector<double> > &feature_datas,
    View &singleton_view)
{

    // Get the predictive logps of the block under each view.
    vector<double> unorm_predictive_logps =
        calc_feature_view_predictive_logps_block(feature_idxs, feature_datas);

    double rand_u = draw_rand_u();
    int draw = numerics::draw_sample_unnormalized(
        unorm_predictive_logps, rand_u);
    View &which_view = get_view(draw);

    // Insert features in the block and aggregate the score_delta.
    double score_delta = 0;
    for (size_t i = 0; i < feature_idxs.size(); ++i){
        score_delta += insert_feature(
            feature_idxs[i], feature_datas[i], which_view);
    }

    // Clear out the singleton view if necessary.
    remove_if_empty(singleton_view);

    return score_delta;
}

double State::remove_feature(
    int feature_idx, const vector<double> &feature_data)
{
    string col_datatype = global_col_datatypes[feature_idx];
    CM_Hypers &hypers = hypers_m[feature_idx];
    map<int, View *>::iterator it = view_lookup.find(feature_idx);
    assert(it != view_lookup.end());
    View &which_view = *(it->second);
    view_lookup.erase(it);
    double data_logp_delta = which_view.remove_col(feature_idx);
    double crp_logp_delta, other_data_logp_delta;
    double score_delta = calc_feature_view_predictive_logp(
        feature_data,
        col_datatype,
        which_view,
        crp_logp_delta,
        other_data_logp_delta,
        hypers,
        feature_idx);
    column_crp_score -= crp_logp_delta;
    data_score -= data_logp_delta;
    assert(abs(other_data_logp_delta - data_logp_delta) < 1E-6);
    return score_delta;
}

double State::remove_feature(
    int feature_idx,
    const vector<double> &feature_data,
    View *&p_singleton_view)
{
    // Retrieve current view of feature_idx.
    map<int, View *>::iterator it = view_lookup.find(feature_idx);
    assert(it != view_lookup.end());
    View &which_view = *(it->second);

    // If current view is already a singleton then reuse it.
    if (which_view.get_num_cols() == 1) {
        p_singleton_view = &which_view;
    } else {
        p_singleton_view = &get_new_view();
    }

    // Remove the feature.
    double score_delta = remove_feature(feature_idx, feature_data);
    return score_delta;
}

double State::transition_feature_gibbs(int feature_idx,
    const vector<double> &feature_data)
{
    double score_delta = 0;
    View *p_singleton_view;
    score_delta += remove_feature(feature_idx, feature_data, p_singleton_view);
    View &singleton_view = *p_singleton_view;
    score_delta += sample_insert_feature(feature_idx, feature_data, singleton_view);
    return score_delta;
}

double State::transition_feature_block_gibbs(
    const vector<int> &feature_idxs,
    const vector<vector<double> > &feature_datas)
{
    double score_delta = 0;

    // Retrieve the current view of feature_idxs[0]. They should all be in
    // the same view, since we are transitioning features which are dependent.
    map<int, View *>::iterator it = view_lookup.find(feature_idxs[0]);
    View &current_view = *(it->second);

    // If current view only contains feature_idxs, then reuse it.
    // Otherwise create a new singleton view as the proposal.
    View &singleton_view =
        (current_view.get_num_cols() == feature_idxs.size())
        ? current_view : get_new_view();

    // Decrement the effective num cols.
    decrement_num_cols_effective();
    current_view.decrement_num_cols_effective();

    // Remove the other dependent feature_idxs.
    for (size_t i = 0; i < feature_idxs.size(); ++i) {
        score_delta += remove_feature(feature_idxs[i], feature_datas[i]);
    }

    // Sample a new view for the feature block.
    score_delta += sample_insert_feature_block(
        feature_idxs, feature_datas, singleton_view);

    // Increment the effective num cols.
    increment_num_cols_effective();
    view_lookup.find(feature_idxs[0])->second->increment_num_cols_effective();

    return score_delta;
}

double propose_singleton_p = .5;

double State::get_proposal_logp(View &proposed_view)
{
    bool is_singleton = proposed_view.get_num_cols() == 0;
    double proposal_logp = 0;
    if (is_singleton) {
        // what is proability of choosing the singleton we're leaving?
        proposal_logp = log(propose_singleton_p) + proposed_view.calc_crp_marginal();
    } else {
        // what is proability of choosing the NON-singleton we're leaving?
        // WARNING: uniform sampling of existing views is baked in
        proposal_logp = log(1 - propose_singleton_p) - log(get_num_views());
    }
    return proposal_logp;
}

double State::get_proposal_log_ratio(View &from_view, View &to_view)
{
    // presumes you've already popped the freature from its view!!!
    double proposal_log_numerator = get_proposal_logp(from_view);
    double proposal_log_denominator = get_proposal_logp(to_view);
    double proposal_log_ratio = proposal_log_numerator - proposal_log_denominator;
    return proposal_log_ratio;
}

double State::mh_choose(int feature_idx,
    const vector<double> &feature_data,
    View &proposed_view)
{
    double score_delta = 0;
    View &original_view = *view_lookup[feature_idx];
    if (&original_view == &proposed_view) {
        // short circuit: no impact
        return 0;
    }
    View *p_singleton_view;
    // Decrement the effective number of columns.
    decrement_num_cols_effective();
    original_view.decrement_num_cols_effective();
    // remove feature from model; get score delta to choose current view
    double original_view_score_delta = remove_feature(feature_idx, feature_data,
            p_singleton_view);
    score_delta = original_view_score_delta;
    View &singleton_view = *p_singleton_view;
    // get score delta to choose proposed view
    double crp_log_delta_new, data_log_delta_new;
    string col_datatype = get(global_col_datatypes, feature_idx);
    CM_Hypers hypers = get(hypers_m, feature_idx);
    double proposed_view_score_delta = calc_feature_view_predictive_logp(
            feature_data,
            col_datatype, proposed_view,
            crp_log_delta_new,
            data_log_delta_new,
            hypers,
            feature_idx);
    double state_log_ratio = proposed_view_score_delta - original_view_score_delta;
    double proposal_log_ratio = get_proposal_log_ratio(original_view,
            proposed_view);
    // Metropolis jump
    double log_r = log(draw_rand_u());
    View *p_insert_into;
    if (log_r < state_log_ratio + proposal_log_ratio) {
        p_insert_into = &proposed_view;
    } else {
        p_insert_into = &original_view;
    }
    score_delta += insert_feature(feature_idx, feature_data, *p_insert_into);
    // Increment the effective number of columns.
    increment_num_cols_effective();
    p_insert_into->decrement_num_cols_effective();
    // clean up
    bool original_was_not_singleton = &original_view != &singleton_view;
    remove_if_empty(original_view);
    if (original_was_not_singleton) {
        remove_if_empty(singleton_view);
    }
    return score_delta;
}

// updated kernel with birth-death process
double State::transition_feature_mh(int feature_idx,
    const vector<double> &feature_data)
{
    double score_delta = 0;
    View *p_proposed_view;
    // do we create a new veiw or move to an existing view?
    bool propose_singleton = (draw_rand_u() < propose_singleton_p);
    if (propose_singleton) {
        p_proposed_view = &get_new_view();
    } else {
        int num_views = views.size();
        int proposed_view_index = draw_rand_i(num_views);
        p_proposed_view = &get_view(proposed_view_index);
    }
    // Metropolis jump
    score_delta = mh_choose(feature_idx, feature_data, *p_proposed_view);
    // clean up
    remove_if_empty(*p_proposed_view);
    return score_delta;
}

double State::transition_features(
    const MatrixD &data, vector<int> which_features)
{

    double score_delta = 0;

    // Determine which features to transition.
    int num_features = which_features.size();
    if (num_features == 0) {
        which_features = create_sequence(data.size2());
        random_shuffle(which_features.begin(), which_features.end(), rng);
    }

    vector<int>::const_iterator it;
    for (it = which_features.begin(); it != which_features.end(); ++it) {

        // Get the feature_idx to be transitioned.
        int feature_idx = *it;

        // Select the transition kernel.
        if (ct_kernel == 0) {
            // For Gibbs, transition feature and all its dependent features.
            vector<int> feature_idxs = get_column_dependencies(feature_idx);
            vector<vector<double> > feature_datas = extract_cols(data,
                feature_idxs);
            score_delta += transition_feature_block_gibbs(
                feature_idxs, feature_datas);
        } else if (ct_kernel == 1) {
            // For MH, transition the feature alone without dependent features.
            vector<double> feature_data = extract_col(data, feature_idx);
            score_delta += transition_feature_mh(feature_idx, feature_data);
        } else {
            printf("Invalid CT_KERNEL");
            assert(0 == 1);
        }
    }

    return score_delta;
}

View &State::get_new_view()
{
    // FIXME: this is a hack
    // it being necessary suggests perhaps I should not be passing
    // global_row_indices and instead always assume its a sequence of 0..N
    vector<int> first_view_cluster_counts = get_view(0).get_cluster_counts();
    int num_vectors = std::accumulate(first_view_cluster_counts.begin(),
            first_view_cluster_counts.end(), 0);
    vector<int> global_row_indices = create_sequence(num_vectors);
    View *p_new_view = new View(global_col_datatypes,
        global_row_indices,
        row_crp_alpha_grid, multinomial_alpha_grid, r_grid, nu_grid,
        vm_b_grid,
        s_grids, mu_grids,
        vm_a_grids, vm_kappa_grids,
        draw_rand_i());
    views.push_back(p_new_view);
    return *p_new_view;
}

View &State::get_view(int view_idx)
{
    assert(0 <= view_idx);
    assert((size_t)view_idx < views.size());
    return *views.at(view_idx);
}

void State::remove_if_empty(View &which_view)
{
    if (which_view.get_num_cols() == 0) {
        vector<View *>::iterator it;
        for (it = views.begin(); it != views.end(); ++it) {
            if (*it == &which_view) {
                views.erase(it);
                which_view.remove_all();
                delete &which_view;
                break;
            }
        }
    }
}

void State::remove_all()
{
    view_lookup.clear();
    vector<View *>::const_iterator it;
    for (it = views.begin(); it != views.end(); ++it) {
        View &view = **it;
        view.remove_all();
        delete &view;
    }
    views.resize(0);
}

double State::get_column_crp_alpha() const
{
    return column_crp_alpha;
}

double State::get_column_crp_score() const
{
    return column_crp_score;
}

double State::get_data_score() const
{
    vector<View *>::const_iterator it;
    double data_score = 0;
    for (it = views.begin(); it != views.end(); ++it) {
        double data_score_i = (*it)->get_score();
        data_score += data_score_i;
    }
    return data_score;
}

double State::get_marginal_logp() const
{
    assert(!isnan(column_crp_score));
    double ds = get_data_score();
    assert(!isnan(ds));
    return column_crp_score + ds;
}

map<string, double> State::get_row_partition_model_hypers_i(
    int view_idx) const
{
    return views.at(view_idx)->get_row_partition_model_hypers();
}

vector<int> State::get_row_partition_model_counts_i(int view_idx) const
{
    return views.at(view_idx)->get_row_partition_model_counts();
}

vector<vector<map<string, double> > > State::get_column_component_suffstats_i(
    int view_idx) const
{
    return views.at(view_idx)->get_column_component_suffstats();
}

vector<CM_Hypers> State::get_column_hypers() const
{
    vector<CM_Hypers> column_hypers;
    int num_cols = get_num_cols();
    map<int, CM_Hypers>::const_iterator it;
    for (int global_col_idx = 0; global_col_idx < num_cols; global_col_idx++) {
        it = hypers_m.find(global_col_idx);
        if (it == hypers_m.end()) {
            continue;
        }
        CM_Hypers hypers_i = it->second;
        // FIXME: actually detect
        hypers_i["fixed"] = 0.;
        column_hypers.push_back(hypers_i);
    }
    return column_hypers;
}

map<string, double> State::get_column_partition_hypers() const
{
    map<string, double> local_hypers;
    local_hypers["alpha"] = get_column_crp_alpha();
    return local_hypers;
}

vector<int> State::get_column_partition_assignments() const
{
    return define_group_ordering(view_lookup, views);
}

vector<int> State::get_column_partition_counts() const
{
    return get_view_counts();
}

std::map<int, std::set<int> > State::get_column_dependencies() const
{
    return column_dependencies;
}

std::map<int, std::set<int> > State::get_column_independencies() const
{
    return column_independencies;
}

vector<int> State::get_column_dependencies(int feature_idx) const
{
    // Prepare the result, and add feature_idx as dependent with itself.
    vector<int> result;
    result.push_back(feature_idx);
    // Add other dependencies, if they exist.
    map<int, set<int> >::const_iterator deps =
        column_dependencies.find(feature_idx);
    if (deps != column_dependencies.end()) {
        std::set<int>::const_iterator itt;
        for (itt = deps->second.begin(); itt != deps->second.end(); ++itt) {
            if (*itt != feature_idx) {
                result.push_back(*itt);
            }
        }
    }
    return result;
}

vector<int> State::get_column_independencies(int feature_idx) const
{
    // Prepare the result.
    vector<int> result;
    // Add independencies, if they exist.
    map<int, set<int> >::const_iterator deps =
        column_independencies.find(feature_idx);
    if (deps != column_dependencies.end()) {
        std::set<int>::const_iterator itt;
        for (itt = deps->second.begin(); itt != deps->second.end(); ++itt) {
            result.push_back(*itt);
        }
    }
    return result;
}

vector<vector<int> > State::get_X_D() const
{
    vector<vector<int> > X_D;
    vector<View *>::const_iterator it;
    for (it = views.begin(); it != views.end(); ++it) {
        View &v = **it;
        vector<int> canonical_clustering = v.get_canonical_clustering();
        X_D.push_back(canonical_clustering);
    }
    return X_D;
}

vector<double> State::get_draw(int row_idx, int random_seed) const
{
    RandomNumberGenerator rng(random_seed);
    vector<View *>::const_iterator it;
    vector<double> _draw;
    vector<int> global_col_indices;
    for (it = views.begin(); it != views.end(); ++it) {
        View &v = **it;
        int randi = rng.nexti(MAX_INT);
        vector<double> draw_i = v.get_draw(row_idx, randi);
        vector<int> global_col_indices_i = v.get_global_col_indices();
        _draw = append(_draw, draw_i);
        global_col_indices = append(global_col_indices, global_col_indices_i);
    }
    int num_cols = get_num_cols();
    vector<double> draw(num_cols);
    for (int idx = 0; idx < num_cols; idx++) {
        int col_idx = global_col_indices[idx];
        double value = _draw[idx];
        draw[col_idx] = value;
    }
    return draw;
}

map<int, vector<int> > State::get_column_groups() const
{
    map<View *, int> view_to_int = vector_to_map(views);
    map<View *, set<int> > view_to_set = group_by_value(view_lookup);
    map<int, vector<int> > view_idx_to_vec;
    vector<View *>::const_iterator it;
    for (it = views.begin(); it != views.end(); ++it) {
        View *p_v = *it;
        int view_idx = view_to_int[p_v];
        set<int> int_set = view_to_set[p_v];
        vector<int> int_vec(int_set.begin(), int_set.end());
        std::sort(int_vec.begin(), int_vec.end());
        view_idx_to_vec[view_idx] = int_vec;
    }
    return view_idx_to_vec;
}

double State::transition_view_i(int which_view,
    const map<int, vector<double> > &row_data_map)
{
    View &v = get_view(which_view);
    double score_delta = v.transition(row_data_map);
    data_score += score_delta;
    return score_delta;
}

// helper for cython
double State::transition_view_i(int which_view, const MatrixD &data)
{
    vector<int> global_column_indices = create_sequence(data.size2());
    View &v = get_view(which_view);
    vector<int> view_cols = get_indices_to_reorder(global_column_indices,
            v.global_to_local);
    const MatrixD data_subset = extract_columns(data, view_cols);
    map<int, vector<double> > data_subset_map = construct_data_map(data_subset);
    return v.transition(data_subset_map);
}

double State::transition_views(const MatrixD &data)
{
    vector<int> global_column_indices = create_sequence(data.size2());
    //
    double score_delta = 0;
    // ordering doesn't matter, don't need to shuffle
    for (int view_idx = 0; view_idx < get_num_views(); view_idx++) {
        View &v = get_view(view_idx);
        vector<int> view_cols = get_indices_to_reorder(global_column_indices,
                v.global_to_local);
        const MatrixD data_subset = extract_columns(data, view_cols);
        map<int, vector<double> > data_subset_map = construct_data_map(data_subset);
        score_delta += v.transition(data_subset_map);
    }
    return score_delta;
}

double State::transition_row_partition_assignments(const MatrixD &data,
    vector<int> which_rows)
{
    vector<int> global_column_indices = create_sequence(data.size2());
    double score_delta = 0;
    //
    int num_rows = which_rows.size();
    if (num_rows == 0) {
        num_rows = data.size1();
        which_rows = create_sequence(num_rows);
        random_shuffle(which_rows.begin(), which_rows.end(), rng);
    }
    vector<View *>::const_iterator svp_it;
    for (svp_it = views.begin(); svp_it != views.end(); ++svp_it) {
        // for each view
        View &v = **svp_it;
        vector<int> view_cols = get_indices_to_reorder(global_column_indices,
                v.global_to_local);
        const MatrixD data_subset = extract_columns(data, view_cols);
        map<int, vector<double> > row_data_map = construct_data_map(data_subset);
        vector<int>::const_iterator vi_it;
        for (vi_it = which_rows.begin(); vi_it != which_rows.end(); ++vi_it) {
            // for each SPECIFIED row
            int row_idx = *vi_it;
            vector<double> vd = row_data_map[row_idx];
            score_delta += v.transition_z(vd, row_idx);
        }
    }
    data_score += score_delta;
    return score_delta;
}

double State::transition_views_zs(const MatrixD &data)
{
    vector<int> global_column_indices = create_sequence(data.size2());
    //
    double score_delta = 0;
    // ordering doesn't matter, don't need to shuffle
    for (int view_idx = 0; view_idx < get_num_views(); view_idx++) {
        View &v = get_view(view_idx);
        vector<int> view_cols = get_indices_to_reorder(global_column_indices,
                v.global_to_local);
        const MatrixD data_subset = extract_columns(data, view_cols);
        map<int, vector<double> > data_subset_map = construct_data_map(data_subset);
        score_delta += v.transition_zs(data_subset_map);
    }
    data_score += score_delta;
    return score_delta;
}

double State::transition_views_row_partition_hyper()
{
    double score_delta = 0;
    for (int view_idx = 0; view_idx < get_num_views(); view_idx++) {
        View &v = get_view(view_idx);
        score_delta += v.transition_crp_alpha();
    }
    data_score += score_delta;
    return score_delta;
}

double State::transition_row_partition_hyperparameters(const vector<int> &
    which_cols)
{
    double score_delta = 0;
    vector<View *> which_views;
    int num_cols = which_cols.size();
    if (num_cols != 0) {
        vector<int>::const_iterator it;
        for (it = which_cols.begin(); it != which_cols.end(); ++it) {
            View *v_p = view_lookup[*it];
            which_views.push_back(v_p);
        }
    } else {
        which_views = views;
    }
    vector<View *>::const_iterator it;
    for (it = which_views.begin(); it != which_views.end(); ++it) {
        score_delta += (*it)->transition_crp_alpha();
    }
    data_score += score_delta;
    return score_delta;
}

double State::transition_column_hyperparameters(vector<int> which_cols)
{
    double score_delta = 0;

    // Use all columns by default.
    int num_cols = which_cols.size();
    if (num_cols == 0) {
        num_cols = view_lookup.size();
        which_cols = create_sequence(num_cols);
        random_shuffle(which_cols.begin(), which_cols.end(), rng);
    }

    // Run the transitions.
    vector<int>::const_iterator it;
    for (it = which_cols.begin(); it != which_cols.end(); ++it) {

        // Retrieve the target column.
        int target_col = *it;
        View &which_view = *(view_lookup[target_col]);

        // Get the dependent columns.
        vector<int> dependent_cols = get_column_dependencies(target_col);

        // XXX FIXME Do not transition hypers for all dependent columns.
        // There will be duplication here if which_cols contains all columns
        // and there are user-specified dependencies.
        // The current usage pattern in panelcat, the main user of block
        // transition, is only specifying one column in each block when cycling
        // through all kernels. The code below ensures that hyperparameters for
        // all dependent columns are also being transitioned.
        vector<int>::const_iterator itt;
        for (itt = dependent_cols.begin(); itt != dependent_cols.end(); ++itt){
            int col_idx = *itt;
            int local_col_idx = which_view.global_to_local[col_idx];
            score_delta += which_view.transition_hypers_i(local_col_idx);
        }
    }

    data_score += score_delta;
    return score_delta;
}

double State::transition_views_col_hypers()
{
    double score_delta = 0;
    for (int view_idx = 0; view_idx < get_num_views(); view_idx++) {
        View &v = get_view(view_idx);
        score_delta += v.transition_hypers();
    }
    data_score += score_delta;
    return score_delta;
}

double State::calc_feature_view_crp_logp(
    const View &v,
    const int &global_col_idx) const
{
    // First check whether the view violates independence constraints.
    // XXX Independence constraints result in non-ergodic chains.
    if (view_violates_independency(v, global_col_idx)) {
        return -INFINITY;
    }
    // Compute CRP log probability.
    // XXX We need to compute the "effective" number of columns, which is the
    // number of column cliques (including cliques of size one).
    int view_column_count = v.get_num_cols_effective();
    int num_columns = get_num_cols_effective();
    double crp_log_delta = numerics::calc_cluster_crp_logp(
        view_column_count, num_columns, column_crp_alpha);
    return crp_log_delta;
}

double State::calc_feature_view_data_logp(
    const vector<double> &col_data,
    const string &col_datatype,
    const View &v,
    const CM_Hypers &hypers,
    const int &global_col_idx) const
{
    // Compute data log probability.
    vector<int> data_global_row_indices = create_sequence(col_data.size());
    double data_log_delta = v.calc_column_predictive_logp(
        col_data, col_datatype, data_global_row_indices, hypers);
    return data_log_delta;
}

double State::calc_feature_view_predictive_logp(
    const vector<double> &col_data,
    const string &col_datatype,
    const View &v,
    double &crp_log_delta,
    double &data_log_delta,
    const CM_Hypers &hypers,
    const int &global_col_idx) const
{
    // Return log score delta as sum of data and CRP prior.
    crp_log_delta = calc_feature_view_crp_logp(v, global_col_idx);
    data_log_delta = calc_feature_view_data_logp(
        col_data, col_datatype, v, hypers, global_col_idx);
    double score_delta = data_log_delta + crp_log_delta;
    return score_delta;
}

bool State::view_violates_independency(
    const View &view, const int &global_col_idx) const
{
    // Return false if global_col_idx has no independence constraints.
    if (column_independencies.count(global_col_idx) == 0){
        return false;
    }
    // Find the independence constraints for global_col_idx.
    set<int> indeps = column_independencies.find(global_col_idx)->second;
    // Check whether any columns in the view is a violator.
    map<int, int>::const_iterator it;
    for (it = view.global_to_local.begin();
        it != view.global_to_local.end();
        ++it) {
        // Violator found.
        if (indeps.count(it->first) > 0){
            return true;
        }
    }
    // No violators found.
    return false;
}

vector<double> State::calc_feature_view_predictive_logps(
    const vector<double> &col_data,
    int global_col_idx) const
{
    vector<double> logps;
    CM_Hypers hypers = get(hypers_m, global_col_idx);
    vector<View *>::const_iterator it;
    double crp_log_delta, data_log_delta;
    string col_datatype = get(global_col_datatypes, global_col_idx);
    for (it = views.begin(); it != views.end(); ++it) {
        View &v = **it;
        double score_delta = calc_feature_view_predictive_logp(col_data,
                col_datatype,
                v,
                crp_log_delta,
                data_log_delta,
                hypers,
                global_col_idx);
        logps.push_back(score_delta);
    }
    return logps;
}

vector<double> State::calc_feature_view_predictive_logps_block(
    const vector<int> &feature_idxs,
    const vector<vector<double> > &feature_datas) const
{
    // Prepare vector of crp_logp and data_logp for each feature_idx.
    vector<vector<double> > unorm_crp_logps_all;
    vector<vector<double> > unorm_data_logps_all;

    // Compute crp_logp and data_logp for each feature.
    for (size_t i = 0; i < feature_idxs.size(); ++i) {
        string col_datatype = get(global_col_datatypes, feature_idxs[i]);
        vector<double> crp_logps = calc_feature_view_crp_logps(feature_idxs[i]);
        vector<double> data_logps = calc_feature_view_data_logps(
            feature_datas[i], feature_idxs[i]);
        unorm_crp_logps_all.push_back(crp_logps);
        unorm_data_logps_all.push_back(data_logps);
    }

    // Sum the data_logps across the features.
    vector<double> unorm_data_logps_sum = std_vector_add(unorm_data_logps_all);

    // Sum and then average the crp_logps across the features.
    // 1. We expect that uncorm_crp_logps_all[i] == unorm_crp_logps_all[j] for
    //    all i,j, since the crp probability of a view is a function of only the
    //    number of columns in that view. However, when there are independence
    //    constraints, the crp probability may be set to zero for a particular
    //    feature_idx.
    // 2. We can take the simple average in direct space (instead of logspace)
    //    since all the values being summed are expected to be the same, or in
    //    the case of an independence constraint the entry with -INFINITY will
    //    satisfy -INFINITY/x = -INFINITY.
    // Example with feature_idxs.size() == 2 and three views.
    //      unorm_crp_logps_all = [[-.5, -.2, -.1], [-.5, -INFINITY, -.1]]
    //      unorm_crp_logps_sum = [-1., -INFINITY, -.2]
    //      unorm_crp_logps_avg = [-.5, -INFINITY, -.1]
    // as desired.
    vector<double> unorm_crp_logps_sum = std_vector_add(unorm_crp_logps_all);
    vector<double> unorm_crp_logps_avg = std_vector_divide_elemwise(
        unorm_crp_logps_sum, unorm_crp_logps_all.size());

    // Compute the predictive_logp as sum of crp_logp and data_logp.
    vector<double> unorm_predictive_logps = std_vector_add(
        unorm_data_logps_sum, unorm_crp_logps_avg);

    return unorm_predictive_logps;
}

vector<double> State::calc_feature_view_crp_logps(
    const int &global_col_idx) const
{
    vector<double> crp_logps;
    vector<View *>::const_iterator it;
    for (it = views.begin(); it != views.end(); ++it) {
        View &v = **it;
        double crp_log_delta = calc_feature_view_crp_logp(
            v, global_col_idx);
        crp_logps.push_back(crp_log_delta);
    }
    return crp_logps;
}

vector<double> State::calc_feature_view_data_logps(
    const vector<double> &col_data,
    const int &global_col_idx) const
{
    vector<double> data_logps;
    CM_Hypers hypers = get(hypers_m, global_col_idx);
    vector<View *>::const_iterator it;
    string col_datatype = get(global_col_datatypes, global_col_idx);
    for (it = views.begin(); it != views.end(); ++it) {
        View &v = **it;
        double data_log_delta= calc_feature_view_data_logp(
            col_data, col_datatype, v, hypers, global_col_idx);
        data_logps.push_back(data_log_delta);
    }
    return data_logps;
}

double State::calc_column_crp_marginal() const
{
    vector<int> view_counts = get_view_counts();
    int num_cols = get_num_cols_effective();
    return numerics::calc_crp_alpha_conditional(view_counts, column_crp_alpha,
            num_cols, true);
}

vector<double> State::calc_column_crp_marginals(const vector<double>
    &alphas_to_score)
const
{
    vector<int> view_counts = get_view_counts();
    vector<double> crp_scores;
    vector<double>::const_iterator it = alphas_to_score.begin();
    int num_cols = get_num_cols_effective();
    for (; it != alphas_to_score.end(); ++it) {
        double alpha_to_score = *it;
        double this_crp_score = numerics::calc_crp_alpha_conditional(view_counts,
                alpha_to_score,
                num_cols,
                true);
        crp_scores.push_back(this_crp_score);
    }
    return crp_scores;
}

double State::calc_row_predictive_logp(const vector<double> &in_vd)
{
    vector<double> view_sum_predictive_logps;
    vector<int> global_column_indices = create_sequence(in_vd.size());
    vector<View *>::const_iterator svp_it;
    for (svp_it = views.begin(); svp_it != views.end(); ++svp_it) {
        // for each view
        View &v = **svp_it;
        vector<int> view_cols = get_indices_to_reorder(global_column_indices,
                v.global_to_local);
        vector<double> use_vd = extract_columns(in_vd, view_cols);
        vector<double> this_view_predictive_logps = \
            v.calc_cluster_vector_predictive_logps(use_vd);
        double this_view_sum_predictive_logp = \
            numerics::logaddexp(this_view_predictive_logps);
        view_sum_predictive_logps.push_back(this_view_sum_predictive_logp);
    }
    double row_predictive_logp = std_vector_sum(view_sum_predictive_logps);
    return row_predictive_logp;
}

double State::transition_column_crp_alpha()
{
    // to make score_crp not calculate absolute, need to track score deltas
    // and apply delta to crp_score
    double crp_score_0 = get_column_crp_score();
    vector<double> unorm_logps = calc_column_crp_marginals(column_crp_alpha_grid);
    double rand_u = draw_rand_u();
    int draw = numerics::draw_sample_unnormalized(unorm_logps, rand_u);
    column_crp_alpha = column_crp_alpha_grid[draw];
    column_crp_score = unorm_logps[draw];
    double crp_score_delta = column_crp_score - crp_score_0;
    return crp_score_delta;
}

double State::transition(const MatrixD &data)
{
    vector<int> which_transitions = create_sequence(3);
    random_shuffle(which_transitions.begin(), which_transitions.end(), rng);
    double score_delta = 0;
    vector<int>::iterator it;
    for (it = which_transitions.begin(); it != which_transitions.end(); ++it) {
        int which_transition = *it;
        if (which_transition == 0) {
            score_delta += transition_views(data);
        } else if (which_transition == 1) {
            vector<int> which_features;
            score_delta += transition_features(data, which_features);
        } else if (which_transition == 2) {
            score_delta += transition_column_crp_alpha();
        }
    }
    return score_delta;
}

void State::increment_num_cols_effective()
{
    num_cols_effective++;
}

void State::decrement_num_cols_effective()
{
    num_cols_effective--;
}

void State::construct_base_hyper_grids(const MatrixD &data, int N_GRID,
    vector<double> ROW_CRP_ALPHA_GRID,
    vector<double> COLUMN_CRP_ALPHA_GRID)
{
    int num_rows = data.size1();
    int num_cols = data.size2();
    if (ROW_CRP_ALPHA_GRID.size() == 0) {
        ROW_CRP_ALPHA_GRID = create_crp_alpha_grid(num_rows, N_GRID);
    }
    if (COLUMN_CRP_ALPHA_GRID.size() == 0) {
        COLUMN_CRP_ALPHA_GRID = create_crp_alpha_grid(num_cols, N_GRID);
    }
    row_crp_alpha_grid = ROW_CRP_ALPHA_GRID;
    column_crp_alpha_grid = COLUMN_CRP_ALPHA_GRID;
    construct_cyclic_base_hyper_grids(N_GRID, num_rows, vm_b_grid);
    construct_continuous_base_hyper_grids(N_GRID, num_rows, r_grid, nu_grid);
    construct_multinomial_base_hyper_grids(N_GRID, num_rows,
        multinomial_alpha_grid);
}

void State::construct_column_hyper_grids(const MatrixD &data,
    const vector<int> &global_col_indices,
    const vector<string> &GLOBAL_COL_DATATYPES,
    const vector<double> &S_GRID,
    const vector<double> &MU_GRID)
{
    int N_GRID = r_grid.size();
    vector<int>::const_iterator it;
    for (it = global_col_indices.begin(); it != global_col_indices.end(); ++it) {
        int global_col_idx = *it;
        string col_datatype = GLOBAL_COL_DATATYPES[global_col_idx];
        if (col_datatype == CONTINUOUS_DATATYPE) {
            if (S_GRID.size() == 0) {
                // FIXME: enable separate setting of S_GRID, MU_GRID
                vector<double> col_data = extract_col(data, global_col_idx);
                construct_continuous_specific_hyper_grid(N_GRID, col_data,
                    s_grids[global_col_idx],
                    mu_grids[global_col_idx]);
            } else {
                s_grids[global_col_idx] = S_GRID;
                mu_grids[global_col_idx] = MU_GRID;
            }
        } else if (col_datatype == CYCLIC_DATATYPE) {
            vector<double> col_data = extract_col(data, global_col_idx);
            construct_cyclic_specific_hyper_grid(N_GRID, col_data,
                vm_a_grids[global_col_idx],
                vm_kappa_grids[global_col_idx]);
        }
    }
}

double State::draw_rand_u()
{
    return rng.next();
}

int State::draw_rand_i(int max)
{
    return rng.nexti(max);
}

CM_Hypers State::uniform_sample_hypers(int global_col_idx)
{
    // presume all grids the same size
    int N_GRID = r_grid.size();
    string col_datatype = global_col_datatypes[global_col_idx];
    CM_Hypers hypers;
    if (col_datatype == CONTINUOUS_DATATYPE) {
        int r_draw = draw_rand_i(N_GRID);
        hypers["r"] = r_grid[r_draw];
        int nu_draw = draw_rand_i(N_GRID);
        hypers["nu"] = nu_grid[nu_draw];
        int s_draw = draw_rand_i(N_GRID);
        hypers["s"] = s_grids[global_col_idx][s_draw];
        int mu_draw = draw_rand_i(N_GRID);
        hypers["mu"] = mu_grids[global_col_idx][mu_draw];
    } else if (col_datatype == CYCLIC_DATATYPE) {
        int b_draw = draw_rand_i(N_GRID);
        hypers["b"] = vm_b_grid[b_draw];
        int a_draw = draw_rand_i(N_GRID);
        hypers["a"] = vm_a_grids[global_col_idx][a_draw];
        int kappa_draw = draw_rand_i(N_GRID);
        hypers["kappa"] = vm_kappa_grids[global_col_idx][kappa_draw];
    } else if (col_datatype == MULTINOMIAL_DATATYPE) {
        int dirichelt_alpha_draw = draw_rand_i(N_GRID);
        hypers["dirichlet_alpha"] = multinomial_alpha_grid[dirichelt_alpha_draw];
        hypers["K"] = global_col_multinomial_counts[global_col_idx];
    } else {
        assert(1 == 0);
    }
    return hypers;
}

double State::sample_column_crp_alpha()
{
    int N_GRID = column_crp_alpha_grid.size();
    return column_crp_alpha_grid[draw_rand_i(N_GRID)];
}

double State::sample_row_crp_alpha()
{
    int N_GRID = row_crp_alpha_grid.size();
    double row_crp_alpha = row_crp_alpha_grid[rng.nexti(N_GRID)];
    return row_crp_alpha;
}

vector<double> State::sample_row_crp_alphas(int N_views)
{
    // needs rng, row_crp_alpha_grid
    vector<double> row_crp_alpha_v;
    for (int view_idx = 0; view_idx < N_views; view_idx++) {
        double row_crp_alpha = sample_row_crp_alpha();
        row_crp_alpha_v.push_back(row_crp_alpha);
    }
    return row_crp_alpha_v;
}

vector<vector<int> > State::generate_col_partition(const vector<int> &
    global_col_indices, const string &col_initialization)
{
    vector<vector<int> > column_partition = draw_crp_init(global_col_indices,
            column_crp_alpha, rng,
            col_initialization);
    return column_partition;
}

vector<vector<vector<int> > > State::generate_row_partitions(
    const vector<int> &global_row_indices,
    const vector<double> &row_crp_alpha_v,
    const string &row_initialization)
{
    vector<vector<vector<int> > > row_partition_v = draw_crp_init(
            global_row_indices,
            row_crp_alpha_v, rng, row_initialization);
    return row_partition_v;
}

void State::init_column_hypers(const vector<int> &global_col_indices)
{
    vector<int>::const_iterator gci_it;
    for (gci_it = global_col_indices.begin(); gci_it != global_col_indices.end();
        ++gci_it) {
        int global_col_idx = *gci_it;
        hypers_m[global_col_idx] = uniform_sample_hypers(global_col_idx);
        if (!hypers_m[global_col_idx].count("fixed")) {
            hypers_m[global_col_idx]["fixed"] = 0;
        }
    }
}

void State::init_views(const MatrixD &data,
    const vector<int> &global_row_indices,
    const vector<int> &global_col_indices,
    const vector<vector<int> > &column_partition,
    const vector<vector<vector<int> > > &row_partition_v,
    const vector<double> &row_crp_alpha_v)
{
    assert(column_partition.size() == row_partition_v.size());
    assert(column_partition.size() == row_crp_alpha_v.size());
    int num_views = column_partition.size();
    for (int view_idx = 0; view_idx < num_views; view_idx++) {
        vector<int> column_indices = column_partition[view_idx];
        int num_cols_effective = get_vector_num_blocks(
            column_indices, get_column_dependencies());
        vector<vector<int> > row_partition = row_partition_v[view_idx];
        double row_crp_alpha = row_crp_alpha_v[view_idx];
        const MatrixD data_subset = extract_columns(data, column_indices);
        View *p_v = new View(data_subset,
            global_col_datatypes,
            row_partition,
            global_row_indices, column_indices,
            num_cols_effective,
            hypers_m,
            row_crp_alpha_grid,
            multinomial_alpha_grid, r_grid, nu_grid,
            vm_b_grid,
            s_grids, mu_grids,
            vm_a_grids, vm_kappa_grids,
            row_crp_alpha,
            draw_rand_i());
        views.push_back(p_v);
        vector<int>::const_iterator ci_it;
        for (ci_it = column_indices.begin(); ci_it != column_indices.end(); ++ci_it) {
            int column_index = *ci_it;
            view_lookup[column_index] = p_v;
        }
    }
}

std::ostream &operator<<(std::ostream &os, const State &s)
{
    os << s.to_string() << endl;
    return os;
}

string State::to_string(const string &join_str, bool top_level) const
{
    stringstream ss;
    if (!top_level) {
        int view_idx = 0;
        vector<View *>::const_iterator it;
        ss << "========" << std::endl;
        for (it = views.begin(); it != views.end(); ++it) {
            View v = **it;
            ss << "view idx: " << view_idx++ << endl;
            ss << v << endl;
            ss << "========" << std::endl;
        }
    }
    ss << "column_crp_alpha: " << column_crp_alpha;
    ss << "; column_crp_score: " << column_crp_score;
    ss << "; data_score: " << get_data_score();
    return ss.str();
}


text:
#ifndef TENSORFLOW_COMPILER_XLA_SERVICE_GPU_NCCL_ALL_TO_ALL_THUNK_H_
#define TENSORFLOW_COMPILER_XLA_SERVICE_GPU_NCCL_ALL_TO_ALL_THUNK_H_

#include "tensorflow/compiler/xla/mlir_hlo/include/mlir-hlo/Dialect/lhlo/IR/lhlo_ops.h"
#include "tensorflow/compiler/xla/service/collective_ops_utils.h"
#include "tensorflow/compiler/xla/service/gpu/buffer_allocations.h"
#include "tensorflow/compiler/xla/service/gpu/nccl_collective_thunk.h"
#include "tensorflow/compiler/xla/service/hlo_instruction.h"
#include "tensorflow/compiler/xla/xla_data.pb.h"

namespace xla {
namespace gpu {

struct NcclAllToAllConfig {
  NcclCollectiveConfig config;
  bool has_split_dimension;
};

// Thunk that performs a NCCL-based All-to-All among CUDA GPU-based replicas.
class NcclAllToAllThunk : public NcclCollectiveThunk {
 public:
  NcclAllToAllThunk(ThunkInfo thunk_info, mlir::lmhlo::AllToAllOp op,
                    std::vector<Buffer> buffers);

  // Returns whether the given instruction can be lowered to a nccl all-to-all
  // call.
  static bool CanImplement(mlir::lmhlo::AllToAllOp op);

  static const char* GetName() { return "AllToAll"; }
  static bool IsDegenerate(mlir::lmhlo::AllToAllOp op, int64_t replica_count,
                           int64_t partition_count) {
    return GetNcclAllToAllConfig(op).config.IsDegenerate(replica_count,
                                                         partition_count);
  }

  static CollectiveOpGroupMode GetGroupMode(mlir::lmhlo::AllToAllOp op) {
    return GetNcclAllToAllConfig(op).config.group_mode;
  }

 protected:
  Status RunNcclCollective(const ExecuteParams& params,
                           ncclComm_t comm) override;

  const NcclCollectiveConfig& config() const override { return config_.config; }

 private:
  static NcclAllToAllConfig GetNcclAllToAllConfig(mlir::lmhlo::AllToAllOp op);

  const NcclAllToAllConfig config_;
  const std::vector<Buffer> buffers_;
};

Status RunAllToAll(bool has_split_dimension,
                   std::vector<DeviceBufferPair>& buffers, se::Stream& stream,
                   ncclComm_t comm);

}  // namespace gpu
}  // namespace xla

#endif  // TENSORFLOW_COMPILER_XLA_SERVICE_GPU_NCCL_ALL_TO_ALL_THUNK_H_

redpajama_set_name: RedPajamaGithub


text:
trait FirstMixin {
  first() { return 'first'; }
}

trait SecondMixin {
  mixin FirstMixin;
  second() { return 'second'; }
}

class MixinOfMixinClass {
  mixin SecondMixin;
}

// ----------------------------------------------------------------------------

var obj = new MixinOfMixinClass();
assertEquals('first', obj.first());
assertEquals('second', obj.second());


text:
/**
 *
 *     Licensed under the Apache License, Version 2.0 (the "License");
 *     you may not use this file except in compliance with the License.
 *     You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *     Unless required by applicable law or agreed to in writing, software
 *     distributed under the License is distributed on an "AS IS" BASIS,
 *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *     See the License for the specific language governing permissions and
 *     limitations under the License.
 *
 */
import * as THREE from 'three';
import ShapesFactory from '../ShapesFactory';
import ShapeParent from './ShapeParent';

class ShapeAzure extends ShapeParent {
  _createInnerGeometry (radius, curveSegments) {
    const polyPath = [
      '0,3,14,35,23,42,14,3,0,3',
      '20,23,24,42,42,0,10,1,35,5,20,23'
    ];
    // X-offset: -21, Y-offset: -21 puts the center of the shape at (0,0), but visual since it is
    // a triangle it doesn't look quite right vertically, hence the Y-offset of -17.
    const newShapes = ShapesFactory.getShapeFromPolyPointsArray(polyPath, ',', -21, -17);

    return new THREE.ShapeGeometry(newShapes, curveSegments);
  }
}
ShapesFactory.registerShape('azure', ShapeAzure);
export default ShapeAzure;


text:
<?php
	class IssueController extends Controller
	{
		public function accessRules()
		{

			return array(
							array('allow', 'roles' => array('admin')),
							array('deny', 'users' => array('*')),
						);
		}

		public function filters()
		{
			return array('accessControl');
		}

		public function actionIndex()
		{
	        $dataProvider = Issue::model()->getAllIssues();
			$issues = $dataProvider->data;

			if (empty($issues))
			{
				Yii::app()->user->setFlash('error', "No book has been issued to Members!");
			}
	        $this->render('index', array('dataProvider' => $dataProvider));

		}

		public function actionreturnBook($UserId, $BookId)
		{
		    if(Yii::app()->request->isPostRequest)
		    {
				$adminModel = new AdminModel;
				if ($adminModel === NULL)
				{
					$type = "message";
					$msg = "Couldn't complete the operation! Please try again!!";
				}
				else
				{
			        $retVal = $adminModel->returnBook($BookId, $UserId);
					if ($retVal)
					{
						$type = "message";
						$msg = "The operation completed successfully!!";

					}
					else
					{
						$type = "message";
						$msg = "Couldn't complete the Operation! Please try again!!";
					}
				}

	            // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
	            if(!isset($_GET['ajax']))
				{
					Yii::app()->user->setFlash($type, $msg);
					$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));
				}
				else
			        echo "

".$msg."

";
		    }
		    else
		        throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
		}

	}
?>


text:
package mano

import (
	"container/list"
	"net/url"
	"regexp"

	"github.com/junhwong/mano/logs"
)

type RouteData struct {
	entry      *RouteEntry
	matchedUrl string
	values     map[string]string
}

type RouteEntry struct {
	method      HttpMethod
	pattern     *regexp.Regexp
	handler     HandlerFunc
	middlewares []Middleware
	paramNames  []string
}

type RouteTable struct {
	routes *list.List
}

func (rt *RouteTable) Register(method HttpMethod, pattern *regexp.Regexp, paramNames []string, handler HandlerFunc, segments int, endsWildcard bool, middlewares ...Middleware) {
	route := &RouteEntry{
		method:      method,
		pattern:     pattern,
		handler:     handler,
		middlewares: middlewares,
		paramNames:  paramNames,
	}
	logs.Debug("map url %v to %v", pattern.String(), handler)
	//TODO:路由的优先级
	rt.routes.PushBack(route)
}

//http://stackoverflow.com/questions/30483652/how-to-get-capturing-group-functionality-in-golang-regular-expressions

// Match 匹配
func (rt *RouteTable) Match(method HttpMethod, url *url.URL) (*RouteData, bool) {
	//url := ""
	var route *RouteEntry
	elem := rt.routes.Front()
	for elem != nil {
		route, _ = elem.Value.(*RouteEntry)
		elem = elem.Next()
		if route == nil || !method.In(route.method) {
			continue
		}

		if route.pattern.MatchString(url.Path) {
			// logs.Debug("%v in %v = %v", method, route.method, method.In(route.method))
			// if !method.In(route.method) {
			// 	logs.Debug("not ok")
			// 	continue
			// }
			return &RouteData{
				entry:      route,
				matchedUrl: url.Path,
			}, true
		}
	}
	return nil, false
}


text:
#ifndef BIO_REMO_H_
#define BIO_REMO_H_

#include "bio/defs.h"
#include "bio/sequence.h"
#include "bio/common.h"

#include <boost/shared_ptr.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/serialization/access.hpp>

#include <antlr/TokenStreamSelector.hpp>

#include <map>
#include <set>

BIO_NS_START

struct Remo {
	typedef std::map<Species, seq_t> remo_map_t;
	remo_map_t map;
};
typedef boost::shared_ptr<Remo> RemoPtr;

template <class OStr>
OStr &
operator<<(OStr & os, const Remo & remo)
{
	for (Remo::remo_map_t::const_iterator i = remo.map.begin(); remo.map.end() != i; ++i) {
		os << i->first << ":" << i->second.size() << ":" << i->second << std::endl;
	}
	return os;
}

typedef std::map<std::string, RemoPtr> RemoMap;
template <class OStr>
OStr &
operator<<(OStr & os, const RemoMap & remo_map)
{
	for (RemoMap::const_iterator i = remo_map.begin(); remo_map.end() != i; ++i)
	{
		os
			<< i->first << std::endl
			<< *(i->second) << std::endl
			<< std::endl;
	}
	return os;
}

/** Some test remos. */
extern RemoMap test_remos;

/** Populate the test remos. */
void build_test_remos();

enum ReMoLocation
{
	REMO_LOC_UPSTREAM,
	REMO_LOC_DOWNSTREAM,
	REMO_LOC_GENEREGION,
	REMO_LOC_UNDEFINED,
};
std::ostream &
operator<<(std::ostream & os, ReMoLocation loc);

enum ReMoSpecies
{
	REMO_SPECIES_CHICK,
	REMO_SPECIES_CHIMP,
	REMO_SPECIES_CIONA,
	REMO_SPECIES_COW,
	REMO_SPECIES_DOG,
	REMO_SPECIES_FLY,
	REMO_SPECIES_FUGU,
	REMO_SPECIES_HUMAN,
	REMO_SPECIES_MOUSE,
	REMO_SPECIES_OPOSSUM,
	REMO_SPECIES_RAT,
	REMO_SPECIES_TETRAODON,
	REMO_SPECIES_XENOPUS,
	REMO_SPECIES_ZEBRAFISH,
	REMO_SPECIES_UNKNOWN,
};
ReMoSpecies parse_remo_species(const std::string & species_string);
std::ostream &
operator<<(std::ostream & os, ReMoSpecies loc);

struct EnsemblId
{
	EnsemblId(const std::string & id = "") : value(id) { }

	std::string value;
	unsigned number;

	bool operator==(const EnsemblId & rhs) const { return value == rhs.value; }

protected:
	friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & value;
		ar & number;
    }
};

/**
A range of a sequence.
*/
struct ReMoRange
{
	int start; /**< Inclusive. */
	int end; /**< Inclusive. */

	ReMoRange(int start = -1, int end = -1)
		: start(start), end(end)
	{ }

	bool operator<(const ReMoRange & rhs) const
	{
		return start < rhs.start || (start == rhs.start && end < rhs.end);
	}

	bool operator==(const ReMoRange & rhs) const
	{
		return start == rhs.start && end == rhs.end;
	}

	bool intersects(const ReMoRange & rhs) const;

	double overlap(const ReMoRange & rhs) const;

	size_t get_size() const;

	int get_centre() const
	{
		return (start + end) / 2;
	}

protected:
	friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & start;
        ar & end;
    }
};
std::ostream &
operator<<(std::ostream & os, const ReMoRange & range);

struct ReMoSequenceId
{
	std::string gene_id;
	std::string transcript_id;
	std::string species;
	unsigned index;
};
bool parse_sequence_id(const std::string & value, ReMoSequenceId & id);
std::string get_gene_id(const std::string & sequence_group_name);

/**
A sub-sequence in a ReMoSequence is identified by its sequence and its position in that sequence.
*/
struct ReMoSubSequence
{
	std::string id;
	ReMoRange range;

	ReMoSubSequence(const std::string & id = "", const ReMoRange & range = ReMoRange())
		: id(id), range(range)
	{ }

	bool operator<(const ReMoSubSequence & rhs) const
	{
		return id < rhs.id || (id == rhs.id && range < rhs.range);
	}

	bool operator==(const ReMoSubSequence & rhs) const
	{
		return id == rhs.id && range == rhs.range;
	}

	typedef std::set<ReMoSubSequence> set_t;

protected:
	friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & id;
        ar & range;
    }
};
std::ostream &
operator<<(std::ostream & os, const ReMoSubSequence & sub_sequence);

struct ReMoExon
{
	EnsemblId id;
	ReMoRange range;

	ReMoExon(const EnsemblId & id = EnsemblId(), const ReMoRange & range = ReMoRange()) : id(id), range(range) { }

	typedef std::list<ReMoExon> list_t;

protected:
	friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & id;
        ar & range;
    }
};

/** A ReMoSequence defines a sequence for one species that holds remos. */
struct ReMoSequence
{
	std::string id;
	int length;
	ReMoLocation location;
	bool has_position;
	int position; /**< Position relative to TSS. */
	ReMoSpecies species;
	ReMoExon::list_t exons;

	ReMoSequence()
		: id("")
		, length(-1)
		, has_position(false)
		, position(0)
		, species(REMO_SPECIES_UNKNOWN)
	{ }

	typedef boost::shared_ptr<ReMoSequence> ptr_t;
	typedef std::list<ptr_t> list_t;

protected:
	friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & id;
        ar & length;
        ar & location;
        ar & has_position;
        ar & position;
        ar & species;
        ar & exons;
    }
};
std::ostream &
operator<<(std::ostream & os, const ReMoSequence & sequence);

struct ReMo
{
	ReMoRange range;
	ReMoRange target_range;
	unsigned conservation;
	unsigned repeat_ratio;
	double belief;
	seq_t masked_sequence;
	seq_t unmasked_sequence;

	ReMo()
		: conservation(0)
		, repeat_ratio(0)
		, belief(0.0)
	{ }

	const seq_t & get_sequence( bool masked = true ) const
	{
		return
			masked
				? masked_sequence
				: unmasked_sequence;
	}

	typedef boost::shared_ptr<ReMo> ptr_t;
	typedef std::list<ptr_t> list_t;
	typedef std::map<std::string, list_t> map_t; /**< Maps sequence ids to a list of remos. */

	/** Inserts the sequence for the remo (list) as one chunk into the insert iterator. */
	template < typename InsIt >
	static
	void
	copy_sequence(
		const list_t & remo_list,
		InsIt insert_it,
		bool masked = true)
	{
		for (list_t::const_iterator r = remo_list.begin();
			remo_list.end() != r;
			++r)
		{
			const seq_t & seq = r->get()->get_sequence( masked );
			std::copy( seq.begin(), seq.end(), insert_it );
		}
	}

protected:
	friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & range;
        ar & target_range;
        ar & conservation;
        ar & repeat_ratio;
        ar & belief;
        ar & masked_sequence;
        ar & unmasked_sequence;
    }
};
std::ostream &
operator<<(std::ostream & os, const ReMo & remo);

struct ReMoBundle
{
	ReMo::map_t remos;
	std::string centre_sequence;

	typedef boost::shared_ptr<ReMoBundle> ptr_t;
	typedef std::list<ptr_t> list_t;
	typedef std::set<ptr_t> set_t;
	typedef std::map<ReMoSubSequence, ptr_t> map_t;
	typedef std::set<std::string> id_set_t;

	ReMoBundle()
	{ }

	/** Get the id of the centre sequence of the comparison. */
	ReMoSubSequence get_centre_sequence_id() const;

	/** Get the set of sequence ids that make up this remo. */
	id_set_t get_sequence_ids() const;

	/** Get the sequence for this id. */
	seq_t get_sequence(const std::string & id, bool masked);

	const ReMo & get_centre_remo() const
	{
		ReMo::map_t::const_iterator r = remos.find(centre_sequence);
		if (remos.end() == r)
		{
			throw BIO_MAKE_STRING("Could not find centre remo: " << centre_sequence);
		}
		if (r->second.empty())
		{
			throw BIO_MAKE_STRING("Centre remo is empty: " << centre_sequence);
		}
		return *(r->second.begin()->get());
	}

protected:
	friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & remos;
		ar & centre_sequence;
    }
};
std::ostream &
operator<<(std::ostream & os, const ReMoBundle & bundle);

struct ReMoSequenceGroup
{
	ReMoSequence::list_t sequences;
	ReMoBundle::map_t remo_bundles;

	ReMoSequenceGroup()
	{ }

	bool operator==(const ReMoSequenceGroup & rhs) const;

	//gets the sequence for the given remo
	ReMoSequence::ptr_t get_sequence_for(const std::string & id);

	/** Get the centre sequence. */
	const ReMoSequence & get_centre_sequence() const
	{
		if (sequences.empty())
		{
			throw std::logic_error( "No sequences in group!" );
		}

		return *(sequences.begin()->get());
	};

	typedef boost::shared_ptr<ReMoSequenceGroup> ptr_t;
	typedef std::list<ptr_t> list_t;

protected:
	friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & sequences;
        ar & remo_bundles;
    }
};
std::ostream &
operator<<(std::ostream & os, const ReMoSequenceGroup & group);

struct ReMoExtraction
{
	ReMoSequenceGroup::list_t sequence_groups;

	typedef boost::shared_ptr<ReMoExtraction> ptr_t;
	typedef std::map<std::string, ReMoBundle::set_t> GeneReMoMap;

	void build_gene_remo_map(GeneReMoMap & map) const;

	bool operator==(const ReMoExtraction & rhs) const;

	void serialise(const boost::filesystem::path & file) const;
	static ptr_t deserialise(const boost::filesystem::path & file);

protected:
	friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & sequence_groups;
    }
};

ReMoExtraction::ptr_t parse_remo_extraction(boost::filesystem::path file);

struct ReMoSharedParserState
{
	antlr::TokenStreamSelector * selector;

	void push_lexer(const char * name) {
		selector->push(name);
	}
	void pop_lexer() {
		selector->pop();
	}
	void log(const char * text) {
		std::cout << text << std::endl;
	}
};

BIO_NS_END

#endif //BIO_REMO_H_


text:
<!doctype html>

    <title>Example: French Desktop Document</title>
    <meta name="description">Internationalization example of site structures using hreflang with canonical, mobile and AMP documents.</meta>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>

    <link rel="amphtml" href="https://ampbyexample.com/internationalization/alternate/fr/index.amp.html" />
    <link rel="alternate" href="https://ampbyexample.com/internationalization/alternate/fr/index.mobile.html" />

    <link rel="alternate" hreflang="en" href="https://ampbyexample.com/internationalization/alternate/" />
    <link rel="alternate" hreflang="fr" href="https://ampbyexample.com/internationalization/alternate/fr/" />
    <link rel="alternate" hreflang="ja" href="https://ampbyexample.com/internationalization/alternate/ja/" />

French Desktop Document

Un document canonique écrit en français.

Autres documents connexes en français:

Le document AMP

Le document mobile

Dans d'autres langues:

En anglais

En japonais

Cette page contient les balises 'link' suivantes:

      <link rel="amphtml" href="https://ampbyexample.com/internationalization/alternate/fr/index.amp.html" />
      <link rel="alternate" href="https://ampbyexample.com/internationalization/alternate/fr/index.mobile.html" />
      <link rel="alternate" hreflang="en" href="https://ampbyexample.com/internationalization/alternate/" />
      <link rel="alternate" hreflang="fr" href="https://ampbyexample.com/internationalization/alternate/fr/" />
      <link rel="alternate" hreflang="ja" href="https://ampbyexample.com/internationalization/alternate/ja/" />

    Retourner à la documentation


text:
package automobile;

public class Automobile {
	private String marca;
	private String modello;
	private double cilindrata;
	private int anno;
	private double velocita;
	private double velocitaMassima;
	private boolean accesa;

	public Automobile(String ma, String mo, double c) {
		marca = ma;
		modello = mo;
		cilindrata = c;
	}

	public String getMarca() {
		return marca;
	}

	public void setMarca(String nuovaMarca) {
		marca = nuovaMarca;
	}

	public String getModello() {
		return modello;
	}

	public void setModello(String m) {
		modello = m;
	}

	public double getCilindrata() {
		return cilindrata;
	}

	public void setCilindrata(double c) {
		cilindrata = c;
	}

	public int getAnno() {
		return anno;
	}

	public void setAnno(int a) {
		anno = a;
	}

	public double getVelocita() {
		return velocita;
	}

	public double getVelocitaMassima() {
		return velocitaMassima;
	}

	public void setVelocitaMassima(double vmax) {
		velocitaMassima = vmax;
	}

	public boolean isAccesa() {
		return accesa;
	}

	public void accendi() {
		accesa = true;
	}

	public void spegni() {
		accesa = false;
		velocita = 0;
	}

	public boolean frena() {
		if (isAccesa() && velocita > 0) { // controllo che la macchina sia accesa
			velocita = velocita - 1; // abbasso la velocità
		}

		if (velocita > 0) { // se non sono fermo restituisco false. altrimenti true
			return false;
		}
		return true;
	}

	public boolean accelera() {
		// verifico che l'auto sia accesa e che possa accelerare
		if (isAccesa() && velocita < velocitaMassima) {
			velocita = velocita + 1;
		}

		if (velocita < velocitaMassima) {
			return true;
		}
		return false;
	}
}


text:
chromeMyAdmin.controller("AddRelationDialogController", ["$scope", "Events", "mySQLClientService", "$q", "targetObjectService", "mySQLQueryService", function($scope, Events, mySQLClientService, $q, targetObjectService, mySQLQueryService) {
    "use strict";

    var onShowDialog = function(table) {
        resetErrorMessage();
        $scope.selectedTable = table;
        $scope.name = "";
        $("#addRelationDialog").modal("show");
        loadColumns(table).then(function(result) {
            updateColumns(result.resultsetRows);
            return loadTables();
        }).then(function(result) {
            updateTargetTables(result.columnDefinitions, result.resultsetRows);
            return loadColumns($scope.targetTable);
        }).then(function(result) {
            updateTargetColumns(result.resultsetRows);
        }, function(reason) {
            $scope.fatalErrorOccurred(reason);
        });
        $scope.onDelete = $scope.onDeletes[0];
        $scope.onUpdate = $scope.onUpdates[0];
    };

    var updateTargetTables = function(columnDefinition, resultsetRows) {
        var targetTables = [];
        for (var i = 0; i < resultsetRows.length; i++) {
            targetTables.push(resultsetRows[i].values[0]);
        }
        $scope.targetTables = targetTables;
        $scope.targetTable = targetTables[0];
    };

    var updateColumns = function(resultsetRows) {
        var columns = [];
        angular.forEach(resultsetRows, function(resultsetRow) {
            columns.push({
                label: resultsetRow.values[0] + " " + resultsetRow.values[1],
                value: resultsetRow.values[0]
            });
        });
        $scope.columns = columns;
        if (columns.length > 0) {
            $scope.column = columns[0].value;
        }
    };

    var updateTargetColumns = function(resultsetRows) {
        var columns = [];
        angular.forEach(resultsetRows, function(resultsetRow) {
            columns.push({
                label: resultsetRow.values[0] + " " + resultsetRow.values[1],
                value: resultsetRow.values[0]
            });
        });
        $scope.targetColumns = columns;
        if (columns.length > 0) {
            $scope.targetColumn = columns[0].value;
        }
    };

    var assignEventHandlers = function() {
        $scope.$on(Events.SHOW_ADD_RELATION_DIALOG, function(event, data) {
            onShowDialog(data.table);
        });
    };

    var loadTables = function() {
        var deferred = $q.defer();
        mySQLQueryService.showTables().then(function(result) {
            deferred.resolve({
                columnDefinitions: result.columnDefinitions,
                resultsetRows: result.resultsetRows
            });
        }, function(reason) {
            deferred.reject(reason);
        });
        return deferred.promise;
    };

    var loadColumns = function(tableName) {
        var deferred = $q.defer();
        mySQLQueryService.showFullColumns(tableName).then(function(result) {
            deferred.resolve({
                columnDefinitions: result.columnDefinitions,
                resultsetRows: result.resultsetRows
            });
        }, function(reason) {
            deferred.reject(reason);
        });
        return deferred.promise;
    };

    var setupItems = function() {
        var constraints = ["", "RESTRICT", "CASCADE", "SET NULL", "NO ACTION"];
        $scope.onDeletes = constraints;
        $scope.onDelete = constraints[0];
        $scope.onUpdates = constraints;
        $scope.onUpdate = constraints[0];
    };

    var resetErrorMessage = function() {
        $scope.errorMessage = "";
    };

    $scope.isErrorMessageVisible = function() {
        return $scope.errorMessage.length > 0;
    };

    $scope.initialize = function() {
        resetErrorMessage();
        assignEventHandlers();
        setupItems();
    };

    $scope.onChangeTargetTable = function() {
        loadColumns($scope.targetTable).then(function(result) {
            updateTargetColumns(result.resultsetRows);
        }, function(reason) {
            $scope.fatalErrorOccurred(reason);
        });
    };

    $scope.addRelation = function() {
        var sql = "ALTER TABLE `" + $scope.selectedTable + "` ";
        sql += "ADD FOREIGN KEY (`" + $scope.column + "`) ";
        sql += "REFERENCES `" + $scope.targetTable + "` (`";
        sql += $scope.targetColumn + "`)";
        if ($scope.onDelete) {
            sql += " ON DELETE " + $scope.onDelete;
        }
        if ($scope.onUpdate) {
            sql += " ON UPDATE " + $scope.onUpdate;
        }
        mySQLClientService.query(sql).then(function(result) {
            if (result.hasResultsetRows) {
                $scope.fatalErrorOccurred("Adding relation failed.");
            } else {
                $("#addRelationDialog").modal("hide");
                targetObjectService.reSelectTable();
            }
        }, function(reason) {
            var errorMessage = "[Error code:" + reason.errorCode;
            errorMessage += " SQL state:" + reason.sqlState;
            errorMessage += "] ";
            errorMessage += reason.errorMessage;
            $scope.errorMessage = errorMessage;
        });
    };

}]);


text:
<?php

namespace test\core\util\log;
use \PHPUnit_Framework_TestCase as PHPUnit_Framework_TestCase;
use core\util\log\FileLogger as FileLogger;
use core\util\param\Validator as Validator;
use core\exception\param\ParamNumberException as ParamNumberException;
use core\exception\param\ParamNotArrayException as ParamNotArrayException;

/*!
 *
 * \page testoverview Testcase Overview
 * \section testutil Utilities
 * \subsection testutilset Testcases for class util\log\FileLogger
 *   PHPUnit_framework_TestCase from package phpunit in /usr/share/php/
 *
 */
class FileLoggerTest extends PHPUnit_Framework_TestCase {

  /**
   * logger that will be created
   */
  private $logger = NULL;

  /**
   * Simple function to check if a FileLogger can be
   * created using namespaces.
   * @small
   */
  public function testNamespace() {
    // if one wants to access a namespace outside of the class'
    // namespace it need to be prepended with '\'
    // use another logfile for each FileLogger instace to avoid removal
    //   of lines in the global one
    $this->logger = new FileLogger("LoggerTest","../../log/test/fileloggertest.log");
    $this->assertNotNull($this->logger);
    $this->logger = NULL;
  }

  /**
   * Tests the creation of a file logger.
   * @small
   */
  public function testLoggerCreate() {
    $this->logger = new FileLogger("LoggerTest","../../log/test/fileloggertest.log");
    $this->assertNotNull($this->logger);
    $this->logger = NULL;
  }

  /**
   * Tests if the logger is accessible by the world.
   * @small
   */
  public function testLoggerAccess() {
    $this->logger = new FileLogger("LoggerTest","../../log/test/fileloggertest.log");
    $this->assertNotNull($this->logger);
    $this->logger = NULL;
  }

  /**
   * Tests the string string formation of file logger.
   * @small
   */
  public function testLoggerFormat() {
    $this->logger = new FileLogger("FileLoggerTest","../../log/test/fileloggertest.log");
    $this->assertStringStartsNotWith(" ", $this->logger->__toString());
    $this->assertStringStartsNotWith("4w5zsdgs", $this->logger->__toString());
    $this->assertStringStartsWith("l.", $this->logger->__toString());
    $this->logger = NULL;
  }

  /**
   * Tests the flattening of objects for a file logger.
   * @small
   */
  public function testLoggerFlatten() {
    $this->logger = new FileLogger("FileLoggerTest","../../log/test/fileloggertest.log");
    $this->assertStringStartsWith("string", $this->logger->flatten("string"));
    $this->assertStringStartsWith("Array", $this->logger->flatten(array()));
    $this->assertStringStartsWith("Array", $this->logger->flatten(array("eins")));
    $this->assertStringStartsWith("test\core\util\log\FileLoggerTest",
                                  $this->logger->flatten($this));
    $this->assertStringStartsWith("l.", $this->logger->flatten($this->logger));
    $this->assertStringStartsWith("NULL", $this->logger->flatten(null));
    $this->logger = NULL;
  }

  /**
   * Tests the debug flattening of objects for a file logger.
   * @small
   */
  public function testLoggerFlattenDebug() {
    $this->logger = new FileLogger("FileLoggerTest","../../log/test/fileloggertest.log");
    $this->assertStringStartsWith("OBJECT(", $this->logger->flatten("string", true));
    $this->assertStringStartsWith("OBJECT(", $this->logger->flatten(array(), true));
    $this->assertStringStartsWith("OBJECT(", $this->logger->flatten($this, true));
    $this->assertStringStartsWith("OBJECT(", $this->logger->flatten($this,true));
    $this->assertStringStartsWith("OBJECT(", $this->logger->flatten($this->logger,true));
    $this->assertStringStartsWith("OBJECT()=NULL", $this->logger->flatten(null,true));
    $this->logger = NULL;
  }

  /**
   * Tests the logging itself for a file logger.
   * @small
   */
  public function testLoggerLogging() {
    $this->logger = new FileLogger("FileLoggerTest","../../log/test/fileloggertest.log");
    $this->assertFileNotExists($this->logger->getf()->get_file());
    $this->logger->log("%",array($this));
    $this->assertFileExists($this->logger->getf()->get_file());
    $this->assertStringNotEqualsFile($this->logger->getf()->get_file(),"");
    $this->logger = NULL;
  }

  /**
   * Tests the cleanage of the logfile.
   * @small
   */
  public function testLogfileCleanage() {
    $this->logger = new FileLogger("FileLoggerTest","../../log/test/fileloggertest.log");
    $this->assertFileExists($this->logger->getf()->get_file());
    $this->logger->getf()->clean();
    $this->assertStringEqualsFile($this->logger->getf()->get_file(),"");
    $this->logger = NULL;
  }

  /**
   * Tests if the first line can be obtained from the logfile.
   * @small
   */
  public function testGetFirstLineFromLogfile() {
    $this->logger = new FileLogger("FileLoggerTest","../../log/test/fileloggertest.log");
    $this->logger->getf()->clean();
    $line = $this->logger->getf()->getfirst();
    $this->assertEquals($line, "");
    $this->logger->log("%\n\n",array($this));
    $line = $this->logger->getf()->getfirst();
    $this->assertNotEquals($line, "");
    $this->assertNotRegExp('/'.PHP_EOL.'/', $line);
    $this->logger = NULL;
  }

  /**
   * Tests if the first line can be obtained from the logfile.
   * @small
   */
  public function testGetLastLineFromLogfile() {
    $this->logger = new FileLogger("FileLoggerTest","../../log/test/fileloggertest.log");
    $this->logger->getf()->clean();
    $line = $this->logger->getf()->getlast();
    $this->assertEquals($line, "");
    $this->logger->log("%\n\n",array($this));
    $line = $this->logger->getf()->getlast();
    $this->assertNotEquals($line, "");
    $this->assertNotRegExp('/'.PHP_EOL.'/', $line);
    $this->logger = NULL;
  }

  /**
   * Tests for a specific type (string,array,object,...)
   * @large
   * @see Log::stringToPriority for logging types
   */
  public function testLoggerLoggingType() {
    $this->logger = new FileLogger("FileLoggerTest","../../log/test/fileloggertest.log");
    $this->logger->getf()->clean();
    $this->logger->log("%",array($this),"EMERG");
    $line = $this->logger->getf()->getlast();
    $this->assertRegExp('/.* FileLoggerTest \[emergency\] core\\\\util\\\\log\\\\FileLogger test\\\\core\\\\util\\\\log\\\\FileLoggerTest.*/',$line);
    $this->logger->log("%",array($this),"ALERT");
    $line = $this->logger->getf()->getlast();
    $this->assertRegExp('/.* FileLoggerTest \[alert\] core\\\\util\\\\log\\\\FileLogger test\\\\core\\\\util\\\\log\\\\FileLoggerTest.*/',$line);
    $this->logger->log("%",array($this),"CRIT");
    $line = $this->logger->getf()->getlast();
    $this->assertRegExp('/.* FileLoggerTest \[critical\] core\\\\util\\\\log\\\\FileLogger test\\\\core\\\\util\\\\log\\\\FileLoggerTest.*/',$line);
    $this->logger->log("%",array($this),"ERR");
    $line = $this->logger->getf()->getlast();
    $this->assertRegExp('/.* FileLoggerTest \[error\] core\\\\util\\\\log\\\\FileLogger test\\\\core\\\\util\\\\log\\\\FileLoggerTest.*/',$line);
    $this->logger->log("%",array($this),"WARNING");
    $line = $this->logger->getf()->getlast();
    $this->assertRegExp('/.* FileLoggerTest \[warning\] core\\\\util\\\\log\\\\FileLogger test\\\\core\\\\util\\\\log\\\\FileLoggerTest.*/',$line);
    $this->logger->log("%",array($this),"NOTICE");
    $line = $this->logger->getf()->getlast();
    $this->assertRegExp('/.* FileLoggerTest \[notice\] core\\\\util\\\\log\\\\FileLogger test\\\\core\\\\util\\\\log\\\\FileLoggerTest.*/',$line);
    $this->logger->log("%",array($this),"INFO");
    $line = $this->logger->getf()->getlast();
    $this->assertRegExp('/.* FileLoggerTest \[info\] core\\\\util\\\\log\\\\FileLogger test\\\\core\\\\util\\\\log\\\\FileLoggerTest.*/',$line);
    // really bad overhead for debug lines, found ~500k chars long lines
    // therefor this check should just be enabled if the dumped objects
    //   do not contain horrible long lines, therefor temporarily disabled
    // TODO: check getFirstLine for such overheads on long debug lines
    //$this->logger->log("%",array($this),"DEBUG");
    //$line = $this->logger->getf()->getlast();
    //$this->assertRegExp('/.* FileLoggerTest \[debug\] core\\\\util\\\\log\\\\FileLogger OBJECT\(test\\\\core\\\\util\\\\log\\\\FileLoggerTest.*/',$line);
    $this->logger = NULL;
  }

  /**
   * Tests for parameter exceptions thrown.
   *
   * NOTE: exceptions thrown does not allow further assertions
   *       without catching them explicitly
   *
   * @expectedException core\exception\param\ParamNotArrayException
   * @expectedExceptionMessage Parameters provided are not an array.
   *
   * @expectedException core\exception\param\ParamNumberException
   * @expectedExceptionMessage Invalid amount of parameters passed.
   *
   */
  public function testParamExceptionsThrown() {
    $this->logger = new FileLogger("FileLoggerTest",
                          "../../log/test/fileloggertest.log");

    // ParamNotArrayException
    $this->logger->log("A param log: %",$this);
    // ParamNumberException
    $this->logger->log("Two param log: %, %", array($this));
  }

  /**
   * Tests for parameter "exceptions" logged.
   *
   * NOTE: exceptions need to be catched by their fully qualified
   *       namespace or alias defined
   *
   * NOTE: '@exceptedException' does not work in conjunction with
   *       try-catch-blocks. it is just simply ignored then.
   *
   */
  public function testParamExceptionsLogged() {
    $this->logger = new FileLogger("FileLoggerTest",
                          "../../log/test/fileloggertest.log");

    // ParamNotArrayException
    try {
      $this->logger->log("A param log: %",$this);
    } catch(ParamNotArrayException $ex) {
      // additionally check the log file
      $line = $this->logger->getf()->getlast();
      $this->assertRegExp('/.* FileLoggerTest \[warning\] core\\\\util\\\\log\\\\FileLogger line [0-9]+: Message replacements are not provided as an array\./',$line);
    }

    // ParamNumberException
    try {
      $this->logger->log("Two param log: %, %", array($this));
    } catch(ParamNumberException $ex) {
      // additionally check the log file
      $line = $this->logger->getf()->getlast();
      $this->assertRegExp('/.* FileLoggerTest \[warning\] core\\\\util\\\\log\\\\FileLogger line [0-9]+: Number of message replacements do not equal\./',$line);
    }

  }

  /**
   * Tests the destruction of a file logger.
   * @small
   */
  public function testLoggerDestroy() {
    // destructors will be called automatically if all references to an object got null'ed
    $this->logger = new FileLogger("LoggerTest","../../log/test/fileloggertest.log");
    $this->logger = NULL;
    $this->assertNull($this->logger);
  }
}

?>


text:
// Copyright (c) Simple Injector Contributors. All rights reserved.
// Licensed under the MIT License. See LICENSE file in the project root for license information.

namespace SimpleInjector
{
    /// <summary>
    /// This enumeration defines in which way the container should run the verification process.
    /// </summary>
    public enum VerificationOption
    {
        /// <summary>
        /// Specifies that the container performs verification only, which means that it will test whether
        /// all registrations can be constructed by iterating the registrations and letting the container
        /// create at least one instance of each registration. An <see cref="System.InvalidOperationException"/>
        /// will be thrown in case the configuration is invalid.
        /// </summary>
        VerifyOnly = 0,

        /// <summary>
        /// Specifies that the container will run diagnostic analysis after the verification succeeded. The
        /// container will diagnose the configuration with a subset of the available diagnostic warnings, that
        /// are most likely an indication of a configuration mistake. A complete set of diagnostic warnings
        /// can be retrieved by calling
        /// <see cref="SimpleInjector.Diagnostics.Analyzer.Analyze">Analyzer.Analyze</see> or by viewing the
        /// container in the Visual Studio debugger, after the verification has succeeded.
        /// </summary>
        VerifyAndDiagnose = 1
    }
}


text:
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Xml.Linq;
using GranitXMLEditorTests;

namespace ExtensionMethods.Tests
{
  [TestClass()]
  public class XCommentExtensionTests
  {
    [TestMethod()]
    public void IsNotCommentedXElement_Test()
    {
      XComment xc = new XComment("dummy comment");
      Assert.IsFalse(xc.IsCommentedXElement());
    }

    [TestMethod()]
    public void IsCommentedXElement_Test()
    {
      XComment commentedXElement = new XComment(TestConstants.TransactionXElem1);
      Assert.IsTrue(commentedXElement.IsCommentedXElement());
    }

    [TestMethod()]
    public void UnCommentXElmenet_Test()
    {
      XComment commentedXElement = new XComment(TestConstants.TransactionXElem1);
      XElement xe = commentedXElement.UnCommentXElmenet();

      Assert.IsTrue(TestConstants.TransactionXElem1.IgnoreWhiteSpaceEquals(xe.ToString()));
    }
  }
}


text:
<?php namespace Otacioglu\Support;
/**
 * Config Class
 *
 * @package  Bytect
 * @author Orcun Otacioglu <otacioglu.orcun@gmail.com>
 * @link http://twitter.com/ootacioglu
 */
class Config
{
	/**
	 * Returns the value of a given path
	 * @param  string $path index of an option inside the init file
	 * @return string      Returns the key of the given index
	 */
	public static function get($path = null)
	{

		if($path) {

			$config = $GLOBALS['config'];

			$path = explode('/', $path);

			foreach($path as $bit) {

				if(isset($config[$bit])) {

					$config = $config[$bit];
				}
			}

			return $config;
		}
	}
}


text:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

	<meta name="generator" content="HTML Tidy for Mac OS X (vers 1st December 2004), see www.w3.org" />
	<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
	<link rel="stylesheet" href="styles.css" type="text/css" media="screen" title="styles" charset="utf-8" />
  <link rel="icon" href="./images/terminal.png" type="image/png" />
	<title>SimpleConsole - Building Console Apps</title>

about

news

tutorial

download

docs


text:
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.

using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Semantics;

namespace Microsoft.CodeAnalysis.Test.Utilities
{
    /// Analyzer used to identify fields that could be declared ReadOnly.
    public class FieldCouldBeReadOnlyAnalyzer : DiagnosticAnalyzer
    {
        private const string SystemCategory = "System";

        public static readonly DiagnosticDescriptor FieldCouldBeReadOnlyDescriptor = new DiagnosticDescriptor(
            "FieldCouldBeReadOnly",
            "Field Could Be ReadOnly",
            "Field is never modified and so could be readonly or const.",
            SystemCategory,
            DiagnosticSeverity.Warning,
            isEnabledByDefault: true);

        /// Gets the set of supported diagnostic descriptors from this analyzer.
        public sealed override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
        {
            get { return ImmutableArray.Create(FieldCouldBeReadOnlyDescriptor); }
        }

        public sealed override void Initialize(AnalysisContext context)
        {
            context.RegisterCompilationStartAction(
                 (compilationContext) =>
                 {
                     HashSet<IFieldSymbol> assignedToFields = new HashSet<IFieldSymbol>();
                     HashSet<IFieldSymbol> mightBecomeReadOnlyFields = new HashSet<IFieldSymbol>();

                     compilationContext.RegisterOperationBlockStartAction(
                         (operationBlockContext) =>
                         {

                             if (operationBlockContext.OwningSymbol is IMethodSymbol containingMethod)
                             {
                                 bool inConstructor = containingMethod.MethodKind == MethodKind.Constructor;
                                 ITypeSymbol staticConstructorType = containingMethod.MethodKind == MethodKind.StaticConstructor ? containingMethod.ContainingType : null;

                                 operationBlockContext.RegisterOperationAction(
                                    (operationContext) =>
                                    {
                                        if (operationContext.Operation is IAssignmentExpression assignment)
                                        {
                                            AssignTo(assignment.Target, inConstructor, staticConstructorType, assignedToFields, mightBecomeReadOnlyFields);
                                        }
                                        else if (operationContext.Operation is IIncrementOrDecrementExpression increment)
                                        {
                                            AssignTo(increment.Target, inConstructor, staticConstructorType, assignedToFields, mightBecomeReadOnlyFields);
                                        }
                                        else
                                        {
                                            throw TestExceptionUtilities.UnexpectedValue(operationContext.Operation);
                                        }
                                    },
                                    OperationKind.SimpleAssignmentExpression,
                                    OperationKind.CompoundAssignmentExpression,
                                    OperationKind.IncrementExpression,
                                    OperationKind.DecrementExpression);

                                 operationBlockContext.RegisterOperationAction(
                                     (operationContext) =>
                                     {
                                         IInvocationExpression invocation = (IInvocationExpression)operationContext.Operation;
                                         foreach (IArgument argument in invocation.Arguments)
                                         {
                                             if (argument.Parameter.RefKind == RefKind.Out || argument.Parameter.RefKind == RefKind.Ref)
                                             {
                                                 AssignTo(argument.Value, inConstructor, staticConstructorType, assignedToFields, mightBecomeReadOnlyFields);
                                             }
                                         }
                                     },
                                     OperationKind.InvocationExpression);
                             }
                         });

                     compilationContext.RegisterSymbolAction(
                         (symbolContext) =>
                         {
                             IFieldSymbol field = (IFieldSymbol)symbolContext.Symbol;
                             if (!field.IsConst && !field.IsReadOnly && !assignedToFields.Contains(field))
                             {
                                 mightBecomeReadOnlyFields.Add(field);
                             }
                         },
                         SymbolKind.Field
                         );

                     compilationContext.RegisterCompilationEndAction(
                         (compilationEndContext) =>
                         {
                             foreach (IFieldSymbol couldBeReadOnlyField in mightBecomeReadOnlyFields)
                             {
                                 Report(compilationEndContext, couldBeReadOnlyField, FieldCouldBeReadOnlyDescriptor);
                             }
                         });
                 });
        }

        private static void AssignTo(IOperation target, bool inConstructor, ITypeSymbol staticConstructorType, HashSet<IFieldSymbol> assignedToFields, HashSet<IFieldSymbol> mightBecomeReadOnlyFields)
        {
            if (target.Kind == OperationKind.FieldReferenceExpression)
            {
                IFieldReferenceExpression fieldReference = (IFieldReferenceExpression)target;
                if (inConstructor && fieldReference.Instance != null)
                {
                    switch (fieldReference.Instance.Kind)
                    {
                        case OperationKind.InstanceReferenceExpression:
                            return;
                    }
                }

                IFieldSymbol targetField = fieldReference.Field;

                if (staticConstructorType != null && targetField.IsStatic && targetField.ContainingType == staticConstructorType)
                {
                    return;
                }

                assignedToFields.Add(targetField);
                mightBecomeReadOnlyFields.Remove(targetField);

                if (fieldReference.Instance != null && fieldReference.Instance.Type.IsValueType)
                {
                    AssignTo(fieldReference.Instance, inConstructor, staticConstructorType, assignedToFields, mightBecomeReadOnlyFields);
                }
            }
        }

        private void Report(CompilationAnalysisContext context, IFieldSymbol field, DiagnosticDescriptor descriptor)
        {
            context.ReportDiagnostic(Diagnostic.Create(descriptor, field.Locations.FirstOrDefault()));
        }
    }
}