28 Mayıs 2017 Pazar

graph out_edges metodu

Giriş
in_edges() metodu ile kardeştir.

Bir grap içinde birden çok iterator tipi bulunur. Bunlar vertex_iterator ve out_edge_iterator olabilir.
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS,
  VertexProperty, EdgeProperty > GraphType;

typedef typename boost::graph_traits< GraphType >::vertex_descriptor VertexType;
typedef typename boost::graph_traits<GraphType>::vertex_iterator VertexIterator;

typedef typename boost::graph_traits< GraphType >::edge_descriptor   EdgeType;
typedef typename boost::graph_traits<GraphType>::out_edge_iterator EdgeIterator;

out_edges metodu
Açıklaması şöyle. Bir out_edge_iterator çifti döndürür.
Iterators relate to a logical range, not the graph. Iterators may not be valid between different ranges of the same graph

Instead, descriptors are graph-wide. Depending on graph model, descriptors may be more stable: if an operation invalidates iterators, it doesn't necessarily invalidate the descriptors corresponding to the same graph elements.

In other words, this makes descriptors more usable as vertex or edge "ID" - or, as Boost Graph would call it, vertex_index or edge_index properties.
Örnek
Şöyle yaparız.
auto range1 = out_edges(vertex1, g);
auto range2 = out_edges(vertex2, g);

assert(range1.first != range2.first); // unspecified or undefined
Örnek
Elimizde şöyle bir Graph olsun.
class VertexInfo
{
public:
    VertexInfo(int i) : id(i) {}
    int id;
};

typedef boost::adjacency_list< boost::setS,
                               boost::listS,    
                               boost::bidirectionalS,
                               VertexInfo,
                               boost::no_property,
                               boost::no_property,
                               boost::listS
                             > Graph;
Bu graph'e vertex ve edge ekleyelim.
Graph g;

Vertex src  = boost::add_vertex(VertexInfo(0), g);
Vertex tar1 = boost::add_vertex(VertexInfo(5), g);
Vertex tar2 = boost::add_vertex(VertexInfo(2), g);
Vertex tar3 = boost::add_vertex(VertexInfo(8), g);

boost::add_edge(src, tar1, g);
boost::add_edge(src, tar2, g);
boost::add_edge(src, tar3, g);
Sonra 0'daki tüm edge'leri dolaşalım.
typename boost::graph_traits<Graph>::out_edge_iterator ei, ei_end;
for(boost::tie(ei, ei_end) = boost::out_edges(src, g); ei != ei_end; ++ei)
{
  std::cout << g[boost::source(*ei, g)].id 
            << " --> " 
            << g[boost::target(*ei, g)].id 
            << std::endl;
}
Çıktı olarak şunu alırız.
0 --> 5
0 --> 2
0 --> 8

26 Mayıs 2017 Cuma

serialization text_oarchive Sınıfı

Giriş
Şu satırı dahil ederiz.
#include <boost/archive/text_oarchive.hpp>
boost serialization kendi "pluggable archive format" yapımızı kullanabilmye imkan tanır. Boost serialization dışında C++11 ile çalışan Cereal serialization kütüphanesi de iyi

Constructor - std::cout
Şöyle yaparız.
boost::archive::text_oarchive ar (std::cout);
Constructor - std::ofstream
Şöyle yaparız.
std::ofstream osf ("test.txt", std::ios::binary);
boost::archive::text_oarchive ar (ofs);
Dosyanın otomatik kapanması için blok içinde kullanmak gerekebilir. Şöyle yaparız.
{
  Foo foo;
  ...
  std::ofstream ofs("foo.txt");
  boost::archive::text_oarchive ar (ofs);
   ar << foo;
}
Constructor - std::ostringstream
Şöyle  yaparız.
std::ostringstream os;
boost::archive::text_oarchive ar (os);
Constructor - boost::asio::ip::tcp::iostream
Şöyle yaparız.
boost::asio::ip::tcp::iostream stream("127.0.0.1", "3000");

boost::archive::text_oarchive archive(stream);
...

stream.close();
operator << metodu
Örnek
Elimizde bir sınıf olsun. Şöyle  yaparız.
MyClass obj;
obj.a = "hello";
obj.b = "world";
ar & msg;
Örnek
Elimizde bir variant olsun. Şöyle yaparız.
using V = boost::variant<std::string, double, int>;

ar << std::vector<V> { 3.14, "std::string", 42 };
Çıktı olarak şunu alırız.
22 serialization::archive 14 0 0 3 0 0 0 1 3.14000000000000012e+00 0 
11 std::string 2 42
Örnek
Şöyle yaparız.
oa << boost::serialization::make_nvp("foo",foo);
Örnek
İki veya daha fazla kez << metodunu çağırabiliriz. Şöyle yaparız.
boost::archive::text_oarchive oa (ofs);
// write class instance to archive
oa << g <<g1;
register_type metodu
Şöyle yaparız.
oa.register_type<Foo>();
Sanırım aynı şeyi şöyle de yapabiliyoruz.
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &ar, const unsigned int version)
{
  ar.template register_type<Foo>();
  ...
}