BufferManagement

来源:互联网 发布:手机pdf阅读软件 编辑:程序博客网 时间:2024/06/01 07:48

BufferManagement.cpp

 

/******************************************************************************
File Name     :
Version       :
Author        :
Created       : 2011/3
Last Modified :
Description   :
Function List :

History       :
1.Date     : 2011/3
Author      :
Modification: Created file

******************************************************************************/
#include "BufferManagement.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct node{
 unsigned int id;
 int data;
 int time;
 int vis_count;
 struct node *next;
}*LinkList,LinkNode;


LinkList L;
LinkNode *pre;
unsigned int num;
unsigned int max_num;
int pmode;

/***************************************************************************************************
Description     : 创建缓存区
Prototype       : void  CreateBuffer( unsigned int ElementCount, RELEASE_MODE Mode);
Input Param     : ElementCount 缓存区可存储的元素个数
                  Mode         缓存区满后元素的释放策略
Output Param    : 无
Return Value    : 无
****************************************************************************************************/
void CreateBuffer(unsigned int ElementCount, RELEASE_MODE Mode)   
{
 int ret;


 L = (LinkList)malloc(sizeof(LinkNode));
 memset(L,NULL,sizeof(L));

 pre = L;

 max_num = ElementCount;
 num = 0;

 ret = SetBufferReleaseMode(Mode);
 if(ret)
  return;

 pmode = Mode;


 return;
}


//策略2(使用次数最少)
LinkNode *get_buffer1(LinkList L)
{
 LinkNode *p;
 LinkNode *tmp;

 p = L->next;
 tmp = p;
 p = p->next;

 while(p)
 {
  if(tmp->vis_count > p->vis_count)
   tmp = p;
  else if(tmp->vis_count == p->vis_count)
  {
   if(tmp->time > p->time)
    tmp = p;
  }
  p = p->next;

 }

 return tmp;
}

//策略1(最久未使用)
LinkNode *get_buffer2(LinkList L)
{
 LinkNode *p;
 LinkNode *tmp;

 p = L->next;
 tmp = p;
 p = p->next;

 while(p)
 {
  if(tmp->time > p->time)
   tmp = p;
  
  p = p->next;
 }

 p = tmp->next;


 return tmp;
}
//释放
void release(LinkList L,LinkNode *tmp)
{
 LinkNode *p;

 p = L->next;
 pre = L;

 while(p)
 {
  if(p == tmp)
  {
   pre->next = p->next;
   free(p);
   break;
  }

  pre = p;
  p = p->next;
 }

}

 

/***************************************************************************************************
Description     : 元素的存操作:存储新的元素或更新已有元素,该ElementId标识的元素在
                  缓存区中不存在表示存储新元素,已经存在则表示更新该元素的值。
Prototype       : int SetElement (unsigned int ElementId, int Value)
Input Param     : ElementId  元素ID
                  Value      元素值
Output Param    : 无
Return Value    : 成功返回0,失败返回-1。没有创建缓存区则返回失败
****************************************************************************************************/
int SetElement(unsigned int ElementId, int Value)  
{
 /*在这里实现功能*/
 LinkNode *p,*q;
 LinkNode *tmp;

 

 p = (LinkNode *)malloc(sizeof(LinkNode));
 p->data = Value;
 p->id = ElementId;
 p->time = 0;
 p->vis_count = 0;

 q = L;


 //是否存在需要更新的数据
 if(num)
 {
  q = q->next;
  while(q)
  {
   if(q->id == p->id)
   {
    q->data = Value;
    q->vis_count++;
    q->time = 1;
    if(q->next)
    {
     while(q->next)
      q = q->next;
     q->time = 0;
    }
    num--;
    break;
   }
   q = q->next;
  }
 }

 //缓冲区中尚无数据
 if(!num)
 {
  pre->next = p;
  pre = p;
  pre->next = NULL;
 }
 
 //缓冲区未满,且数据不需要更新,直接添加在链表尾端
 //p->time = 0:代表最久未被使用的数据
 //p->time = 1:代表最近更新过的数据
 if((!q) && (num < max_num))
 {
  pre->time = 0;
  p->time = 1;

  pre->next = p;
  pre = p;
  pre->next = NULL;

 }
 //缓冲区已满,使用缓冲区释放策略
 else if(!q && (num >= max_num))
 {
  if(pmode == 1)
  { 
   tmp = get_buffer1(L);

  }
  else if(pmode == 0)
  {
   tmp = get_buffer2(L);
  }
  else
   return -1;

  //释放指定缓冲区
  release(L,tmp);
  num--;

  //将数据添加在缓冲区内
  while(pre->next)
   pre = pre->next;

  pre->time = 0;
  p->time = 1;

  pre->next = p;
  pre = p;
  pre->next = NULL;
 }
 
 num++;


 return 0;
}

 

/***************************************************************************************************
Description     : 元素的取操作:获取指定元素的值
Prototype       : int GetElement(int ElementId, int *pValue);
Input Param     : ElementId  元素ID
Output Param    : *pValue    元素值
Return Value    : 成功返回0,失败返回-1 。若没有创建缓存区或者缓存区中不存在该元素,返回失败
****************************************************************************************************/
int GetElement(unsigned int ElementId, int *pValue)  
{
 LinkNode *p;

 p = L->next;

 //进行入参判断
 if((ElementId > 65535) || (ElementId <= 0))
  return -1;

 while(p)
 {
  if(p->id == ElementId)
  {
   *pValue = p->data;
   p->time++;
   p->vis_count++;
   return 0;
  }
  p = p->next;
 }


 return -1;
}

 

/***************************************************************************************************
Description     : 设置缓存区的元素释放策略
Prototype       : int SetBufferReleaseMode(RELEASE_MODE Mode);
Input Param     : Mode  缓存区容量不足时元素的释放策略
Output Param    : 无
Return Value    : 成功返回0,失败返回-1。没有创建缓存区则返回失败
****************************************************************************************************/
int SetBufferReleaseMode(RELEASE_MODE Mode)   
{
 LinkNode *p;
 LinkNode *tmp;

 if(num == 0)
  return 0;

 p = L->next;
 tmp = p;
 if(Mode == 1)
 { 
  tmp = get_buffer1(L);

 }
 else if(Mode == 0)
 {
  tmp = get_buffer2(L);
 }
 else
  return -1;

 release(L,tmp);
 
 
 return 0; 
}

 

/***************************************************************************************************
Description     : 销毁创建的缓存区,包括缓存区内的数据
Prototype       : void DestroyBuffer( );
Input Param     : 无
Output Param    : 无
Return Value    : 无
****************************************************************************************************/
void DestroyBuffer()
{
 LinkNode *p,*pre;

 pre = L;
 p = pre->next;
 while(p)
 {
  free(pre);

  pre = p;
  p = p->next;

 }
 return;
}

.hhhhhhhhhhhhhhhhhhhhhhhhhhhh

#ifndef _BUFFER_MANAGEMENT_H
#define _BUFFER_MANAGEMENT_H


typedef enum tag_release_mode
{
 RELEASE_ELEMENT_USED_EARLY = 0,    //最久未使用
 RELEASE_ELEMENT_USED_UNCOMMON      //使用次数最少
}RELEASE_MODE;

void CreateBuffer(unsigned int ElementCount, RELEASE_MODE Mode);
void DestroyBuffer();
int SetElement(unsigned int ElementId, int Value);
int GetElement(unsigned int ElementId, int *pValue);
int SetBufferReleaseMode(RELEASE_MODE Mode);

#endif

 

原创粉丝点击