aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/Library/ml_transcode/LinkedQueue.cpp
blob: 7896c7bbf5067fb038a95f2efe556a72312ddf30 (plain) (blame)
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
129
130
131
132
133
134
135
136
137
138
139
#include "LinkedQueue.h"

LinkedQueue::LinkedQueue()
{
	size=0;
	head=NULL;
	tail=NULL;
	bm=NULL;
	bmpos=0;
	InitializeCriticalSection(&cs);
}

void LinkedQueue::lock()
{
	EnterCriticalSection(&cs);
	//wchar_t buf[100]; wsprintf(buf,L"Lock taken by %x",GetCurrentThreadId()); OutputDebugString(buf);
}
void LinkedQueue::unlock()
{
	LeaveCriticalSection(&cs);
	//wchar_t buf[100]; wsprintf(buf,L"Lock released by %x",GetCurrentThreadId()); OutputDebugString(buf);
}

LinkedQueue::~LinkedQueue()
{
	lock();
	QueueElement * q=head;
	while(q) { QueueElement *p=q; q=q->next; delete p; }
	unlock();
	DeleteCriticalSection(&cs);
}

void LinkedQueue::Offer(void * e)
{
	lock();
	if(size==0) { size++; head=tail=new QueueElement(e); unlock(); return; }
	tail->next=new QueueElement(e);
	tail->next->prev=tail;
	tail=tail->next;
	size++;
	bm=NULL;
	unlock();
}

void * LinkedQueue::Poll()
{
	lock();
	if(size == 0) { unlock(); return NULL; }
	size--;
	void * r = head->elem;
	QueueElement * q = head;
	head=head->next;
	if(head!=NULL) head->prev=NULL;
	else tail=NULL;
	delete q;
	bm=NULL;
	unlock();
	return r;
}

void * LinkedQueue::Peek()
{
	lock();
	void * ret=head?head->elem:NULL;
	unlock();
	return ret;
}

QueueElement * LinkedQueue::Find(int x)
{
	if(x>=size || x<0) return NULL;
	if(x == 0) return head;
	if(x == size-1) return tail;
	if(!bm) { bm=head; bmpos=0; }
	int diffh = x;
	int difft = (size-1) - x;
	int diffbm = x - bmpos;
	diffbm>0?diffbm:-diffbm;
	if(diffh < difft && diffh < diffbm) { bm=head; bmpos=0; }
	else if(diffh >= difft && diffbm >= difft) { bm=tail; bmpos=size-1; }
	while(bmpos > x && bm) { bm=bm->prev; bmpos--; }
	while(bmpos < x && bm) { bm=bm->next; bmpos++; }
	return bm;
}

void * LinkedQueue::Get(int pos)
{
	lock();
	QueueElement * e = Find(pos);
	unlock();
	return e?e->elem:NULL;
}

void LinkedQueue::Set(int pos, void * val)
{
	lock();
	QueueElement * e = Find(pos);
	if(e) e->elem=val;
	unlock();
}

void* LinkedQueue::Del(int pos)
{
	lock();
	QueueElement * e = Find(pos);
	if(!e) { unlock(); return NULL; }
	else if(size == 1) head=tail=NULL;
	else if(e==head)
	{
		head=head->next;
		head->prev=NULL;
	}
	else if(e==tail)
	{
		tail=tail->prev;
		tail->next=NULL;
	}
	else
	{
		e->prev->next = e->next;
		e->next->prev = e->prev;
	}
	size--;
	bm=NULL;
	unlock();
	void * ret = e->elem;
	delete e;
	return ret;
}

int LinkedQueue::GetSize()
{
	return size;
	/*
	lock();
	int s = size;
	unlock();
	return s;*/
}