23 Haziran 2017 Cuma

bimap

Bimap Sınıfı
Giriş
Şu dosya dahil edilir.
#include <boost/bimap.hpp>
Kolay kullanım için şu satırı dahil ederiz.
using namespace boost::bimaps;
Açıklaması şöyle
Boost.Bimap is a bidirectional maps library for C++. With Boost.Bimap you can create associative containers in which both types can be used as key. A bimap<X,Y> can be thought of as a combination of a std::map<X,Y> and a std::map<Y,X>. The learning curve of bimap is almost flat if you know how to use standard containers. A great deal of effort has been put into mapping the naming scheme of the STL in Boost.Bimap. The library is designed to match the common STL containers.
Bimap ile iki yönlü 1 -> 1, 1 <- 1 1 ->N ilişki tutmak mümkün.

Kullanıbilecek Tipler
Şöyle
Side collection type       Dereferenced data
--------------------------------------------
set_of                     constant
multiset_of                constant
unordered_set_of           constant
unordered_multiset_of      constant
list_of                    mutable
vector_of                  mutable
unconstrained_set_of       mutable

Bimap'in left ve right tarafında kullanılan veri yapısı için diğer include dosyaları kullanılır.

Şu satırları dahil ederiz.
#include <boost/bimap/multiset_of.hpp>
#include <boost/bimap/unordered_set_of.hppp>
#include <boost/bimap/set_of.hpp>
Tanımlama - 1 -> 1
Şöyle yaparız.
typedef boost::bimap<int, std::string> bm_type;
bm_type bm;
Tanımlama - 1 -> N
Örnek 1
Şöyle yaparız.
typedef bimap< set_of< std::string >, list_of< int > > bm_type;
bm_type bm;

bm.left["one"] = 1; // "one" -> 1
bm.left["one"] = 2; // replaced: "one" -> 2
Şöyle yaparız.
typedef bimap<int, multiset_of<int> > bm_type;
Örnek 2
Şöyle yaparız.
typedef bimap<set_of<unsigned long int>,
        multiset_of<std::pair<unsigned long int, unsigned long int> > > bm_type;
typedef bimap_type::value_type position;
bimap_type bm;
Tanımlama - N -> 1
Şöyle yaparız.
typedef boost::bimap<bimaps::multiset_of<int>, bimaps::set_of<int>> bimap_t;
Constructor
Şöyle yaparız.
bm_type bm;
insert metodu
insert işlemi için value_type ya da left_value_type ya da right_value_type kullanılır. left ve right birbirlerini ihlal edemezler. Örneğin <int ,int> olan bir bimap'in sol tarafına key = 1 value = 2 insert edersek, sağ tarafa key = 3, value = 1 insert edemeyiz. Şöyle yaparız.
bm.insert (bm_type::value_type (1, 1));
value_type için typedef yaparsak şöyle yaparız.
typedef bimap_reference::value_type position;

bm.insert(position(123456, 100000));

left Alanı
Veri yapısına erişmek için şöyle yaparız.
auto & leftSide = bm.left;
multiset_of Sınıfı
Giriş
Şu satırı dahil ederiz.
#include <boost/bimap/multiset_of.hpp>
std::multimap sınıfı ile aynıdır.

at metodu
Bu metod mevcut değil. Şöyle yapamayız.
auto itt = bm.right.at({100000,50000});
equal_range metodu
Şöyle yaparız.
using ritr = bimap_reference::right_const_iterator;

std::pair<ritr, ritr> range = bm.right.equal_range(100000);

for (auto itr = range.first; itr != range.second, ++itr)
{
  ...
}
find metodu
Örnek 1
Şöyle yaparız. Ancak bu yöntem sadece tek bir nesneyi verir. Sağ tarafı 50000 olan tüm nesneleri bulmak için aslında equal_range kullanılır.
auto it = bm.right.find(std::make_pair(100000,50000));
std::cout<<"from right: "<<it->first.first <<std::endl;    
Örnek 2
Şöyle yaparız.
typedef bimap_t::left_const_iterator l_itr_t;
typedef std::pair<l_itr_t,l_itr_t> l_itr_range_t;

l_itr_range_t ii = bm.left.equal_range(1);

for(l_itr_t it = ii.first; it != ii.second; ++it)
{
...
}  
Örnek 3
Şöyle yaparız.
for (auto p : boost::make_iterator_range(bm.left.equal_range(1)))
{
  ...
}
set_of Sınıfı
Giriş
Şu satırı dahil ederiz.
#include <boost/bimap/set_of.hpp>
at metodu
Şöyle yaparız.
int x = bm.left.at (5);
Şöyle yaparız.
auto it = bm.left.at(123456);
begin metodu
Şöyle yaparız.
typedef boost::bimap<float /*distance*/, int /*id*/> neighbor_list;
neighbor_list node_a;

//fill up neighbors of node_a

//get neighbor id
node_a.neighbor.left.begin()->second;

//get distance
node_a.neighbor.left.begin()->first;
erase metodu
Şöyle yaparız.
bm.right.erase(bm.right.find(3));
operator []
Şöyle yaparız.
bm.left["one"] = 1; // "one" -> 1
Eğer sağ taraftaki nesne mutable değilse (multiset_of gibi) derleme hatası alırız. Açıklaması şöyle
set_of and unordered_set_of map views overload operator[] to retrieve the associated data of a given key only when the other collection type is a mutable one. In these cases it works in the same way as the standard.
Hata şöyledir.
OPERATOR_BRACKET_IS_NOT_SUPPORTED

Hiç yorum yok:

Yorum Gönder