src/share/classes/com/sun/tools/javac/util/ListBuffer.java

changeset 1444
170e486632d9
parent 581
f2fdd52e4e87
child 2047
5f915a0c9615
     1.1 --- a/src/share/classes/com/sun/tools/javac/util/ListBuffer.java	Tue Dec 11 15:05:55 2012 -0800
     1.2 +++ b/src/share/classes/com/sun/tools/javac/util/ListBuffer.java	Wed Dec 12 20:26:56 2012 +0100
     1.3 @@ -1,5 +1,5 @@
     1.4  /*
     1.5 - * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
     1.6 + * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
     1.7   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.8   *
     1.9   * This code is free software; you can redistribute it and/or modify it
    1.10 @@ -52,19 +52,20 @@
    1.11  
    1.12      /** The list of elements of this buffer.
    1.13       */
    1.14 -    public List<A> elems;
    1.15 +    private List<A> elems;
    1.16  
    1.17 -    /** A pointer pointing to the last, sentinel element of `elems'.
    1.18 +    /** A pointer pointing to the last element of 'elems' containing data,
    1.19 +     *  or null if the list is empty.
    1.20       */
    1.21 -    public List<A> last;
    1.22 +    private List<A> last;
    1.23  
    1.24      /** The number of element in this buffer.
    1.25       */
    1.26 -    public int count;
    1.27 +    private int count;
    1.28  
    1.29      /** Has a list been created from this buffer yet?
    1.30       */
    1.31 -    public boolean shared;
    1.32 +    private boolean shared;
    1.33  
    1.34      /** Create a new initially empty list buffer.
    1.35       */
    1.36 @@ -73,8 +74,8 @@
    1.37      }
    1.38  
    1.39      public final void clear() {
    1.40 -        this.elems = new List<A>(null,null);
    1.41 -        this.last = this.elems;
    1.42 +        this.elems = List.nil();
    1.43 +        this.last = null;
    1.44          count = 0;
    1.45          shared = false;
    1.46      }
    1.47 @@ -103,22 +104,23 @@
    1.48      /** Copy list and sets last.
    1.49       */
    1.50      private void copy() {
    1.51 -        List<A> p = elems = new List<A>(elems.head, elems.tail);
    1.52 -        while (true) {
    1.53 -            List<A> tail = p.tail;
    1.54 -            if (tail == null) break;
    1.55 -            tail = new List<A>(tail.head, tail.tail);
    1.56 -            p.setTail(tail);
    1.57 -            p = tail;
    1.58 +        if (elems.nonEmpty()) {
    1.59 +            List<A> orig = elems;
    1.60 +
    1.61 +            elems = last = List.<A>of(orig.head);
    1.62 +
    1.63 +            while ((orig = orig.tail).nonEmpty()) {
    1.64 +                last.tail = List.<A>of(orig.head);
    1.65 +                last = last.tail;
    1.66 +            }
    1.67          }
    1.68 -        last = p;
    1.69 -        shared = false;
    1.70      }
    1.71  
    1.72      /** Prepend an element to buffer.
    1.73       */
    1.74      public ListBuffer<A> prepend(A x) {
    1.75          elems = elems.prepend(x);
    1.76 +        if (last == null) last = elems;
    1.77          count++;
    1.78          return this;
    1.79      }
    1.80 @@ -128,9 +130,13 @@
    1.81      public ListBuffer<A> append(A x) {
    1.82          x.getClass(); // null check
    1.83          if (shared) copy();
    1.84 -        last.head = x;
    1.85 -        last.setTail(new List<A>(null,null));
    1.86 -        last = last.tail;
    1.87 +        List<A> newLast = List.<A>of(x);
    1.88 +        if (last != null) {
    1.89 +            last.tail = newLast;
    1.90 +            last = newLast;
    1.91 +        } else {
    1.92 +            elems = last = newLast;
    1.93 +        }
    1.94          count++;
    1.95          return this;
    1.96      }
    1.97 @@ -192,8 +198,9 @@
    1.98       */
    1.99      public A next() {
   1.100          A x = elems.head;
   1.101 -        if (elems != last) {
   1.102 +        if (!elems.isEmpty()) {
   1.103              elems = elems.tail;
   1.104 +            if (elems.isEmpty()) last = null;
   1.105              count--;
   1.106          }
   1.107          return x;
   1.108 @@ -205,10 +212,10 @@
   1.109          return new Iterator<A>() {
   1.110              List<A> elems = ListBuffer.this.elems;
   1.111              public boolean hasNext() {
   1.112 -                return elems != last;
   1.113 +                return !elems.isEmpty();
   1.114              }
   1.115              public A next() {
   1.116 -                if (elems == last)
   1.117 +                if (elems.isEmpty())
   1.118                      throw new NoSuchElementException();
   1.119                  A elem = elems.head;
   1.120                  elems = elems.tail;
   1.121 @@ -263,4 +270,8 @@
   1.122      public A peek() {
   1.123          return first();
   1.124      }
   1.125 +
   1.126 +    public A last() {
   1.127 +        return last != null ? last.head : null;
   1.128 +    }
   1.129  }

mercurial