Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions include/meta/caching/maps/locking_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <mutex>

#include "meta/util/optional.h"
#include "meta/hashing/hash.h"

namespace meta
{
Expand Down Expand Up @@ -76,10 +77,10 @@ class locking_map
util::optional<Value> find(const Key& key) const;

/// iterator type for locking_maps
using iterator = typename std::unordered_map<Key, Value>::iterator;
using iterator = typename std::unordered_map<Key, Value, hashing::hash<>>::iterator;
/// const_iterator type for locking_maps
using const_iterator =
typename std::unordered_map<Key, Value>::const_iterator;
typename std::unordered_map<Key, Value, hashing::hash<>>::const_iterator;

/**
* @return an iterator to the beginning of the map
Expand All @@ -103,7 +104,7 @@ class locking_map

private:
/// the underlying map used for storage
std::unordered_map<Key, Value> map_;
std::unordered_map<Key, Value, hashing::hash<>> map_;
/// the mutex that synchronizes accesses into the map
mutable std::mutex mutables_;
};
Expand Down
3 changes: 2 additions & 1 deletion include/meta/caching/shard_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ class generic_shard_cache
* The hash function used for determining which shard a key
* belongs to.
*/
std::hash<Key> hasher_;
// std::hash<Key> hasher_;
hashing::hash<> hasher_;
};

/**
Expand Down
122 changes: 122 additions & 0 deletions include/meta/embeddings/wmd/min_cost_flow.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/**
* @file min_cost_flow.h
* @author lolik111
*
* All files in META are dual-licensed under the MIT and NCSA licenses. For more
* details, consult the file LICENSE.mit and LICENSE.ncsa in the root of the
* project.
*/

#ifndef FAST_EMD_MIN_COST_FLOW_H
#define FAST_EMD_MIN_COST_FLOW_H

#include <cassert>
#include <cmath>
#include <cstdlib>
#include <list>
#include <vector>

namespace meta
{
namespace embeddings
{
template <typename T>
struct edge;

template <typename T>
struct edge_weighted;

template <typename NumT>
class min_cost_flow
{

public:
NumT emd_hat(const std::vector<NumT>& supply,
const std::vector<NumT>& demand,
const std::vector<std::vector<NumT>>& cost);

// e - supply(positive) and demand(negative).
// c[i] - edges that goes from node i. first is the second nod
// x - the flow is returned in it
NumT compute_min_cost_flow(std::vector<NumT>& e,
const std::vector<std::list<edge<NumT>>>& c,
std::vector<std::list<edge_weighted<NumT>>>& x);

private:
size_t _num_nodes;
std::vector<size_t> _nodes_to_demand;

template <typename T>
static T integral_emd_hat(const std::vector<T>& supply,
const std::vector<T>& demand,
const std::vector<std::vector<T>>& cost);

void compute_shortest_path(
std::vector<NumT>& d, std::vector<size_t>& prev,

size_t from, std::vector<std::list<edge<NumT>>>& cost_forward,
std::vector<std::list<edge_weighted<NumT>>>& cost_backward,

const std::vector<NumT>& e, size_t& l);

void heap_decrease_key(std::vector<edge<NumT>>& demand,
std::vector<size_t>& nodes_to_demand, size_t v,
NumT alt);

void heap_remove_first(std::vector<edge<NumT>>& demand,
std::vector<size_t>& nodes_to_demand);

void heapify(std::vector<edge<NumT>>& demand,
std::vector<size_t>& nodes_to_demand, size_t i);

void swap_heap(std::vector<edge<NumT>>& demand,
std::vector<size_t>& nodes_to_demand, size_t i, size_t j);

size_t LEFT(size_t i)
{
return 2 * (i + 1) - 1;
}

size_t RIGHT(size_t i)
{
return 2 * (i + 1); // 2*(i+1)+1-1
}

size_t PARENT(size_t i)
{
return (i - 1) / 2;
}
};
}
}

#include "min_cost_flow.tcc"

#endif // FAST_EMD_MIN_COST_FLOW_H

// Copyright (c) 2009-2012, Ofir Pele
// 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.
// * Neither the name of the The Hebrew University of Jerusalem 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.
Loading