<commit_before>/*

Copyright (c) 2013, Project OSRM, <NAME>, others
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.

*/

#ifndef BINARYHEAP_H_INCLUDED
#define BINARYHEAP_H_INCLUDED

//Not compatible with non contiguous node ids

#include <boost/unordered_map.hpp>

#include <cassert>

#include <algorithm>
#include <limits>
#include <map>
#include <vector>

template< typename NodeID, typename Key >
class ArrayStorage {
public:

    ArrayStorage( size_t size ) : positions( new Key[size] ) {
        memset(positions, 0, size*sizeof(Key));
    }

    ~ArrayStorage() {
        delete[] positions;
    }

    Key &operator[]( NodeID node ) {
        return positions[node];
    }

    void Clear() {}

private:
    Key* positions;
};

template< typename NodeID, typename Key >
class MapStorage {
public:

    MapStorage( size_t ) {}

    Key &operator[]( NodeID node ) {
        return nodes[node];
    }

    void Clear() {
        nodes.clear();
    }

private:
    std::map< NodeID, Key > nodes;

};

template< typename NodeID, typename Key >
class UnorderedMapStorage {
public:

	UnorderedMapStorage( size_t ) {
		//hash table gets 1000 Buckets
		nodes.rehash(1000);
	}

    Key &operator[]( const NodeID node ) {
    	return nodes[node];
    }

    void Clear() {
        nodes.clear();
    }

private:
    boost::unordered_map< NodeID, Key > nodes;
};

template<typename NodeID = unsigned>
struct _SimpleHeapData {
    NodeID parent;
    _SimpleHeapData( NodeID p ) : parent(p) { }
};

template < typename NodeID, typename Key, typename Weight, typename Data, typename IndexStorage = ArrayStorage<NodeID, NodeID> >
class BinaryHeap {
private:
    BinaryHeap( const BinaryHeap& right );
    void operator=( const BinaryHeap& right );
public:
    typedef Weight WeightType;
    typedef Data DataType;

    BinaryHeap( size_t maxID )
    : nodeIndex( maxID ) {
        Clear();
    }

    void Clear() {
        heap.resize( 1 );
        insertedNodes.clear();
        heap[0].weight = std::numeric_limits< Weight >::min();
        nodeIndex.Clear();
    }

    Key Size() const {
        return static_cast<Key>( heap.size() - 1 );
    }

    void Insert( NodeID node, Weight weight, const Data &data ) {
        HeapElement element;
        element.index = static_cast<NodeID>(insertedNodes.size());
        element.weight = weight;
        const Key key = static_cast<Key>(heap.size());
        heap.push_back( element );
        insertedNodes.push_back( HeapNode( node, key, weight, data ) );
        nodeIndex[node] = element.index;<commit_msg>added auxiliary Empty() function<commit_after>/*

Copyright (c) 2013, Project OSRM, <NAME>, others
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.

*/

#ifndef BINARYHEAP_H_INCLUDED
#define BINARYHEAP_H_INCLUDED

//Not compatible with non contiguous node ids

#include <boost/unordered_map.hpp>

#include <cassert>

#include <algorithm>
#include <limits>
#include <map>
#include <vector>

template< typename NodeID, typename Key >
class ArrayStorage {
public:

    ArrayStorage( size_t size ) : positions( new Key[size] ) {
        memset(positions, 0, size*sizeof(Key));
    }

    ~ArrayStorage() {
        delete[] positions;
    }

    Key &operator[]( NodeID node ) {
        return positions[node];
    }

    void Clear() {}

private:
    Key* positions;
};

template< typename NodeID, typename Key >
class MapStorage {
public:

    MapStorage( size_t ) {}

    Key &operator[]( NodeID node ) {
        return nodes[node];
    }

    void Clear() {
        nodes.clear();
    }

private:
    std::map< NodeID, Key > nodes;

};

template< typename NodeID, typename Key >
class UnorderedMapStorage {
public:

	UnorderedMapStorage( size_t ) {
		//hash table gets 1000 Buckets
		nodes.rehash(1000);
	}

    Key &operator[]( const NodeID node ) {
    	return nodes[node];
    }

    void Clear() {
        nodes.clear();
    }

private:
    boost::unordered_map< NodeID, Key > nodes;
};

template<typename NodeID = unsigned>
struct _SimpleHeapData {
    NodeID parent;
    _SimpleHeapData( NodeID p ) : parent(p) { }
};

template < typename NodeID, typename Key, typename Weight, typename Data, typename IndexStorage = ArrayStorage<NodeID, NodeID> >
class BinaryHeap {
private:
    BinaryHeap( const BinaryHeap& right );
    void operator=( const BinaryHeap& right );
public:
    typedef Weight WeightType;
    typedef Data DataType;

    BinaryHeap( size_t maxID )
    : nodeIndex( maxID ) {
        Clear();
    }

    void Clear() {
        heap.resize( 1 );
        insertedNodes.clear();
        heap[0].weight = std::numeric_limits< Weight >::min();
        nodeIndex.Clear();
    }

    Key Size() const {
        return static_cast<Key>( heap.size() - 1 );
    }

    bool Empty() const {
        return 0 == Size();
    }

    void Insert( NodeID node, Weight weight, const Data &data ) {
        HeapElement element;
        element.index = static_cast<NodeID>(insertedNodes.size());
        element.weight = weight;
        const Key key = static_cast<Key>(heap.size());
        heap.push_back( element );
        insertedNodes.push_back( HeapNode( node, key, weight, data ) );
        nodeIndex[node] = element.index;
{'fraction_non_alphanumeric': 0.09026222163657927, 'fraction_numerical': 0.004348398998550534, 'mean_word_length': 3.501779359430605, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7191075', 'n_tokens_mistral': 2277, 'n_tokens_neox': 2104, 'n_words': 804}