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 long value, final boolean strict) {
 124         deleted.clear(ArrayIndex.toLongIndex(index));
 125         return super.set(index, value, strict);
 126     }
 127 
 128     @Override
 129     public ArrayData set(final int index, final double value, final boolean strict) {
 130         deleted.clear(ArrayIndex.toLongIndex(index));
 131         return super.set(index, value, strict);
 132     }
 133 
 134     @Override
 135     public boolean has(final int index) {
 136         return super.has(index) && deleted.isClear(ArrayIndex.toLongIndex(index));
 137     }
 138 
 139     @Override
 140     public ArrayData delete(final int index) {
 141         final long longIndex = ArrayIndex.toLongIndex(index);
 142         assert longIndex >= 0 && longIndex < length();
 143         deleted.set(longIndex);
 144         underlying.setEmpty(index);
 145         return this;
 146     }
 147 
 148     @Override
 149     public ArrayData delete(final long fromIndex, final long toIndex) {
 150         assert fromIndex >= 0 && fromIndex <= toIndex && toIndex < length();
 151         deleted.setRange(fromIndex, toIndex + 1);
 152         underlying.setEmpty(fromIndex, toIndex);
 153         return this;
 154     }
 155 
 156     @Override
 157     public Object pop() {
 158         final long index = length() - 1;
 159 
 160         if (super.has((int)index)) {
 161             final boolean isDeleted = deleted.isSet(index);
 162             final Object value = super.pop();
 163 
 164             return isDeleted ? UNDEFINED : value;
 165         }
 166 
 167         return super.pop();
 168     }
 169 
 170     @Override
 171     public ArrayData slice(final long from, final long to) {
 172         final ArrayData newArray = underlying.slice(from, to);
 173         final DeletedArrayFilter newFilter = new DeletedArrayFilter(newArray);
 174         newFilter.getDeleted().copy(deleted);
 175         newFilter.getDeleted().shiftLeft(from, newFilter.length());
 176 
 177         return newFilter;
 178     }
 179 
 180     private BitVector getDeleted() {
 181         return deleted;
 182     }
 183 }