Oct-15-2020, 03:26 PM
i was thinking about the concept of a new container type. this is not for any particular need i have so i have no need to implement this. the name "ring" seems to make sense. maybe someone has a better name. a ring is very much like a list.
does anyone think this might have some uses. it seems to me like practice implementing a container.
a ring is a list with the end connected back to its own beginning. indexing is modulo the len() of the ring. a ring of 8 items can be indexed at [90] and access the very same position as [2] or [-6]. as in a list, index [-1] is the last item in a ring. a ring can be empty. trying to index an empty ring raises IndexError. indexing beyond the size of the ring indexes % len(ring)
ring.pop(pos) removes and returns one item from the ring, making the ring smaller by one. pop defaults to -1.
ring.insert(item,pos) inserts item where it comes to be after position pos making the ring one larger. pos defaults to -1 which inserts between the last and first like append() for a list.
ring.insertbefore(item,pos) might be implemented.
ring.rotateforward(count) shifts items forward in the right with items in the end (at [-1]) moving into the beginning (at [0]). this is repeated count times. a negative count rotates in reverse.
ring.reverse() flips a ring over, so to speak. all items will now be in reverse order.
ring() creates an new empty ring.
ring(ring) creates a duplicate ring with the same items and references.
ring(list/tuple) creates a ring with all items and references in the same order.
ring(a,b,c,d) is like ring((a,b,c,d))
to create a ring with one container do ring((container)) or do ring([container]).
a ring used as an iterator starts at position 0 and never ends.
does anyone think this might have some uses. it seems to me like practice implementing a container.
a ring is a list with the end connected back to its own beginning. indexing is modulo the len() of the ring. a ring of 8 items can be indexed at [90] and access the very same position as [2] or [-6]. as in a list, index [-1] is the last item in a ring. a ring can be empty. trying to index an empty ring raises IndexError. indexing beyond the size of the ring indexes % len(ring)
ring.pop(pos) removes and returns one item from the ring, making the ring smaller by one. pop defaults to -1.
ring.insert(item,pos) inserts item where it comes to be after position pos making the ring one larger. pos defaults to -1 which inserts between the last and first like append() for a list.
ring.insertbefore(item,pos) might be implemented.
ring.rotateforward(count) shifts items forward in the right with items in the end (at [-1]) moving into the beginning (at [0]). this is repeated count times. a negative count rotates in reverse.
ring.reverse() flips a ring over, so to speak. all items will now be in reverse order.
ring() creates an new empty ring.
ring(ring) creates a duplicate ring with the same items and references.
ring(list/tuple) creates a ring with all items and references in the same order.
ring(a,b,c,d) is like ring((a,b,c,d))
to create a ring with one container do ring((container)) or do ring([container]).
a ring used as an iterator starts at position 0 and never ends.