/* * Copyright 2014 Google Inc. All rights reserved. * * 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. */ #ifndef SEMISTATIC_GRAPH_DEFN_H #define SEMISTATIC_GRAPH_DEFN_H #include namespace fruit { namespace impl { inline bool SemistaticGraphInternalNodeId::operator==(const SemistaticGraphInternalNodeId& x) const { return id == x.id; } inline bool SemistaticGraphInternalNodeId::operator<(const SemistaticGraphInternalNodeId& x) const { return id < x.id; } template inline SemistaticGraph::node_iterator::node_iterator(NodeData* itr) : itr(itr) {} template inline Node& SemistaticGraph::node_iterator::getNode() { FruitAssert(itr->edges_begin != 1); return itr->node; } template inline bool SemistaticGraph::node_iterator::isTerminal() { FruitAssert(itr->edges_begin != 1); return itr->edges_begin == 0; } template inline void SemistaticGraph::node_iterator::setTerminal() { FruitAssert(itr->edges_begin != 1); itr->edges_begin = 0; } template inline bool SemistaticGraph::node_iterator::operator==(const node_iterator& other) const { return itr == other.itr; } template inline SemistaticGraph::const_node_iterator::const_node_iterator(const NodeData* itr) : itr(itr) {} template inline SemistaticGraph::const_node_iterator::const_node_iterator(node_iterator itr) : itr(itr.itr) {} template inline const Node& SemistaticGraph::const_node_iterator::getNode() { FruitAssert(itr->edges_begin != 1); return itr->node; } template inline bool SemistaticGraph::const_node_iterator::isTerminal() { FruitAssert(itr->edges_begin != 1); return itr->edges_begin == 0; } template inline bool SemistaticGraph::const_node_iterator::operator==(const const_node_iterator& other) const { return itr == other.itr; } template inline typename SemistaticGraph::edge_iterator SemistaticGraph::node_iterator::neighborsBegin() { FruitAssert(itr->edges_begin != 0); FruitAssert(itr->edges_begin != 1); return edge_iterator{reinterpret_cast(itr->edges_begin)}; } template inline SemistaticGraph::edge_iterator::edge_iterator(InternalNodeId* itr) : itr(itr) {} template inline typename SemistaticGraph::node_iterator SemistaticGraph::edge_iterator::getNodeIterator(node_iterator nodes_begin) { return node_iterator{nodeAtId(nodes_begin.itr, *itr)}; } template inline void SemistaticGraph::edge_iterator::operator++() { ++itr; } template inline typename SemistaticGraph::node_iterator SemistaticGraph::edge_iterator::getNodeIterator(std::size_t i, node_iterator nodes_begin) { itr += i; return getNodeIterator(nodes_begin); } template inline typename SemistaticGraph::node_iterator SemistaticGraph::begin() { return node_iterator{nodes.begin()}; } template inline typename SemistaticGraph::node_iterator SemistaticGraph::end() { return node_iterator{nodes.end()}; } template inline typename SemistaticGraph::const_node_iterator SemistaticGraph::end() const { return const_node_iterator{nodes.end()}; } template inline typename SemistaticGraph::node_iterator SemistaticGraph::at(NodeId nodeId) { InternalNodeId internalNodeId = node_index_map.at(nodeId); return node_iterator{nodeAtId(internalNodeId)}; } template inline typename SemistaticGraph::const_node_iterator SemistaticGraph::find(NodeId nodeId) const { const InternalNodeId* internalNodeIdPtr = node_index_map.find(nodeId); if (internalNodeIdPtr == nullptr) { return const_node_iterator{nodes.end()}; } else { const NodeData* p = nodeAtId(*internalNodeIdPtr); if (p->edges_begin == 1) { return const_node_iterator{nodes.end()}; } return const_node_iterator{p}; } } template inline typename SemistaticGraph::node_iterator SemistaticGraph::find(NodeId nodeId) { const InternalNodeId* internalNodeIdPtr = node_index_map.find(nodeId); if (internalNodeIdPtr == nullptr) { return node_iterator{nodes.end()}; } else { NodeData* p = nodeAtId(*internalNodeIdPtr); if (p->edges_begin == 1) { return node_iterator{nodes.end()}; } return node_iterator{p}; } } template inline typename SemistaticGraph::NodeData* SemistaticGraph::nodeAtId(InternalNodeId internalNodeId) { return nodeAtId(nodes.data(), internalNodeId); } template inline const typename SemistaticGraph::NodeData* SemistaticGraph::nodeAtId(InternalNodeId internalNodeId) const { return nodeAtId(nodes.data(), internalNodeId); } template inline typename SemistaticGraph::NodeData* SemistaticGraph::nodeAtId(NodeData* nodes_begin, InternalNodeId internalNodeId) { FruitAssert(internalNodeId.id % sizeof(NodeData) == 0); NodeData* p = reinterpret_cast(reinterpret_cast(nodes_begin) + internalNodeId.id); // The code above is faster (the compiler doesn't have to worry about internalNodeId.id%sizeof(NodeData), that we know // to be 0). FruitAssert(p == nodes_begin + internalNodeId.id / sizeof(NodeData)); return p; } template inline const typename SemistaticGraph::NodeData* SemistaticGraph::nodeAtId(const NodeData* nodes_begin, InternalNodeId internalNodeId) { FruitAssert(internalNodeId.id % sizeof(NodeData) == 0); const NodeData* p = reinterpret_cast(reinterpret_cast(nodes_begin) + internalNodeId.id); // The code above is faster (the compiler doesn't have to worry about internalNodeId.id%sizeof(NodeData), that we know // to be 0). FruitAssert(p == nodes_begin + internalNodeId.id / sizeof(NodeData)); return p; } } // namespace impl } // namespace fruit #endif // SEMISTATIC_GRAPH_INLINES_H