1 /*
   2  * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.nashorn.internal.runtime.arrays;
  27 
  28 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
  29 import java.lang.reflect.Array;
  30 import jdk.nashorn.internal.runtime.BitVector;
  31 
  32 /**
  33  * This filter handles the deletion of array elements.
  34  */
  35 final class DeletedArrayFilter extends ArrayFilter {
  36     /** Bit vector tracking deletions. */
  37     private final BitVector deleted;
  38 
  39     DeletedArrayFilter(final ArrayData underlying) {
  40         super(underlying);
  41         this.deleted = new BitVector(underlying.length());
  42     }
  43 
  44     @Override
  45     public ArrayData copy() {
  46         final DeletedArrayFilter copy = new DeletedArrayFilter(underlying.copy());
  47         copy.getDeleted().copy(deleted);
  48         return copy;
  49     }
  50 
  51     @Override
  52     public Object[] asObjectArray() {
  53         final Object[] value = super.asObjectArray();
  54 
  55         for (int i = 0; i < value.length; i++) {
  56             if (deleted.isSet(i)) {
  57                 value[i] = UNDEFINED;
  58             }
  59         }
  60 
  61         return value;
  62     }
  63 
  64     @Override
  65     public Object asArrayOfType(final Class<?> componentType) {
  66         final Object value = super.asArrayOfType(componentType);
  67         final Object undefValue = convertUndefinedValue(componentType);
  68         final int l = Array.getLength(value);
  69         for (int i = 0; i < l; i++) {
  70             if (deleted.isSet(i)) {
  71                 Array.set(value, i, undefValue);
  72             }
  73         }
  74 
  75         return value;
  76     }
  77 
  78     @Override
  79     public void shiftLeft(final int by) {
  80         super.shiftLeft(by);
  81         deleted.shiftLeft(by, length());
  82     }
  83 
  84     @Override
  85     public ArrayData shiftRight(final int by) {
  86         super.shiftRight(by);
  87         deleted.shiftRight(by, length());
  88         return this;
  89     }
  90 
  91     @Override
  92     public ArrayData ensure(final long safeIndex) {
  93         if (safeIndex >= SparseArrayData.MAX_DENSE_LENGTH && safeIndex >= length()) {
  94             return new SparseArrayData(this, safeIndex + 1);
  95         }
  96 
  97         super.ensure(safeIndex);
  98         deleted.resize(length());
  99 
 100         return this;
 101     }
 102 
 103     @Override
 104     public ArrayData shrink(final long newLength) {
 105         super.shrink(newLength);
 106         deleted.resize(length());
 107         return this;
 108     }
 109 
 110     @Override
 111     public ArrayData set(final int index, final Object value, final boolean strict) {
 112         deleted.clear(ArrayIndex.toLongIndex(index));
 113         return super.set(index, value, strict);
 114     }
 115 
 116     @Override
 117     public ArrayData set(final int index, final int value, final boolean strict) {
 118         deleted.clear(ArrayIndex.toLongIndex(index));
 119         return super.set(index, value, strict);
 120     }
 121 
 122     @Override
 123     public ArrayData set(final int index, final double value, final boolean strict) {
 124         deleted.clear(ArrayIndex.toLongIndex(index));
 125         return super.set(index, value, strict);
 126     }
 127 
 128     @Override
 129     public boolean has(final int index) {
 130         return super.has(index) && deleted.isClear(ArrayIndex.toLongIndex(index));
 131     }
 132 
 133     @Override
 134     public ArrayData delete(final int index) {
 135         final long longIndex = ArrayIndex.toLongIndex(index);
 136         assert longIndex >= 0 && longIndex < length();
 137         deleted.set(longIndex);
 138         underlying.setEmpty(index);
 139         return this;
 140     }
 141 
 142     @Override
 143     public ArrayData delete(final long fromIndex, final long toIndex) {
 144         assert fromIndex >= 0 && fromIndex <= toIndex && toIndex < length();
 145         deleted.setRange(fromIndex, toIndex + 1);
 146         underlying.setEmpty(fromIndex, toIndex);
 147         return this;
 148     }
 149 
 150     @Override
 151     public Object pop() {
 152         final long index = length() - 1;
 153 
 154         if (super.has((int)index)) {
 155             final boolean isDeleted = deleted.isSet(index);
 156             final Object value = super.pop();
 157 
 158             return isDeleted ? UNDEFINED : value;
 159         }
 160 
 161         return super.pop();
 162     }
 163 
 164     @Override
 165     public ArrayData slice(final long from, final long to) {
 166         final ArrayData newArray = underlying.slice(from, to);
 167         final DeletedArrayFilter newFilter = new DeletedArrayFilter(newArray);
 168         newFilter.getDeleted().copy(deleted);
 169         newFilter.getDeleted().shiftLeft(from, newFilter.length());
 170 
 171         return newFilter;
 172     }
 173 
 174     private BitVector getDeleted() {
 175         return deleted;
 176     }
 177 }