001    /*
002     *  This file is part of the Jikes RVM project (http://jikesrvm.org).
003     *
004     *  This file is licensed to You under the Eclipse Public License (EPL);
005     *  You may not use this file except in compliance with the License. You
006     *  may obtain a copy of the License at
007     *
008     *      http://www.opensource.org/licenses/eclipse-1.0.php
009     *
010     *  See the COPYRIGHT.txt file distributed with this work for information
011     *  regarding copyright ownership.
012     */
013    package org.jikesrvm.compilers.opt.util;
014    
015    
016    final class SpaceEffGraphNodeListHeader {
017      SpaceEffGraphNodeList _first;
018      SpaceEffGraphNodeList _last;
019    
020      SpaceEffGraphNodeList first() {
021        return _first;
022      }
023    
024      SpaceEffGraphNodeList last() {
025        return _last;
026      }
027    
028      public void append(SpaceEffGraphNode node) {
029        SpaceEffGraphNodeList p = new SpaceEffGraphNodeList();
030        p._node = node;
031        SpaceEffGraphNodeList last = _last;
032        if (last == null) {
033          // will be the case for first edge.
034          _first = p;
035          _last = p;
036        } else {
037          // there is at least one node.
038          last._next = p;
039          p._prev = last;           // doubly linked list.
040          _last = p;
041        }
042      }
043    
044      public boolean add(SpaceEffGraphNode node) {
045        SpaceEffGraphNodeList p = first();
046        SpaceEffGraphNodeList prev = first();
047        if (p == null) {
048          // will be the case for first node.
049          p = new SpaceEffGraphNodeList();
050          p._node = node;
051          _first = p;
052          _last = p;
053          return true;
054        }
055        while (p != null) {
056          if (p._node == node) {
057            // node already in list.
058            return false;
059          }
060          prev = p;
061          p = p._next;
062        }
063        prev._next = new SpaceEffGraphNodeList();
064        prev._next._node = node;
065        prev._next._prev = prev;                    // doubly linked list.
066        _last = prev._next;
067        return true;
068      }
069    }