Boost库之circular_buffer

Boost库之circular_buffer

原文:http://blog.csdn.net/byxdaz/article/details/71631470

         Boost.Circular_buffer维护了一块连续内存块作为缓存区,当缓存区内的数据存满时,继续存入数据就覆盖掉旧的数据。Boost.Circular_buffer维护了一块连续内存块作为缓存区,当缓存区内的数据存满时,继续存入数据就覆盖掉旧的数据。它是一个与STL兼容的容器,类似于 std::liststd::deque,并且支持随机存取。circular_buffer 被特别设计为提供固定容量的存储大小。当其容量被用完时,新插入的元素会覆盖缓冲区头部或尾部(取决于使用何种插入操作)的元素。

[cpp]  view plain  copy
  1. #include <boost/thread/locks.hpp>  
  2. #include <boost/thread/shared_mutex.hpp>  
  3. #include <boost/circular_buffer.hpp>  
  4. typedef unsigned int size_type;  
  5.   
  6. template<typename T>  
  7. class SafeCircularBuffer  
  8. {  
  9. public:  
  10.     SafeCircularBuffer(int size) :  
  11.       buffer_(size)  
  12.       {  
  13.       };  
  14.       size_type capacity()  
  15.       {  
  16.           boost::shared_lock< boost::shared_mutex > read_lock(buffer_mutex_);  
  17.           return buffer_.capacity();  
  18.       };  
  19.       void set_capacity(size_type new_capacity)  
  20.       {  
  21.           boost::unique_lock< boost::shared_mutex > write_lock(buffer_mutex_);  
  22.           buffer_.set_capacity(new_capacity);  
  23.       };  
  24.       size_type size() const  
  25.       {  
  26.           boost::shared_lock< boost::shared_mutex > read_lock(buffer_mutex_);  
  27.           return buffer_.size();  
  28.       };  
  29.       bool push_back(const T& item)  
  30.       {  
  31.           boost::unique_lock< boost::shared_mutex > write_lock(buffer_mutex_);  
  32.           if (buffer_.capacity() == 0)  
  33.               return false;  
  34.   
  35.           if (!buffer_.full())  
  36.           {  
  37.               buffer_.push_back(item);  
  38.               return true;  
  39.           }  
  40.   
  41.           return false;  
  42.       };  
  43.       bool push_front(const T& item)  
  44.       {  
  45.           boost::unique_lock< boost::shared_mutex > write_lock(buffer_mutex_);  
  46.           if (buffer_.capacity() == 0)  
  47.               return false;  
  48.           buffer_.push_front(item);  
  49.           return true;  
  50.       };  
  51.       void clear()  
  52.       {  
  53.           boost::unique_lock< boost::shared_mutex > write_lock(buffer_mutex_);  
  54.           buffer_.clear();  
  55.       };  
  56.       T& front()  
  57.       {  
  58.           boost::shared_lock< boost::shared_mutex > read_lock(buffer_mutex_);  
  59.           return buffer_.front();  
  60.       };  
  61.       void pop_front()  
  62.       {  
  63.           boost::unique_lock< boost::shared_mutex > write_lock(buffer_mutex_);  
  64.           buffer_.pop_front();  
  65.       };  
  66.       void pop_back()  
  67.       {  
  68.           boost::unique_lock< boost::shared_mutex > write_lock(buffer_mutex_);  
  69.           buffer_.pop_back();  
  70.       };  
  71.       unsigned int size()  
  72.       {  
  73.           boost::shared_lock< boost::shared_mutex > read_lock(buffer_mutex_);  
  74.           return buffer_.size();  
  75.       };  
  76.       bool empty()  
  77.       {  
  78.           boost::shared_lock< boost::shared_mutex > read_lock(buffer_mutex_);  
  79.           return buffer_.empty();  
  80.       };  
  81.       bool full()  
  82.       {  
  83.           boost::shared_lock< boost::shared_mutex > read_lock(buffer_mutex_);  
  84.           return buffer_.full();  
  85.       };  
  86.       T& at(size_t index)  
  87.       {  
  88.           boost::shared_lock< boost::shared_mutex > read_lock(buffer_mutex_);  
  89.           try  
  90.           {  
  91.               buffer_.at(index);  
  92.           }  
  93.           catch(std::out_of_range& ex)  
  94.           {  
  95.               throw(ex);  
  96.           }  
  97.           return buffer_[index];  
  98.       };  
  99.       T& operator[](size_t index)  
  100.       {  
  101.           boost::shared_lock< boost::shared_mutex > read_lock(buffer_mutex_);  
  102.           return buffer_[index];  
  103.       };  
  104. private:  
  105.     boost::circular_buffer<T> buffer_;  
  106.     boost::shared_mutex  buffer_mutex_;         //读写锁  
  107. };  
  108.   
  109. #include <boost/circular_buffer.hpp>  
  110. #include <iostream>  
  111. using namespace std;  
  112.   
  113. int main()  
  114. {  
  115.     //非线程安全circular_buffer  
  116.     typedef boost::circular_buffer<int> circular_buffer;  
  117.     circular_buffer cb(3);  
  118.     cout << cb.capacity() <<endl;  
  119.     cout << cb.size() <<endl;  
  120.   
  121.     cb.push_back(0);  
  122.     cb.push_back(1);  
  123.     cb.push_back(2);  
  124.     cout << cb.size() <<endl;  
  125.   
  126.     //设置新容量大小  
  127.     cb.set_capacity(6);  
  128.     cb.push_back(3);  
  129.     cb.push_back(4);  
  130.     cb.push_back(5);  
  131.     cout << cb.size() << endl;  
  132.       
  133.     //数据循环(从某个位置开始)  
  134.     for(int i=0; i<cb.size(); i++)  
  135.     {  
  136.         cout << cb[i];  
  137.         if(i != cb.size()-1)  
  138.         {  
  139.             cout << ",";  
  140.         }  
  141.     }  
  142.     cout<<endl;  
  143.     cb.rotate(cb.begin()+1);  
  144.     for(int i=0; i<cb.size(); i++)  
  145.     {  
  146.         cout << cb[i];  
  147.         if(i != cb.size()-1)  
  148.         {  
  149.             cout << ",";  
  150.         }  
  151.     }  
  152.     cout<<endl;  
  153.   
  154.     //线程安全circular_buffer  
  155.     SafeCircularBuffer<double> scbCircularBuffer(3);  
  156.     cout << scbCircularBuffer.capacity() <<endl;  
  157.     cout << scbCircularBuffer.size() <<endl;  
  158.   
  159.     scbCircularBuffer.push_back(1.0999f);  
  160.     scbCircularBuffer.push_back(1.1f);  
  161.     scbCircularBuffer.push_back(2.2f);  
  162.   
  163.     cout << scbCircularBuffer.size() <<endl;  
  164.     scbCircularBuffer.push_back(3.3f);  
  165.     cout << scbCircularBuffer[0]<<endl;  
  166.     int k = 0;  
  167. }  

Boost库之circular_buffer

  514人阅读  评论(0)  收藏  举报
  分类:
boost(14) 

         Boost.Circular_buffer维护了一块连续内存块作为缓存区,当缓存区内的数据存满时,继续存入数据就覆盖掉旧的数据。Boost.Circular_buffer维护了一块连续内存块作为缓存区,当缓存区内的数据存满时,继续存入数据就覆盖掉旧的数据。它是一个与STL兼容的容器,类似于 std::liststd::deque,并且支持随机存取。circular_buffer 被特别设计为提供固定容量的存储大小。当其容量被用完时,新插入的元素会覆盖缓冲区头部或尾部(取决于使用何种插入操作)的元素。

[cpp]  view plain  copy
  1. #include <boost/thread/locks.hpp>  
  2. #include <boost/thread/shared_mutex.hpp>  
  3. #include <boost/circular_buffer.hpp>  
  4. typedef unsigned int size_type;  
  5.   
  6. template<typename T>  
  7. class SafeCircularBuffer  
  8. {  
  9. public:  
  10.     SafeCircularBuffer(int size) :  
  11.       buffer_(size)  
  12.       {  
  13.       };  
  14.       size_type capacity()  
  15.       {  
  16.           boost::shared_lock< boost::shared_mutex > read_lock(buffer_mutex_);  
  17.           return buffer_.capacity();  
  18.       };  
  19.       void set_capacity(size_type new_capacity)  
  20.       {  
  21.           boost::unique_lock< boost::shared_mutex > write_lock(buffer_mutex_);  
  22.           buffer_.set_capacity(new_capacity);  
  23.       };  
  24.       size_type size() const  
  25.       {  
  26.           boost::shared_lock< boost::shared_mutex > read_lock(buffer_mutex_);  
  27.           return buffer_.size();  
  28.       };  
  29.       bool push_back(const T& item)  
  30.       {  
  31.           boost::unique_lock< boost::shared_mutex > write_lock(buffer_mutex_);  
  32.           if (buffer_.capacity() == 0)  
  33.               return false;  
  34.   
  35.           if (!buffer_.full())  
  36.           {  
  37.               buffer_.push_back(item);  
  38.               return true;  
  39.           }  
  40.   
  41.           return false;  
  42.       };  
  43.       bool push_front(const T& item)  
  44.       {  
  45.           boost::unique_lock< boost::shared_mutex > write_lock(buffer_mutex_);  
  46.           if (buffer_.capacity() == 0)  
  47.               return false;  
  48.           buffer_.push_front(item);  
  49.           return true;  
  50.       };  
  51.       void clear()  
  52.       {  
  53.           boost::unique_lock< boost::shared_mutex > write_lock(buffer_mutex_);  
  54.           buffer_.clear();  
  55.       };  
  56.       T& front()  
  57.       {  
  58.           boost::shared_lock< boost::shared_mutex > read_lock(buffer_mutex_);  
  59.           return buffer_.front();  
  60.       };  
  61.       void pop_front()  
  62.       {  
  63.           boost::unique_lock< boost::shared_mutex > write_lock(buffer_mutex_);  
  64.           buffer_.pop_front();  
  65.       };  
  66.       void pop_back()  
  67.       {  
  68.           boost::unique_lock< boost::shared_mutex > write_lock(buffer_mutex_);  
  69.           buffer_.pop_back();  
  70.       };  
  71.       unsigned int size()  
  72.       {  
  73.           boost::shared_lock< boost::shared_mutex > read_lock(buffer_mutex_);  
  74.           return buffer_.size();  
  75.       };  
  76.       bool empty()  
  77.       {  
  78.           boost::shared_lock< boost::shared_mutex > read_lock(buffer_mutex_);  
  79.           return buffer_.empty();  
  80.       };  
  81.       bool full()  
  82.       {  
  83.           boost::shared_lock< boost::shared_mutex > read_lock(buffer_mutex_);  
  84.           return buffer_.full();  
  85.       };  
  86.       T& at(size_t index)  
  87.       {  
  88.           boost::shared_lock< boost::shared_mutex > read_lock(buffer_mutex_);  
  89.           try  
  90.           {  
  91.               buffer_.at(index);  
  92.           }  
  93.           catch(std::out_of_range& ex)  
  94.           {  
  95.               throw(ex);  
  96.           }  
  97.           return buffer_[index];  
  98.       };  
  99.       T& operator[](size_t index)  
  100.       {  
  101.           boost::shared_lock< boost::shared_mutex > read_lock(buffer_mutex_);  
  102.           return buffer_[index];  
  103.       };  
  104. private:  
  105.     boost::circular_buffer<T> buffer_;  
  106.     boost::shared_mutex  buffer_mutex_;         //读写锁  
  107. };  
  108.   
  109. #include <boost/circular_buffer.hpp>  
  110. #include <iostream>  
  111. using namespace std;  
  112.   
  113. int main()  
  114. {  
  115.     //非线程安全circular_buffer  
  116.     typedef boost::circular_buffer<int> circular_buffer;  
  117.     circular_buffer cb(3);  
  118.     cout << cb.capacity() <<endl;  
  119.     cout << cb.size() <<endl;  
  120.   
  121.     cb.push_back(0);  
  122.     cb.push_back(1);  
  123.     cb.push_back(2);  
  124.     cout << cb.size() <<endl;  
  125.   
  126.     //设置新容量大小  
  127.     cb.set_capacity(6);  
  128.     cb.push_back(3);  
  129.     cb.push_back(4);  
  130.     cb.push_back(5);  
  131.     cout << cb.size() << endl;  
  132.       
  133.     //数据循环(从某个位置开始)  
  134.     for(int i=0; i<cb.size(); i++)  
  135.     {  
  136.         cout << cb[i];  
  137.         if(i != cb.size()-1)  
  138.         {  
  139.             cout << ",";  
  140.         }  
  141.     }  
  142.     cout<<endl;  
  143.     cb.rotate(cb.begin()+1);  
  144.     for(int i=0; i<cb.size(); i++)  
  145.     {  
  146.         cout << cb[i];  
  147.         if(i != cb.size()-1)  
  148.         {  
  149.             cout << ",";  
  150.         }  
  151.     }  
  152.     cout<<endl;  
  153.   
  154.     //线程安全circular_buffer  
  155.     SafeCircularBuffer<double> scbCircularBuffer(3);  
  156.     cout << scbCircularBuffer.capacity() <<endl;  
  157.     cout << scbCircularBuffer.size() <<endl;  
  158.   
  159.     scbCircularBuffer.push_back(1.0999f);  
  160.     scbCircularBuffer.push_back(1.1f);  
  161.     scbCircularBuffer.push_back(2.2f);  
  162.   
  163.     cout << scbCircularBuffer.size() <<endl;  
  164.     scbCircularBuffer.push_back(3.3f);  
  165.     cout << scbCircularBuffer[0]<<endl;  
  166.     int k = 0;  
  167. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值