15

Click here to load reader

Brief introduction of Boost.ICL

  • Upload
    yak1ex

  • View
    1.874

  • Download
    0

Embed Size (px)

DESCRIPTION

Not yet presented anywhere. Uploaded via PDF, thus layout is nearer from original but without notes

Citation preview

Page 1: Brief introduction of Boost.ICL

ICL(Interval Container Library)

2011/2/26 Boost 勉強会 #4 未発表

@yak_ex / 新康孝

詳解紹介

Page 2: Brief introduction of Boost.ICL

自己紹介

• 氏名: 新康孝 (あたらし やすたか)

• Twitter ID: yak_ex

• Web: http://yak3.myhome.cx:8080/junks

• C++ / Perl が主戦場

• 現在、仕事でコードに触れていないので競技プログラミング(TopCoder、Codeforces)で潤い補充

• 闇の軍団に憧れるただの C++ 好き– 今回は ICL のさわりだけ、というかさわりしか分からない

Page 3: Brief introduction of Boost.ICL

ぼくのかんがえた「ぶーすと」のぶんるい

実装より

アプリより

大規模小規模

Page 4: Brief introduction of Boost.ICL

実装より

アプリより

大規模小規模

operatorstimer rational type_traits

random

array

graph

regex

lexical_cast

iterators

test

crc

~1.25

any

function

tuple

thread

tokenizerpool

ぼくのかんがえた「ぶーすと」のぶんるい

progress_display たんはココ

Page 5: Brief introduction of Boost.ICL

実装より

アプリより

大規模小規模

timer rational type_traits

random

array

graph

regex

lexical_cast

iterators

test

crc

~1.30

any

function

tuple

thread

preprocessor

異次元

lambda

date_time

format

multi_array

filesystem

mploptional

spirit

tokenizerpool

ぼくのかんがえた「ぶーすと」のぶんるい

progress_display たんはココ

Page 6: Brief introduction of Boost.ICL

timer rational

実装より

アプリより

大規模小規模

type_traits

random

array

graph

regex

lexical_cast

iterators

test

crc

~1.35

any

function

tuple

thread

preprocessor

異次元

lambda

date_time

format

multi_array

filesystem

mploptional

spirit

variant

multi_index

program_options

range

serialization

parameterptr_container

wave

foreach

statechart

typeof

xpressive

asio

bimap

gil

fusion

interprocess

intrusive

tokenizerpool

ぼくのかんがえた「ぶーすと」のぶんるい

progress_display たんはココ

Page 7: Brief introduction of Boost.ICL

timer rational

実装より

アプリより

大規模小規模

type_traits

random

array

graph

regex

lexical_cast

iterators

test

crc

~1.40

any

function

tuple

thread

preprocessor

異次元

lambda

date_time

format

multi_array

filesystem

mploptional

spirit

variant

multi_index

program_options

range

serialization

parameterptr_container

wave

foreach

statechart

typeof

xpressive

asio

bimap

gil

fusion

interprocess

intrusive

accmulators

unordered

proto

scope_exit

flyweight

tokenizerpool

ぼくのかんがえた「ぶーすと」のぶんるい

progress_display たんはココ

Page 8: Brief introduction of Boost.ICL

timer rational

実装より

アプリより

大規模小規模

type_traits

random

array

graph

regex

lexical_cast

iterators

test

crc

~1.45

any

function

tuple

thread

preprocessor

異次元

lambda

date_time

format

multi_array

filesystem

mploptional

spirit

variant

multi_index

program_options

range

serialization

parameterptr_container

wave

foreach

statechart

typeof

xpressive

asio

bimap

gil

fusion

interprocess

intrusive

accmulators

unordered

proto

scope_exit

flyweight

tokenizer

property_tree

uuid

pool

msm

polygon

ぼくのかんがえた「ぶーすと」のぶんるい

progress_display たんはココ

Page 9: Brief introduction of Boost.ICL

timer rational

実装より

アプリより

大規模小規模

type_traits

random

array

graph

regex

lexical_cast

iterators

test

crc

~1.46

any

function

tuple

thread

preprocessor

異次元

lambda

date_time

format

multi_array

filesystem

mploptional

spirit

variant

multi_index

program_options

range

serialization

parameterptr_container

wave

foreach

statechart

typeof

xpressive

asio

bimap

gil

fusion

interprocess

intrusive

accmulators

unordered

proto

scope_exit

flyweight

tokenizer

property_tree

uuid

pool

msm

polygonICL

ぼくのかんがえた「ぶーすと」のぶんるい

progress_display たんはココ

Page 10: Brief introduction of Boost.ICL

ここで競技コーディングのお時間です• akira さん(仮名)はパーティーを開くことになりました。N 人のお客さんが参加する予定ですが参加時間帯[si, ti)が皆ばらばらです。場所の予約のために参加人数の最も多い時間帯を知りたいのでプログラムを作って akira さんを助けてあげましょう。

– 1 ≦ N ≦ 1000

– -109 ≦ si < ti ≦ 109, si, ti は整数 (i=1,2,…,N)

– 参加人数が同じ時間帯が隣接している場合、参加者が異なっていても一つの時間帯と見なす

– 同一参加人数の時間帯が複数ある場合は最長の時間帯を、同じ長さの時間帯が複数ある場合は開始時刻がもっとも早い時間帯を返す

– 入力: N<改行>s1<空白>t1<改行>…sN<空白>tN<改行>

– 出力:開始時刻<空白>終了時刻<空白>人数<改行>

Page 11: Brief introduction of Boost.ICL

解答例全文 using ICL#include <iostream>

#include <algorithm>

#include <utility>

#include <boost/icl/interval_map.hpp>

int main(void)

{

boost::icl::interval_map<int, int> sum;

int n;

std::cin >> n;

for(int i = 0; i < n; ++i) {

int s, t;

std::cin >> s >> t;

sum += std::make_pair(boost::icl::interval<int>::right_open(s, t), 1);

}

typedef boost::icl::interval_map<int, int>::value_type value_type;

auto it = max_element(sum.begin(), sum.end(), [](const value_type &v1, const value_type &v2) {

return

v1.second < v2.second ||

(v1.second == v2.second && v1.first.upper() - v1.first.lower() < v2.first.upper() - v2.first.lower()) ||

(v1.second == v2.second && v1.first.upper() - v1.first.lower() == v2.first.upper() - v2.first.lower() &&

v1.first.lower() < v2.first.lower());

});

std::cout << it->first.lower() << ' ' << it->first.upper() << ' ' << it->second << std::endl;

return 0;

}

#include 含めて 25 行

Page 12: Brief introduction of Boost.ICL

解答例全文 using ICL#include <iostream>

#include <algorithm>

#include <utility>

#include <boost/icl/interval_map.hpp>

int main(void)

{

boost::icl::interval_map<int, int> sum;

int n;

std::cin >> n;

for(int i = 0; i < n; ++i) {

int s, t;

std::cin >> s >> t;

sum += std::make_pair(boost::icl::interval<int>::right_open(s, t), 1);

}

typedef boost::icl::interval_map<int, int>::value_type value_type;

auto it = max_element(sum.begin(), sum.end(), [](const value_type &v1, const value_type &v2) {

return

v1.second < v2.second ||

(v1.second == v2.second && v1.first.upper() - v1.first.lower() < v2.first.upper() - v2.first.lower()) ||

(v1.second == v2.second && v1.first.upper() - v1.first.lower() == v2.first.upper() - v2.first.lower() &&

v1.first.lower() < v2.first.lower());

});

std::cout << it->first.lower() << ' ' << it->first.upper() << ' ' << it->second << std::endl;

return 0;

}

ヘッダのインクルード

入力と登録

最大値取得

出力

Page 13: Brief introduction of Boost.ICL

解答例入力と登録boost::icl::interval_map<int, int> sum;

int n;

std::cin >> n;

for(int i = 0; i < n; ++i) {

int s, t;

std::cin >> s >> t;

sum += std::make_pair(boost::icl::interval<int>::right_open(s, t), 1);

}

右開区間 [s, t)

1

1

1 1

2

+1

1

+

1 1

1

split_interval_map

interval_map

今回は数だが集合も可能

Page 14: Brief introduction of Boost.ICL

解答例最大値取得typedef boost::icl::interval_map<int, int>::value_type value_type;

auto it = max_element(sum.begin(), sum.end(), [](const value_type &v1, const value_type &v2) {

return

v1.second < v2.second ||

(v1.second == v2.second &&

v1.first.upper() - v1.first.lower() < v2.first.upper() - v2.first.lower()) ||

(v1.second == v2.second &&

v1.first.upper() - v1.first.lower() == v2.first.upper() - v2.first.lower() &&

v1.first.lower() < v2.first.lower());

});

// 集計値は second

// 区間は first (upper(), lower() で境界値)

集計された区間の結果を iteration 可能なので普通に max_element で最大値取得

Page 15: Brief introduction of Boost.ICL

Interval Container Library• 1.46 で導入• 開区間、閉区間等全部取り扱い可能• 区間に対する集合演算、「集計」が可能• interval_set,

interval_map

• 区間の統合方法として join, separate, split を選択可能• ストリーム出力有り(例:{(1,3][4.6](7,8]})• Traits 用意すれば自前の interval を突っ込むことも可能• 数値に限定されないので例えば

cotinuous_interval<std::string> w(“a”, “c”) // right_open

とか書くと a, b で始まる文字列全部を表すことになる。• Example がいっぱいなのでそれ見ればOK

interval_set a

interval_set b

interval_set a - b