Commit b661cecf authored by Adrián Gómez Brandón's avatar Adrián Gómez Brandón
Browse files

Initial commit

parent f13193a4
dataset* datasets/
*build* dataset/
.idea* cmake-build-debug/
build/
.idea/
.DS_Store/ .DS_Store/
[submodule "external/sdsl-lite"] [submodule "external/sdsl-lite"]
path = external/sdsl-lite path = external/sdsl-lite
url = https://github.com/simongog/sdsl-lite.git url = https://github.com/simongog/sdsl-lite.git
[submodule "external/repair_gn"]
path = external/repair_gn
url = https://gitlab.lbd.org.es/agomez/repair_gn.git
...@@ -2,14 +2,14 @@ cmake_minimum_required(VERSION 2.8.7) ...@@ -2,14 +2,14 @@ cmake_minimum_required(VERSION 2.8.7)
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules") set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules")
include(AppendCompilerFlags) include(AppendCompilerFlags)
project(name_project) project(trajectory-coder)
set(PROJECT_VENDOR "name_author") set(PROJECT_VENDOR "name_author")
set(CMAKE_PROJECT_NAME "name_project") set(CMAKE_PROJECT_NAME "trajectory-coder")
set(PROJECT_CONTACT "project_contact") set(PROJECT_CONTACT "project_contact")
#Change to "Debug" when you need it #Change to "Debug" when you need it
set(CMAKE_BUILD_TYPE "Release") set(CMAKE_BUILD_TYPE "Debug")
# C++11 compiler Check # C++11 compiler Check
if(NOT CMAKE_CXX_COMPILER_VERSION) # work around for cmake versions smaller than 2.8.10 if(NOT CMAKE_CXX_COMPILER_VERSION) # work around for cmake versions smaller than 2.8.10
...@@ -46,6 +46,7 @@ else() ...@@ -46,6 +46,7 @@ else()
endif() endif()
add_subdirectory(external/sdsl-lite) add_subdirectory(external/sdsl-lite)
add_subdirectory(external/repair_gn)
include_directories("${PROJECT_SOURCE_DIR}/external/googletest/include" include_directories("${PROJECT_SOURCE_DIR}/external/googletest/include"
${CMAKE_HOME_DIRECTORY}/include ${CMAKE_HOME_DIRECTORY}/include
...@@ -53,9 +54,16 @@ include_directories("${PROJECT_SOURCE_DIR}/external/googletest/include" ...@@ -53,9 +54,16 @@ include_directories("${PROJECT_SOURCE_DIR}/external/googletest/include"
${CMAKE_BINARY_DIR}/external/sdsl-lite/external/libdivsufsort/include/ ${CMAKE_BINARY_DIR}/external/sdsl-lite/external/libdivsufsort/include/
${CMAKE_HOME_DIRECTORY}/external/googletest/include ${CMAKE_HOME_DIRECTORY}/external/googletest/include
${CMAKE_HOME_DIRECTORY}/external/sdsl-lite/external/FastPFor/headers/ ${CMAKE_HOME_DIRECTORY}/external/sdsl-lite/external/FastPFor/headers/
${CMAKE_HOME_DIRECTORY}/external/repair_gn
${STXXL_INCLUDE_DIRS} ${STXXL_INCLUDE_DIRS}
) )
add_executable(template_executable src/main.cpp) add_executable(template_executable src/example.cpp)
target_link_libraries(template_executable sdsl divsufsort divsufsort64) target_link_libraries(template_executable sdsl divsufsort divsufsort64)
add_executable(repair_example src/repair_example.cpp)
target_link_libraries(repair_example libRepairGN sdsl divsufsort divsufsort64)
add_executable(repair_test test/repair_test.cpp)
target_link_libraries(repair_test libRepairGN sdsl divsufsort divsufsort64 gtest)
Subproject commit 596c043d72082c8c2350bd1414605e1d31bb5d47
/***
BSD 2-Clause License
Copyright (c) 2018, Adrián
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**/
//
// Created by Adrián on 27/07/2019.
//
#ifndef INC_CODES_ALTERNATIVE_CODE_HPP
#define INC_CODES_ALTERNATIVE_CODE_HPP
#include <stdint.h>
namespace codes {
class alternative_code {
public:
static uint32_t encode(int32_t value){
const int64_t mask = value >> 31;
return ((uint64_t) (( value + mask ) ^ mask) << 1) | ((uint64_t) value >> 31);
}
static int32_t decode(uint32_t value){
const bool negate = value & 0x0001;
return ((value >> 1) ^ -negate) + negate;
}
};
}
#endif //INC_2D_BLOCK_TREE_UTIL_HPP
/***
BSD 2-Clause License
Copyright (c) 2018, Adrián
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**/
//
// Created by Adrián on 31/12/20.
//
#ifndef TRAJECTORY_CODER_SPIRAL_CODER_HPP
#define TRAJECTORY_CODER_SPIRAL_CODER_HPP
namespace coder {
class spiral_coder {
public:
static uint32_t encode(int32_t x, int32_t y){
//If the object is not moving, return 0
if(x == 0 && y == 0){
return 0;
}
//If the object is moving:
uint32_t max = std::max(std::abs(x), std::abs(y));
//Square side length
//long length = 2*max + 1;
uint32_t length = (max << 1) + 1;
//Initial position
//long initialPosition = static_cast<long>(pow(length - 2, 2));
uint32_t initialPosition = (length - 2) * (length -2);
//std::cout << initialPosition << "," << length << "," << max << std::endl;
if(x == max){ //If we are on the right
return initialPosition + max - y;
}else if(x == -max){ //If we are on the left
return initialPosition + max + 2*(length -1) + y;
}else if(y == max){ //If we are on the top
return initialPosition + max + 3*(length -1) + x;
}else{ //(y == -max) If we are on the bottom
return initialPosition + max + (length -1) - x;
}
}
static std::pair<int32_t, int32_t> decode(uint32_t code){
if(code == 0){
return std::pair<int32_t, int32_t>(0, 0);
}
//If the object is moving:
uint32_t sqrt = (uint32_t) std::floor(std::sqrt(code));
//Square side length
uint32_t length = (sqrt % 2) + 1 + sqrt;
//Max distance
uint32_t max = (length - 1) >> 1;
//Initial position
uint32_t initialPosition = (length-2) * (length-2);
//Difference between position and inititalposition
uint32_t diff = code - initialPosition;
//Calculate zone
uint32_t zone = diff / (length-1);
if(zone == 0){ //If we are on the right
return std::pair<int32_t, int32_t>(max, max - diff);
}else if(zone == 1){ //If we are on the bottom
return std::pair<int32_t, int32_t>((length -1)+ max - diff, -max);
}else if(zone == 2){ //If we are on the left
return std::pair<int32_t, int32_t>(-max, diff - max - 2*(length -1));
}else {//(zone == 3) If we are on the top
return std::pair<int32_t, int32_t>(diff - max - 3*(length -1), max);
}
};
};
}
#endif //TRAJECTORY_CODER_SPIRAL_CODER_HPP
//
// Created by adrian on 25/06/17.
//
#ifndef INC_CODES_ZETA_ORDER_HPP
#define INC_CODES_ZETA_ORDER_HPP
#include <stdint.h>
#include <utility>
namespace codes {
class zeta_order {
private:
/*static constexpr uint64_t masks[6] = {0x5555555555555555 , 0x3333333333333333,
0x0F0F0F0F0F0F0F0F, 0x00FF00FF00FF00FF, 0x0000FFFF0000FFFF, 0x00000000FFFFFFFF};
static constexpr uint64_t offset[5] = {1, 2, 4, 8, 16};*/
static uint32_t interleaving_number_encode(uint32_t x){
x &= 0x0000FFFF;
x = (x | (x << 8)) & 0x00FF00FF;
x = (x | (x << 4)) & 0x0F0F0F0F;
x = (x | (x << 2)) & 0x33333333;
x = (x | (x << 1)) & 0x55555555;
return x;
}
static uint32_t interleaving_number_decode(uint32_t x){
x &= 0x55555555;
x = (x ^(x >> 1)) & 0x33333333;
x = (x ^(x >> 2)) & 0x0F0F0F0F;
x = (x ^(x >> 4)) & 0x00FF00FF;
x = (x ^(x >> 8)) & 0x0000FFFF;
return x;
}
public:
static uint32_t encode(uint32_t x, uint32_t y){
return (interleaving_number_encode(y) << 1) | interleaving_number_encode(x);
}
static std::pair<uint32_t, uint32_t> decode(uint32_t code){
uint32_t x = interleaving_number_decode(code);
uint32_t y = interleaving_number_decode(code >> 1);
return {x, y};
}
};
}
#endif //INC_CODES_ZETA_ORDER_HPP
/***
BSD 2-Clause License
Copyright (c) 2018, Adrián
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**/
//
// Created by Adrián on 31/12/20.
//
#ifndef TRAJECTORY_CODER_REPAIR_HPP
#define TRAJECTORY_CODER_REPAIR_HPP
#include <grammar/repair_adapter.hpp>
#include <util/memory.hpp>
namespace grammar {
template <class t_value = uint32_t>
class repair {
public:
typedef uint64_t size_type;
typedef t_value value_type;
private:
size_type m_alpha;
std::vector<value_type> m_values;
std::vector<value_type> m_rules;
public:
const size_type &alpha = m_alpha;
const std::vector<value_type> &values = m_values;
const std::vector<value_type> &rules = m_rules;
private:
void copy(const repair& p){
m_alpha = p.m_period;
m_values = p.m_values;
m_rules = p.m_rules;
}
void construction(const std::vector<value_type> &log){
int32_t max_value = 0;
int* log_repair = new int[log.size()];
std::memmove(log_repair, &log[0], sizeof(int) * log.size());
repair_adapter m_repair_adapter;
int total_MB = util::memory::total_memory_megabytes() * 0.8;
m_repair_adapter.run(log_repair, log.size(), total_MB);
m_alpha = m_repair_adapter.alpha;
//6. We add extra information to the rules
m_values = std::vector<value_type>(m_repair_adapter.lenC);
std::memmove(&m_values[0], m_repair_adapter.c, sizeof(int) * m_repair_adapter.lenC);
m_rules = std::vector<value_type>(2*m_repair_adapter.lenR);
std::memmove(&m_rules[0], m_repair_adapter.rules, 2*sizeof(int) * m_repair_adapter.lenR);
}
inline value_type left_rule(const value_type val){
auto r_i = val - m_alpha;
return m_rules[r_i*2];
}
inline value_type right_rule(const value_type val){
auto r_i = val - m_alpha;
return m_rules[r_i*2+1];
}
public:
repair(){};
repair(const std::vector<value_type> &log){
construction(log);
}
void decompress_entry(value_type val, std::vector<value_type> &result){
while(val >= m_alpha ){
decompress_entry(left_rule(val), result);
val = right_rule(val);
}
result.push_back(val);
}
std::vector<value_type> decompress(){
std::vector<value_type> result;
auto i = 0;
for(const auto &c : m_values){
decompress_entry(c, result);
++i;
}
return result;
}
//! Assignment move operation
repair& operator=(repair&& p) {
if (this != &p) {
m_values = std::move(p.m_values);
m_rules = std::move(p.m_rules);
m_alpha = std::move(p.m_alpha);
}
return *this;
}
//! Assignment operator
repair& operator=(const repair& p)
{
if (this != &p) {
copy(p);
}
return *this;
}
//! Copy constructor
repair(const repair& p)
{
copy(p);
}
//! Move constructor
repair(repair&& p)
{
*this = std::move(p);
}
//! Swap method
/*! Swaps the content of the two data structure.
* You have to use set_vector to adjust the supported bit_vector.
* \param bp_support Object which is swapped.
*/
void swap(repair& p)
{
// m_bp.swap(bp_support.m_bp); use set_vector to set the supported bit_vector
std::swap(m_values, p.m_values);
std::swap(m_rules, p.m_rules);
std::swap(m_alpha, p.m_alpha);
}
//! Serializes the snapshot to a stream.
/*!
* \param out The outstream to which the data structure is written.
* \return The number of bytes written to out.
*/
uint64_t serialize(std::ostream& out, sdsl::structure_tree_node* v=nullptr, std::string name="")const
{
sdsl::structure_tree_node* child = sdsl::structure_tree::add_child(v, name, sdsl::util::class_name(*this));
uint64_t written_bytes = 0;
written_bytes += sdsl::write_member(m_alpha, out, child, "alpha");
written_bytes += sdsl::write_member(m_values.size(), out, child, "values_size");
written_bytes += sdsl::serialize_vector(m_values, out, child, "values");
written_bytes += sdsl::write_member(m_rules.size(), out, child, "rules_size");
written_bytes += sdsl::serialize_vector(m_rules, out, child, "rules");
//written_bytes += sdsl::serialize_vector(m_log_object, out, child, "log_objects");
sdsl::structure_tree::add_size(child, written_bytes);
return written_bytes;
}
void load(std::istream &in){
uint64_t m_rules_size = 0, m_values_size=0, m_samples_size = 0;
sdsl::read_member(m_alpha, in);
sdsl::read_member(m_values_size, in);
m_values.resize(m_values_size);
sdsl::load_vector(m_values, in);
sdsl::read_member(m_rules_size, in);
m_rules.resize(m_rules_size);
sdsl::load_vector(m_rules, in);
}
};
}
#endif //TRAJECTORY_CODER_REPAIR_HPP
/***
BSD 2-Clause License
Copyright (c) 2018, Adrián
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**/
//
// Created by Adrián on 31/12/20.
//
#ifndef TRAJECTORY_CODER_REPAIR_ADAPTER_HPP
#define TRAJECTORY_CODER_REPAIR_ADAPTER_HPP
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>
#include <sdsl/int_vector.hpp>
#include <include/irepair.h>
#include <vector>
#include <util/file.hpp>
namespace grammar {
typedef struct {
int left, right;
} Tpair;
class repair_adapter {
public:
Tpair* rules;
uint lenR;
int* c;
uint lenC;
int alpha;
private:
void read_rules_repair(const char *fileR) {
FILE *Tf;
struct stat s;
if (stat(fileR, &s) != 0) {
fprintf(stderr, "Error: cannot stat file %s\n", fileR);
exit(1);
}
uint64_t n = s.st_size;
Tf = fopen(fileR, "r");
if (Tf == NULL) {
fprintf(stderr, "Error: cannot open file %s for reading\n", fileR);
exit(1);
}
if (fread(&alpha, sizeof(int), 1, Tf) != 1) {
fprintf(stderr, "Error: cannot read file %s\n", fileR);
exit(1);
}
lenR = (n - sizeof(int)) / sizeof(Tpair);
rules = (Tpair *) malloc(lenR * sizeof(Tpair));
if (fread(rules, sizeof(Tpair), lenR, Tf) != lenR) {
fprintf(stderr, "Error: cannot read file %s\n", fileR);
exit(1);
}
fclose(Tf);
}
void read_c_repair(const char *fileC) {
FILE *Cf;
struct stat s;
if (stat(fileC, &s) != 0) {
fprintf(stderr, "Error: cannot stat file %s\n", fileC);
exit(1);
}
lenC = s.st_size / sizeof(int);
Cf = fopen(fileC, "r");
if (Cf == NULL) {
fprintf(stderr, "Error: cannot open file %s for reading\n", fileC);
exit(1);
}
c = (int *) malloc(lenC * sizeof(int));
if (fread(c, sizeof(int), lenC, Cf) != lenC) {
fprintf(stderr, "Error: cannot read file %s\n", fileC);
exit(1);
}
fclose(Cf);
}
int* read_keys_repair(const char *fileC, uint &lenK) {
FILE *Cf;
struct stat s;
if (stat(fileC, &s) != 0) {
fprintf(stderr, "Error: cannot stat file %s\n", fileC);
exit(1);
}
lenK = s.st_size / sizeof(int);