Skip to content

Commit

Permalink
Merge branch 'master' into attributes/fix_compile_errors_from_fix_int…
Browse files Browse the repository at this point in the history
…eger_conversion_errors
  • Loading branch information
grenewode authored Apr 12, 2018
2 parents fe13aef + f149040 commit 5115b40
Show file tree
Hide file tree
Showing 18 changed files with 156 additions and 84 deletions.
12 changes: 11 additions & 1 deletion examples/base/array.cc
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
// This file is part of Empirical, https://github.com/devosoft/Empirical
// Copyright (C) Michigan State University, 2016-2017.
// Copyright (C) Michigan State University, 2016-2018.
// Released under the MIT Software license; see doc/LICENSE

#include "base/array.h"

#define A_SIZE 50

// Function to print an std::array; will it work with emp::array?
template <size_t N>
void ArrayPrint(const std::array<int,N> & ar) {
for (int x : ar) std::cout << x << " ";
std::cout << std::endl;
}

int main()
{
emp::array<int, A_SIZE> test_array;
Expand All @@ -14,6 +21,9 @@ int main()
test_array[i] = (int) (i * i);
}

std::cout << "First array: " << std::endl;
ArrayPrint<A_SIZE>(test_array);

int sum = 0;
for (size_t i = 0; i < A_SIZE; i++) {
sum += test_array[i];
Expand Down
1 change: 1 addition & 0 deletions examples/data/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CFLAGS_version := -std=c++14
# Emscripten compiler information
CXX_web := emcc
CXX_native := g++
#CXX_native := clang++

OFLAGS_native_debug := -g -pedantic -DEMP_TRACK_MEM -Wnon-virtual-dtor -Wcast-align -Woverloaded-virtual -Wconversion -Weffc++
OFLAGS_native_opt := -O3 -DNDEBUG
Expand Down
6 changes: 3 additions & 3 deletions examples/timing/pointers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ int main()
std::clock_t base_start_time = std::clock();

std::vector<int *> v_base(N);
for (size_t i = 0; i < N; i++) v_base[i] = new int((i*7)%N);
for (size_t i = 0; i < N; i++) v_base[i] = new int((int)((i*7)%N));
std::vector<int *> v_base2(v_base);
std::sort( v_base2.begin(), v_base2.end(), [](int *p1,int *p2){ return *p1 < *p2; } );
v_base.resize(0);
Expand All @@ -43,7 +43,7 @@ int main()
std::clock_t std_start_time = std::clock();

std::vector<std::shared_ptr<int>> v_std(N);
for (size_t i = 0; i < N; i++) v_std[i] = std::make_shared<int>( (i*7)%N );
for (size_t i = 0; i < N; i++) v_std[i] = std::make_shared<int>( (int)((i*7)%N) );
std::vector<std::shared_ptr<int>> v_std2(v_std);
std::sort( v_std2.begin(), v_std2.end(),
[](std::shared_ptr<int> p1, std::shared_ptr<int> p2){ return *p1 < *p2; } );
Expand All @@ -61,7 +61,7 @@ int main()
std::clock_t emp_start_time = std::clock();

std::vector<emp::Ptr<int>> v_emp(N);
for (size_t i = 0; i < N; i++) v_emp[i] = emp::NewPtr<int>((i*7)%N);
for (size_t i = 0; i < N; i++) v_emp[i] = emp::NewPtr<int>((int)((i*7)%N));
std::vector<emp::Ptr<int>> v_emp2(v_emp);
std::sort( v_emp2.begin(), v_emp2.end(),
[](emp::Ptr<int> p1, emp::Ptr<int> p2){ return *p1 < *p2; } );
Expand Down
6 changes: 6 additions & 0 deletions source/base/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ namespace emp {
array(InputIt first, InputIt last) : base_t(first, last), valid(true) { emp_assert(size() == N); }
~array() { valid=false; } // No longer valid when array is deleted.

operator std::array<T,N>() {
std::array<T,N> ar;
for (size_t i = 0; i < N; i++) ar[i] = base_t::operator[](i);
return ar;
}

constexpr size_t size() const { return N; }

iterator begin() noexcept { return iterator(base_t::begin(), this); }
Expand Down
5 changes: 3 additions & 2 deletions source/base/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
* This class is a drop-in wrapper for std::vector, adding on bounds checking.
* If EMP_NDEBUG is set then it reverts back to std::vector.
*
* @todo Need an automatic conversion from emp::vector to std::vector and back to interface with
* non-empirical code.
* @todo Debug code: member functions that take iterators should also take emp iterators that verify
* whether those iterators are valid.
*/


Expand Down Expand Up @@ -62,6 +62,7 @@ namespace emp {
const vec_t * v_ptr;
int revision;

// @CAO: For the moment cannot create an emp iterator from a base since we don't know vector to use.
// iterator_wrapper(const ITERATOR_T & _in)
// : ITERATOR_T(_in), v_ptr(nullptr), revision(0) { ; }
iterator_wrapper(const ITERATOR_T & _in, const vec_t * _v)
Expand Down
2 changes: 1 addition & 1 deletion source/meta/type_traits.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// This file is part of Empirical, https://github.com/devosoft/Empirical
// Copyright (C) Michigan State University, 2016-2017.
// Copyright (C) Michigan State University, 2016-2018.
// Released under the MIT Software license; see doc/LICENSE
//
// Extensions on the standard library type traits to handle Empirical classes
Expand Down
4 changes: 2 additions & 2 deletions source/tools/set_utils.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @note This file is part of Empirical, https://github.com/devosoft/Empirical
* @copyright Copyright (C) Michigan State University, MIT Software license; see doc/LICENSE.md
* @date 2016-2017
* @date 2016-2018
*
* @file set_utils.h
* @brief Tools to save and load data from classes.
Expand Down Expand Up @@ -68,7 +68,7 @@ namespace emp {
return result;
}

/// Compute the set difference of @param s1 and @param s2 (elements that are in S1 but no S2)
/// Compute the set difference of @param s1 and @param s2 (elements that are in S1 but not S2)
template <typename T>
std::set<T> difference(std::set<T> & s1, emp::vector<T> s2) {
// Based on PierreBdR's answer to https://stackoverflow.com/questions/283977/c-stl-set-difference
Expand Down
8 changes: 4 additions & 4 deletions source/web/JSWrap.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @note This file is part of Empirical, https://github.com/devosoft/Empirical
* @copyright Copyright (C) Michigan State University, MIT Software license; see doc/LICENSE.md
* @date 2015-2017
* @date 2015-2018
*
* @file JSWrap.h
* @brief Wrap a C++ function and convert it to an integer that can be called from Javascript
Expand Down Expand Up @@ -116,7 +116,7 @@ namespace emp {
arg_var = tmp_var; // @CAO Do we need to free the memory in tmp_var?
}

template <int ARG_ID, size_t SIZE, typename T> static void LoadArg(std::array<T, SIZE> & arg_var){
template <int ARG_ID, size_t SIZE, typename T> static void LoadArg(emp::array<T, SIZE> & arg_var){
EM_ASM_ARGS({emp_i.__outgoing_array = emp_i.cb_args[$0];}, ARG_ID);
pass_array_to_cpp(arg_var);
}
Expand Down Expand Up @@ -265,7 +265,7 @@ namespace emp {
}

template <typename T, size_t N>
static void StoreReturn(const std::array<T, N> & ret_var) {
static void StoreReturn(const emp::array<T, N> & ret_var) {
pass_array_to_javascript(ret_var);
EM_ASM({ emp_i.cb_return = emp_i.__incoming_array; });
}
Expand All @@ -292,7 +292,7 @@ namespace emp {
}

template <typename T, size_t N>
static void StoreReturn(const std::array<T, N> & ret_var, std::string var) {
static void StoreReturn(const emp::array<T, N> & ret_var, std::string var) {
pass_array_to_javascript(ret_var);
EM_ASM_ARGS({ emp_i.curr_obj[Pointer_stringify($0)] = emp_i.__incoming_array;}, var.c_str());
}
Expand Down
17 changes: 13 additions & 4 deletions source/web/d3/axis.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
#ifndef __AXIS_H__
#define __AXIS_H__
/**
* @note This file is part of Empirical, https://github.com/devosoft/Empirical
* @copyright Copyright (C) Michigan State University, MIT Software license; see doc/LICENSE.md
* @date 2017-2018
*
* @file axis.h
* @brief Handle drawing of axes on D3 graphts.
*/

#ifndef EMP_D3_AXIS_H
#define EMP_D3_AXIS_H

#include "../js_utils.h"
#include "../../tools/string_utils.h"
Expand Down Expand Up @@ -171,7 +180,7 @@ namespace D3 {
}

template <typename T, std::size_t SIZE>
Axis& SetTickValues(std::array<T, SIZE> values) {
Axis& SetTickValues(emp::array<T, SIZE> values) {
emp::pass_array_to_javascript(values);

EM_ASM_ARGS({
Expand Down Expand Up @@ -231,7 +240,7 @@ namespace D3 {
/// transition, then the rescaling will be animated.
template <typename T>
Axis& Rescale(double new_min, double new_max, const D3::SelectionOrTransition<T> & svg){
this->scale.SetDomain(std::array<double, 2>({{new_min, new_max}}));
this->scale.SetDomain(emp::array<double, 2>{{new_min, new_max}});
ApplyAxis(svg.Select("#"+dom_id));
return *this;
}
Expand Down
15 changes: 12 additions & 3 deletions source/web/d3/dataset.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
#ifndef __LOAD_DATA_H__
#define __LOAD_DATA_H__
/**
* @note This file is part of Empirical, https://github.com/devosoft/Empirical
* @copyright Copyright (C) Michigan State University, MIT Software license; see doc/LICENSE.md
* @date 2016-2018
*
* @file dataset.h
* @brief Tools to maintain data in D3.
*/

#ifndef EMP_D3_LOAD_DATA_H
#define EMP_D3_LOAD_DATA_H

#include <functional>

Expand Down Expand Up @@ -225,7 +234,7 @@ namespace D3 {

/// Put the last row of the array into arr
template <std::size_t N, typename T>
void GetLastRow(std::array<T, N> & arr) {
void GetLastRow(emp::array<T, N> & arr) {
EM_ASM_ARGS({
emp_i.__outgoing_array = js.objects[$0][js.objects[$0].length - 1];
}, GetID());
Expand Down
27 changes: 18 additions & 9 deletions source/web/d3/layout.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
#ifndef __LAYOUT_H__
#define __LAYOUT_H__
/**
* @note This file is part of Empirical, https://github.com/devosoft/Empirical
* @copyright Copyright (C) Michigan State University, MIT Software license; see doc/LICENSE.md
* @date 2016-2018
*
* @file layout.h
* @brief Tools for laying out nodes in D3.
*/

#ifndef EMP_D3_LAYOUT_H
#define EMP_D3_LAYOUT_H

#include "d3_init.h"
#include "dataset.h"
Expand Down Expand Up @@ -80,8 +89,8 @@ namespace D3{

make_line = new D3::LinkGenerator("horizontal");

// std::function<std::array<double, 2>(NODE_TYPE, int, int)> projection = [](NODE_TYPE n, int i, int k){
// return std::array<double, 2>({n.y(), n.x()});
// std::function<emp::array<double, 2>(NODE_TYPE, int, int)> projection = [](NODE_TYPE n, int i, int k){
// return emp::array<double, 2>({n.y(), n.x()});
// };
//
// emp::JSWrap(projection, "projection");
Expand All @@ -96,8 +105,8 @@ namespace D3{

make_line = new D3::LinkGenerator("horizontal");

// std::function<std::array<double, 2>(NODE_TYPE, int, int)> projection = [](NODE_TYPE n, int i, int k){
// return std::array<double, 2>({{n.y(), n.x()}});
// std::function<emp::array<double, 2>(NODE_TYPE, int, int)> projection = [](NODE_TYPE n, int i, int k){
// return emp::array<double, 2>({{n.y(), n.x()}});
// };
//
// emp::JSWrap(projection, "projection");
Expand All @@ -123,7 +132,7 @@ namespace D3{
//to do more with it. It would be nice to return the enter selection for
//links too, but C++ makes that super cumbersome, and it's definitely the less
//common use case
std::array<Selection, 4> GenerateNodesAndLinks(Selection svg) {
emp::array<Selection, 4> GenerateNodesAndLinks(Selection svg) {
int node_enter = NextD3ID();
int node_exit = NextD3ID();
int link_enter = NextD3ID();
Expand Down Expand Up @@ -179,8 +188,8 @@ namespace D3{
js.objects[$7] = linkExit;
}, this->id, data->GetID(), make_line->GetID(), svg.GetID(), node_enter, node_exit, link_enter, link_exit);
std::cout << "Done generating" << std::endl;
return std::array<Selection, 4>({{Selection(node_enter), Selection(node_exit),
Selection(link_enter), Selection(link_exit)}});
return emp::array<Selection, 4>{{Selection(node_enter), Selection(node_exit),
Selection(link_enter), Selection(link_exit)}};
}


Expand Down
19 changes: 14 additions & 5 deletions source/web/d3/scales.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
#ifndef __SCALES_H__
#define __SCALES_H__
/**
* @note This file is part of Empirical, https://github.com/devosoft/Empirical
* @copyright Copyright (C) Michigan State University, MIT Software license; see doc/LICENSE.md
* @date 2016-2018
*
* @file scales.h
* @brief Tools for scaling graph axes in D3.
*/

#ifndef EMP_D3_SCALES_H
#define EMP_D3_SCALES_H

#include "d3_init.h"
#include "utils.h"
Expand All @@ -26,7 +35,7 @@ namespace D3 {
/// will be interpolated with a function determined by the type of the scale.
/// Array should contain same number of elements as the one used to set the domain.
template <typename T, size_t SIZE>
Scale& SetRange(std::array<T,SIZE> values) {
Scale& SetRange(emp::array<T,SIZE> values) {
emp::pass_array_to_javascript(values);
EM_ASM_ARGS({js.objects[$0].range(emp_i.__incoming_array);}, this->id);
return *this;
Expand All @@ -40,7 +49,7 @@ namespace D3 {
/// Set the input values corresponding to values in the range.
/// Array should contain same number of elements as the one used to set the range.
template <typename T, size_t SIZE>
Scale& SetDomain(std::array<T,SIZE> values) {
Scale& SetDomain(emp::array<T,SIZE> values) {
emp::pass_array_to_javascript(values);
EM_ASM_ARGS({js.objects[$0].domain(emp_i.__incoming_array);}, this->id);
return *this;
Expand Down Expand Up @@ -139,7 +148,7 @@ namespace D3 {
LinearScale(bool derived) : IdentityScale(true) {;}

template <typename T, size_t SIZE>
LinearScale& SetRangeRound(std::array<T,SIZE> values) {
LinearScale& SetRangeRound(emp::array<T,SIZE> values) {
emp::pass_array_to_javascript(values);
EM_ASM_ARGS({js.objects[$0].rangeRound(emp.__incoming_array);}, this->id);
return *this;
Expand Down
23 changes: 16 additions & 7 deletions source/web/d3/svg_shapes.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
#ifndef __SVG_SHAPES_H__
#define __SVG_SHAPES_H__
/**
* @note This file is part of Empirical, https://github.com/devosoft/Empirical
* @copyright Copyright (C) Michigan State University, MIT Software license; see doc/LICENSE.md
* @date 2016-2018
*
* @file svg_shapes.h
* @brief Tools to build common SVG shapes.
*/

#ifndef EMP_D3_SVG_SHAPES_H
#define EMP_D3_SVG_SHAPES_H

#include "d3_init.h"
#include "selection.h"
Expand Down Expand Up @@ -28,7 +37,7 @@ namespace D3 {
/// Assumes [data] is an array of 2-element arrays describing (x,y) coordinates and makes
/// the line that connects them
template <typename T, size_t SIZE>
std::string Generate(std::array<std::array<T, 2>, SIZE> & data){
std::string Generate(emp::array<emp::array<T, 2>, SIZE> & data){
emp::pass_array_to_javascript(data);

char * buffer = (char *)EM_ASM_INT({
Expand All @@ -46,7 +55,7 @@ namespace D3 {
/// Draws the path associated with [data] onto the [s] selection (must contain a single SVG)
/// element).
template <typename T, std::size_t SIZE>
Selection DrawShape(std::array<std::array<T, 2>, SIZE> & data, Selection & s) {
Selection DrawShape(emp::array<emp::array<T, 2>, SIZE> & data, Selection & s) {
Selection path = s.Append("path");
path.SetAttr("d", Generate(data));
return path;
Expand All @@ -65,7 +74,7 @@ namespace D3 {

/// If you pass a triple-nested array, it will be treated as an array of paths
template <typename T, std::size_t SIZE, std::size_t SIZE2>
Selection DrawShape(std::array<std::array<std::array<T, 2>, SIZE>,\
Selection DrawShape(emp::array<emp::array<emp::array<T, 2>, SIZE>,\
SIZE2> & data) {
Selection group = Select("svg").Append("g");
for (auto arr: data) {
Expand Down Expand Up @@ -229,7 +238,7 @@ namespace D3 {
/// As an example, the default function expects data like this (array of arrays):
/// [[0,0], [1,1], [2,2]]
/// And has (a Javascript equivalent of) this accessor:
/// int x(std::array<int, 2> d) {return d[0];}
/// int x(emp::array<int, 2> d) {return d[0];}
///
/// If your data instead looked like this (array of Javascript objects with x and y values):
/// [{x:0, y:0}, {x:1, y:1}, {x:2, y:2}]
Expand Down Expand Up @@ -276,7 +285,7 @@ namespace D3 {
/// As an example, the default function expects data like this (array of arrays):
/// [[0,0], [1,1], [2,2]]
/// And has (a Javascript equivalent of) this accessor:
/// int x(std::array<int, 2> d) {return d[1];}
/// int x(emp::array<int, 2> d) {return d[1];}
///
/// If your data instead looked like this (array of Javascript objects with x and y values):
/// [{x:0, y:0}, {x:1, y:1}, {x:2, y:2}]
Expand Down
Loading

0 comments on commit 5115b40

Please sign in to comment.