Commit 79726d43 authored by David Geisler's avatar David Geisler

moved utils to module

parent 74a601e7
......@@ -47,12 +47,8 @@ PROJECT(pe-tools-lsh)
SET(CMAKE_CXX_STANDARD 20)
INCLUDE_DIRECTORIES(include)
#include default scripts for pe builds
INCLUDE(cmake/default.cmake)
#add all sub directories containing a CMakeLists.txt
ADDSUBDIRS()
ADDSUBDIRS()
\ No newline at end of file
/****************************************************************************
* Copyright (C) 2019 by David Geisler <david.geisler@uni-tuebingen.de> *
* *
* This file is part of the Perception Engineering Toolbox 1.0. *
* Redistribution and use in source and binary forms, with or without *
* modification, are permitted provided that the following conditions *
* are met: *
* *
* 1. Redistributions of source code must retain the above copyright *
* notice, this list of conditions and the following disclaimer. *
* 2. 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. *
* 3. Neither the name of the copyright holder nor the names of its *
* contributors may be used to endorse or promote products derived from *
* this software without specific prior written permission. *
* *
* 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. *
* *
* Correspondence should be directed to *
* Eberhard Karls Universität Tübingen: *
* *
* Eberhard Karls Universität Tübingen *
* Mathematisch-Naturwissenschaftliche Fakultät *
* Technische Informatik - Perception Engineering *
* David Geisler *
* Sand 14 *
* D-72076 Tübingen *
* GERMANY *
* www.uni-tuebingen.de/en *
* *
* email: david.geisler@uni-tuebingen.de *
****************************************************************************/
/**
* @author David Geisler
* @copyright Copyright (C) 2019 by David Geisler <david.geisler@uni-tuebingen.de>
* @date 22 Oct 2019
*
* @package utue.pe.utils
*
* @brief Defines a very simple HashMap
*/
#ifndef UTUE_PE_UTILS_HASHMAP_H
#define UTUE_PE_UTILS_HASHMAP_H
#include <utue/pe/utils/HashSet.h>
namespace utue::pe::utils {
/**
* Hash wrapper to only hash the key of the HashMap tuple
* @tparam Key Type of the key value
* @tparam Value Type of the value
* @tparam Hash Base hash function
*/
template<class Key, class Value, class Hash>
class KeyHash {
private:
/**
* Base hash function
*/
Hash m_hash;
public:
/**
* Calculates the hash of the of a HashMap tuple
* @param pair HashMap tuple
* @return Hash of the key
*/
unsigned long operator()(const std::pair<Key,Value>& pair) const {
return this->m_hash(pair.first);
}
};
/**
* Comparator wrapper to only compare the keys of two HashMap tuples
* @tparam Key Type of the HashMap key
* @tparam Value Type of the HashMap value
* @tparam Equal Base comparator type
*/
template<class Key, class Value, class Equal>
class KeyEqual {
private:
/**
* Base comparator
*/
Equal m_equal;
public:
/**
* Compares two HashMap tuples by their keys
* @param left First HashMap tuple
* @param right Second HashMap tuple
* @return True if both keys are equal
*/
bool operator()(const std::pair<Key,Value>& left, const std::pair<Key,Value>& right) const {
return this->m_equal(left.first,right.first);
}
};
/**
* @brief Very basic implementation of a chained hash map
*/
template<class Key, class Value, class Hash = std::hash<Key>, class Equal = std::equal_to<Key>>
class HashMap : public HashSet<std::pair<Key,Value>,KeyHash<Key,Value,Hash>,KeyEqual<Key,Value,Equal>> {
private:
/**
* Base HashSet type
*/
typedef HashSet<std::pair<Key,Value>,KeyHash<Key,Value,Hash>,KeyEqual<Key,Value,Equal>> BaseHashSet;
public:
/**
* Create a HashMap with a given number of buckets and a seed for hashing function
* @param buckets Number of buckets
* @param seed Seed of the hashing function
*/
explicit HashMap(const unsigned int& buckets = 100, const unsigned long& seed = 0) : BaseHashSet(buckets,seed) { }
/**
* Inserts an element to the HashSet
* @param element
*/
void insert(const Key& key, const Value& value) {
this->insert(std::pair<Key,Value>(key,value));
}
/**
* Returns the element by a given key. Is no element present a new entry will created.
* @param key Key of the seeked element
* @param defaultValue Value of the new element if no element was found
* @return Reference to the element in the HashMap
*/
Value& operator()(const Key& key, const Value defaultValue = Value()) {
return this->BaseHashSet::insert(std::pair<Key,Value>(key,Value())).second;
}
/**
* Returns the element by a given key. If no ement was found, the default value is returned
* @param key Key of the seeked element
* @param defaultValue Value will be returned if no element was found
* @return Const reference to the found element value or defaultValue
*/
const Value& operator()(const Key& key, const Value& defaultValue = Value()) const {
unsigned long i;
i = this->BaseHashSet::hash(std::pair<Key,Value>(key,defaultValue));
for(const auto& it : this->m_buckets[i])
if(this->m_equal(it,std::pair<Key,Value>(key,defaultValue)))
return it.second;
return defaultValue;
}
};
}
#endif //UTUE_PE_UTILS_HASHMAP_H
/****************************************************************************
* Copyright (C) 2019 by David Geisler <david.geisler@uni-tuebingen.de> *
* *
* This file is part of the Perception Engineering Toolbox 1.0. *
* Redistribution and use in source and binary forms, with or without *
* modification, are permitted provided that the following conditions *
* are met: *
* *
* 1. Redistributions of source code must retain the above copyright *
* notice, this list of conditions and the following disclaimer. *
* 2. 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. *
* 3. Neither the name of the copyright holder nor the names of its *
* contributors may be used to endorse or promote products derived from *
* this software without specific prior written permission. *
* *
* 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. *
* *
* Correspondence should be directed to *
* Eberhard Karls Universität Tübingen: *
* *
* Eberhard Karls Universität Tübingen *
* Mathematisch-Naturwissenschaftliche Fakultät *
* Technische Informatik - Perception Engineering *
* David Geisler *
* Sand 14 *
* D-72076 Tübingen *
* GERMANY *
* www.uni-tuebingen.de/en *
* *
* email: david.geisler@uni-tuebingen.de *
****************************************************************************/
/**
* @author David Geisler
* @copyright Copyright (C) 2019 by David Geisler <david.geisler@uni-tuebingen.de>
* @date 22 Oct 2019
*
* @package utue.pe.utils
*
* @brief Defines an HashSet for tokens
*/
#ifndef UTUE_PE_UTILS_HASHSET_H
#define UTUE_PE_UTILS_HASHSET_H
#include <memory>
#include <list>
#include <vector>
namespace utue::pe::utils {
/**
* @brief Very basic implementation of a chained hash set
*/
template<class T, class Hash = std::hash<T>, class Equal = std::equal_to<T>>
class HashSet {
public:
/**
* Fast bucket iterator type
*/
typedef typename std::list<std::list<T>*>::iterator FastBucketIterator;
/**
* Slow bucket iterator type
*/
typedef typename std::vector<std::list<T>>::iterator SlowBucketIterator;
/**
* Fast bucket iterator type
*/
typedef typename std::list<std::list<T>*>::const_iterator FastConstBucketIterator;
/**
* Slow bucket iterator type
*/
typedef typename std::vector<std::list<T>>::const_iterator SlowConstBucketIterator;
/**
* Bucket iterator container
* @tparam IteratorType Type of the Iterator (either FastBucketIterator or SlowBucketIterator)
*/
template<typename IteratorType>
class Iterator {
private:
/**
* Read/Write iterator that points to the first bucket
*/
IteratorType m_begin;
/**
* Read/write iterator that points one past the last bucket
*/
IteratorType m_end;
public:
/**
* Create a new bucket iterator container
* @param begin Read/Write iterator that points to the first bucket
* @param end Read/write iterator that points one past the last bucket
*/
Iterator(const IteratorType& begin, const IteratorType& end) : m_begin(begin), m_end(end) {
}
/**
* Returns a read/write iterator that points to the first
* bucket. Iteration is done in ordinary element order.
* @return Iterator that points to the first bucket.
*/
IteratorType begin() {
return this->m_begin;
}
/**
* Returns a read/write iterator that points one past the last
* bucket. Iteration is done in ordinary element order.
* @return Iterator that points one past to the last bucket.
*/
IteratorType end() {
return this->m_end;
}
};
protected:
/**
* Hash function
*/
Hash m_hash;
/**
* Comparator
*/
Equal m_equal;
/**
* List of buckets. Each bucket contains a linked list to resolve hash collisions
*/
std::vector<std::list<T>> m_buckets;
/**
* Stores the active buckets in a linked list
*/
std::list<std::list<T>*> m_active_buckets;
/**
* Seed of the hashing function
*/
unsigned long m_seed;
public:
/**
* Create a HashSet with a given number of buckets and a seed for hashing function
* @param buckets Number of buckets
* @param seed Seed of the hashing function
*/
HashSet(const unsigned int& buckets = 100, const unsigned long& seed = 0) :
m_hash(),
m_equal(),
m_buckets(),
m_active_buckets(),
m_seed(seed) {
this->m_buckets.resize(buckets);
}
/**
* Inserts an element to the HashSet
* @param element
*/
T& insert(const T& element) {
unsigned long i;
i = this->hash(element);
if(!this->m_buckets[i].empty()) {
for (auto& it : this->m_buckets[i])
if (this->m_equal(it, element))
return it;
} else {
this->m_active_buckets.emplace_back(&this->m_buckets[i]);
}
return this->m_buckets[i].emplace_back(element);
}
[[nodiscard]] bool has(const T& element) {
unsigned long i;
i = this->hash(element);
for (auto& it : this->m_buckets[i])
if (this->m_equal(it, element))
return true;
return false;
}
/**
* Returns a iterator container for fast bucket iteration.
* Only active buckets will be considered.
* @return Iterator container
*/
[[nodiscard]] Iterator<FastBucketIterator> fastIterator() {
return Iterator<FastBucketIterator>(this->m_active_buckets.begin(),this->m_active_buckets.end());
}
/**
* Returns a iterator container for slow bucket iteration.
* All buckets will be considered, if active or not.
* @return Iterator container
*/
[[nodiscard]] Iterator<SlowBucketIterator> slowIterator() {
return Iterator<SlowBucketIterator>(this->m_buckets.begin(),this->m_buckets.end());
}
/**
* Returns a iterator container for fast bucket iteration.
* Only active buckets will be considered.
* @return Iterator container
*/
[[nodiscard]] Iterator<FastConstBucketIterator> fastIterator() const {
return Iterator<FastConstBucketIterator>(this->m_active_buckets.begin(),this->m_active_buckets.end());
}
/**
* Returns a iterator container for slow bucket iteration.
* All buckets will be considered, if active or not.
* @return Iterator container
*/
[[nodiscard]] Iterator<SlowConstBucketIterator> slowIterator() const {
return Iterator<SlowConstBucketIterator>(this->m_buckets.begin(),this->m_buckets.end());
}
/**
* Returns the maximal bucket load
* @return Bucket load
*/
[[nodiscard]] unsigned long maxLoad() const {
unsigned long load;
unsigned long tmp;
load = 0;
for(const auto& bucket : this->m_active_buckets) {
tmp = bucket->size();
if(tmp > load)
load = tmp;
}
return load;
}
/**
* Returns the average bucket load
* @return Bucket load
*/
[[nodiscard]] float avgLoad() const {
float load;
load = 0.0f;
for(const auto& bucket : this->m_active_buckets)
load += bucket->size();
return load / this->m_active_buckets.size();
}
protected:
/**
* Calculate the hash -> index to the corresponding bucket
* @param element Element to hash
* @return Hash which points to the corresponding bucket
*/
unsigned long hash(const T& element) const {
unsigned long hash;
hash = this->m_hash(element);
if(this->m_seed != 0)
hash = std::_Hash_impl::__hash_combine(this->m_seed, hash);
return hash % this->m_buckets.size();
}
};
}
#endif //UTUE_PE_TOOLS_LSH_MINHASH_TOKENHASHSET_H
/****************************************************************************
* Copyright (C) 2019 by David Geisler <david.geisler@uni-tuebingen.de> *
* *
* This file is part of the Perception Engineering Toolbox 1.0. *
* Redistribution and use in source and binary forms, with or without *
* modification, are permitted provided that the following conditions *
* are met: *
* *
* 1. Redistributions of source code must retain the above copyright *
* notice, this list of conditions and the following disclaimer. *
* 2. 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. *
* 3. Neither the name of the copyright holder nor the names of its *
* contributors may be used to endorse or promote products derived from *
* this software without specific prior written permission. *
* *
* 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. *
* *
* Correspondence should be directed to *
* Eberhard Karls Universität Tübingen: *
* *
* Eberhard Karls Universität Tübingen *
* Mathematisch-Naturwissenschaftliche Fakultät *
* Technische Informatik - Perception Engineering *
* David Geisler *
* Sand 14 *
* D-72076 Tübingen *
* GERMANY *
* www.uni-tuebingen.de/en *
* *
* email: david.geisler@uni-tuebingen.de *
****************************************************************************/
/**
* @author David Geisler
* @copyright Copyright (C) 2019 by David Geisler <david.geisler@uni-tuebingen.de>
* @date 22 Oct 2019
*
* @package utue.pe.utils
*
* @brief Collection routines for random number generation
*/
#ifndef UTUE_PE_TOOLS_RANDOM_H
#define UTUE_PE_TOOLS_RANDOM_H
namespace utue::pe::utils {
/**
* @brief Provides random number generators
*/
class Random {
public:
/**
* Sets the seed of the random number generator
* @param seed
*/
static void seed(const unsigned long& seed = 0);
/**
* @brief Provides uniform distributed random number generators
*/
class Uniform {
public:
/**
* Generates random numbers in a given value range
* @tparam T Type of number
* @param min minimal bound of range
* @param max maximal bound of range
* @return random number between min and max
*/
template<typename T>
static T range(const T& min, const T& max);
};
};
}
#endif //UTUE_PE_TOOLS_RANDOM_H
/****************************************************************************
* Copyright (C) 2019 by David Geisler <david.geisler@uni-tuebingen.de> *
* *
* This file is part of the Perception Engineering Toolbox 1.0. *
* Redistribution and use in source and binary forms, with or without *
* modification, are permitted provided that the following conditions *
* are met: *
* *
* 1. Redistributions of source code must retain the above copyright *
* notice, this list of conditions and the following disclaimer. *
* 2. 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. *
* 3. Neither the name of the copyright holder nor the names of its *
* contributors may be used to endorse or promote products derived from *
* this software without specific prior written permission. *
* *
* 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. *
* *
* Correspondence should be directed to *
* Eberhard Karls Universität Tübingen: *
* *
* Eberhard Karls Universität Tübingen *
* Mathematisch-Naturwissenschaftliche Fakultät *
* Technische Informatik - Perception Engineering *
* David Geisler *
* Sand 14 *
* D-72076 Tübingen *
* GERMANY *
* www.uni-tuebingen.de/en *
* *
* email: david.geisler@uni-tuebingen.de *
****************************************************************************/
/**
* @author David Geisler
* @copyright Copyright (C) 2019 by David Geisler <david.geisler@uni-tuebingen.de>
* @date 22 Oct 2019
*
* @package utue.pe.utils
*
* @brief Matlab like Tic Toc functionality for easy runtime measurements
*/
#ifndef UTUE_PE_TOOLS_TICTOC_H
#define UTUE_PE_TOOLS_TICTOC_H