From: Nicolas C. <war...@fr...> - 2003-04-30 07:53:15
|
> I'm playing (while waiting for compiles) at writting a circular, mutable, > doubly linked list data structure. The advantage of this is that inserts > anywhere into the list are O(1), if you have a pointer into the list. > > Enum is almost a perfect vehicle for this- internal to the enum I'm > keeping a reference to the current element. If I could just 'reach into' > the enum and snag this reference, all would be good with the world. But I > can't figure out how to do that (this may be because it's friday afternoon > of a long, stressfull week and my brain is mush). With circular lists, your next() function will of course never raise No_more_elements, and count() should also be something like : let rec infinite_count() = infinite_count() And doing an *.of_enum with a cicular list will of course run until you kill the process or run out of memory, same for Enum.iter... You could hack something like waiting for physical equality with the first element to break the loop, but if there is a map done before, you will never get it ! Enums are right know (and should remain) enumerations on finite set of elements, I don't really see how you could use them with an infinite set... So once you get your Enum from a circular list, how are you supposed to use it ? ( remember that next() is hidden ! ) I suggest then that you write a different module, having an of_enum function that will create an list and make it circular but no enum function since such Enum cannot be used later :-) Nicolas Cannasse |