@@ -606,10 +606,10 @@ def count(self, value):
606606 return np .count_nonzero (self .deque == value )
607607
608608 def __getitem__ (self , index ):
609- return self .queue [index ]
609+ return self .deque [index ]
610610
611611 def __setitem__ (self , index , value ):
612- self .queue [index ] = value
612+ self .deque [index ] = value
613613
614614 def __getattr__ (self , name ):
615615 """
@@ -944,4 +944,121 @@ def _reverse_in_place(array):
944944
945945
946946if __name__ == "__main__" :
947- NumpyDeque (maxsize = 5 )
947+ # Create an empty deque that stores up to 10 float64 numbers
948+ d = NumpyDeque (maxsize = 10 , dtype = np .float64 )
949+
950+ # Create a deque with 5 int64 zeros (the deque is initialized to maxsize with 0).
951+ d = NumpyDeque (maxsize = 5 , fill = 0 , dtype = np .int64 )
952+
953+ # Create a deque from an array. Its maxsize is automatically set to 5.
954+ d = NumpyDeque .array ([1 , 2 , 3 , 4 , 5 ])
955+
956+ # Create a deque from an array. Its maxsize is set to 5.
957+ d = NumpyDeque .array ([1 , 2 , 3 ], 5 )
958+
959+ ### Adding to Right of The Deque
960+
961+ d = NumpyDeque (maxsize = 5 , dtype = np .int64 )
962+
963+ # Put a value to the right on the deque
964+ d .put (5 )
965+ d .put (7 )
966+ d .put (9 )
967+ print (d ) # Output: NumpyDeque([5, 7, 9])
968+ d .put (11 )
969+ d .put (13 )
970+ print (d ) # Output: NumpyDeque([5, 7, 9, 11, 13])
971+ d .put (15 ) # 5 is dropped
972+ print (d ) # Output: NumpyDeque([7, 9, 11, 13, 15])
973+
974+ d .putter ([1 , 2 , 3 ])
975+ print (d ) # Output: NumpyDeque([13, 15, 1, 2, 3])
976+
977+ d .putter ([- 1 , - 2 , - 3 , - 4 , - 5 , - 6 , - 7 ])
978+ print (d ) # Output: NumpyDeque([-3, -4, -5, -6, -7])
979+
980+ d .putter ([1 , 2 , 3 , 4 , 5 ])
981+ print (d ) # Output: NumpyDeque([1, 2, 3, 4, 5])
982+
983+ ### Adding to Left of The Deque
984+
985+ d = NumpyDeque (maxsize = 5 , dtype = np .int64 )
986+
987+ # Put a value to the right on the deque
988+ d .putleft (5 )
989+ d .putleft (7 )
990+ d .putleft (9 )
991+ print (d ) # Output: NumpyDeque([9, 7, 5])
992+ d .putleft (11 )
993+ d .putleft (13 )
994+ print (d ) # Output: NumpyDeque([13, 11, 9, 7, 5])
995+ d .putleft (15 ) # 5 is dropped
996+ print (d ) # Output: NumpyDeque([15, 13, 11, 9, 7])
997+
998+ d .putterleft ([1 , 2 , 3 ])
999+ print (d ) # Output: NumpyDeque([3, 2, 1, 15, 13])
1000+
1001+ d .putterleft ([- 1 , - 2 , - 3 , - 4 , - 5 , - 6 , - 7 ])
1002+ print (d ) # Output: NumpyDeque([-7, -6, -5, -4, -3])
1003+
1004+ d .putterleft ([1 , 2 , 3 , 4 , 5 ])
1005+ print (d ) # Output: NumpyDeque([5, 4, 3, 2, 1])
1006+
1007+ ### Removing Elements
1008+
1009+ d = NumpyDeque .array ([1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ])
1010+
1011+ # Remove and return the last element
1012+ print (d ) # Output: NumpyDeque([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
1013+ rightmost_value = d .pop ()
1014+ print (d ) # Output: NumpyDeque([1, 2, 3, 4, 5, 6, 7, 8, 9])
1015+ print (rightmost_value ) # Output: 10
1016+
1017+ # Remove and return the first element
1018+ leftmost_value = d .popleft ()
1019+ print (d ) # Output: NumpyDeque([2, 3, 4, 5, 6, 7, 8, 9])
1020+ print (leftmost_value ) # Output: 1
1021+
1022+ # Remove and return the third element
1023+ third_value = d .drop (2 )
1024+ print (d ) # Output: NumpyDeque([2, 3, 5, 6, 7, 8, 9])
1025+ print (third_value ) # Output: 4
1026+
1027+ # If the number 8 and 1 are found, remove the first appearance
1028+ d .remove (8 )
1029+ print (d ) # Output: NumpyDeque([2, 3, 5, 6, 7, 9])
1030+ d .remove (1 ) # Nothing happens
1031+ print (d ) # Output: NumpyDeque([2, 3, 5, 6, 7, 9])
1032+
1033+ ### Slicing
1034+
1035+ d = NumpyDeque .array ([0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ], maxsize = 10 )
1036+
1037+ # Slice behaves like NumPy arrays, but be careful with indexes
1038+ print (d [1 :4 ]) # Output: [1, 2, 3]
1039+
1040+ d [1 :3 ] = [- 1 , - 2 ] # Output: [2, 3, 4]
1041+ print (d ) # Output: NumpyDeque([0, -1, -2, 3, 4, 5, 6, 7, 8, 9])
1042+
1043+ # Note that values move once maxsize is exceeded
1044+ print (d [2 ]) # Output: -2
1045+ d .put (10 )
1046+ print (d ) # Output: NumpyDeque([-1, -2, 3, 4, 5, 6, 7, 8, 9, 10])
1047+ print (d [2 ]) # Output: 3
1048+ d .put (11 )
1049+ print (d ) # Output: NumpyDeque([-2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
1050+ print (d [2 ]) # Output: 4
1051+ d .putleft (99 )
1052+ print (d ) # Output: NumpyDeque([99, -2, 3, 4, 5, 6, 7, 8, 9, 10])
1053+ print (d [2 ]) # Output: 3
1054+
1055+ # Becareful about the size
1056+ d = NumpyDeque (maxsize = 5 )
1057+ d .put (5 )
1058+ d .put (4 )
1059+ d .put (3 )
1060+ print (d ) # Output: NumpyDeque([5, 4, 3])
1061+ try :
1062+ print (d [3 ]) # Raises index error!!!
1063+ except IndexError :
1064+ pass
0 commit comments