循环缓冲类

4月27日去迈瑞面试,要求写一个循环缓冲类,真心没接触过,结果可想而知。

下午回到实验室,认真写了一下,完成代码贴出来。

  1: //mindry_buffer.h
  2: 
  3: #ifndef MINDRY_BUFFER_H_H
  4: #define MINDRY_BUFFER_H_H
  5: 
  6: class CMindryBuffer
  7: {
  8: public:
  9:   bool isFull();//缓冲区是否满
 10:   bool isEmpty();//缓冲区是否空
 11:   void Empty();//置空缓冲区
 12:   int GetLength();//缓冲区大小
 13: 
 14:   CMindryBuffer(int size);
 15:   virtual ~CMindryBuffer();
 16:     
 17:   int Write(char * buf, int count);//写缓冲
 18:   int Read(char * buf, int count);//读缓冲
 19: 
 20: private:
 21:   bool m_bEmpty;//缓冲区空标识
 22:   bool m_bFull;//缓冲区满标识
 23: 
 24:   char * m_pBuf;//缓冲区指针
 25:   int m_nBufSize;//缓冲区大小
 26:   int m_nReadPos;//读数据位置
 27:   int m_nWritePos;//写数据位置
 28: 
 29: };
 30: 
 31: #endif
  1: //mindry_buffer.cpp
  2: #include "mindry_buffer.h"
  3: #include <cstring>
  4: 
  5: CMindryBuffer::CMindryBuffer(int size)
  6: {
  7:   m_nBufSize = size;
  8:   m_nReadPos = 0;
  9:   m_nWritePos = 0;
 10:   m_pBuf = new char[m_nBufSize];
 11:   m_bEmpty = true;
 12:   m_bFull = false;
 13: }
 14: 
 15: CMindryBuffer::~CMindryBuffer()
 16: {
 17:   delete[] m_pBuf;
 18: }
 19: 
 20: int CMindryBuffer::Write(char *buf, int count)
 21: {
 22:   if(count <= 0) return 0;
 23:   
 24:   m_bEmpty = false;//buffer is full
 25: 
 26:   if(m_bFull)
 27:   {
 28:     return 0;
 29:   }
 30:   else //if(m_nReadPos == m_nWritePos) //buffer is empty
 31:   {
 32:   /*
 33:            empty      m_nReadPos        empty
 34:     |-------------------------|---------------------------| m_nBufSize
 35:                          m_nWritePos  
 36:     */
 37:     int rightcount = m_nBufSize - m_nWritePos;
 38:     if(rightcount > count)//space is enough
 39:     {
 40:       memcpy(m_pBuf + m_nWritePos, buf, count);
 41:       m_nWritePos = (m_nWritePos + count)%m_nBufSize;
           //m_nWritePos += count;
 42:       m_bFull = (m_nWritePos == m_nReadPos);
 43:       return count;
 44:     }
 45:     else
 46:     {
 47:       memcpy(m_pBuf + m_nWritePos, buf, rightcount);
 48:       m_nWritePos = (m_nReadPos > count - rightcount) ? 
 49:         count - rightcount : m_nWritePos;//is left space enough?yes,copy;no, no action;
 50:       memcpy(m_pBuf, buf + rightcount, m_nWritePos);
 51:       m_bFull = (m_nWritePos == m_nReadPos);
 52:       return rightcount + m_nWritePos;
 53:     }
 54:   }
 55: 
 56:   /*
 57:            empty   m_nReadPos        m_nWritePos    empty
 58:     |-----------------|------------------|-------------------| m_nBufSize
 59:                             data               rightcount
 60:   */
 61:   else //if(m_nReadPos < m_nWritePos)//buffer is not full
 62:   {
 63:     int rightcount = m_nBufSize - m_nWritePos;
 64:     if(rightcount > count) //rest space is enough
 65:     {
 66:       memcpy(m_pBuf + m_nWritePos, buf, count);
 67:       m_nWritePos = (m_nWritePos + count)%m_nBufSize;
           //m_nWritePos += count;
 68:       m_bFull = (m_nReadPos == m_nWritePos);
 69:       return count;
 70:     }
 71:     else//rest space is not enough
 72:     {
 73:       memcpy(m_pBuf + m_nWritePos, buf, rightcount);
 74:       m_nWritePos = (m_nReadPos > count - rightcount) ? 
 75:         count - rightcount : m_nReadPos;
 76:       memcpy(m_pBuf, buf + rightcount, m_nWritePos);
 77:       m_bFull = (m_nWritePos == m_nReadPos);
 78:       return rightcount + m_nWritePos;
 79:     }
 80:   }
 81: }  
 82: 
 83: int CMindryBuffer::Read(char *buf, int count)
 84: {
 85:   if(count < 0) return 0;
 86:   
 87:   m_bFull = false;
 88:   if(m_bEmpty)
 89:   {
 90:     return 0;
 91:   }
 92:   else //if(m_nReadPos == m_nWritePos) //buffer is full
 93:   {
 94:     /*
 95:               data       m_nReadPos        data
 96:     |--------------------|--------------------|m_nBufSize
 97:                          m_nWritePos    rightcount
 98:     */
 99:     int rightcount = m_nBufSize - m_nReadPos;
100:     if(rightcount > count)
101:     {
102:       memcpy(buf, m_pBuf + m_nReadPos, count);
103:       m_nReadPos = (m_nReadPos + count)%m_nBufSize;
           //m_nReadPos += count;
104:       m_bEmpty = (m_nReadPos == m_nWritePos);
105:       return count;
106:     }
107:     else
108:     {
109:       memcpy(buf, m_pBuf + m_nReadPos, rightcount);
110:       m_nReadPos = (m_nWritePos >= count - rightcount) ? 
111:         count - rightcount : m_nWritePos;
112:             memcpy(buf + rightcount, m_pBuf, m_nReadPos);
113:       m_bEmpty = (m_nReadPos == m_nWritePos);
114:       return rightcount + m_nReadPos;
115:     }
116: 
117:   }
118:   else //if(m_nReadPos < m_nWritePos)
119:   {
120:     /*
121:                     m_nReadPos  data  m_nWritePos  
122:     |-----------------|----------------|-----------------|m_nBufSize
123: 
124:     */
125:     int rightcount = m_nWritePos - m_nReadPos;
126:     
127:     int temp = (rightcount > count) ? count : rightcount;
128:       memcpy(buf, m_pBuf + m_nReadPos, temp);
129:     m_nReadPos = (m_nReadPos + temp)%m_nBufSize;
         //m_nReadPos += temp;
130:     m_bEmpty = (m_nWritePos == m_nReadPos);
131:     return temp;
132:   }
133:   else if(m_nReadPos > m_nWritePos)
134:   {
135:     /*
136:                data     m_nWritePos       m_nReadPos    data        m_nBufSize
137:         |------------------|------------------|--------------------|
138: 
139:     */
140:     int rightcount = m_nBufSize - m_nReadPos;
141:     
142:     if(rightcount > count)
143:     {
144:       memcpy(buf, m_pBuf + m_nReadPos, count);
145:       m_nReadPos = (m_nReadPos + count)%m_nBufSize;
                  //m_nReadPos += count;
146:       m_bEmpty = (m_nWritePos == m_nReadPos);
147:       return count;
148:     }
149:     else
150:     {
151:       memcpy(buf, m_pBuf + m_nReadPos, rightcount);
152:       m_nReadPos = (m_nWritePos > count - rightcount) ? 
153:         count - rightcount : m_nWritePos;
154:       memcpy(buf, m_pBuf, m_nReadPos);
155:       m_bEmpty = (m_nWritePos == m_nReadPos);
156:         return rightcount + m_nReadPos;
157:     }
158:   }
159: }
160: 
161: 
162: int CMindryBuffer::GetLength()
163: {
164:   if(m_bEmpty)
165:   {
166:     return 0;
167:   }
168:   if(m_bFull)
169:   {
170:     return m_nBufSize;
171:   }
172:   if(m_nReadPos < m_nWritePos)
173:   {
174:     return m_nReadPos - m_nWritePos;
175:   }
176:   else  
177:   {
178:     return m_nBufSize - (m_nReadPos - m_nWritePos);
179:   }
180: }
181: 
182: 
183: void CMindryBuffer::Empty()
184: {
185:   m_nReadPos = 0;
186:   m_nWritePos =0;
187:   m_bEmpty = true;
188:   m_bFull = false;
189: }
190: 
191: bool CMindryBuffer::isEmpty()
192: {
193:   return m_bEmpty;
194: }
195: 
196: bool CMindryBuffer::isFull()
197: {
198:   return m_bFull;
199: }
  1: #include "mindry_buffer.h"
  2: #include <iostream>
  3: using namespace std;
  4: 
  5: int main(int argc, char * argv[])
  6: {
  7:   CMindryBuffer * mindrybuffer = new CMindryBuffer(100);
  8:   char * datachar = new char[40];
  9:   char * reschar = new char[20];
 10: 
 11:   int res = 0;
 12:   int res0 = 0;
 13: 
 14:   for(int i=0; i < 40; ++i)
 15:   {
 16:     datachar[i] = i + 1;
 17:   }
 18: 
 19:   res = mindrybuffer->Write(datachar, 40);
 20:   printf("write total:%d
", res);
 21:   res = mindrybuffer->Write(datachar, 40);
 22:   printf("write total:%d
", res);
 23:   res = mindrybuffer->Write(datachar, 40);
 24:   printf("write total:%d
", res);
 25:   res = mindrybuffer->Write(datachar, 40);
 26:   printf("write total:%d
", res);
 27: 
 28:   res0 = mindrybuffer->Read(reschar, 20);
 29:   printf("read total:%d
", res0);
 30: 
 31:   res = mindrybuffer->Write(datachar, 40);
 32:   printf("write total:%d
", res);
 33:   res = mindrybuffer->Write(datachar, 40);
 34:   printf("write total:%d
", res);
 35:   res = mindrybuffer->Write(datachar, 40);
 36:   printf("write total:%d
", res);
 37: 
 38:   for(int j=0; j < 20; ++j)
 39:   {
 40:     if(j % 10 == 0) cout<<endl;
 41:     printf("%d ",reschar[j]);
 42:   }
 43:   return 0;
 44: }

问题:面试的主管说我之前写的代码不合规范,请各位大牛指点一下。

(面试的时候没有能够实现方法,但是基本的框架结构都和这个是一样)

作者:Lucas Hsueh
文章部分是自己的学习体会、代码等,还有收集和整理其他技术牛人的文章。
原文地址:https://www.cnblogs.com/lucas-hsueh/p/3708953.html