-
Notifications
You must be signed in to change notification settings - Fork 1
/
ring_buffer.c
128 lines (118 loc) · 2 KB
/
ring_buffer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include "ring_buffer.h"
/*
キューの生成
返り値はqueueポインタ
*/
Queue *make_queue(int n)
{
Queue *que = malloc(sizeof(Queue));
if (que != NULL)
{
que->front = 0;
que->rear = 0;
que->count = 0;
que->size = n;
que->buff = malloc(sizeof(double) * n);
if (que->buff == NULL)
{
free(que);
printf("メモリ不足\n");
return NULL;//segmantaion fault起こす
}
}
return que;
}
int queue_length(Queue *que)
{
return que->count;
}
int is_full(Queue *que)
{
return que->count == que->size;
//trueならば1を返す
}
//Queueの最後に追加した値を返すだけ
double getLast(Queue *que)
{
if (is_empty(que))
{
printf("queue is empty\n");
return 0;
}
return que->buff[que->rear-1];
}
//queueに追加
int enqueue(Queue *que, double x)
{
if (que->rear == que->size)
{
que->rear = 0;
}
if (is_full(que))
{
printf("enqueue failed\n");
return -1; //false
}
que->buff[que->rear++] = x;
que->count++;
return 0;//true
}
// キューは空か
int is_empty(Queue *que)
{
return que->count == 0;
}
// データを取り出す
double dequeue(Queue *que)
{
if (is_empty(que))
{
printf("queue is empty\n");
return 0;
}
double x = que->buff[que->front];
que->front++;
que->count--;
if (que->front == que->size)
{
que->front = 0;
}
return x;
}
//キューを削除
int queue_delete(Queue *que)
{
free(que->buff);
free(que);
return 0;
}
//double型用の比較関数
static int dCmp(const void *p, const void *q )
{
if( *(double*)p > *(double*)q ) return 1;
if( *(double*)p < *(double*)q ) return -1;
return 0;
}
//キューの中身のmaxとminの差を計算
double queue_diff(Queue *que)
{
int i;
int n = que->count;
double list[n];
for(i=0; i<n; i++)
{
if(que->front+i <= que->size-1)
{
list[i] = (double)que->buff[que->front+i];
}
else
{
list[i] = (double)que->buff[que->front+i-que->size];
}
}
qsort(list,n,sizeof(double),dCmp);
return list[n-1] - list[0];
}