<commit_before>#include <cstdio>
#include <cstdlib>

#include <MultiRegions/ExpList.h>
#include <MultiRegions/ExpList1D.h>
#include <MultiRegions/ExpList2D.h>
#include <MultiRegions/ExpList3D.h>

using namespace Nektar;

int main(int argc, char *argv[])
{
    if(argc != 2)
    {
        cerr << "Usage: XmlToVtk  meshfile" << endl;
        exit(1);
    }

    LibUtilities::SessionReaderSharedPtr vSession
        = LibUtilities::SessionReader::CreateInstance(argc, argv);

    //----------------------------------------------
    // Read in mesh from input file
    string meshfile(argv[argc-1]);
    SpatialDomains::MeshGraphSharedPtr graphShPt =
        SpatialDomains::MeshGraph::Read(meshfile);
    //----------------------------------------------

    //----------------------------------------------
    // Set up Expansion information
    SpatialDomains::ExpansionMap emap = graphShPt->GetExpansions();
    SpatialDomains::ExpansionMapIter it;

    for (it = emap.begin(); it != emap.end(); ++it)
    {
        for (int i = 0; i < it->second->m_basisKeyVector.size(); ++i)
        {
            LibUtilities::BasisKey  tmp1 = it->second->m_basisKeyVector[i];
            LibUtilities::PointsKey tmp2 = tmp1.GetPointsKey();
            it->second->m_basisKeyVector[i] = LibUtilities::BasisKey(
                tmp1.GetBasisType(), tmp1.GetNumModes(),
                LibUtilities::PointsKey(tmp2.GetNumPoints(),
                                        LibUtilities::eGaussLobattoLegendre));
        }
    }
    //----------------------------------------------

    //----------------------------------------------
    // Define Expansion
    int expdim  = graphShPt->GetMeshDimension();
    Array<OneD, MultiRegions::ExpListSharedPtr> Exp(1);

    switch(expdim)
    {
        case 1:
        {
            MultiRegions::ExpList1DSharedPtr Exp1D;
            Exp1D = MemoryManager<MultiRegions::ExpList1D>
                ::AllocateSharedPtr(vSession,graphShPt);
            Exp[0] = Exp1D;
            break;
        }
        case 2:
        {
            MultiRegions::ExpList2DSharedPtr Exp2D;
            Exp2D = MemoryManager<MultiRegions::ExpList2D>
                ::AllocateSharedPtr(vSession,graphShPt);
            Exp[0] =  Exp2D;
            break;
        }
        case 3:
        {
            MultiRegions::ExpList3DSharedPtr Exp3D;
            Exp3D = MemoryManager<MultiRegions::ExpList3D>
                ::AllocateSharedPtr(vSession,graphShPt);
            Exp[0] =  Exp3D;
            break;
        }
        default:
        {
            ASSERTL0(false,"Expansion dimension not recognised");
            break;
        }
    }
    //----------------------------------------------

    //----------------------------------------------
    // Write out VTK file.
    string   outname(strtok(argv[argc-1],"."));
    outname += ".vtu";
    ofstream outfile(outname.c_str());

    Exp[0]->WriteVtkHeader(outfile);
    // For each field write header and footer, since there is no field data.
    for(int i = 0; i < Exp[0]->GetExpSize(); ++i)
    {
        Exp[0]->WriteVtkPieceHeader(outfile,i);
        Exp[0]->WriteVtkPieceFooter(outfile,i);
    }
    Exp[0]->WriteVtkFooter(outfile);
    //----------------------------------------------

    return 0;
}

<commit_msg>Changed XmlToVtk points distribution to PolyEvenlySpaced, same number of quadrature points in each spatial direction.<commit_after>#include <cstdio>
#include <cstdlib>

#include <MultiRegions/ExpList.h>
#include <MultiRegions/ExpList1D.h>
#include <MultiRegions/ExpList2D.h>
#include <MultiRegions/ExpList3D.h>

using namespace Nektar;

int main(int argc, char *argv[])
{
    if(argc != 2)
    {
        cerr << "Usage: XmlToVtk  meshfile" << endl;
        exit(1);
    }

    LibUtilities::SessionReaderSharedPtr vSession
        = LibUtilities::SessionReader::CreateInstance(argc, argv);

    //----------------------------------------------
    // Read in mesh from input file
    string meshfile(argv[argc-1]);
    SpatialDomains::MeshGraphSharedPtr graphShPt =
        SpatialDomains::MeshGraph::Read(meshfile);
    //----------------------------------------------

    //----------------------------------------------
    // Set up Expansion information
    SpatialDomains::ExpansionMap emap = graphShPt->GetExpansions();
    SpatialDomains::ExpansionMapIter it;

    for (it = emap.begin(); it != emap.end(); ++it)
    {
        for (int i = 0; i < it->second->m_basisKeyVector.size(); ++i)
        {
            LibUtilities::BasisKey  tmp1 = it->second->m_basisKeyVector[i];
            LibUtilities::PointsKey tmp2 = tmp1.GetPointsKey();
            it->second->m_basisKeyVector[i] = LibUtilities::BasisKey(
                tmp1.GetBasisType(), tmp1.GetNumModes(),
                LibUtilities::PointsKey(tmp1.GetNumModes(),
                                        LibUtilities::ePolyEvenlySpaced));
        }
    }
    //----------------------------------------------

    //----------------------------------------------
    // Define Expansion
    int expdim  = graphShPt->GetMeshDimension();
    Array<OneD, MultiRegions::ExpListSharedPtr> Exp(1);

    switch(expdim)
    {
        case 1:
        {
            MultiRegions::ExpList1DSharedPtr Exp1D;
            Exp1D = MemoryManager<MultiRegions::ExpList1D>
                ::AllocateSharedPtr(vSession,graphShPt);
            Exp[0] = Exp1D;
            break;
        }
        case 2:
        {
            MultiRegions::ExpList2DSharedPtr Exp2D;
            Exp2D = MemoryManager<MultiRegions::ExpList2D>
                ::AllocateSharedPtr(vSession,graphShPt);
            Exp[0] =  Exp2D;
            break;
        }
        case 3:
        {
            MultiRegions::ExpList3DSharedPtr Exp3D;
            Exp3D = MemoryManager<MultiRegions::ExpList3D>
                ::AllocateSharedPtr(vSession,graphShPt);
            Exp[0] =  Exp3D;
            break;
        }
        default:
        {
            ASSERTL0(false,"Expansion dimension not recognised");
            break;
        }
    }
    //----------------------------------------------

    //----------------------------------------------
    // Write out VTK file.
    string   outname(strtok(argv[argc-1],"."));
    outname += ".vtu";
    ofstream outfile(outname.c_str());

    Exp[0]->WriteVtkHeader(outfile);
    // For each field write header and footer, since there is no field data.
    for(int i = 0; i < Exp[0]->GetExpSize(); ++i)
    {
        Exp[0]->WriteVtkPieceHeader(outfile,i);
        Exp[0]->WriteVtkPieceFooter(outfile,i);
    }
    Exp[0]->WriteVtkFooter(outfile);
    //----------------------------------------------

    return 0;
}

<|endoftext|>


<commit_before>#include <SFML/Graphics.hpp>
#include "grid.h"
#include "cell.h"
#include <math.h>
#include <iostream>

using namespace sf;

int main()
{
	//Calling grid here
	//this will be 1027 / 32 which is defend in grid.cpp
	int gridWidth = grid::windowWidth / grid::x;
	//this will be 720 / 32 which also defend in grid.cpp
	int gridHeight = grid::windowHeight / grid::y;
	//Calling cell setup
	cell::Setup(gridWidth, gridHeight);
	int drawingCells[32][32];
	//starting with the window
	sf::RenderWindow window(sf::VideoMode(grid::windowWidth, grid::windowHeight), "Welcome to Mohanad's Game Of Life");
	bool Gamestart;

	while (window.isOpen())
	{
		sf::Event event;
		while (window.pollEvent(event))
		{
			if (event.type == sf::Event::Closed)
				window.close();
			if (event.type == Event::KeyPressed) {
				switch (event.key.code) {
				case Keyboard::Escape:
					printf("Bye Bye and see you next time \n");
					exit(0);

				}
			}
			if (event.type == sf::Event::MouseButtonPressed) {

				int clickX = (event.mouseButton.x / grid::x);
				int clickY = (event.mouseButton.y / grid::y);
				drawingCells[clickY][clickX] = drawingCells[clickY][clickX] == 1 ? 0.5 : 1;

				//checking is the mouse clicked or not
				printf("mouse clicked \n");
			}
			//this will run the game
			if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::R)
			{

				if (Gamestart = true) {
					for (size_t row = 0; row < gridHeight; row++)
					{
						int count = 0;
						if (drawingCells[32 - 1][32 - 1]) {
							++count;
						}
						if (drawingCells[32 - 1][32 - 1]) {
							++count;
						}
						if (drawingCells[32 + 1][32 - 1]) {
							++count;
						}
						if (drawingCells[32 + 1][32]) {
							++count;
						}
						if (drawingCells[32 + 1][32 + 1]) {
							++count;
						}
						if (drawingCells[32][32 + 1]) {
							++count;
						}
						if (drawingCells[32 - 1][32 + 1]) {
							++count;
						}
						if (drawingCells[32 - 1][32]) {
							++count;
						}
						else {
							(Gamestart = false);

						}

						//	grid::logicOfCurrentGeneration(drawingCells);
						printf("Game has start enjoy \n");
					}

					window.clear();

					for (size_t row = 0; row < grid::x; row++)
					{
						for (size_t column = 0; column < grid::y; column++)
						{
							int state = drawingCells[row][column];
							if (state == 1) {
								cell::Setcolor(Color::White);
							}
							else if (state == 0) {
								cell::Setcolor(Color::Blue);

							}
							cell::Setposition((column * gridWidth), (row * gridHeight));
							window.draw(cell::target);

						}

					}
					window.display();

				}

			}

		}
	}
}<commit_msg>Commit<commit_after>#include <SFML/Graphics.hpp>
#include "grid.h"
#include "cell.h"
#include <math.h>
#include <iostream>

using namespace sf;

int main()
{
	//Calling grid here
	//this will be 1027 / 32 which is defend in grid.cpp
	int gridWidth = grid::windowWidth / grid::x;
	//this will be 720 / 32 which also defend in grid.cpp
	int gridHeight = grid::windowHeight / grid::y;
	//Calling cell setup
	cell::Setup(gridWidth, gridHeight);
	int drawingCells[32][32];
	//starting with the window
	sf::RenderWindow window(sf::VideoMode(grid::windowWidth, grid::windowHeight), "Welcome to Mohanad's Game Of Life");
	bool Gamestart;

	while (window.isOpen())
	{
		sf::Event event;
		while (window.pollEvent(event))
		{
			if (event.type == sf::Event::Closed)
				window.close();
			if (event.type == Event::KeyPressed) {
				switch (event.key.code) {
				case Keyboard::Escape:
					printf("Bye Bye and see you next time \n");
					exit(0);

				}
			}
			if (event.type == sf::Event::MouseButtonPressed) {

				int clickX = (event.mouseButton.x / grid::x);
				int clickY = (event.mouseButton.y / grid::y);
				drawingCells[clickY][clickX] = drawingCells[clickY][clickX] == 1 ? 0.5 : 1;

				//checking is the mouse clicked or not
				printf("mouse clicked \n");
			}
			//this will run the game
			if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::R)
			{

				if (Gamestart = true) {
					for (size_t row = 0; row < gridHeight; row++)
					{
						int count = 0;
						if (drawingCells[32 - 1][32 - 1]) {
							++count;
						}
						if (drawingCells[32 - 1][32 - 1]) {
							++count;
						}
						if (drawingCells[32 + 1][32 - 1]) {
							++count;
						}
						if (drawingCells[32 + 1][32]) {
							++count;
						}
						if (drawingCells[32 + 1][32 + 1]) {
							++count;
						}
						if (drawingCells[32][32 + 1]) {
							++count;
						}
						if (drawingCells[32 - 1][32 + 1]) {
							++count;
						}
						if (drawingCells[32 - 1][32]) {
							++count;
						}
						else {
							(Gamestart = false);

						}

						printf("Start \n");
					}

					window.clear();

					for (size_t row = 0; row < grid::x; row++)
					{
						for (size_t column = 0; column < grid::y; column++)
						{
							int state = drawingCells[row][column];
							if (state == 1) {
								cell::Setcolor(Color::White);
							}
							else if (state == 0) {
								cell::Setcolor(Color::Blue);

							}
							cell::Setposition((column * gridWidth), (row * gridHeight));
							window.draw(cell::target);

						}

					}
					window.display();

				}

			}

		}
	}
}<|endoftext|>


<commit_before>/**
 * @file   nullable_attribute.cc
 *
 * @section LICENSE
 *
 * The MIT License
 *
 * @copyright Copyright (c) 2020-2021 TileDB, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * @section DESCRIPTION
 *
 * When run, this program will create a simple 2D dense array with one fixed
 * nullable attribute and one var-sized nullable attribute, write some data
 * to it, and read the data back on both attributes.
 */

#include <iostream>
#include <tiledb/tiledb>

using namespace tiledb;

// Name of array.
std::string array_name("nullable_attributes_array");

void create_array() {
  // Create a TileDB context
  Context ctx;

  // The array will be 2x2 with dimensions "rows" and "cols", with domain [1,2]
  Domain domain(ctx);
  domain.add_dimension(Dimension::create<int>(ctx, "rows", {{1, 2}}, 2))
      .add_dimension(Dimension::create<int>(ctx, "cols", {{1, 2}}, 2));

  // The array will be dense
  ArraySchema schema(ctx, TILEDB_DENSE);
  schema.set_domain(domain).set_order({{TILEDB_ROW_MAJOR, TILEDB_ROW_MAJOR}});

  // Create two attributes "a1" and "a2", the first fixed and the second
  // variable-sized.
  Attribute a1 = Attribute::create<int>(ctx, "a1");
  Attribute a2 = Attribute::create<std::vector<int>>(ctx, "a2");

  // Set both attributes as nullable
  a1.set_nullable(true);
  a2.set_nullable(true);

  schema.add_attribute(a1);
  schema.add_attribute(a2);

  // Create the (empty) array on disk.
  Array::create(array_name, schema);
}

void write_array() {
  Context ctx;

  // Prepare some data for the array
  std::vector<int> a1_data = {100, 200, 300, 400};
  std::vector<int> a2_data = {10, 10, 20, 30, 30, 30, 40, 40};
  std::vector<uint64_t> a2_el_off = {0, 2, 3, 6};
  std::vector<uint64_t> a2_off;
  for (auto e : a2_el_off)
    a2_off.push_back(e * sizeof(int));

  // Open the array for writing and create the query
  Array array(ctx, array_name, TILEDB_WRITE);
  Query query(ctx, array);
  query.set_layout(TILEDB_ROW_MAJOR);

  // Specify the validity buffer for each attribute
  std::vector<uint8_t> a1_validity_buf = {1, 0, 0, 1};
  std::vector<uint8_t> a2_validity_buf = {0, 1, 1, 0};

  // Set the query buffers specifying the validity for each data
  query.set_buffer_nullable("a1", a1_data, a1_validity_buf)
      .set_buffer_nullable("a2", a2_off, a2_data, a2_validity_buf);

  // Perform the write and close the array.
  query.submit();
  array.close();
}

void read_array() {
  Context ctx;

  // Prepare the array for reading
  Array array(ctx, array_name, TILEDB_READ);

  // Prepare the vectors that will hold the results
  std::vector<int> a1_data(4);
  std::vector<uint8_t> a1_validity_buf(a1_data.size());

  std::vector<int> a2_data(8);
  std::vector<uint64_t> a2_off(4);
  std::vector<uint8_t> a2_validity_buf(a2_off.size());

  // Prepare and submit the query, and close the array
  Query query(ctx, array);
  query.set_layout(TILEDB_ROW_MAJOR);

  // Read the full array
  const std::vector<int> subarray_full = {1, 2, 1, 2};
  query.set_subarray(subarray_full);

  // Set the query buffers specifying the validity for each data
  query.set_buffer_nullable("a1", a1_data, a1_validity_buf)
      .set_buffer_nullable("a2", a2_off, a2_data, a2_validity_buf);
  query.submit();
  array.close();

  // Print out the data we read for each nullable atttribute
  unsigned long i = 0;
  std::cout << "a1: " << std::endl;
  for (i = 0; i < 4; ++i) {
    std::cout << (a1_validity_buf[i] > 0 ? std::to_string(a1_data[i]) : "NULL");
    std::cout << " ";
  }
  std::cout << std::endl;

  std::cout << "a2: " << std::endl;
  for (i = 0; i < 4; ++i) {
    if (a2_validity_buf[i] > 0) {
      std::cout << "{ ";
      std::cout << std::to_string(a2_data[i * 2]);
      std::cout << ", ";
      std::cout << std::to_string(a2_data[i * 2 + 1]);
      std::cout << " }";
    } else {
      std::cout << "{ NULL }";
    }
    std::cout << " ";
  }
  std::cout << std::endl;
}

int main() {
  Context ctx;
  if (Object::object(ctx, array_name).type() == Object::Type::Array) {
    tiledb::Object::remove(ctx, array_name);
  }
  create_array();
  write_array();
  read_array();

  return 0;
}
<commit_msg>Add nullable string to nullable attribute example<commit_after>/**
 * @file   nullable_attribute.cc
 *
 * @section LICENSE
 *
 * The MIT License
 *
 * @copyright Copyright (c) 2020-2021 TileDB, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * @section DESCRIPTION
 *
 * When run, this program will create a simple 2D dense array with one fixed
 * nullable attribute and one var-sized nullable attribute, write some data
 * to it, and read the data back on both attributes.
 */

#include <iostream>
#include <tiledb/tiledb>

using namespace tiledb;

// Name of array.
std::string array_name("nullable_attributes_array");

void create_array() {
  // Create a TileDB context
  Context ctx;

  // The array will be 2x2 with dimensions "rows" and "cols", with domain [1,2]
  Domain domain(ctx);
  domain.add_dimension(Dimension::create<int>(ctx, "rows", {{1, 2}}, 2))
      .add_dimension(Dimension::create<int>(ctx, "cols", {{1, 2}}, 2));

  // The array will be dense
  ArraySchema schema(ctx, TILEDB_DENSE);
  schema.set_domain(domain).set_order({{TILEDB_ROW_MAJOR, TILEDB_ROW_MAJOR}});

  // Create two attributes "a1" and "a2", the first fixed and the second
  // variable-sized.
  Attribute a1 = Attribute::create<int>(ctx, "a1");
  Attribute a2 = Attribute::create<std::vector<int>>(ctx, "a2");
  auto a3 = Attribute(ctx, "a3", TILEDB_STRING_UTF8);
  a3.set_cell_val_num(TILEDB_VAR_NUM);

  // Set both attributes as nullable
  a1.set_nullable(true);
  a2.set_nullable(true);
  a3.set_nullable(true);

  schema.add_attribute(a1);
  schema.add_attribute(a2);
  schema.add_attribute(a3);

  // Create the (empty) array on disk.
  Array::create(array_name, schema);
}

void write_array() {
  Context ctx;

  // Prepare some data for the array
  std::vector<int> a1_data = {100, 200, 300, 400};
  std::vector<int> a2_data = {10, 10, 20, 30, 30, 30, 40, 40};
  std::vector<uint64_t> a2_el_off = {0, 2, 3, 6};
  std::vector<uint64_t> a2_off;
  for (auto e : a2_el_off)
    a2_off.push_back(e * sizeof(int));

  const char a3_data_char[] = "abcdewxyz";
  std::vector<char> a3_data(a3_data_char, a3_data_char + 9);
  std::vector<uint64_t> a3_el_off = {0, 3, 4, 5};
  std::vector<uint64_t> a3_off;
  for (auto e : a3_el_off)
    a3_off.push_back(e * sizeof(char));

  // Open the array for writing and create the query
  Array array(ctx, array_name, TILEDB_WRITE);
  Query query(ctx, array);
  query.set_layout(TILEDB_ROW_MAJOR);

  // Specify the validity buffer for each attribute
  std::vector<uint8_t> a1_validity_buf = {1, 0, 0, 1};
  std::vector<uint8_t> a2_validity_buf = {0, 1, 1, 0};
  std::vector<uint8_t> a3_validity_buf = {1, 0, 0, 1};

  // Set the query buffers specifying the validity for each data
  query.set_buffer_nullable("a1", a1_data, a1_validity_buf)
      .set_buffer_nullable("a2", a2_off, a2_data, a2_validity_buf)
      .set_buffer_nullable("a3", a3_off, a3_data, a3_validity_buf);

  // Perform the write and close the array.
  query.submit();
  array.close();
}

void read_array() {
  Context ctx;

  // Prepare the array for reading
  Array array(ctx, array_name, TILEDB_READ);

  // Prepare the vectors that will hold the results
  std::vector<int> a1_data(4);
  std::vector<uint8_t> a1_validity_buf(a1_data.size());

  std::vector<int> a2_data(8);
  std::vector<uint64_t> a2_off(4);
  std::vector<uint8_t> a2_validity_buf(a2_off.size());

  std::vector<char> a3_data(1000);
  std::vector<uint64_t> a3_off(10);
  std::vector<uint8_t> a3_validity_buf(a3_off.size());

  // Prepare and submit the query, and close the array
  Query query(ctx, array);
  query.set_layout(TILEDB_ROW_MAJOR);

  // Read the full array
  const std::vector<int> subarray_full = {1, 2, 1, 2};
  query.set_subarray(subarray_full);

  // Set the query buffers specifying the validity for each data
  query.set_buffer_nullable("a1", a1_data, a1_validity_buf)
      .set_buffer_nullable("a2", a2_off, a2_data, a2_validity_buf)
      .set_buffer_nullable("a3", a3_off, a3_data, a3_validity_buf);
  query.submit();

  if (query.query_status() == tiledb::Query::Status::INCOMPLETE) {
    std::cerr << "** Query did not complete! **" << std::endl;
  }

  auto result_elements = query.result_buffer_elements();
  array.close();

  // Unpack a3 result to a vector of strings
  std::vector<std::string> a3_results;
  auto a3_result_elements = result_elements["a3"];
  uint64_t a3_offsets_num = a3_result_elements.first;
  uint64_t a3_data_num = a3_result_elements.second;

  uint64_t start = 0;
  uint64_t end = 0;

  for (size_t i = 0; i < a3_offsets_num; i++) {
    start = a3_off[i];
    end = (i == a3_offsets_num - 1) ? a3_data_num : a3_off[i + 1];
    a3_results.push_back(
        std::string(a3_data.data() + start, a3_data.data() + end));
  }

  // Print out the data we read for each nullable atttribute
  unsigned long i = 0;
  std::cout << "a1: " << std::endl;
  for (i = 0; i < 4; ++i) {
    std::cout << (a1_validity_buf[i] > 0 ? std::to_string(a1_data[i]) : "NULL");
    std::cout << " ";
  }
  std::cout << std::endl;

  std::cout << "a2: " << std::endl;
  for (i = 0; i < 4; ++i) {
    if (a2_validity_buf[i] > 0) {
      std::cout << "{ ";
      std::cout << std::to_string(a2_data[i * 2]);
      std::cout << ", ";
      std::cout << std::to_string(a2_data[i * 2 + 1]);
      std::cout << " }";
    } else {
      std::cout << "{ NULL }";
    }
    std::cout << " ";
  }

  std::cout << std::endl << "a3: " << std::endl;
  for (i = 0; i < 4; ++i) {
    if (a3_validity_buf[i] > 0)
      std::cout << "  " << a3_results[i] << std::endl;
    else
      std::cout << "  "
                << "NULL" << std::endl;
  }

  std::cout << std::endl;
}

int main() {
  Context ctx;
  if (Object::object(ctx, array_name).type() == Object::Type::Array) {
    tiledb::Object::remove(ctx, array_name);
  }
  create_array();
  write_array();
  read_array();

  return 0;
}
<|endoftext|>


<commit_before>
#include "UnionLowestLevelSimpleScanOperator.h"

namespace srch2 {
namespace instantsearch {

////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////// merge when lists are sorted by ID Only top K////////////////////////////

UnionLowestLevelSimpleScanOperator::UnionLowestLevelSimpleScanOperator() {
	queryEvaluator = NULL;
}

UnionLowestLevelSimpleScanOperator::~UnionLowestLevelSimpleScanOperator(){
	//TODO
}
bool UnionLowestLevelSimpleScanOperator::open(QueryEvaluatorInternal * queryEvaluator, PhysicalPlanExecutionParameters & params){
	// first save the pointer to QueryEvaluator
	this->queryEvaluator = queryEvaluator;

	// 1. get the pointer to logical plan node
	LogicalPlanNode * logicalPlanNode = this->getPhysicalPlanOptimizationNode()->getLogicalPlanNode();
	// 2. Get the Term object
	Term * term = NULL;
	if(params.isFuzzy){
		term = logicalPlanNode->fuzzyTerm;
	}else{
		term = logicalPlanNode->exactTerm;
	}
	// 3. Get the ActiveNodeSet from the logical plan
	PrefixActiveNodeSet * activeNodeSet = logicalPlanNode->stats->getActiveNodeSetForEstimation(params.isFuzzy);

	// 4. Create the iterator and save it as a member of the class for future calls to getNext
	if (term->getTermType() == TERM_TYPE_PREFIX) { // prefix term
        for (LeafNodeSetIterator iter (activeNodeSet, term->getThreshold()); !iter.isDone(); iter.next()) {
            TrieNodePointer leafNode;
            TrieNodePointer prefixNode;
            unsigned distance;
            iter.getItem(prefixNode, leafNode, distance);
            // get inverted list pointer and save it
            shared_ptr<vectorview<unsigned> > invertedListReadView;
            this->queryEvaluator->getInvertedIndex()->
            		getInvertedListReadView(leafNode->getInvertedListOffset() , invertedListReadView);
            this->invertedLists.push_back(invertedListReadView);
            this->invertedListPrefixes.push_back(prefixNode);
            this->invertedListLeafNodes.push_back(leafNode);
            this->invertedListDistances.push_back(distance);
            this->invertedListIDs.push_back(leafNode->getInvertedListOffset());
        }
	}else{ // complete term
        for (ActiveNodeSetIterator iter(activeNodeSet, term->getThreshold()); !iter.isDone(); iter.next()) {
            TrieNodePointer trieNode;
            unsigned distance;
            iter.getItem(trieNode, distance);
            distance = activeNodeSet->getEditdistanceofPrefix(trieNode);
            depthInitializeSimpleScanOperator(trieNode, trieNode, distance, term->getThreshold());
        }
	}
	this->invertedListOffset = 0;
	this->cursorOnInvertedList = 0;

}
PhysicalPlanRecordItem * UnionLowestLevelSimpleScanOperator::getNext(const PhysicalPlanExecutionParameters & params) {

	if(this->invertedListOffset >= this->invertedLists.size()){
		return NULL;
	}
	// we dont have any list with size zero
	ASSERT(this->cursorOnInvertedList < this->invertedLists.at(this->invertedListOffset)->size());

	// 1. get the pointer to logical plan node
	LogicalPlanNode * logicalPlanNode = this->getPhysicalPlanOptimizationNode()->getLogicalPlanNode();
	// 2. Get the Term object
	Term * term = NULL;
	if(params.isFuzzy){
		term = logicalPlanNode->fuzzyTerm;
	}else{
		term = logicalPlanNode->exactTerm;
	}

	// find the next record and check the its validity
	unsigned recordID = this->invertedLists.at(this->invertedListOffset)->at(this->cursorOnInvertedList);

	unsigned recordOffset =
			this->queryEvaluator->getInvertedIndex()->getKeywordOffset(recordID, this->invertedListIDs.at(this->invertedListOffset));

    bool foundValidHit = 0;
    float termRecordStaticScore = 0;
    unsigned termAttributeBitmap = 0;
    while (1) {
        if (this->queryEvaluator->getInvertedIndex()->isValidTermPositionHit(recordID, recordOffset,
                term->getAttributeToFilterTermHits(), termAttributeBitmap,
                termRecordStaticScore) ) {
            foundValidHit = 1;
            break;
        }
        this->cursorOnInvertedList ++;
        if (this->cursorOnInvertedList < this->invertedLists.at(this->invertedListOffset)->size()) {
        	recordID = this->invertedLists.at(this->invertedListOffset)->at(this->cursorOnInvertedList);
            // calculate record offset online
        	recordOffset =
        				this->queryEvaluator->getInvertedIndex()->getKeywordOffset(recordID, this->invertedListIDs.at(this->invertedListOffset));
        } else {
        	this->invertedListOffset ++;
			this->cursorOnInvertedList = 0;
        	if(this->invertedListOffset < this->invertedLists.size()){
        		recordID = this->invertedLists.at(this->invertedListOffset)->at(this->cursorOnInvertedList);
				// calculate record offset online
				recordOffset =
							this->queryEvaluator->getInvertedIndex()->getKeywordOffset(recordID, this->invertedListIDs.at(this->invertedListOffset));
        	}else{
        		return NULL;
        	}
        }
    }

    if(foundValidHit == 0){
    	return NULL;
    }

	// return the item.
	PhysicalPlanRecordItem * newItem = this->queryEvaluator->getPhysicalPlanRecordItemFactory()->createRecordItem();
	// record id
	newItem->setRecordId(recordID);
	// edit distance
	vector<unsigned> editDistances;
	editDistances.push_back(this->invertedListDistances.at(this->cursorOnInvertedList));
	newItem->setRecordMatchEditDistances(editDistances);
	// matching prefix
	vector<TrieNodePointer> matchingPrefixes;
	matchingPrefixes.push_back(this->invertedListLeafNodes.at(this->cursorOnInvertedList)); // TODO this might be wrong
	newItem->setRecordMatchingPrefixes(matchingPrefixes);
	// runtime score
	bool isPrefixMatch = this->invertedListPrefixes.at(this->invertedListOffset) ==
			this->invertedListLeafNodes.at(this->invertedListOffset);
	////// TODO ???????????????????????????? RANKER
	newItem->setRecordRuntimeScore(	DefaultTopKRanker::computeTermRecordRuntimeScore(termRecordStaticScore,
            this->invertedListDistances.at(this->invertedListOffset),
            term->getKeyword()->size(),
            isPrefixMatch,
            params.prefixMatchPenalty , term->getSimilarityBoost()));
	// static score
	newItem->setRecordStaticScore(termRecordStaticScore);
	// attributeBitmap
	vector<unsigned> attributeBitmaps;
	attributeBitmaps.push_back(termAttributeBitmap);
	newItem->setRecordMatchAttributeBitmaps(attributeBitmaps);
	// !!!! runtime score is not set here !!!! (we don't have it here and we don't need it here)

	// prepare for next call
	this->cursorOnInvertedList ++;
	if(this->cursorOnInvertedList > this->invertedLists.at(this->invertedListOffset)->size()){
		this->invertedListOffset ++;
		this->cursorOnInvertedList = 0;
	}

	return newItem;
}
bool UnionLowestLevelSimpleScanOperator::close(PhysicalPlanExecutionParameters & params){

	this->invertedLists.clear();
	this->invertedListDistances.clear();
	this->invertedListLeafNodes.clear();
	this->invertedListPrefixes.clear();
	this->invertedListIDs.clear();
	this->cursorOnInvertedList = 0;
	this->invertedListOffset = 0;
	queryEvaluator = NULL;

}
bool UnionLowestLevelSimpleScanOperator::verifyByRandomAccess(PhysicalPlanRandomAccessVerificationParameters & parameters) {
	//TODO
}

void UnionLowestLevelSimpleScanOperator::depthInitializeSimpleScanOperator(
		const TrieNode* trieNode, const TrieNode* prefixNode, unsigned distance, unsigned bound){
    if (trieNode->isTerminalNode())
        // get inverted list pointer and save it
        shared_ptr<vectorview<unsigned> > invertedListReadView;
        this->queryEvaluator->getInvertedIndex()->
        		getInvertedListReadView(trieNode->getInvertedListOffset() , invertedListReadView);
        this->invertedLists.push_back(invertedListReadView);
        this->invertedListPrefixes.push_back(prefixNode);
        this->invertedListLeafNodes.push_back(trieNode);
        this->invertedListDistances.push_back(distance);
        this->invertedListIDs.push_back(trieNode->getInvertedListOffset() );
    if (distance < bound) {
        for (unsigned int childIterator = 0; childIterator < trieNode->getChildrenCount(); childIterator++) {
            const TrieNode *child = trieNode->getChild(childIterator);
            depthInitializeSimpleScanOperator(child, trieNode, distance+1, bound);
        }
    }
}

// The cost of open of a child is considered only once in the cost computation
// of parent open function.
unsigned UnionLowestLevelSimpleScanOptimizationOperator::getCostOfOpen(const PhysicalPlanExecutionParameters & params){
	//TODO
}
// The cost of getNext of a child is multiplied by the estimated number of calls to this function
// when the cost of parent is being calculated.
unsigned UnionLowestLevelSimpleScanOptimizationOperator::getCostOfGetNext(const PhysicalPlanExecutionParameters & params) {
	//TODO
}
// the cost of close of a child is only considered once since each node's close function is only called once.
unsigned UnionLowestLevelSimpleScanOptimizationOperator::getCostOfClose(const PhysicalPlanExecutionParameters & params) {
	//TODO
}
void UnionLowestLevelSimpleScanOptimizationOperator::getOutputProperties(IteratorProperties & prop){
	// no output property
}
void UnionLowestLevelSimpleScanOptimizationOperator::getRequiredInputProperties(IteratorProperties & prop){
	// the only requirement for input is to be directly connected to inverted index,
	// so since no operator outputs PhysicalPlanIteratorProperty_LowestLevel TVL will be pushed down to lowest level
	prop.addProperty(PhysicalPlanIteratorProperty_LowestLevel);
}
PhysicalPlanNodeType UnionLowestLevelSimpleScanOptimizationOperator::getType() {
	return PhysicalPlanNode_UnionLowestLevelSimpleScanOperator;
}
bool UnionLowestLevelSimpleScanOptimizationOperator::validateChildren(){
	if(getChildrenCount() > 0){ // this operator cannot have any children
		return false;
	}
	return true;
}

}
}
<commit_msg>Simple Scan operator.<commit_after>
#include "UnionLowestLevelSimpleScanOperator.h"
#include "operation/QueryEvaluatorInternal.h"

namespace srch2 {
namespace instantsearch {

////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////// merge when lists are sorted by ID Only top K////////////////////////////

UnionLowestLevelSimpleScanOperator::UnionLowestLevelSimpleScanOperator() {
	queryEvaluator = NULL;
}

UnionLowestLevelSimpleScanOperator::~UnionLowestLevelSimpleScanOperator(){
	//TODO
}
bool UnionLowestLevelSimpleScanOperator::open(QueryEvaluatorInternal * queryEvaluator, PhysicalPlanExecutionParameters & params){
	// first save the pointer to QueryEvaluator
	this->queryEvaluator = queryEvaluator;

	// 1. get the pointer to logical plan node
	LogicalPlanNode * logicalPlanNode = this->getPhysicalPlanOptimizationNode()->getLogicalPlanNode();
	// 2. Get the Term object
	Term * term = NULL;
	if(params.isFuzzy){
		term = logicalPlanNode->fuzzyTerm;
	}else{
		term = logicalPlanNode->exactTerm;
	}
	// 3. Get the ActiveNodeSet from the logical plan
	PrefixActiveNodeSet * activeNodeSet = logicalPlanNode->stats->getActiveNodeSetForEstimation(params.isFuzzy);

	// 4. Create the iterator and save it as a member of the class for future calls to getNext
	if (term->getTermType() == TERM_TYPE_PREFIX) { // prefix term
        for (LeafNodeSetIterator iter (activeNodeSet, term->getThreshold()); !iter.isDone(); iter.next()) {
            TrieNodePointer leafNode;
            TrieNodePointer prefixNode;
            unsigned distance;
            iter.getItem(prefixNode, leafNode, distance);
            // get inverted list pointer and save it
            shared_ptr<vectorview<unsigned> > invertedListReadView;
            this->queryEvaluator->getInvertedIndex()->
            		getInvertedListReadView(leafNode->getInvertedListOffset() , invertedListReadView);
            this->invertedLists.push_back(invertedListReadView);
            this->invertedListPrefixes.push_back(prefixNode);
            this->invertedListLeafNodes.push_back(leafNode);
            this->invertedListDistances.push_back(distance);
            this->invertedListIDs.push_back(leafNode->getInvertedListOffset());
        }
	}else{ // complete term
        for (ActiveNodeSetIterator iter(activeNodeSet, term->getThreshold()); !iter.isDone(); iter.next()) {
            TrieNodePointer trieNode;
            unsigned distance;
            iter.getItem(trieNode, distance);
            distance = activeNodeSet->getEditdistanceofPrefix(trieNode);
            depthInitializeSimpleScanOperator(trieNode, trieNode, distance, term->getThreshold());
        }
	}
	this->invertedListOffset = 0;
	this->cursorOnInvertedList = 0;

}
PhysicalPlanRecordItem * UnionLowestLevelSimpleScanOperator::getNext(const PhysicalPlanExecutionParameters & params) {

	if(this->invertedListOffset >= this->invertedLists.size()){
		return NULL;
	}
	// we dont have any list with size zero
	ASSERT(this->cursorOnInvertedList < this->invertedLists.at(this->invertedListOffset)->size());

	// 1. get the pointer to logical plan node
	LogicalPlanNode * logicalPlanNode = this->getPhysicalPlanOptimizationNode()->getLogicalPlanNode();
	// 2. Get the Term object
	Term * term = NULL;
	if(params.isFuzzy){
		term = logicalPlanNode->fuzzyTerm;
	}else{
		term = logicalPlanNode->exactTerm;
	}

	// find the next record and check the its validity
	unsigned recordID = this->invertedLists.at(this->invertedListOffset)->at(this->cursorOnInvertedList);

	unsigned recordOffset =
			this->queryEvaluator->getInvertedIndex()->getKeywordOffset(recordID, this->invertedListIDs.at(this->invertedListOffset));

    bool foundValidHit = 0;
    float termRecordStaticScore = 0;
    unsigned termAttributeBitmap = 0;
    while (1) {
        if (this->queryEvaluator->getInvertedIndex()->isValidTermPositionHit(recordID, recordOffset,
                term->getAttributeToFilterTermHits(), termAttributeBitmap,
                termRecordStaticScore) ) {
            foundValidHit = 1;
            break;
        }
        this->cursorOnInvertedList ++;
        if (this->cursorOnInvertedList < this->invertedLists.at(this->invertedListOffset)->size()) {
        	recordID = this->invertedLists.at(this->invertedListOffset)->at(this->cursorOnInvertedList);
            // calculate record offset online
        	recordOffset =
        				this->queryEvaluator->getInvertedIndex()->getKeywordOffset(recordID, this->invertedListIDs.at(this->invertedListOffset));
        } else {
        	this->invertedListOffset ++;
			this->cursorOnInvertedList = 0;
        	if(this->invertedListOffset < this->invertedLists.size()){
        		recordID = this->invertedLists.at(this->invertedListOffset)->at(this->cursorOnInvertedList);
				// calculate record offset online
				recordOffset =
							this->queryEvaluator->getInvertedIndex()->getKeywordOffset(recordID, this->invertedListIDs.at(this->invertedListOffset));
        	}else{
        		return NULL;
        	}
        }
    }

    if(foundValidHit == 0){
    	return NULL;
    }

	// return the item.
	PhysicalPlanRecordItem * newItem = this->queryEvaluator->getPhysicalPlanRecordItemFactory()->createRecordItem();
	// record id
	newItem->setRecordId(recordID);
	// edit distance
	vector<unsigned> editDistances;
	editDistances.push_back(this->invertedListDistances.at(this->cursorOnInvertedList));
	newItem->setRecordMatchEditDistances(editDistances);
	// matching prefix
	vector<TrieNodePointer> matchingPrefixes;
	matchingPrefixes.push_back(this->invertedListLeafNodes.at(this->cursorOnInvertedList)); // TODO this might be wrong
	newItem->setRecordMatchingPrefixes(matchingPrefixes);
	// runtime score
	bool isPrefixMatch = this->invertedListPrefixes.at(this->invertedListOffset) ==
			this->invertedListLeafNodes.at(this->invertedListOffset);
	////// TODO ???????????????????????????? RANKER
	newItem->setRecordRuntimeScore(	DefaultTopKRanker::computeTermRecordRuntimeScore(termRecordStaticScore,
            this->invertedListDistances.at(this->invertedListOffset),
            term->getKeyword()->size(),
            isPrefixMatch,
            params.prefixMatchPenalty , term->getSimilarityBoost()));
	// static score
	newItem->setRecordStaticScore(termRecordStaticScore);
	// attributeBitmap
	vector<unsigned> attributeBitmaps;
	attributeBitmaps.push_back(termAttributeBitmap);
	newItem->setRecordMatchAttributeBitmaps(attributeBitmaps);
	// !!!! runtime score is not set here !!!! (we don't have it here and we don't need it here)

	// prepare for next call
	this->cursorOnInvertedList ++;
	if(this->cursorOnInvertedList > this->invertedLists.at(this->invertedListOffset)->size()){
		this->invertedListOffset ++;
		this->cursorOnInvertedList = 0;
	}

	return newItem;
}
bool UnionLowestLevelSimpleScanOperator::close(PhysicalPlanExecutionParameters & params){

	this->invertedLists.clear();
	this->invertedListDistances.clear();
	this->invertedListLeafNodes.clear();
	this->invertedListPrefixes.clear();
	this->invertedListIDs.clear();
	this->cursorOnInvertedList = 0;
	this->invertedListOffset = 0;
	queryEvaluator = NULL;

}
bool UnionLowestLevelSimpleScanOperator::verifyByRandomAccess(PhysicalPlanRandomAccessVerificationParameters & parameters) {
	  //do the verification
	PrefixActiveNodeSet *prefixActiveNodeSet =
			this->getPhysicalPlanOptimizationNode()->getLogicalPlanNode()->stats->getActiveNodeSetForEstimation(parameters.isFuzzy);

	Term * term = this->getPhysicalPlanOptimizationNode()->getLogicalPlanNode()->exactTerm;

	unsigned termSearchableAttributeIdToFilterTermHits = term->getAttributeToFilterTermHits();
	// assume the iterator returns the ActiveNodes in the increasing order based on edit distance
	for (ActiveNodeSetIterator iter(prefixActiveNodeSet, term->getThreshold());
			!iter.isDone(); iter.next()) {
		const TrieNode *trieNode;
		unsigned distance;
		iter.getItem(trieNode, distance);

		unsigned minId = trieNode->getMinId();
		unsigned maxId = trieNode->getMaxId();
		if (term->getTermType() == srch2::instantsearch::TERM_TYPE_COMPLETE) {
			if (trieNode->isTerminalNode())
				maxId = minId;
			else
				continue;  // ignore non-terminal nodes
		}

		unsigned matchingKeywordId;
		float termRecordStaticScore;
		unsigned termAttributeBitmap;
		if (this->queryEvaluator->getForwardIndex()->haveWordInRange(parameters.recordToVerify->getRecordId(), minId, maxId,
				termSearchableAttributeIdToFilterTermHits,
				matchingKeywordId, termAttributeBitmap, termRecordStaticScore)) {
			parameters.termRecordMatchingPrefixes.push_back(trieNode);
			parameters.attributeBitmaps.push_back(termAttributeBitmap);
			parameters.prefixEditDistances.push_back(distance);
			bool isPrefixMatch = ( (!trieNode->isTerminalNode()) || (minId != matchingKeywordId) );
			parameters.runTimeTermRecordScores.push_back(DefaultTopKRanker::computeTermRecordRuntimeScore(termRecordStaticScore, distance,
						term->getKeyword()->size(),
						isPrefixMatch,
						parameters.prefixMatchPenalty , term->getSimilarityBoost() ) );
			parameters.staticTermRecordScores.push_back(termRecordStaticScore);
			// parameters.positionIndexOffsets ????
			return true;
		}
	}
	return false;
}

void UnionLowestLevelSimpleScanOperator::depthInitializeSimpleScanOperator(
		const TrieNode* trieNode, const TrieNode* prefixNode, unsigned distance, unsigned bound){
    if (trieNode->isTerminalNode()){
        // get inverted list pointer and save it
        shared_ptr<vectorview<unsigned> > invertedListReadView;
        this->queryEvaluator->getInvertedIndex()->
        		getInvertedListReadView(trieNode->getInvertedListOffset() , invertedListReadView);
        this->invertedLists.push_back(invertedListReadView);
        this->invertedListPrefixes.push_back(prefixNode);
        this->invertedListLeafNodes.push_back(trieNode);
        this->invertedListDistances.push_back(distance);
        this->invertedListIDs.push_back(trieNode->getInvertedListOffset() );
    }
    if (distance < bound) {
        for (unsigned int childIterator = 0; childIterator < trieNode->getChildrenCount(); childIterator++) {
            const TrieNode *child = trieNode->getChild(childIterator);
            depthInitializeSimpleScanOperator(child, trieNode, distance+1, bound);
        }
    }
}

// The cost of open of a child is considered only once in the cost computation
// of parent open function.
unsigned UnionLowestLevelSimpleScanOptimizationOperator::getCostOfOpen(const PhysicalPlanExecutionParameters & params){
	//TODO
}
// The cost of getNext of a child is multiplied by the estimated number of calls to this function
// when the cost of parent is being calculated.
unsigned UnionLowestLevelSimpleScanOptimizationOperator::getCostOfGetNext(const PhysicalPlanExecutionParameters & params) {
	//TODO
}
// the cost of close of a child is only considered once since each node's close function is only called once.
unsigned UnionLowestLevelSimpleScanOptimizationOperator::getCostOfClose(const PhysicalPlanExecutionParameters & params) {
	//TODO
}
void UnionLowestLevelSimpleScanOptimizationOperator::getOutputProperties(IteratorProperties & prop){
	// no output property
}
void UnionLowestLevelSimpleScanOptimizationOperator::getRequiredInputProperties(IteratorProperties & prop){
	// the only requirement for input is to be directly connected to inverted index,
	// so since no operator outputs PhysicalPlanIteratorProperty_LowestLevel TVL will be pushed down to lowest level
	prop.addProperty(PhysicalPlanIteratorProperty_LowestLevel);
}
PhysicalPlanNodeType UnionLowestLevelSimpleScanOptimizationOperator::getType() {
	return PhysicalPlanNode_UnionLowestLevelSimpleScanOperator;
}
bool UnionLowestLevelSimpleScanOptimizationOperator::validateChildren(){
	if(getChildrenCount() > 0){ // this operator cannot have any children
		return false;
	}
	return true;
}

}
}
<|endoftext|>


<commit_before>//   Copyright 2016-2017 Jean-Francois Poilpret
//
//   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.

#include <fastarduino/time.h>
#include <fastarduino/devices/mpu6050.h>

#if defined(ARDUINO_UNO)
#define HARDWARE_UART 1
#include <fastarduino/uart.h>
static constexpr const board::USART UART = board::USART::USART0;
static constexpr const uint8_t OUTPUT_BUFFER_SIZE = 64;
// Define vectors we need in the example
REGISTER_UATX_ISR(0)
#elif defined(BREADBOARD_ATTINYX4)
#define HARDWARE_UART 0
#include <fastarduino/soft_uart.h>
static constexpr const board::DigitalPin TX = board::DigitalPin::D8_PB0;
static constexpr const uint8_t OUTPUT_BUFFER_SIZE = 64;
#else
#error "Current target is not yet supported!"
#endif

// UART for traces
static char output_buffer[OUTPUT_BUFFER_SIZE];
#if HARDWARE_UART
static serial::hard::UATX<UART> uart{output_buffer};
#else
static serial::soft::UATX<TX> uart{output_buffer};
#endif
static streams::FormattedOutput<streams::OutputBuffer> out = uart.fout();

using devices::magneto::MPU6050;
using devices::magneto::AllSensors;

using streams::dec;
using streams::hex;
using streams::flush;

void trace_i2c_status(uint8_t expected_status, uint8_t actual_status)
{
	if (expected_status != actual_status)
		out << F("status expected = ") << expected_status << F(", actual = ") << actual_status << '\n' << flush;
}

using ACCELEROMETER = MPU6050<i2c::I2CMode::Fast>;

int main() __attribute__((OS_main));
int main()
{
	board::init();
	sei();
#if HARDWARE_UART
	uart.register_handler();
#endif
	uart.begin(115200);
	out.width(2);
	out << F("Start\n") << flush;

	ACCELEROMETER::MANAGER manager;
	manager.begin();
	out << F("I2C interface started\n") << flush;

	ACCELEROMETER mpu{manager};

	bool ok = mpu.begin();
	out << dec << F("begin() ") << ok << '\n' << flush;
	while (true)
	{
		AllSensors sensors;
		ok = mpu.all_measures(sensors);
		out << dec << F("all_measures() ") << ok << '\n' << flush;
		if (ok)
		{
			out	<< dec
				<< F("Gyro x = ") << sensors.gyro.x
				<< F(", y = ") << sensors.gyro.y
				<< F(", z = ") << sensors.gyro.z << '\n' << flush;
			out	<< dec
				<< F("Accel x = ") << sensors.accel.x
				<< F(", y = ") << sensors.accel.y
				<< F(", z = ") << sensors.accel.z << '\n' << flush;
			//TODO methods to convert data: accel, gyro, temperature
			// Also check the temperature precision as per datasheet
			out << dec << F("Temp = ") << mpu.convert_temp_to_centi_degrees(sensors.temperature) << F(" centi-C\n") << flush;
		}
		time::delay_ms(1000);
	}

	// Stop TWI interface
	//===================
	manager.end();
	out << F("End\n") << flush;
}
<commit_msg>Improve MPU6050 example to convert accelerometer/gyroscope raw values to physical values.<commit_after>//   Copyright 2016-2017 Jean-Francois Poilpret
//
//   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.

#include <fastarduino/time.h>
#include <fastarduino/devices/mpu6050.h>

#if defined(ARDUINO_UNO)
#define HARDWARE_UART 1
#include <fastarduino/uart.h>
static constexpr const board::USART UART = board::USART::USART0;
static constexpr const uint8_t OUTPUT_BUFFER_SIZE = 64;
// Define vectors we need in the example
REGISTER_UATX_ISR(0)
#elif defined(BREADBOARD_ATTINYX4)
#define HARDWARE_UART 0
#include <fastarduino/soft_uart.h>
static constexpr const board::DigitalPin TX = board::DigitalPin::D8_PB0;
static constexpr const uint8_t OUTPUT_BUFFER_SIZE = 64;
#else
#error "Current target is not yet supported!"
#endif

// UART for traces
static char output_buffer[OUTPUT_BUFFER_SIZE];
#if HARDWARE_UART
static serial::hard::UATX<UART> uart{output_buffer};
#else
static serial::soft::UATX<TX> uart{output_buffer};
#endif
static streams::FormattedOutput<streams::OutputBuffer> out = uart.fout();

using utils::UnitPrefix;
using utils::map;
using devices::magneto::MPU6050;
using devices::magneto::AllSensors;
using devices::magneto::AccelRange;
using devices::magneto::GyroRange;
using devices::magneto::ACCEL_RANGE_G;
using devices::magneto::GYRO_RANGE_DPS;

using streams::dec;
using streams::hex;
using streams::flush;

static constexpr const GyroRange GYRO_RANGE = GyroRange::RANGE_250;
static constexpr const AccelRange ACCEL_RANGE = AccelRange::RANGE_2G;

inline int16_t gyro(int16_t value)
{
	return map(value, UnitPrefix::CENTI, GYRO_RANGE_DPS(GYRO_RANGE), 15);
}
inline int16_t accel(int16_t value)
{
	return map(value, UnitPrefix::MILLI, ACCEL_RANGE_G(ACCEL_RANGE), 15);
}

void trace_i2c_status(uint8_t expected_status, uint8_t actual_status)
{
	if (expected_status != actual_status)
		out << F("status expected = ") << expected_status << F(", actual = ") << actual_status << '\n' << flush;
}

using ACCELEROMETER = MPU6050<i2c::I2CMode::Fast>;

int main() __attribute__((OS_main));
int main()
{
	board::init();
	sei();
#if HARDWARE_UART
	uart.register_handler();
#endif
	uart.begin(115200);
	out.width(2);
	out << F("Start\n") << flush;

	ACCELEROMETER::MANAGER manager;
	manager.begin();
	out << F("I2C interface started\n") << flush;

	ACCELEROMETER mpu{manager};

	bool ok = mpu.begin(GyroRange::RANGE_250, AccelRange::RANGE_2G);
	out << dec << F("begin() ") << ok << '\n' << flush;
	while (true)
	{
		AllSensors sensors;
		ok = mpu.all_measures(sensors);
		out << dec << F("all_measures() ") << ok << '\n' << flush;
		if (ok)
		{
			out	<< dec
				<< F("raw Gyro x = ") << sensors.gyro.x
				<< F(", y = ") << sensors.gyro.y
				<< F(", z = ") << sensors.gyro.z << '\n' << flush;
			out	<< dec
				<< F("cdps Gyro x = ") << gyro(sensors.gyro.x)
				<< F(", y = ") << gyro(sensors.gyro.y)
				<< F(", z = ") << gyro(sensors.gyro.z) << '\n' << flush;
			out	<< dec
				<< F("raw Accel x = ") << sensors.accel.x
				<< F(", y = ") << sensors.accel.y
				<< F(", z = ") << sensors.accel.z << '\n' << flush;
			out	<< dec
				<< F("mG Accel x = ") << accel(sensors.accel.x)
				<< F(", y = ") << accel(sensors.accel.y)
				<< F(", z = ") << accel(sensors.accel.z) << '\n' << flush;
			// Also check the temperature precision as per datasheet
			out << dec << F("Temp = ") << mpu.convert_temp_to_centi_degrees(sensors.temperature) << F(" centi-C\n") << flush;
		}
		time::delay_ms(1000);
	}

	// Stop TWI interface
	//===================
	manager.end();
	out << F("End\n") << flush;
}
<|endoftext|>


<commit_before>/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the Qt Mobility Components.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "gallerymodel.h"

#include "qgalleryitemlist.h"

GalleryModel::GalleryModel(QObject *parent)
    : QAbstractItemModel(parent)
    , mediaList(0)
    , columnNames(1)
    , displayKeys(1, -1)
    , displayFields(1)
    , decorationKeys(1, -1)
    , decorationFields(1)
{
}

GalleryModel::~GalleryModel()
{
}

QVariant GalleryModel::data(const QModelIndex &index, int role) const
{
    if (index.isValid()) {
        int key = -1;

        if (role == Qt::DisplayRole || role == Qt::EditRole)
            key = displayKeys.at(index.column());
        else if (role == Qt::DecorationRole)
            key = decorationKeys.at(index.column());
        else if (role >= Qt::UserRole)
            key = userKeys.at(role - Qt::UserRole);

        if (key >= 0)
            return mediaList->metaData(index.row(), key);
    }
    return QVariant();
}

bool GalleryModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (index.isValid() && role == Qt::EditRole) {
        int key = displayKeys.at(index.column());

        if (key >= 0) {
            mediaList->setMetaData(index.row(), key, value);

            return true;
        }
    }
    return false;
}

Qt::ItemFlags GalleryModel::flags(const QModelIndex &index) const
{
    Qt::ItemFlags flags = Qt::ItemIsEnabled;

    if (index.isValid()) {
        int key = displayKeys.at(index.column());

        if (key >= 0 && mediaList->propertyAttributes(key) & QGalleryProperty::CanWrite)
            flags |= Qt::ItemIsEditable;
    }

    return flags;
}

QModelIndex GalleryModel::index(int row, int column, const QModelIndex &parent) const
{
    if (!parent.isValid() && mediaList
            && row >= 0 && row < mediaList->count()
            && column >= 0 && column < displayFields.count()) {

        // Ideally we'd use the scroll position of the view to set the cursor position
        if (row > 0 && row < mediaList->count() - 1) {
            const_cast<QGalleryItemList *>(mediaList)->setCursorPosition(row);
        }

        return createIndex(row, column);
    }
    return QModelIndex();
}

QModelIndex GalleryModel::parent(const QModelIndex &) const
{
    return QModelIndex();
}

QVariant GalleryModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    return orientation == Qt::Horizontal && role == Qt::DisplayRole
            ? columnNames.value(section)
            : QVariant();
}

int GalleryModel::rowCount(const QModelIndex &parent) const
{
    return !parent.isValid() && mediaList
            ? mediaList->count()
            : 0;
}

int GalleryModel::columnCount(const QModelIndex &parent) const
{
    return !parent.isValid() ? displayFields.count() : 0;
}

void GalleryModel::setColumnCount(int count)
{
    if (displayFields.count() > count) {
        beginRemoveColumns(QModelIndex(), count, displayFields.count() - 1);

        columnNames.resize(count);
        displayFields.resize(count);
        displayKeys.resize(count);
        decorationFields.resize(count);
        decorationKeys.resize(count);

        endRemoveColumns();
    } else if (displayFields.count() < count) {
        int index = displayFields.count();

        beginInsertColumns(QModelIndex(), index, count - 1);

        columnNames.resize(count);
        displayFields.resize(count);
        displayKeys.fill(-1, count);
        decorationFields.resize(count);
        decorationKeys.fill(-1, count);

        endInsertColumns();
    }
}

QGalleryItemList *GalleryModel::list() const
{
    return mediaList;
}

void GalleryModel::setList(QGalleryItemList *list)
{
    beginResetModel();

    if (mediaList) {
        disconnect(mediaList, SIGNAL(inserted(int,int)), this, SLOT(inserted(int,int)));
        disconnect(mediaList, SIGNAL(removed(int,int)), this, SLOT(removed(int,int)));
        disconnect(mediaList, SIGNAL(moved(int,int,int)), this, SLOT(moved(int,int,int)));
        disconnect(mediaList, SIGNAL(metaDataChanged(int,int,QList<int>)),
                this, SLOT(metaDataChanged(int,int,QList<int>)));

        displayKeys.fill(-1);
        decorationKeys.fill(-1);
        userKeys.fill(-1);
    }

    mediaList = list;

    if (mediaList) {
        for (int i = 0; i < displayFields.count(); ++i)
            displayKeys[i] = mediaList->propertyKey(displayFields.at(i));

        for (int i = 0; i < decorationFields.count(); ++i)
            decorationKeys[i] = mediaList->propertyKey(decorationFields.at(i));

        for (int i = 0; i < userFields.count(); ++i)
            userKeys[i] = mediaList->propertyKey(userFields.at(i));

        connect(mediaList, SIGNAL(inserted(int,int)), this, SLOT(inserted(int,int)));
        connect(mediaList, SIGNAL(removed(int,int)), this, SLOT(removed(int,int)));
        connect(mediaList, SIGNAL(moved(int,int,int)), this, SLOT(moved(int,int,int)));
        connect(mediaList, SIGNAL(metaDataChanged(int,int,QList<int>)),
                   this, SLOT(metaDataChanged(int,int,QList<int>)));
    }
    endResetModel();
}

QString GalleryModel::columnName(int column) const
{
    return columnNames.at(column);
}

void GalleryModel::setColumnName(int column, const QString &name)
{
    columnNames[column] = name;

    emit headerDataChanged(Qt::Horizontal, column, column);
}

QString GalleryModel::displayFieldForColumn(int column) const
{
    return displayFields.at(column);
}

void GalleryModel::setDisplayFieldForColumn(int column, const QString &field)
{
    displayFields[column] = field;

    if (mediaList) {
        displayKeys[column] = mediaList->propertyKey(field);

        emit dataChanged(createIndex(0, column), createIndex(mediaList->count() - 1, column));
    }
}

QString GalleryModel::decorationFieldForColumn(int column) const
{
    return decorationFields.at(column);
}

void GalleryModel::setDecorationFieldForColumn(int column, const QString &field)
{
    decorationFields[column] = field;

    if (mediaList) {
        decorationKeys[column] = mediaList->propertyKey(field);

        emit dataChanged(createIndex(0, column), createIndex(mediaList->count() - 1, column));
    }
}

QVector<QString> GalleryModel::userRoleFields() const
{
    return userFields;
}

void GalleryModel::setUserRoleFields(const QVector<QString> &fields)
{
    userFields = fields;
    userKeys.fill(-1, userFields.count());

    if (mediaList) {
        for (int i = 0; i < userFields.count(); ++i)
            userKeys[i] = mediaList->propertyKey(userFields.at(i));

        emit dataChanged(
                createIndex(0, 0),
                createIndex(mediaList->count() - 1, displayFields.count()));
    }
}

void GalleryModel::removed(int index, int count)
{
    beginRemoveRows(QModelIndex(), index, index + count - 1);
    endRemoveRows();
}

void GalleryModel::inserted(int index, int count)
{
    beginInsertRows(QModelIndex(), index, index + count - 1);
    endInsertRows();
}

void GalleryModel::moved(int from, int to, int count)
{
    beginMoveRows(QModelIndex(), from, from + count - 1, QModelIndex(), to);
}

void GalleryModel::metaDataChanged(int index, int count, const QList<int> &)
{
    emit dataChanged(
            createIndex(index, 0),
            createIndex(index + count -1, displayFields.count() - 1));
}

<commit_msg>Reduce noise in calls to setCursorPosition.<commit_after>/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the Qt Mobility Components.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "gallerymodel.h"

#include "qgalleryitemlist.h"

GalleryModel::GalleryModel(QObject *parent)
    : QAbstractItemModel(parent)
    , mediaList(0)
    , columnNames(1)
    , displayKeys(1, -1)
    , displayFields(1)
    , decorationKeys(1, -1)
    , decorationFields(1)
{
}

GalleryModel::~GalleryModel()
{
}

QVariant GalleryModel::data(const QModelIndex &index, int role) const
{
    if (index.isValid()) {
        int key = -1;

        if (role == Qt::DisplayRole || role == Qt::EditRole)
            key = displayKeys.at(index.column());
        else if (role == Qt::DecorationRole)
            key = decorationKeys.at(index.column());
        else if (role >= Qt::UserRole)
            key = userKeys.at(role - Qt::UserRole);

        if (key >= 0)
            return mediaList->metaData(index.row(), key);
    }
    return QVariant();
}

bool GalleryModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (index.isValid() && role == Qt::EditRole) {
        int key = displayKeys.at(index.column());

        if (key >= 0) {
            mediaList->setMetaData(index.row(), key, value);

            return true;
        }
    }
    return false;
}

Qt::ItemFlags GalleryModel::flags(const QModelIndex &index) const
{
    Qt::ItemFlags flags = Qt::ItemIsEnabled;

    if (index.isValid()) {
        int key = displayKeys.at(index.column());

        if (key >= 0 && mediaList->propertyAttributes(key) & QGalleryProperty::CanWrite)
            flags |= Qt::ItemIsEditable;
    }

    return flags;
}

QModelIndex GalleryModel::index(int row, int column, const QModelIndex &parent) const
{
    if (!parent.isValid() && mediaList
            && row >= 0 && row < mediaList->count()
            && column >= 0 && column < displayFields.count()) {

        // Ideally we'd use the scroll position of the view to set the cursor position
        if (row < mediaList->count() - 1) {
            const int position = mediaList->cursorPosition();
            const int pageSize = mediaList->minimumPagedItems();

            if (row - 16 < position && position > 0)
                mediaList->setCursorPosition(qMax(0, row - 16));
            else if (row + 16 > position + pageSize)
                mediaList->setCursorPosition(qMax(0, row + 16 - pageSize));
        }

        return createIndex(row, column);
    }
    return QModelIndex();
}

QModelIndex GalleryModel::parent(const QModelIndex &) const
{
    return QModelIndex();
}

QVariant GalleryModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    return orientation == Qt::Horizontal && role == Qt::DisplayRole
            ? columnNames.value(section)
            : QVariant();
}

int GalleryModel::rowCount(const QModelIndex &parent) const
{
    return !parent.isValid() && mediaList
            ? mediaList->count()
            : 0;
}

int GalleryModel::columnCount(const QModelIndex &parent) const
{
    return !parent.isValid() ? displayFields.count() : 0;
}

void GalleryModel::setColumnCount(int count)
{
    if (displayFields.count() > count) {
        beginRemoveColumns(QModelIndex(), count, displayFields.count() - 1);

        columnNames.resize(count);
        displayFields.resize(count);
        displayKeys.resize(count);
        decorationFields.resize(count);
        decorationKeys.resize(count);

        endRemoveColumns();
    } else if (displayFields.count() < count) {
        int index = displayFields.count();

        beginInsertColumns(QModelIndex(), index, count - 1);

        columnNames.resize(count);
        displayFields.resize(count);
        displayKeys.fill(-1, count);
        decorationFields.resize(count);
        decorationKeys.fill(-1, count);

        endInsertColumns();
    }
}

QGalleryItemList *GalleryModel::list() const
{
    return mediaList;
}

void GalleryModel::setList(QGalleryItemList *list)
{
    beginResetModel();

    if (mediaList) {
        disconnect(mediaList, SIGNAL(inserted(int,int)), this, SLOT(inserted(int,int)));
        disconnect(mediaList, SIGNAL(removed(int,int)), this, SLOT(removed(int,int)));
        disconnect(mediaList, SIGNAL(moved(int,int,int)), this, SLOT(moved(int,int,int)));
        disconnect(mediaList, SIGNAL(metaDataChanged(int,int,QList<int>)),
                this, SLOT(metaDataChanged(int,int,QList<int>)));

        displayKeys.fill(-1);
        decorationKeys.fill(-1);
        userKeys.fill(-1);
    }

    mediaList = list;

    if (mediaList) {
        for (int i = 0; i < displayFields.count(); ++i)
            displayKeys[i] = mediaList->propertyKey(displayFields.at(i));

        for (int i = 0; i < decorationFields.count(); ++i)
            decorationKeys[i] = mediaList->propertyKey(decorationFields.at(i));

        for (int i = 0; i < userFields.count(); ++i)
            userKeys[i] = mediaList->propertyKey(userFields.at(i));

        connect(mediaList, SIGNAL(inserted(int,int)), this, SLOT(inserted(int,int)));
        connect(mediaList, SIGNAL(removed(int,int)), this, SLOT(removed(int,int)));
        connect(mediaList, SIGNAL(moved(int,int,int)), this, SLOT(moved(int,int,int)));
        connect(mediaList, SIGNAL(metaDataChanged(int,int,QList<int>)),
                   this, SLOT(metaDataChanged(int,int,QList<int>)));
    }
    endResetModel();
}

QString GalleryModel::columnName(int column) const
{
    return columnNames.at(column);
}

void GalleryModel::setColumnName(int column, const QString &name)
{
    columnNames[column] = name;

    emit headerDataChanged(Qt::Horizontal, column, column);
}

QString GalleryModel::displayFieldForColumn(int column) const
{
    return displayFields.at(column);
}

void GalleryModel::setDisplayFieldForColumn(int column, const QString &field)
{
    displayFields[column] = field;

    if (mediaList) {
        displayKeys[column] = mediaList->propertyKey(field);

        emit dataChanged(createIndex(0, column), createIndex(mediaList->count() - 1, column));
    }
}

QString GalleryModel::decorationFieldForColumn(int column) const
{
    return decorationFields.at(column);
}

void GalleryModel::setDecorationFieldForColumn(int column, const QString &field)
{
    decorationFields[column] = field;

    if (mediaList) {
        decorationKeys[column] = mediaList->propertyKey(field);

        emit dataChanged(createIndex(0, column), createIndex(mediaList->count() - 1, column));
    }
}

QVector<QString> GalleryModel::userRoleFields() const
{
    return userFields;
}

void GalleryModel::setUserRoleFields(const QVector<QString> &fields)
{
    userFields = fields;
    userKeys.fill(-1, userFields.count());

    if (mediaList) {
        for (int i = 0; i < userFields.count(); ++i)
            userKeys[i] = mediaList->propertyKey(userFields.at(i));

        emit dataChanged(
                createIndex(0, 0),
                createIndex(mediaList->count() - 1, displayFields.count()));
    }
}

void GalleryModel::removed(int index, int count)
{
    beginRemoveRows(QModelIndex(), index, index + count - 1);
    endRemoveRows();
}

void GalleryModel::inserted(int index, int count)
{
    beginInsertRows(QModelIndex(), index, index + count - 1);
    endInsertRows();
}

void GalleryModel::moved(int from, int to, int count)
{
    beginMoveRows(QModelIndex(), from, from + count - 1, QModelIndex(), to);
}

void GalleryModel::metaDataChanged(int index, int count, const QList<int> &)
{
    emit dataChanged(
            createIndex(index, 0),
            createIndex(index + count -1, displayFields.count() - 1));
}

<|endoftext|>


<commit_before>#include <stack>

#include "events-impl.h"

#include "protocol/stress.pb.h"
#include "protocol/vtrc-rpc-lowlevel.pb.h"

#include "vtrc-client-base/vtrc-client.h"

#include "vtrc-common/vtrc-closure-holder.h"
#include "vtrc-common/vtrc-call-context.h"

#include "vtrc-chrono.h"
#include "vtrc-thread.h"

namespace stress {

    using namespace vtrc;
    namespace gpb = google::protobuf;

namespace {

    typedef vtrc::chrono::high_resolution_clock high_resolution_clock;
    typedef high_resolution_clock::time_point   time_point;

    size_t get_micro( const time_point &f, const time_point &l )
    {
        return chrono::duration_cast<chrono::microseconds>( l - f ).count( );
    }

    class events_impl: public vtrc_example::stress_events {

        client::vtrc_client_wptr client_;

        time_point last_event_point_;

    public:
        events_impl( vtrc::shared_ptr<vtrc::client::vtrc_client> c )
            :client_(c)
            ,last_event_point_(high_resolution_clock::now( ))
        { }
    private:

        void event(::google::protobuf::RpcController* controller,
                 const ::vtrc_example::event_req* request,
                 ::vtrc_example::event_res* response,
                 ::google::protobuf::Closure* done)
        {
            common::closure_holder holder( done );

            time_point this_event_point = high_resolution_clock::now( );

            std::string name( request->is_event( ) ? "Event" : "Callback" );

            std::cout << name << " id '" << request->id( ) << "'"
                      << "; ";
            if( request->id( ) > 0 ) {
                std::cout
                      << "previous: "
                      << get_micro( last_event_point_, this_event_point )
                      << " microseconds ago"
                      << "; thread " << this_thread::get_id( );
            }
            std::cout << "\n";

            last_event_point_ = this_event_point;
        }

        std::string get_stack( )
        {
            client::vtrc_client_sptr locked( client_.lock( ) );

            if( locked ) { /// just on case;

                const common::call_context *cc(locked->get_call_context( ));
                size_t depth = cc->depth( );
                std::ostringstream oss;

                size_t from_server = 0;
                size_t from_client = 0;

                bool is_server = true;
                while( cc ) {

                    from_server += is_server ? 1 : 0;
                    from_client += is_server ? 0 : 1;

                    is_server = !is_server;
                    cc = cc->next( );
                }
                oss << "depth: "        << depth        << "; "
                    << "client depth: " << from_client  << "; "
                    << "server depth: " << from_server;
                return oss.str( );
            }

            return "<>failed<>?";
        }

        std::string get_stack1( )
        {
            client::vtrc_client_sptr locked( client_.lock( ) );

            if( locked ) { /// just on case;

                const common::call_context *cc(locked->get_call_context( ));
                std::ostringstream oss;
                bool from_server = true;
                while( cc ) {
                    oss << (from_server ? ">" : "<");
                    from_server = !from_server;
                    cc = cc->next( );
                }
                return oss.str( );
            }

            return "<>failed<>?";
        }

        std::string get_stack2( )
        {
            client::vtrc_client_sptr locked( client_.lock( ) );

            if( locked ) { /// just on case;

                const common::call_context *cc(locked->get_call_context( ));
                std::ostringstream oss;
                bool from_server = true;
                while( cc ) {
                    oss << (from_server ? "S>" : "C<") << ":"
                        << cc->get_lowlevel_message( )->call( ).service_id( )
                        << "::"
                        << cc->get_lowlevel_message( )->call( ).method_id( );
                    cc = cc->next( );
                    from_server = !from_server;
                    if( cc ) {
                        oss << "->";
                    }
                }
                return oss.str( );
            }

            return "<>failed<>?";
        }

        void recursive_callback(::google::protobuf::RpcController* controller,
                 const ::vtrc_example::recursive_call_req* request,
                 ::vtrc_example::recursive_call_res* response,
                 ::google::protobuf::Closure* done)
        {
            common::closure_holder holder( done );

            std::string stack(get_stack( ));

            if( request->balance( ) == 0 ) {
                std::cout << "Last recursive_callback\n";
                std::cout << "last stack: " << stack
                          << "\n";
            } else {
                std::cout << "[IN ] balance: " << request->balance( )
                          << "; stack: " << stack
                          << "\n";
                client::vtrc_client_sptr locked(client_.lock( ));
                if( locked ) {
                    vtrc::shared_ptr<interface> impl(
                        create_stress_client(locked,
                            common::rpc_channel::USE_CONTEXT_CALL));
                    impl->recursive_call( request->balance( ) - 1 );
                }
                std::cout << "[OUT] balance: " << request->balance( )
                          << "; stack: " << stack
                          << "\n";
            }
        }

    };
}

    gpb::Service *create_events( vtrc::shared_ptr<client::vtrc_client> c )
    {
        return new events_impl( c );
    }

}
<commit_msg>examples<commit_after>#include <stack>

#include "events-impl.h"

#include "protocol/stress.pb.h"
#include "protocol/vtrc-rpc-lowlevel.pb.h"

#include "vtrc-client-base/vtrc-client.h"

#include "vtrc-common/vtrc-closure-holder.h"
#include "vtrc-common/vtrc-call-context.h"

#include "vtrc-chrono.h"
#include "vtrc-thread.h"

namespace stress {

    using namespace vtrc;
    namespace gpb = google::protobuf;

namespace {

    typedef vtrc::chrono::high_resolution_clock high_resolution_clock;
    typedef high_resolution_clock::time_point   time_point;

    size_t get_micro( const time_point &f, const time_point &l )
    {
        return chrono::duration_cast<chrono::microseconds>( l - f ).count( );
    }

    class events_impl: public vtrc_example::stress_events {

        client::vtrc_client_wptr client_;

        time_point last_event_point_;

    public:
        events_impl( vtrc::shared_ptr<vtrc::client::vtrc_client> c )
            :client_(c)
            ,last_event_point_(high_resolution_clock::now( ))
        { }
    private:

        void event(::google::protobuf::RpcController* controller,
                 const ::vtrc_example::event_req* request,
                 ::vtrc_example::event_res* response,
                 ::google::protobuf::Closure* done)
        {
            common::closure_holder holder( done );

            time_point this_event_point = high_resolution_clock::now( );

            std::string name( request->is_event( ) ? "Event" : "Callback" );

            std::cout << name << " id '" << request->id( ) << "'"
                      << "; ";
            if( request->id( ) > 0 ) {
                std::cout
                      << "previous: "
                      << get_micro( last_event_point_, this_event_point )
                      << " microseconds ago"
                      << "; thread " << this_thread::get_id( );
            }
            std::cout << "\n";

            last_event_point_ = this_event_point;
        }

        std::string get_stack( )
        {
            client::vtrc_client_sptr locked( client_.lock( ) );

            if( locked ) { /// just on case;

                const common::call_context *cc(locked->get_call_context( ));
                size_t depth = cc->depth( );
                std::ostringstream oss;

                size_t from_server = 0;
                size_t from_client = 0;

                bool is_server = true;
                while( cc ) {

                    from_server += is_server ? 1 : 0;
                    from_client += is_server ? 0 : 1;

                    is_server = !is_server;
                    cc = cc->next( );
                }
                oss << "depth: "        << depth        << "; "
                    << "client depth: " << from_client  << "; "
                    << "server depth: " << from_server;
                return oss.str( );
            }

            return "<>failed<>?";
        }

        std::string get_stack1( )
        {
            client::vtrc_client_sptr locked( client_.lock( ) );

            if( locked ) { /// just on case;

                const common::call_context *cc(locked->get_call_context( ));
                std::ostringstream oss;
                bool from_server = true;
                while( cc ) {
                    oss << (from_server ? ">" : "<");
                    from_server = !from_server;
                    cc = cc->next( );
                }
                return oss.str( );
            }

            return "<>failed<>?";
        }

        std::string get_stack2( )
        {
            client::vtrc_client_sptr locked( client_.lock( ) );

            if( locked ) { /// just on case;

                const common::call_context *cc(locked->get_call_context( ));
                std::ostringstream oss;
                bool from_server = true;
                while( cc ) {
                    oss << (from_server ? "S>" : "C<") << ":"
                        << cc->get_lowlevel_message( )->call( ).service_id( )
                        << "::"
                        << cc->get_lowlevel_message( )->call( ).method_id( );
                    cc = cc->next( );
                    from_server = !from_server;
                    if( cc ) {
                        oss << "->";
                    }
                }
                return oss.str( );
            }

            return "<>failed<>?";
        }

        void recursive_callback(::google::protobuf::RpcController* controller,
                 const ::vtrc_example::recursive_call_req* request,
                 ::vtrc_example::recursive_call_res* response,
                 ::google::protobuf::Closure* done)
        {
            common::closure_holder holder( done );

            std::string stack(get_stack( ));

            std::cout << "[IN ] balance: " << request->balance( )
                      << "; stack: " << stack
                      << "\n";

            client::vtrc_client_sptr locked(client_.lock( ));

            if( locked ) {
                vtrc::shared_ptr<interface> impl(
                    create_stress_client(locked,
                        common::rpc_channel::USE_CONTEXT_CALL));
                impl->recursive_call( request->balance( ) - 1 );
            }
            if( request->balance( ) == 1 ) {
                std::cout << "============= EXIT =============\n";
            }
            std::cout << "[OUT] balance: " << request->balance( )
                      << "; stack: " << stack
                      << "\n";
        }

    };
}

    gpb::Service *create_events( vtrc::shared_ptr<client::vtrc_client> c )
    {
        return new events_impl( c );
    }

}
<|endoftext|>


<commit_before>#include <unistd.h>
#include <boost/filesystem.hpp>
#include "zerocash/ZerocashParams.h"
#include "coins.h"
#include "util.h"
#include "init.h"
#include "primitives/transaction.h"
#include "crypto/equihash.h"
#include "chainparams.h"
#include "pow.h"

#include "zcbenchmarks.h"

struct timeval tv_start;

void timer_start()
{
    gettimeofday(&tv_start, 0);
}

double timer_stop()
{
    double elapsed;
    struct timeval tv_end;
    gettimeofday(&tv_end, 0);
    elapsed = double(tv_end.tv_sec-tv_start.tv_sec) +
        (tv_end.tv_usec-tv_start.tv_usec)/double(1000000);
    return elapsed;
}

double benchmark_sleep()
{
    timer_start();
    sleep(1);
    return timer_stop();
}

double benchmark_parameter_loading()
{
    // FIXME: this is duplicated with the actual loading code
    boost::filesystem::path pk_path = ZC_GetParamsDir() / "zc-testnet-public-alpha-proving.key";
    boost::filesystem::path vk_path = ZC_GetParamsDir() / "zc-testnet-public-alpha-verification.key";

    timer_start();
    auto vk_loaded = libzerocash::ZerocashParams::LoadVerificationKeyFromFile(
        vk_path.string(),
        INCREMENTAL_MERKLE_TREE_DEPTH
    );
    auto pk_loaded = libzerocash::ZerocashParams::LoadProvingKeyFromFile(
        pk_path.string(),
        INCREMENTAL_MERKLE_TREE_DEPTH
    );
    libzerocash::ZerocashParams zerocashParams = libzerocash::ZerocashParams(
        INCREMENTAL_MERKLE_TREE_DEPTH,
        &pk_loaded,
        &vk_loaded
    );
    return timer_stop();
}

double benchmark_create_joinsplit()
{
    CScript scriptPubKey;

    std::vector<PourInput> vpourin;
    std::vector<PourOutput> vpourout;

    while (vpourin.size() < NUM_POUR_INPUTS) {
        vpourin.push_back(PourInput(INCREMENTAL_MERKLE_TREE_DEPTH));
    }

    while (vpourout.size() < NUM_POUR_OUTPUTS) {
        vpourout.push_back(PourOutput(0));
    }

    /* Get the anchor of an empty commitment tree. */
    IncrementalMerkleTree blank_tree(INCREMENTAL_MERKLE_TREE_DEPTH);
    std::vector<unsigned char> newrt_v(32);
    blank_tree.getRootValue(newrt_v);
    uint256 anchor = uint256(newrt_v);

    timer_start();
    CPourTx pourtx(*pzerocashParams,
                   scriptPubKey,
                   anchor,
                   {vpourin[0], vpourin[1]},
                   {vpourout[0], vpourout[1]},
                   0,
                   0);
    double ret = timer_stop();
    assert(pourtx.Verify(*pzerocashParams));
    return ret;
}

double benchmark_verify_joinsplit(const CPourTx &joinsplit)
{
    timer_start();
    joinsplit.Verify(*pzerocashParams);
    return timer_stop();
}

double benchmark_solve_equihash()
{
    const char *testing = "testing";
    Equihash eh {Params(CBaseChainParams::MAIN).EquihashN(), Params(CBaseChainParams::MAIN).EquihashK()};
    crypto_generichash_blake2b_state eh_state;
    eh.InitialiseState(eh_state);
    crypto_generichash_blake2b_update(&eh_state, (const unsigned char*)testing, strlen(testing));
    timer_start();
    eh.BasicSolve(eh_state);
    return timer_stop();
}

double benchmark_verify_equihash()
{
    CChainParams params = Params(CBaseChainParams::MAIN);
    CBlock genesis = Params(CBaseChainParams::MAIN).GenesisBlock();
    CBlockHeader genesis_header = genesis.GetBlockHeader();
    timer_start();
    CheckEquihashSolution(&genesis_header, params);
    return timer_stop();
}

<commit_msg>Benchmark a random equihash input.<commit_after>#include <unistd.h>
#include <boost/filesystem.hpp>
#include "zerocash/ZerocashParams.h"
#include "coins.h"
#include "util.h"
#include "init.h"
#include "primitives/transaction.h"
#include "crypto/equihash.h"
#include "chainparams.h"
#include "pow.h"
#include "sodium.h"
#include "streams.h"

#include "zcbenchmarks.h"

struct timeval tv_start;

void timer_start()
{
    gettimeofday(&tv_start, 0);
}

double timer_stop()
{
    double elapsed;
    struct timeval tv_end;
    gettimeofday(&tv_end, 0);
    elapsed = double(tv_end.tv_sec-tv_start.tv_sec) +
        (tv_end.tv_usec-tv_start.tv_usec)/double(1000000);
    return elapsed;
}

double benchmark_sleep()
{
    timer_start();
    sleep(1);
    return timer_stop();
}

double benchmark_parameter_loading()
{
    // FIXME: this is duplicated with the actual loading code
    boost::filesystem::path pk_path = ZC_GetParamsDir() / "zc-testnet-public-alpha-proving.key";
    boost::filesystem::path vk_path = ZC_GetParamsDir() / "zc-testnet-public-alpha-verification.key";

    timer_start();
    auto vk_loaded = libzerocash::ZerocashParams::LoadVerificationKeyFromFile(
        vk_path.string(),
        INCREMENTAL_MERKLE_TREE_DEPTH
    );
    auto pk_loaded = libzerocash::ZerocashParams::LoadProvingKeyFromFile(
        pk_path.string(),
        INCREMENTAL_MERKLE_TREE_DEPTH
    );
    libzerocash::ZerocashParams zerocashParams = libzerocash::ZerocashParams(
        INCREMENTAL_MERKLE_TREE_DEPTH,
        &pk_loaded,
        &vk_loaded
    );
    return timer_stop();
}

double benchmark_create_joinsplit()
{
    CScript scriptPubKey;

    std::vector<PourInput> vpourin;
    std::vector<PourOutput> vpourout;

    while (vpourin.size() < NUM_POUR_INPUTS) {
        vpourin.push_back(PourInput(INCREMENTAL_MERKLE_TREE_DEPTH));
    }

    while (vpourout.size() < NUM_POUR_OUTPUTS) {
        vpourout.push_back(PourOutput(0));
    }

    /* Get the anchor of an empty commitment tree. */
    IncrementalMerkleTree blank_tree(INCREMENTAL_MERKLE_TREE_DEPTH);
    std::vector<unsigned char> newrt_v(32);
    blank_tree.getRootValue(newrt_v);
    uint256 anchor = uint256(newrt_v);

    timer_start();
    CPourTx pourtx(*pzerocashParams,
                   scriptPubKey,
                   anchor,
                   {vpourin[0], vpourin[1]},
                   {vpourout[0], vpourout[1]},
                   0,
                   0);
    double ret = timer_stop();
    assert(pourtx.Verify(*pzerocashParams));
    return ret;
}

double benchmark_verify_joinsplit(const CPourTx &joinsplit)
{
    timer_start();
    joinsplit.Verify(*pzerocashParams);
    return timer_stop();
}

double benchmark_solve_equihash()
{
    CBlock pblock;
    CEquihashInput I{pblock};
    CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
    ss << I;

    Equihash eh {Params(CBaseChainParams::MAIN).EquihashN(), Params(CBaseChainParams::MAIN).EquihashK()};
    crypto_generichash_blake2b_state eh_state;
    eh.InitialiseState(eh_state);
    crypto_generichash_blake2b_update(&eh_state, (unsigned char*)&ss[0], ss.size());

    uint256 nonce;
    randombytes_buf(nonce.begin(), 32);
    crypto_generichash_blake2b_update(&eh_state,
                                    nonce.begin(),
                                    nonce.size());

    timer_start();
    eh.BasicSolve(eh_state);
    return timer_stop();
}

double benchmark_verify_equihash()
{
    CChainParams params = Params(CBaseChainParams::MAIN);
    CBlock genesis = Params(CBaseChainParams::MAIN).GenesisBlock();
    CBlockHeader genesis_header = genesis.GetBlockHeader();
    timer_start();
    CheckEquihashSolution(&genesis_header, params);
    return timer_stop();
}

<|endoftext|>


<commit_before>//
//  ClockDeferrer.hpp
//  Clock Signal
//
//  Created by Thomas Harte on 23/08/2018.
//  Copyright © 2018 Thomas Harte. All rights reserved.
//

#ifndef ClockDeferrer_h
#define ClockDeferrer_h

#include <vector>

/*!
	A ClockDeferrer maintains a list of ordered actions and the times at which
	they should happen, and divides a total execution period up into the portions
	that occur between those actions, triggering each action when it is reached.

	@c Class should be a class that implements @c advance(TimeUnit), to advance
	that amount of time.
*/
template <typename TimeUnit> class ClockDeferrer {
	public:
		/// Constructs a ClockDeferrer that will call target.advance in between deferred actions.
		ClockDeferrer(std::function<void(TimeUnit)> &&target) : target_(std::move(target)) {}

		/*!
			Schedules @c action to occur in @c delay units of time.

			Actions must be scheduled in the order they will occur. It is undefined behaviour
			to schedule them out of order.
		*/
		void defer(TimeUnit delay, const std::function<void(void)> &action) {
			pending_actions_.emplace_back(delay, action);
		}

		/*!
			Runs for @c length units of time.

			The target's @c advance will be called with one or more periods that add up to @c length;
			any scheduled actions will be called between periods.
		*/
		void run_for(TimeUnit length) {
			// If there are no pending actions, just run for the entire length.
			// This should be the normal branch.
			if(pending_actions_.empty()) {
				target_(length);
				return;
			}

			// Divide the time to run according to the pending actions.
			while(length > TimeUnit(0)) {
				TimeUnit next_period = pending_actions_.empty() ? length : std::min(length, pending_actions_[0].delay);
				target_(next_period);
				length -= next_period;

				off_t performances = 0;
				for(auto &action: pending_actions_) {
					action.delay -= next_period;
					if(!action.delay) {
						action.action();
						++performances;
					}
				}
				if(performances) {
					pending_actions_.erase(pending_actions_.begin(), pending_actions_.begin() + performances);
				}
			}
		}

	private:
		std::function<void(TimeUnit)> target_;

		// The list of deferred actions.
		struct DeferredAction {
			TimeUnit delay;
			std::function<void(void)> action;

			DeferredAction(TimeUnit delay, const std::function<void(void)> &action) : delay(delay), action(std::move(action)) {}
		};
		std::vector<DeferredAction> pending_actions_;
};

#endif /* ClockDeferrer_h */
<commit_msg>Corrects documentation.<commit_after>//
//  ClockDeferrer.hpp
//  Clock Signal
//
//  Created by Thomas Harte on 23/08/2018.
//  Copyright © 2018 Thomas Harte. All rights reserved.
//

#ifndef ClockDeferrer_h
#define ClockDeferrer_h

#include <vector>

/*!
	A ClockDeferrer maintains a list of ordered actions and the times at which
	they should happen, and divides a total execution period up into the portions
	that occur between those actions, triggering each action when it is reached.
*/
template <typename TimeUnit> class ClockDeferrer {
	public:
		/// Constructs a ClockDeferrer that will call target(period) in between deferred actions.
		ClockDeferrer(std::function<void(TimeUnit)> &&target) : target_(std::move(target)) {}

		/*!
			Schedules @c action to occur in @c delay units of time.

			Actions must be scheduled in the order they will occur. It is undefined behaviour
			to schedule them out of order.
		*/
		void defer(TimeUnit delay, const std::function<void(void)> &action) {
			pending_actions_.emplace_back(delay, action);
		}

		/*!
			Runs for @c length units of time.

			The constructor-supplied target will be called with one or more periods that add up to @c length;
			any scheduled actions will be called between periods.
		*/
		void run_for(TimeUnit length) {
			// If there are no pending actions, just run for the entire length.
			// This should be the normal branch.
			if(pending_actions_.empty()) {
				target_(length);
				return;
			}

			// Divide the time to run according to the pending actions.
			while(length > TimeUnit(0)) {
				TimeUnit next_period = pending_actions_.empty() ? length : std::min(length, pending_actions_[0].delay);
				target_(next_period);
				length -= next_period;

				off_t performances = 0;
				for(auto &action: pending_actions_) {
					action.delay -= next_period;
					if(!action.delay) {
						action.action();
						++performances;
					}
				}
				if(performances) {
					pending_actions_.erase(pending_actions_.begin(), pending_actions_.begin() + performances);
				}
			}
		}

	private:
		std::function<void(TimeUnit)> target_;

		// The list of deferred actions.
		struct DeferredAction {
			TimeUnit delay;
			std::function<void(void)> action;

			DeferredAction(TimeUnit delay, const std::function<void(void)> &action) : delay(delay), action(std::move(action)) {}
		};
		std::vector<DeferredAction> pending_actions_;
};

#endif /* ClockDeferrer_h */
<|endoftext|>