Example #1
0
  private Token receive0() {
    Node s = new Node(null, false); // the node to append

    retry:
    for (; ; ) { // restart on append race
      Object item = tryMatch(null, null, false);
      if (item != NO_MATCH) {
        s.item = item;
        return new Token(s, null);
      }

      Node pred = tryAppend(s, false);
      if (pred == null) continue retry; // lost race vs opposite mode

      if (sendClosed) {
        s.item = CHANNEL_CLOSED;
        unsplice(pred, s);
        setReceiveClosed();
        return new Token(s, null);
      }

      requestUnpark(s, Strand.currentStrand());
      return new Token(s, pred);
    }
  }
  /**
   * Removes a node from head of queue,
   *
   * @return the node
   */
  private Node<E> extract() {
    Node<E> current = head;
    head = head.next;

    current.item = head.item;
    head.item = null;

    return current;
  }
Example #3
0
  /**
   * Implements all queuing methods. See above for explanation.
   *
   * @param e the item or null for take
   * @param haveData true if this is a put, else a take
   * @param how NOW, ASYNC, SYNC, or TIMED
   * @param nanos timeout in nanosecs, used only if mode is TIMED
   * @return an item if matched, else e
   * @throws NullPointerException if haveData mode but e is null
   */
  private Object xfer1(Message e, boolean haveData, int how, long nanos) throws SuspendExecution {
    assert how == SYNC || how == TIMED;
    if (haveData && (e == null)) throw new NullPointerException();

    Node s = null; // the node to append, if needed

    retry:
    for (; ; ) { // restart on append race
      Object item = tryMatch(null, e, haveData);
      if (item != NO_MATCH) return item;

      if (s == null) s = new Node(e, haveData);
      Node pred = tryAppend(s, haveData);
      if (pred == null) continue retry; // lost race vs opposite mode

      if (!haveData && sendClosed) {
        s.item = CHANNEL_CLOSED;
        unsplice(pred, s);
        setReceiveClosed();
        return CHANNEL_CLOSED;
      }

      return awaitMatch(s, pred, e, (how == TIMED), nanos);
    }
  }
  public E take() throws InterruptedException {
    Node<E> x;
    final AtomicInteger count = this.count;
    final ReentrantLock takeLock = this.takeLock;
    takeLock.lockInterruptibly();
    try {
      try {
        while (count.get() == 0) notEmpty.await();
      } catch (InterruptedException ie) {
        notEmpty.signal(); // propagate to a non-interrupted thread
        throw ie;
      }

      x = extract();
      if (count.getAndDecrement() > 1) notEmpty.signal();
    } finally {
      takeLock.unlock();
    }

    E result = x.item;

    // temporary clearence
    x.item = null;
    x.next = null;

    return result;
  }
  public Item dequeue() // delete and return a random item
      {
    if (isEmpty()) {
      throw new java.util.NoSuchElementException("RandomizedQueue is empty");
    }

    int index = StdRandom.uniform(0, size);
    Node node = findNode(index);

    Item item = node.item;
    Node prevnode = node.prev;
    Node nextnode = node.next;

    if (prevnode != null) {
      prevnode.next = nextnode;
    } else {
      first = nextnode;
    }

    if (nextnode != null) {
      nextnode.prev = prevnode;
    } else {
      last = prevnode;
    }

    node.item = null;
    node.next = null;
    node.prev = null;

    size--;
    return item;
  }
Example #6
0
  /**
   * Will add a new item to the start of the deque. Will throw an error if an empty item is passed
   * through. If this is the first item that has been added to the deque, then this will also set
   * the last item in the deque to be the first.
   *
   * @param item The item to add to the start of the deque
   */
  public void addFirst(Item item) {
    if (item == null) {
      throw new NullPointerException("Woah, you can't add nothing");
    }

    Node previousFirst = first;
    first = new Node();
    first.item = item;
    first.previous = null;

    if (previousFirst == null) {
      // This is the first time something has been added to the deque
      first.next = null;
    } else {
      first.next = previousFirst;
      previousFirst.previous = first;
    }

    // Is this the only node in the deque?
    if (last == null) {
      last = first;
    }

    N++;
  }
 public void remove() {
   Node<E> l = lastRet;
   if (l == null) throw new IllegalStateException();
   // rely on a future traversal to relink.
   l.item = null;
   lastRet = null;
 }
Example #8
0
 /**
  * Adds the item to this stack.
  *
  * @param item the item to add
  */
 public void push(Item item) {
   Node<Item> oldfirst = first;
   first = new Node<Item>();
   first.item = item;
   first.next = oldfirst;
   N++;
 }
Example #9
0
 /** Add the item to the bag. */
 public void add(Item item) {
   Node oldfirst = first;
   first = new Node();
   first.item = item;
   first.next = oldfirst;
   N++;
 }
  public E poll() {
    final AtomicInteger count = this.count;
    if (count.get() == 0) return null;

    Node<E> x = null;
    final ReentrantLock takeLock = this.takeLock;
    takeLock.lock();

    try {
      if (count.get() > 0) {
        x = extract();
        if (count.getAndDecrement() > 1) notEmpty.signal();
      }
    } finally {
      takeLock.unlock();
    }

    if (x != null) {
      E result = x.item;

      // temporary clearence
      x.item = null;
      x.next = null;

      return result;
    }

    return null;
  }
Example #11
0
 public void setNext(Node next) {
   Node oldNode = (Node) pj.find(this);
   Node newNode = new Node(oldNode.item);
   newNode.item = item;
   newNode.next = next;
   pj.update(oldNode, newNode);
 }
Example #12
0
 public void push(T item) {
   Node oldFirst = first;
   first = new Node();
   first.item = item;
   first.next = oldFirst;
   size++;
 }
Example #13
0
 /** Add the item to the stack. */
 public void push(Item item) {
   Node oldfirst = first;
   first = new Node();
   first.item = item;
   first.next = oldfirst;
   N++;
   assert check();
 }
Example #14
0
 public void enqueue(Item item) {
   Node oldlast = last;
   Node last = new Node();
   last.item = item;
   if (isEmpty()) first = last;
   else oldlast.next = last;
   N++;
 }
 /**
  * Replaces the element at the specified position in this {@code LinkedList} with the specified
  * element. Returns the element previously at the specified index.
  *
  * @param index The index of the element to replace.
  * @param element The element to be stored at the specified index.
  * @return The element previously at the specified index.
  * @throws IndexOutOfBoundsException if the specified index value is out of range.
  */
 @Override
 public E set(int index, E element) {
   rangeCheck(index);
   Node<E> n = nodeAt(index);
   E oldItem = n.item;
   n.item = element;
   return oldItem;
 }
 /**
  * Sets the given element to the underlying {@code LinkedList} at the current cursor index.
  *
  * @param element The element to set at the specified index in the backing {@code LinkedList}.
  * @throws ConcurrentModificationException if the expected modification count does not equal
  *     that of the underlying {@code LinkedList}.
  * @throws IllegalStateException if the {@link #next()} method has not previously been called or
  *     the {@link #remove()} method has already been called after the last call to the {@link
  *     #next()} method.
  */
 @Override
 public void set(E element) {
   if (lastReturned == null) {
     throw new IllegalStateException("No current index!");
   }
   checkForComodification();
   lastReturned.item = element;
 }
Example #17
0
 public void enqueue(String str) {
   Node oldlast = last;
   last = new Node();
   last.item = str;
   last.next = null;
   if (isEmpty()) first = last;
   else oldlast.next = last;
 }
Example #18
0
 /** Add the item to the stack. */
 public void push(Item item) {
   Node previous = head;
   head = new Node();
   head.item = item;
   head.previous = previous;
   N++;
   assert check();
 }
Example #19
0
 /**
  * Adds the item to this queue.
  *
  * @param item the item to add
  */
 public void enqueue(Item item) {
   Node<Item> oldlast = last;
   last = new Node<Item>();
   last.item = item;
   last.next = null;
   if (isEmpty()) first = last; // 防止oldlast==null,而访问.next报空指针异常
   else oldlast.next = last;
   N++;
 }
  /**
   * Store node for reuse it later.
   *
   * @param node node for reuse.
   */
  private void freeNode(Node<E> node) {
    node.item = null;

    Node<E> oldNew;
    do {
      oldNew = freeNode;
      node.next = oldNew;
    } while (!casNewNode(oldNew, node));
  }
Example #21
0
 public void engueue(Item item) {
   Node oldLast = last;
   last = new Node();
   last.item = item;
   last.next = null;
   count++;
   if (isEmpty()) first = last;
   else oldLast.next = last;
 }
Example #22
0
 public void addFirst(Item item) // insert the item at the front
     {
   if (item == null) {
     throw new java.lang.NullPointerException();
   }
   Node oldfirst = first;
   first = new Node();
   first.item = item;
   first.next = oldfirst;
   size++;
 }
Example #23
0
 // add the item to the end
 public void addLast(Item item) {
   if (item == null) throw new java.lang.NullPointerException();
   Node oldLast = last;
   last = new Node();
   last.item = item;
   last.last = oldLast;
   last.next = null;
   if (oldLast != null) oldLast.next = last;
   else first = last;
   N++;
 }
Example #24
0
 public void addLast(Item item) // insert the item at the end
     {
   if (item == null) {
     throw new java.lang.NullPointerException();
   }
   Node oldLast = last;
   last = new Node();
   last.item = item;
   last.next = null;
   oldLast.next = last;
 }
Example #25
0
 /** Add the item to the queue. */
 public void enqueue(Item item) {
   Node x = new Node();
   x.item = item;
   if (isEmpty()) {
     first = x;
     last = x;
   } else {
     last.next = x;
     last = x;
   }
   N++;
 }
 public void append(Item item) {
   Node n = new Node();
   n.item = item;
   n.next = null;
   if (isEmpty()) {
     first = n;
     last = n;
   } else {
     last.next = n;
     last = n;
   }
   N++;
 }
 public void enqueue(Item item) // add the item
     {
   if (item == null) {
     throw new java.lang.NullPointerException("Not allowed to add null");
   }
   Node oldfirst = first;
   first = new Node();
   first.item = item;
   first.next = oldfirst;
   first.prev = null;
   if (oldfirst != null) oldfirst.prev = first;
   else last = first;
   size++;
 }
 /**
  * Removes all elements contained within this {@code LinkedList}. After this method call, {@link
  * #isEmpty()} should always return {@code true}.
  *
  * <p>This operation constitutes a structural modification.
  */
 @Override
 public void clear() {
   Node<E> next;
   for (Node<E> n = first; n != null; n = next) {
     next = n.nextLink;
     n.item = null;
     n.previousLink = null;
     n.nextLink = null;
   }
   first = null;
   last = null;
   size = 0;
   modCount++;
 }
Example #29
0
    public void insertAtEnd(Item a) {
      Node new_node = new Node();
      new_node.item = a;

      if (size == 0) {
        first = new_node;
        last = new_node;
      } else {
        last.next = new_node;
        last = new_node;
      }

      size++;
    }
Example #30
0
  /*
   * insert the item at the end
   */
  public void addLast(Item item) {
    if (item == null) {
      throw new java.lang.NullPointerException();
    } else {
      if (N == 0) { // adding to the empty deque
        // full new node construction
        first = new Node();
        first.item = item;

        // links from new node
        first.prev = sPre;
        first.next = sPost;
        last = first;

        // links to new node
        sPre.next = first;
        sPost.prev = first;
      } else {
        // StdOut.println("\naddLast() case 2");
        // node before which to insert
        Node oldLast = last;

        // full new node construction
        last = new Node();
        last.item = item;

        // links from new node
        last.prev = oldLast;
        last.next = sPost;

        // links to new node
        oldLast.next = last;
        sPost.prev = last;
      }
      N++;
    }
  }