dlvhex  2.5.0
include/dlvhex2/AggregateAtomTable.h
Go to the documentation of this file.
00001 /* dlvhex -- Answer-Set Programming with external interfaces.
00002  * Copyright (C) 2005-2007 Roman Schindlauer
00003  * Copyright (C) 2006-2015 Thomas Krennwallner
00004  * Copyright (C) 2009-2016 Peter Schüller
00005  * Copyright (C) 2011-2016 Christoph Redl
00006  * Copyright (C) 2015-2016 Tobias Kaminski
00007  * Copyright (C) 2015-2016 Antonius Weinzierl
00008  *
00009  * This file is part of dlvhex.
00010  *
00011  * dlvhex is free software; you can redistribute it and/or modify it
00012  * under the terms of the GNU Lesser General Public License as
00013  * published by the Free Software Foundation; either version 2.1 of
00014  * the License, or (at your option) any later version.
00015  *
00016  * dlvhex is distributed in the hope that it will be useful, but
00017  * WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019  * Lesser General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU Lesser General Public
00022  * License along with dlvhex; if not, write to the Free Software
00023  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00024  * 02110-1301 USA.
00025  */
00026 
00034 #ifndef AGGREGATEATOMTABLE_HPP_INCLUDED__12102010
00035 #define AGGREGATEATOMTABLE_HPP_INCLUDED__12102010
00036 
00037 #include "dlvhex2/PlatformDefinitions.h"
00038 #include "dlvhex2/Atoms.h"
00039 #include "dlvhex2/Table.h"
00040 
00041 DLVHEX_NAMESPACE_BEGIN
00042 
00044 class AggregateAtomTable:
00045 public Table<
00046 // value type is symbol struct
00047 AggregateAtom,
00048 // index is
00049 boost::multi_index::indexed_by<
00050 // address = running ID for constant access
00051 boost::multi_index::random_access<
00052 boost::multi_index::tag<impl::AddressTag>
00053 >
00054 >
00055 >
00056 {
00057     // types
00058     public:
00059         typedef Container::index<impl::AddressTag>::type AddressIndex;
00060         //typedef Container::index<impl::KindTag>::type KindIndex;
00061         //typedef Container::index<impl::TupleTag>::type TupleIndex;
00062 
00063         // methods
00064     public:
00071         inline const AggregateAtom& getByID(ID id) const throw ();
00072 
00076         inline ID storeAndGetID(const AggregateAtom& atom) throw();
00077 };
00078 
00079 // retrieve by ID
00080 // assert that id.kind is correct for Term
00081 // assert that ID exists
00082 const AggregateAtom&
00083 AggregateAtomTable::getByID(
00084 ID id) const throw ()
00085 {
00086     assert(id.isAtom() || id.isLiteral());
00087     assert(id.isAggregateAtom());
00088 
00089     ReadLock lock(mutex);
00090     const AddressIndex& idx = container.get<impl::AddressTag>();
00091     // the following check only works for random access indices, but here it is ok
00092     assert( id.address < idx.size() );
00093     return idx.at(id.address);
00094 }
00095 
00096 
00097 // store symbol, assuming it does not exist (this is only asserted)
00098 ID AggregateAtomTable::storeAndGetID(
00099 const AggregateAtom& atm) throw()
00100 {
00101     assert(ID(atm.kind,0).isAtom());
00102     assert(ID(atm.kind,0).isAggregateAtom());
00103     assert(!atm.tuple.empty());
00104 
00105     AddressIndex::const_iterator it;
00106     bool success;
00107 
00108     WriteLock lock(mutex);
00109     AddressIndex& idx = container.get<impl::AddressTag>();
00110     boost::tie(it, success) = idx.push_back(atm);
00111     (void)success;
00112     assert(success);
00113 
00114     return ID(
00115         atm.kind,                // kind
00116                                  // address
00117         container.project<impl::AddressTag>(it) - idx.begin()
00118         );
00119 }
00120 
00121 
00122 DLVHEX_NAMESPACE_END
00123 #endif                           // AGGREGATEATOMTABLE_HPP_INCLUDED__12102010
00124 
00125 // vim:expandtab:ts=4:sw=4:
00126 // mode: C++
00127 // End: