312 lines
7.5 KiB
C
312 lines
7.5 KiB
C
|
#include "Queue.h"
|
||
|
#include "queue_mem.h"
|
||
|
#include <string.h>
|
||
|
static char link_node_flag = 0;
|
||
|
static char link_BH_node_flag = 0;
|
||
|
static char link_stage_node_flag = 0;
|
||
|
|
||
|
//初始化队列,设置个数
|
||
|
void InitQueue(q_que* q, int num, char c)
|
||
|
{
|
||
|
if (c == 0)
|
||
|
{
|
||
|
(*q) = (q_que)malloc(sizeof(que));
|
||
|
(*q)->front = (q_node)malloc(sizeof(node));
|
||
|
}
|
||
|
else if (c == 1)
|
||
|
{
|
||
|
(*q) = (q_que)node_mem(sizeof(que));
|
||
|
(*q)->front = (q_node)node_mem(sizeof(node));
|
||
|
}
|
||
|
(*q)->mem_way = c;
|
||
|
(*q)->max_size = num;
|
||
|
(*q)->currentSize = 0;
|
||
|
(*q)->link_flag = 0;
|
||
|
(*q)->state = STATE_EMPTY;
|
||
|
|
||
|
(*q)->front->next = NULL;
|
||
|
(*q)->rear = (*q)->front;
|
||
|
}
|
||
|
|
||
|
//判断队列是否为空
|
||
|
bool EmptyQueue(q_que q)
|
||
|
{
|
||
|
if (q->rear == q->front)
|
||
|
return true;
|
||
|
else
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
//入队
|
||
|
void InsertQueue(q_que q, unsigned short int* src, int size, unsigned short int* max_min)
|
||
|
{
|
||
|
q_node n;
|
||
|
if (q->link_flag == 1)
|
||
|
{
|
||
|
n = q->front->next;
|
||
|
q->front = q->front->next;
|
||
|
q->rear = q->rear->next;
|
||
|
}
|
||
|
else if (q->link_flag == 0)
|
||
|
{
|
||
|
if (q->mem_way == 1)
|
||
|
{
|
||
|
n = (q_node)node_mem(sizeof(node));
|
||
|
if (n == NULL)
|
||
|
{
|
||
|
printf("Get mem error !!");
|
||
|
return;
|
||
|
}
|
||
|
n->f_data = (unsigned short int*)node_mem(size * sizeof(unsigned short int));
|
||
|
}
|
||
|
else if (q->mem_way == 0)
|
||
|
{
|
||
|
n = (q_node)malloc(sizeof(node));
|
||
|
if (n == NULL)
|
||
|
return;
|
||
|
n->f_data = (unsigned short int*)calloc(size, sizeof(unsigned short int));
|
||
|
}
|
||
|
else
|
||
|
n = (q_node)node_mem(sizeof(node));
|
||
|
}
|
||
|
else
|
||
|
n = (q_node)node_mem(sizeof(node));
|
||
|
|
||
|
memcpy(&(n->f_size), &size, sizeof(unsigned short int));
|
||
|
memcpy(n->f_data, src, size * sizeof(unsigned short int));
|
||
|
memcpy(&(n->max), max_min, sizeof(unsigned short int));
|
||
|
memcpy(&(n->min), max_min+1, sizeof(unsigned short int));
|
||
|
|
||
|
if (q->link_flag == 0)
|
||
|
{
|
||
|
//将new指向的结点插入到链式队列中
|
||
|
n->next = NULL; //这里等价于new->next = NULL;
|
||
|
q->rear->next = n;
|
||
|
//让rear指针指向新的队尾结点
|
||
|
q->rear = n; //等价于q->rear = new;
|
||
|
q->currentSize++;
|
||
|
}
|
||
|
if (q->currentSize == q->max_size && q->link_flag == 0)
|
||
|
{
|
||
|
q->rear->next = q->front->next;
|
||
|
q->link_flag = 1;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//出队(删除队首元素)
|
||
|
void DeleteQueue(q_que q)
|
||
|
{
|
||
|
q_node n;
|
||
|
if (EmptyQueue(q))
|
||
|
return;
|
||
|
// n = q->front;
|
||
|
// q->front = q->front->next;
|
||
|
// q->state = STATE_INCOMPLETE;
|
||
|
// free(n);
|
||
|
n = q->front;
|
||
|
q->front = q->front->next;
|
||
|
if (q->currentSize > 0)
|
||
|
q->currentSize--;
|
||
|
if (q->rear == n)//如果队尾指针指向要出队的那个结点,则更改队尾指针
|
||
|
q->rear = q->front;
|
||
|
free(n);
|
||
|
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
//打印队列 float list
|
||
|
void Display_f(q_que q)
|
||
|
{
|
||
|
q_node n;
|
||
|
int i = 0;
|
||
|
n = q->front->next;
|
||
|
if (n == NULL)
|
||
|
{
|
||
|
return;//队列为空
|
||
|
}
|
||
|
if (q->link_flag == 0)
|
||
|
{
|
||
|
while (n != NULL)
|
||
|
{
|
||
|
for (i = 0; i < n->f_size; i++)
|
||
|
printf("%d ", *(n->f_data + i));
|
||
|
n = n->next;
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
do
|
||
|
{
|
||
|
for (i = 0; i < n->f_size; i++)
|
||
|
printf("%d ", *(n->f_data + i));
|
||
|
n = n->next;
|
||
|
} while (n != q->front->next);
|
||
|
}
|
||
|
printf("\n");
|
||
|
}
|
||
|
// 用来读取十分长度的数据,先对比出这十分钟内最大最小值,
|
||
|
// 再归一化,复制数据
|
||
|
void copy_data_stage(q_que q, unsigned char* dst, int index)
|
||
|
{
|
||
|
q_node n;
|
||
|
|
||
|
int m_index = 0, j = 0, i = 0;
|
||
|
unsigned short int md_u, md_d;
|
||
|
float md;
|
||
|
//int i = 0,j=0;
|
||
|
n = q->front->next;
|
||
|
q->max = 0;
|
||
|
q->min = 50000;
|
||
|
if (n == NULL)
|
||
|
{
|
||
|
return;//队列为空
|
||
|
}
|
||
|
if (index <= q->currentSize)
|
||
|
{
|
||
|
//printf("start get\n");
|
||
|
q->state = STATE_CPOYING;
|
||
|
m_index = index;
|
||
|
while (m_index--) //算出最大最小值
|
||
|
{
|
||
|
if (n->max > q->max)
|
||
|
q->max = n->max;
|
||
|
if (n->min < q->min)
|
||
|
q->min = n->min;
|
||
|
n = n->next;
|
||
|
}
|
||
|
n = q->front->next;
|
||
|
j = 0;
|
||
|
while (index--)
|
||
|
{
|
||
|
for (i = 0; i < n->f_size; i++)
|
||
|
{
|
||
|
md_u = n->f_data[i] - q->min;
|
||
|
md_d = q->max - q->min;
|
||
|
md_d = md_d + 1;
|
||
|
md = (md_u / md_d) * 128;
|
||
|
*(dst + j) = (unsigned char)md;
|
||
|
j++;
|
||
|
}
|
||
|
n = n->next;
|
||
|
}
|
||
|
q->state = STATE_INCOMPLETE;
|
||
|
q->max = 0;
|
||
|
q->min = 50000;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//获取逆位的数据,倒数后几秒的数据,归一化
|
||
|
void copy_data_up_v(q_que q, unsigned short int* sdst,unsigned char* cdst, int index)
|
||
|
{
|
||
|
q_node n;
|
||
|
int q_max = 0, i = 0, j = 0;
|
||
|
float md_u, md_d;
|
||
|
float md;
|
||
|
n = q->front->next;
|
||
|
q->max = 0;
|
||
|
q->min = 50000;
|
||
|
if (n == NULL)
|
||
|
{
|
||
|
return;//队列为空
|
||
|
}
|
||
|
q_max = q->currentSize;
|
||
|
if (index <= q_max)
|
||
|
{
|
||
|
//printf("t: p max:%d p min:%d \n",q->max,q->min);
|
||
|
q->state = STATE_CPOYING;//正在复制
|
||
|
while (q_max--) //n != NULL
|
||
|
{
|
||
|
if (index > q_max)
|
||
|
{
|
||
|
memcpy(sdst, n->f_data, n->f_size * sizeof(unsigned short int));
|
||
|
sdst = sdst + n->f_size;
|
||
|
if (n->max > q->max)
|
||
|
q->max = n->max;
|
||
|
if (n->min < q->min)
|
||
|
q->min = n->min;
|
||
|
}
|
||
|
n = n->next;
|
||
|
}
|
||
|
|
||
|
md_d = q->max - q->min+1;
|
||
|
for (i = 0; i < index* (q->front->next->f_size); i++)
|
||
|
{
|
||
|
md_u = *(sdst + i) - q->min;
|
||
|
md = (md_u / md_d) * 128;
|
||
|
q_max=(int)md;
|
||
|
*(cdst + i) = (unsigned char)q_max;
|
||
|
if(*(cdst + i)>128)
|
||
|
*(cdst + i)=128;
|
||
|
}
|
||
|
q->state = STATE_INCOMPLETE;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
// 获取顺位的数据,前列几秒的数据
|
||
|
void copy_data_down(q_que q, unsigned short int* dst, int index)
|
||
|
{
|
||
|
q_node n;
|
||
|
// int i = 0,j=0;
|
||
|
n = q->front->next;
|
||
|
q->max = 0;
|
||
|
q->min = 50000;
|
||
|
if (n == NULL)
|
||
|
{
|
||
|
return;//队列为空
|
||
|
}
|
||
|
if (index <= q->currentSize)
|
||
|
{
|
||
|
//printf("start get\n");
|
||
|
q->state = STATE_CPOYING;
|
||
|
//j = 0;
|
||
|
while (index--)
|
||
|
{
|
||
|
memcpy(dst, n->f_data, n->f_size * sizeof(unsigned short int));
|
||
|
dst = dst + n->f_size;
|
||
|
|
||
|
if (n->max > q->max)
|
||
|
q->max = n->max;
|
||
|
if (n->min < q->min)
|
||
|
q->min = n->min;
|
||
|
n = n->next;
|
||
|
}
|
||
|
q->state = STATE_INCOMPLETE;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//获取逆位的数据,倒数后几秒的数据
|
||
|
void copy_data_up(q_que q, unsigned short int* dst, int index)
|
||
|
{
|
||
|
q_node n;
|
||
|
int q_max = 0, i = 0, j = 0;
|
||
|
n = q->front->next;
|
||
|
q->max = 0;
|
||
|
q->min = 50000;
|
||
|
if (n == NULL)
|
||
|
{
|
||
|
return;//队列为空
|
||
|
}
|
||
|
q_max = q->currentSize;
|
||
|
if (index <= q_max)
|
||
|
{
|
||
|
q->state = STATE_CPOYING;//正在复制
|
||
|
while (q_max--) //n != NULL
|
||
|
{
|
||
|
if (index > q_max)
|
||
|
{
|
||
|
memcpy(dst, n->f_data, n->f_size * sizeof(unsigned short int));
|
||
|
dst = dst + n->f_size;
|
||
|
if (n->max > q->max)
|
||
|
q->max = n->max;
|
||
|
if (n->min < q->min)
|
||
|
q->min = n->min;
|
||
|
}
|
||
|
n = n->next;
|
||
|
}
|
||
|
q->state = STATE_INCOMPLETE;
|
||
|
}
|
||
|
}
|
||
|
|