]> git.tdb.fi Git - r2c2.git/commitdiff
Use std::find to locate block iterators in BlockAllocator
authorMikko Rasa <tdb@tdb.fi>
Wed, 19 Jun 2013 13:25:08 +0000 (16:25 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 19 Jun 2013 13:25:08 +0000 (16:25 +0300)
source/libr2c2/blockallocator.cpp
source/libr2c2/blockallocator.h

index f73697a5b45381f8173b045ce70de1ac0abfd590..7dcc15b57acdf268ab6dc92ecf5bad2f91570158 100644 (file)
@@ -15,6 +15,16 @@ using namespace Msp;
 
 namespace R2C2 {
 
+struct BlockAllocator::BlockMatch
+{
+       const Block &block;
+
+       BlockMatch(const Block &b): block(b) { }
+
+       bool operator()(const BlockIter &bi) const { return &*bi==&block; }
+};
+
+
 BlockAllocator::BlockAllocator(Train &t):
        train(t),
        cur_blocks_end(blocks.end()),
@@ -82,7 +92,7 @@ const BlockIter &BlockAllocator::last_current() const
 
 const BlockIter &BlockAllocator::iter_for(const Block &block) const
 {
-       BlockList::const_iterator i = find_block(blocks.begin(), blocks.end(), block);
+       BlockList::const_iterator i = find_if(blocks.begin(), blocks.end(), IterBlockMatch(block));
        if(i==blocks.end())
                throw key_error(&block);
        return *i;
@@ -90,19 +100,12 @@ const BlockIter &BlockAllocator::iter_for(const Block &block) const
 
 bool BlockAllocator::has_block(const Block &block) const
 {
-       return find_block(blocks.begin(), blocks.end(), block)!=blocks.end();
+       return find_if(blocks.begin(), blocks.end(), IterBlockMatch(block))!=blocks.end();
 }
 
 bool BlockAllocator::is_block_current(const Block &block) const
 {
-       return find_block(blocks.begin(), cur_blocks_end, block)!=cur_blocks_end;
-}
-
-BlockAllocator::BlockList::const_iterator BlockAllocator::find_block(const BlockList::const_iterator &begin, const BlockList::const_iterator &end, const Block &block) const
-{
-       BlockList::const_iterator i;
-       for(i=begin; (i!=end && &**i!=&block); ++i) ;
-       return i;
+       return find_if(blocks.begin(), cur_blocks_end, IterBlockMatch(block))!=cur_blocks_end;
 }
 
 void BlockAllocator::reserve_more()
index 6eecbd4e2c531eb0cbbcc49363a0910598fca774..3c974f21975b06933317eb4c068ae408ae4fce80 100644 (file)
@@ -28,6 +28,8 @@ public:
        };
 
 private:
+       struct BlockMatch;
+
        typedef std::list<BlockIter> BlockList;
 
        Train &train;
@@ -52,10 +54,7 @@ public:
 
        bool has_block(const Block &) const;
        bool is_block_current(const Block &) const;
-private:
-       BlockList::const_iterator find_block(const BlockList::const_iterator &, const BlockList::const_iterator &, const Block &) const;
 
-public:
        void reserve_more();
 private:
        bool reserve_block(const BlockIter &);