dlvhex  2.5.0
include/dlvhex2/BuiltinAtomTable.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 BUILTINATOMTABLE_HPP_INCLUDED__12102010
00035 #define BUILTINATOMTABLE_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 BuiltinAtomTable:
00045 public Table<
00046 // value type is symbol struct
00047 BuiltinAtom,
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 BuiltinAtom& getByID(ID id) const throw ();
00072 
00076         inline ID storeAndGetID(const BuiltinAtom& atom) throw();
00077 };
00078 
00079 // retrieve by ID
00080 // assert that id.kind is correct for Term
00081 // assert that ID exists
00082 const BuiltinAtom&
00083 BuiltinAtomTable::getByID(
00084 ID id) const throw ()
00085 {
00086     assert(id.isAtom() || id.isLiteral());
00087     assert(id.isBuiltinAtom());
00088     ReadLock lock(mutex);
00089     const AddressIndex& idx = container.get<impl::AddressTag>();
00090     // the following check only works for random access indices, but here it is ok
00091     assert( id.address < idx.size() );
00092     return idx.at(id.address);
00093 }
00094 
00095 
00096 // store symbol, assuming it does not exist (this is only asserted)
00097 ID BuiltinAtomTable::storeAndGetID(
00098 const BuiltinAtom& atm) throw()
00099 {
00100     assert(ID(atm.kind,0).isAtom());
00101     assert(ID(atm.kind,0).isBuiltinAtom());
00102     assert(!atm.tuple.empty());
00103 
00104     bool success;
00105     AddressIndex::const_iterator it;
00106 
00107     WriteLock lock(mutex);
00108     AddressIndex& idx = container.get<impl::AddressTag>();
00109     boost::tie(it, success) = idx.push_back(atm);
00110     (void)success;
00111     assert(success);
00112 
00113     return ID(
00114         atm.kind,                // kind
00115                                  // address
00116         container.project<impl::AddressTag>(it) - idx.begin()
00117         );
00118 }
00119 
00120 
00121 DLVHEX_NAMESPACE_END
00122 #endif                           // BUILTINATOMTABLE_HPP_INCLUDED__12102010
00123 
00124 // vim:expandtab:ts=4:sw=4:
00125 // mode: C++
00126 // End: