Archive for April, 2007

# Make the extracted nodes a closed circle. $last->_link_to( $first ); return $first; } Note the destroy()routine. It

Monday, April 30th, 2007

# Make the extracted nodes a closed circle. $last->_link_to( $first ); return $first; } Note the destroy()routine. It walks through all of the elements in a list and breaks their links. We use a manual destruction technique instead of the special routine DESTROY()(all uppercase) because of the subtleties of reference counting. DESTROY() runs when an object’s reference count falls to zero. But unfortunately, that will never happen spontaneously for doubleobjects because they always have two references pointing at them from their two neighbors, even if all the named variables that point to them go out of scope. If your code were to manually invoke the destroy()routine for one element on each of your doublelists just as you were finished with them, they would be freed up correctly. But that is a bother. What you can do instead is use a separate object for the header of each of your lists: package double_head; sub new { my $class = shift; my $info = shift; my $dummy = double->new; bless [ $dummy, $info ], $class; } The newmethod creates a double_headobject that refers to a dummy doubleelement (which is not considered to be a part of the list): sub DESTROY { my $self = shift; my $dummy = $self->[0]; $dummy->destroy; } The DESTROYmethod is automatically called when the double_headobject goes out of scope. Since the double_headobject has no looped references, this actually happens, and when it does, the entire list is freed with its destroy method:break Page 69 # Prepend to the dummy header to append to the list. sub append { my $self = shift; $self->[0]->prepend( shift ); return $self; } # Append to the dummy header to prepend to the list. sub prepend { my $self = shift;
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision ecommerce web hosting services

The appendand prependmethods insert an entire second list (Web site development)

Monday, April 30th, 2007

The appendand prependmethods insert an entire second list after or before an element. The internal contentmethod will be overridden later in double_headto accommodate the difference between a list denoted by its first element and a list denoted by a header: # $elem1->append( $elem2 ) # $elem->append( $head ) # # Insert the list headed by another node (or by a list) after # this node, return self. sub append { my ( $node, $add ) = @_; if ( $add = $add->content ) { $add->prev->_link_to( $node->next ); $node->_link_to( $add ); } return $node; } # Insert before this node, return self. sub prepend { my ( $node, $add ) = @_; if ( $add = $add->content ) { $node->prev->_link_to( $add->next ); $add->_link_to( $node ); } return $node; } The remove method can extract a sublist out of a list.break # Content of a node is itself unchanged # (needed because for a list head, content must remove all of # the elements from the list and return them, leaving the head # containing an empty list). sub content { return shift; } # Remove one or more nodes from their current list and return the # first of them. # The caller must ensure that there is still some reference Page 68 # to the remaining other elements. sub remove { my $first = shift; my $last = shift || $first; # Remove it from the old list. $first->prev->_link_to( $last->next );
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision servlet hosting services

my $self = { val=>shift }; bless $self, $class; return $self->_link_to( $self ); } # $elem1->_link_to( $elem2 ) # # Join this node to another, return self. # (This is for internal use only, it doesn’t not care whether (Bulletproof web design)

Monday, April 30th, 2007

my $self = { val=>shift }; bless $self, $class; return $self->_link_to( $self ); } # $elem1->_link_to( $elem2 ) # # Join this node to another, return self. # (This is for internal use only, it doesn’t not care whether # the elements linked are linked into any sort of correct # list order.) sub _link_to { my ( $node, $next ) = @_; $node->next( $next ); return $next->prev( $node ); } The destroymethod can be used to break all of the links in a list (see double_headlater in this chapter): sub destroy { my $node = shift; while( $node ) { my $next = $node->next; $node->prev(undef); $node->next(undef); $node = $next; } } The nextand prevmethods provide access to the links, to either follow or change them:break # $cur = $node->next # $new = $node->next( $new ) # # Get next link, or set (and return) a new value in next link. sub next { my $node = shift; Page 67 return @_ ? ($node->{next} = shift) : $node->{next}; } # $cur = $node->prev # $new = $node->prev( $new ) # # Get prev link, or set (and return) a new value in prev link. sub prev { my $node = shift; return @_ ? ($node->{prev} = shift) : $node->{prev}; }
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision shared web hosting services

using a data structure format that has loops, (Msn web hosting)

Monday, April 30th, 2007

using a data structure format that has loops, you should not be managing it with inline co1de, but with subroutines or a package that checks every operation for any change in list consistency information and that provides a means for cleaning up afterwards. A package can have a DESTROY()method that will be called whenever an object of the package goes out of scope. A method with that name has a special meaning to Perl: the routine gets called automatically when Perl determines that an object should be freed (because its reference count has gone to zero). So for a structure with cyclical references, the DESTROY() method can be used to run cycle-breaking code such as that just shown. Doubly-Linked Lists A prime candidate for the cleanup mechanism just described is the doubly-linked list. Instead of one link field in each element, there are two. One points to the next element, as in the previous linked lists; the other points back to the previous element. It is also common for the ends of a doubly-linked list to be joined in a circle. Note that this data structure creates cycles from the circular linking of the ends, as well as a cycle from the forward and backward links between every adjacent pair of elements. The link to the previous element means that it is not necessary to search through the entire list to find a node’s predecessor. It is also possible to move back multiple positions on the list, which you can’t do by keeping only a predecessor pointer. Of course, this flexibility comes at a cost: whenever a link is changed, the back link must also be changed, so every linking operation is twice as expensive. Sometimes it’s worth it. When using circular doubly-linked lists, it is useful to keep an element linked to itself when it is not on any list. That bit of hygiene makes it possible to have many of the operations work consistently for either a single element or a list of multiple elements. Consider, for example, the append()and prepend()functions about to be described, which insert one or many elements before or after a specific element. These functions work on a list that has only a single element so long as it points to itself. They fail if you have removed that element from another list without relinking the standalone element to point to itself. (The code for a singlylinked list earlier in this chapter overwrites the link field whenever it inserts an element into a list, so the code will work fine whatever old value was in the link field.)break Page 66 Here’s a package doublethat can carry out doubly-linked list operations. Parts of it are designed to coexist with the package double_headshown later in this chapter. The new method is a typical object creation function. The _link_tomethod is only for internal use; it connects two elements as neighbors within a list: package double; # $node = double->new( $val ); # # Create a new double element with value $val. sub new { my $class = shift; $class = ref($class) || $class;
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check Vision mysql hosting services

Msn web hosting - Figure 3-7. Memory leak caused by deleting circular references Make sure that you break

Sunday, April 29th, 2007

Figure 3-7. Memory leak caused by deleting circular references Make sure that you break the circle while you still have access to the values. Don’t make any circular loops of references in the first place. Circular lists have this problem since each of the elements is pointed at by another. Keeping a tail pointer in the value field of a dummy header can have the same problem: it points to its own element when the list is empty. What do you do about this? If your program runs for a long time, and has lots of cyclic data structures coming and going, it may slow to a crawl as it develops huge memory requirements. It might eventually crash, or get swapped out and never swapped back in. These are not normally considered good operational characteristics for a long-running program! In this case, you can’t just ignore the problem but must help Perl’s garbage collector. Suppose our process scheduler had the ability to halt and that it was used many times. The chain of processes each time would never be reclaimed (because of the circular link) unless the halt operation provided some assistance:break # . . . in the list of opcodes for the earlier scheduler example elsif ($quit_cause == $HALT) { # we’re quitting - first break the process chain Page 65 $pred->link(undef); return; } This need to break reference loops is a reason to use a packaged set of routines. If you are
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision make web site services

# “abc” remains in use $p = 1; # “abc” is freed At the end of the (Simple web server)

Sunday, April 29th, 2007

# “abc” remains in use $p = 1; # “abc” is freed At the end of the block, $yhas gone out of scope. Its value, “def”, had a count of 1 so it can be freed. $xhas also gone out of scope, but its value “abc”had a count of 2. The count is decremented to 1 and the value is not freed it is still accessible through $p. Later, $pis reassigned, overwriting the reference to “abc”. This means that the count for “abc”is decremented. Since its count is now zero, it is freed. Reference counting is usually quite effective, but it breaks down when you have a circle of reference values. When the last outside variable that points to any of them is destroyed or changed, they all still have a nonzero count. Here’s an example (shown in Figure 3-7): # start a new scope { # two variables my $p1 = 1; my $p2 = 2; # point them at each other $p1 = $p2; $p2 = $p1; } # end scope After the block was exited, the two values still have a nonzero count, but $p1and $p2no longer exist, so there is no way that the program can ever access them. You know the old joke: ‘’Doctor, it hurts when I do this.” “So, don’t do that.” That’s Perl’s answer to this problem. (For now, at least this situation may change in future releases.) Perl leaves it to the programmer to solve this. Here are some possible solutions: Ignore the problem and it will go away when your program terminates.break Page 64
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision j2ee hosting services

IO_wait($current_process); $current_process = $next_process; } elsif ( $quit_cause == $IO_COMPLETE ) { # Some I/O has completed - add the process # waiting for it back into the list. # If the current process is Idle, progress to # the new process immediately. # Otherwise, continue the current process until

Sunday, April 29th, 2007

IO_wait($current_process); $current_process = $next_process; } elsif ( $quit_cause == $IO_COMPLETE ) { # Some I/O has completed - add the process # waiting for it back into the list. # If the current process is Idle, progress to # the new process immediately. # Otherwise, continue the current process until # the end of its slice. $io_process->link( $current_process ); $pred = $pred->link( $io_process ); } elsif ( $quit_cause = $QUIT ) { # This process has completed - remove it from the list. $next_process = $pred->link( $current_process->link ); $current_process = $next_process; } elsif ( $quit_cause = $FORK ) { # Fork a new process. Put it at the end of the list. $new_process = new process( $current_process->process_info ); $new_process->link( $current_process ); $pred = $pred->link( $new_process ); } # run the current process $quit_cause = $current_process->run; } There are a few gaps in this code. Turning it into a complete operating system is left as an exercise for the reader. Garbage Collection in Perl Normally, Perl determines when a value is still needed using a technique called reference counting, which is simple and quick and creates no unpredictable delays in operation. The Perl interpreter keeps a reference counter for each value. When a value is created and assigned to a variable, the counter is set to one. If an additional reference is created to point to it, the count is incremented. A reference can go away for two reasons. First, when a block is exited, any variables that were defined in that scope are destroyed. The reference counts for their values is decremented. Second, if a new value is assigned that replaces a reference value, the count of the value that was previously referenced is decremented. Whenever a reference count goes to zero, there are no more variables referring to that value, so itcontinue Page 63 can be destroyed. (If the deleted value is a reference, deletion causes a cascading effect for a while, since destroying the reference can reduce the reference count of the value that it refers to.) my $p; { my $x = “abc”; my $y = “def”; $p = $x; # the value “abc” now has a count of two } # “def” is freed
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision j2ee hosting services

my ( $class, $name, $state ) = @_; my $self = { name=>$name, state=>$state }; return bless $self, $class; } # link method - get or set the link to the next process # Usage: # $next = $proc->link; # Or: (Web hosting contract)

Sunday, April 29th, 2007

my ( $class, $name, $state ) = @_; my $self = { name=>$name, state=>$state }; return bless $self, $class; } # link method - get or set the link to the next process # Usage: # $next = $proc->link; # Or: # $proc->link($other_proc); sub link { my $process = shift; return @_ ? ($process->{link} = shift) : $process->{link}; } # . . . and a few other routines . . . } # Create the idle process. Its state contains a program that # loops forever, giving up its slice immediately each time. $idle = new process(”Idle”, $idle_state); # Create the “Boot” process, which loads some program in from # disk, initializes and queues the process state for that # program, and then exits. $boot = new process(”Boot”, $boot_state); # Set up the circular link $idle->link($boot); $boot->link($idle); # and get ready to run, as if we just finished a slice for $idle. $pred = $boot; $current_process = $idle; $quit_cause = $SLICE_OVER; # Here’s the scheduler - it never exits. while ( 1 ) { Page 62 if ( $quit_cause == $SLICE_OVER ) { # Move to the next process. $pred = $current_process; $current_process = $current_process->link; } elsif ( $quit_cause == $IO_BLOCK ) { # The current process has issued some I/O. # Remove it from the list, and move on to the next $next_process = $pred->link( $current_process->link ); # Add $current_process to a list for the I/O device.
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check Vision professional web hosting services

which element is to be considered the head. (Web site templates)

Saturday, April 28th, 2007

which element is to be considered the head. You can use circular lists when a list of items to be processed can require more than one processing pass for each item. A server process might be an example, since it would try to give each of its requests some time in turn rather than permit one possibly large request from delaying all of the others excessively. A circular linked list gives you most of the capabilities of a deque. You can easily add elements to the end or beginning. (Just keep the list pointer always pointing at the tail, whose successor is by definition the head. Add new elements after the tail, either leaving the list pointer unchanged or changing it to point to the new element. The first option leaves the new element at the head of the list, while the second leaves the new element at the tail.) Removing elements from the head is equally easy. Deleting the element after the tail removes the head element. However, you can’t delete the last element of the list without scanning the entire list to find its predecessor. This is the one way that a circular linked list is less capable than a deque. The circular linked list also has one capability that a deque lacks: you can inexpensively rotate the circle simply by reassigning the list pointer. A deque implemented as an array requires two splice operations to accomplish a rotation, which might be expensive if the array is long. In practice, however, the most common change to the list pointer is to move it to the next element, which is an inexpensive operation for either a circular linked list or a deque (just shiftthe head off the deque and then pushit back onto the tail). With a circular linked list, as with the standard linked list, you must handle the possibility that the list is empty. Using a dummy element is no longer a good solution, because it becomes more awkward to move the list pointer. (The dummy element would have to be unlinked from its position between the tail and the head and then relinked between the new tail and head). Instead, just make the code that removes an element check whether it is the only element in the list and, if so, set the list pointer to undef.break Page 61 Here’s the code for a very simple operating system that uses a circular linked list for its runnable processes. Each process is run for a little while. It stops when it has used up its time slice, blocks for an I/O operation, or terminates. It can also stop momentarily when some I/O operation being conducted for another process completes which re-enables that other process. We avoid the empty list problem here by having an Idleprocess that is always ready to run.break { # process # This package defines a process object. package process; # new - create a process object sub new {
Note: In case you are looking for affordable webhost to host and run your web application check Vision http web server services

last; ################################################## } } One final operation that (Web hosting control panel)

Saturday, April 28th, 2007

last; ################################################## } } One final operation that can occasionally be useful is reversing the elements of a list: # $list = list_reverse( $list ) # Reverse the order of the elements of a list. sub list_reverse { my $old = shift; my $new = undef; while (my $cur = $old) { $old = $old->[NEXT]; $cur->[NEXT] = $new; $new = $cur; } return $new; } We could have used the previous routine instead of a tail pointer when reading lines from a file: # Alternate way to build list of lines from STDIN: my $list; while (<>) { $list = [ $list, $_ ]; } $list = list_reverse( $list ); However, the extra pass through the list to reverse it is slower than building the list correctly (with the tail pointer). Additionally, if you often need to traverse a list backward, you’ll probably instead prefer to use doubly-linked lists as described a bit later. The previous material on linked lists has been fairly slow-moving and detailed. Now, we’re going to pick up the pace. (If you absorbed the previous part, you should be able to apply the same principles to the following variants. However, you are more likely to be using a packaged module for them, so precise understanding of all of the implementation details is not so important as understanding their costs and benefits.)break Page 60 Circular Linked Lists One common variation of the linked list is the circular linked list, which has no beginning and no end. Here, instead of using undefto denote the end of the list, the last element points back to the first. Because of the circular link, the idea of the head and tail of the list gets fuzzier. The list pointer (e.g., $list) is no longer the only way to access the element at the head of the linked list you can get to it from any element by following the right number of links. This means that you can simply reassign the list pointer to point to a different element to change
Note: In case you are looking for affordable and reliable webhost to host and run your business application check Vision ftp web hosting services