-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathring_buffer.py
More file actions
276 lines (243 loc) · 7.51 KB
/
Copy pathring_buffer.py
File metadata and controls
276 lines (243 loc) · 7.51 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
'''
@author-name: Rishab Katta
'''
class Node:
__slots__ = "value", "link"
def __init__( self, value, link = None ):
""" Create a new node and optionally link it to an existing one.
param value: the value to be stored in the new node
param link: the node linked to this one
"""
self.value = value
self.link = link
def __str__( self ):
""" Return a string representation of the contents of
this node. The link is not included.
"""
return str( self.value )
def __repr__( self ):
""" Return a string that, if evaluated, would recreate
this node and the node to which it is linked.
This function should not be called for a circular
list.
"""
return "Node(" + repr( self.value ) + "," + \
repr( self.link ) + ")"
class RingBuffer:
__slots__ = "maxsize", "sizer", "head", "tail"
def __init__(self,msize):
'''
Initialize all the instance variables of Ring Buffer
:param msize: Max Size of the Ring buffer
'''
self.sizer = 0
self.maxsize = msize
self.head=None
self.tail=None
def insert_keep_new(self,data):
'''
Insert elements in such a way to preserve the newest added elements
:param data: Value to be added
:return:
'''
tmp = Node(data)
if self.head==None:
self.head=tmp
self.tail=tmp
self.tail.link = self.head
self.sizer +=1
else:
if not self.is_full():
self.tail.link=tmp
self.tail=self.tail.link
self.tail.link=self.head
self.sizer += 1
else:
self.head=self.head.link
self.tail.link=tmp
self.tail=self.tail.link
self.tail.link=self.head
# self.size += 1
def insert_keep_old(self,data):
'''
Insert elements in such a way that oldest elements are to be preserved
:param data: Value to be added to the Ring Buffer
:return:
'''
tmp = Node(data)
if self.head == None:
self.head = tmp
self.tail = tmp
self.tail.link = self.head
self.sizer += 1
else:
if not self.is_full():
self.tail.link = tmp
self.tail = self.tail.link
self.tail.link = self.head
self.sizer += 1
else:
pass #asked and confirmed with Prof Passino.
# cur = self.head
# while (True):
# if cur.link is self.tail:
# break
# cur=cur.link
# cur.link = tmp
# self.tail=tmp
# self.tail.link=self.head
def __str__(self):
'''
Return a String Representation of all the elements in the Ring Buffer
:return: String of all the elements in the Ring buffer
'''
str_ring = ""
if self.head is None:
return str_ring + ""
else:
cur = self.head
while cur.link is not self.head:
str_ring +=str(cur.value) + " "
cur = cur.link
return str_ring + str(cur.value)
def find(self,value):
'''
Find if the value is present in the Ring Buffer or not
:param value: Value to be searched for
:return: Cursor at which the Value is present.
'''
cur = self.head
if cur is None:
return -1
if cur.link == self.head:
if cur.value == value:
return cur
else:
return -1
while True:
if cur.value == value:
return cur
elif(cur.link is self.head):
break
cur = cur.link
return -1
def get(self, index):
'''
I wrote this function to get the element at the specified position in the Ring Buffer
:param index: Index position of the element
:return: element at the index specified
'''
cur =self.head
if(index>self.size()):
return -1
if self.size()>0:
for i in range(0,index):
cur = cur.link
return cur.value
def capacity(self):
'''
return max size of the Ring Buffer
:return: max size of ring buffer
'''
return self.maxsize
def size(self):
'''
function returns current size of the ring buffer
:return: current size of the ring buffer
'''
return self.sizer
def is_full(self):
'''
checks to see if the buffer is filled upto capacity or not
:return:
'''
if self.sizer == self.maxsize:
return True
return False
def replace(self, cursor, val):
'''
replace the value in the ring buffer with the value in the cursor
:param cursor: Cursor containing the value to be replaced with
:param val: value in the Ring buffer
:return:
'''
cur = self.find(val)
if cur ==-1:
print("cursor with that value not found")
return
cur.value = cursor.value
def remove_oldest(self):
'''
Remove oldest value in the ring buffer
:return:
'''
if self.size() >1:
self.head = self.head.link
self.tail.link = self.head
self.sizer =self.sizer-1
return
if self.size() ==1:
self.head = None
self.tail =None
self.sizer=0
return
def remove_newest(self):
'''
remove newest value in the ring Buffer
:return:
'''
if self.size()>1:
cur =self.head
while True:
if cur.link is self.tail:
break
cur = cur.link
self.tail =cur
self.tail.link = self.head
self.sizer = self.sizer - 1
return
if self.size()==1:
self.head = None
self.tail = None
self.sizer=0
return
def test():
'''
Test various functionalities of the Ring Buffer like Insert_keep_new, Insert_keep_old,
Remove_oldest, Remove_newest, find, Is_full etc.
:return:
'''
rb = RingBuffer(3)
rb.insert_keep_new(1)
rb.insert_keep_new(2)
rb.insert_keep_old(3)
print(rb)
rb.remove_newest()
rb.remove_oldest()
print(rb)
print(rb.is_full())
rb.insert_keep_new(4)
rb.insert_keep_new(5)
rb.insert_keep_old(6)
print(rb)
rb.remove_newest()
rb.remove_oldest()
print(rb)
rb.insert_keep_new(7)
rb.insert_keep_new(8)
rb.insert_keep_old(9)
rb.insert_keep_new(10)
rb.insert_keep_new(11)
rb.insert_keep_old(12)
print(rb)
rb.remove_newest()
rb.remove_oldest()
print(rb)
rb.insert_keep_new(20)
rb.insert_keep_new(25)
print(rb.is_full())
print("Searching for 5")
rb.replace(Node(5),2)
print(rb)
if __name__ == '__main__':
test()