Path: utzoo!attcan!uunet!yale!cmcl2!lanl!jlg From: jlg@lanl.gov (Jim Giles) Newsgroups: comp.lang.fortran Subject: Re: data types without pointers Message-ID: <4108@lanl.gov> Date: 24 Sep 88 00:37:25 GMT Organization: Los Alamos National Laboratory Lines: 109 > [...] Try representing a dynamic length > fifo using Hoare's structure and answering the questions: > > 1) What happens when I add to a fifo? > 2) How do I represent this structure? > 3) How do I keep track of the head/tail? > 4) How do I detect an empty fifo? Fifo's _can_ be implemented as recursive data structures, but arrays with indices are easier and more efficient. So I'll do that with a Fortran 8x-like syntax. MODULE FIFO !declare a module full of the FIFO queuing !routines Type FIFO integer head=0, tail=0, size=0 ! initialize as well as declare logical full=.true., empty=.true. allocatable real :: data(:) ! array is dynamic - can be resized end type FIFO subroutine ADDTO (queue, item) assignment C assignment subroutines - all the user says is 'queue = item' FIFO :: queue real item constant integer :: default_increase = 200 !initialize to 200 if (queue%full) then queue%size=queue%size+default_increase allocate(queue%data(size)) !I assume that allocate will resize !if the array already existed endif queue%head = queue%head + 1 if (queue%head.gt.queue%size) queue%head=1 if (queue%head.eq.queue%tail) queue%full=.true. queue%data(queue%head) = item queue%empty=.false. return end subroutine ADDTO subroutine TAKEFROM (item, queue) assignment C All the user says is 'item = queue' FIFO :: queue real item if (queue%empty) then C CODE FOR EMPTY QUEUE REQUEST endif queue%tail = queue%tail + 1 if (queue%tail.gt.size) queue%tail = 1 if (queue%head.eq.queue%tail) queue%empty=.true. item = queue%data(queue%tail) queue%full = .false. return end subroutine TAKEFROM END MODULE FIFO Program FIFO_USER include fifo FIFO :: p, q !declare two different queues real :: x, y ... p = 5.0 !push 5.0 onto queue p x = p !get next item off p ... if (p%empty) then !easy to test if queue is empty ... END PROGRAM FIFO_USER Note: Fortran 8x is not case sensitive but I tried to use the same case for each item consistently anyway. Well, there you are. You even have the assignment operators you asked for. There aren't any pointers here either. > BTW, recursive structures are a very clumsy way to think about bags, I agree. So don't implement them with recursive structures. Recursive structures aren't for everything. Use the structure which best fits the problem. This means: rarely use pointers - if you can avoid it. And, if you're talking about language _design_, don't force pointers onto your users. > [...] I said that > if I don't have the data types and have to build my own abstractions, > I prefer to use pointers to do it. And (rather obviously) I'd rather not. Certainly I don't want something like C where I get pointers defined automatically every time I delcare a dynamically allocatable data item - whether I need the pointer or not! It is better to have different functionality actually have different syntax as well. Most uses of pointers are actually something else in disguise. J. Giles Los Alamos