| /*=============================================================================# |
| # Copyright (c) 2016, 2018 Stephan Wahlbrink and others. |
| # |
| # This program and the accompanying materials are made available under the |
| # terms of the Eclipse Public License 2.0 which is available at |
| # https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| # which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| # |
| # SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| # |
| # Contributors: |
| # Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation |
| #=============================================================================*/ |
| |
| package org.eclipse.statet.internal.r.debug.core.model; |
| |
| import java.util.function.Consumer; |
| |
| import org.eclipse.statet.jcommons.lang.NonNullByDefault; |
| import org.eclipse.statet.jcommons.lang.Nullable; |
| |
| |
| @NonNullByDefault |
| public class RElementVariableStore { |
| |
| |
| private static final int SEGMENT_LENGTH= 1000; |
| |
| |
| private final long length; |
| private final @Nullable RElementVariable[] @Nullable [] arrays; |
| |
| |
| public RElementVariableStore(final long length) { |
| this.length= length; |
| |
| this.arrays= new @Nullable RElementVariable[ |
| (this.length > 0) ? |
| (int) ((this.length - 1) / SEGMENT_LENGTH) + 1 : |
| 0 ] @Nullable []; |
| } |
| |
| |
| public void set(final long idx, final RElementVariable value) { |
| final int idx0= (int) (idx / SEGMENT_LENGTH); |
| @Nullable RElementVariable[] segment= this.arrays[idx0]; |
| if (segment == null) { |
| segment= this.arrays[idx0]= new @Nullable RElementVariable[ |
| (idx0 == this.arrays.length - 1) ? |
| (int) ((this.length - 1) % SEGMENT_LENGTH) + 1 : |
| SEGMENT_LENGTH]; |
| } |
| final int idx1= (int) (idx % SEGMENT_LENGTH); |
| segment[idx1]= value; |
| } |
| |
| public @Nullable RElementVariable get(final long idx) { |
| final int idx0= (int) (idx / SEGMENT_LENGTH); |
| final @Nullable RElementVariable[] segment= this.arrays[idx0]; |
| return (segment != null) ? segment[(int) (idx % SEGMENT_LENGTH)] : null; |
| } |
| |
| public @Nullable RElementVariable clear(final long idx) { |
| final int idx0= (int) (idx / SEGMENT_LENGTH); |
| final @Nullable RElementVariable[] segment= this.arrays[idx0]; |
| if (segment == null) { |
| return null; |
| } |
| final int idx1= (int) (idx % SEGMENT_LENGTH); |
| final RElementVariable value= segment[idx1]; |
| segment[idx1]= null; |
| return value; |
| } |
| |
| // public RElementVariable[] get(final long firstIdx, final int length) { |
| // final RElementVariable[] array= new RElementVariable[length]; |
| // int idx0= (int) (firstIdx / SEGMENT_LENGTH); |
| // int idx1= (int) (firstIdx % SEGMENT_LENGTH); |
| // for (int i= 0; i < length;) { |
| // final RElementVariable[] segment= this.arrays[idx0]; |
| // final int l= Math.min(length - i, SEGMENT_LENGTH - idx1); |
| // System.arraycopy(segment, idx1, array, i, l); |
| // idx0++; |
| // idx1= 0; |
| // i+= l; |
| // } |
| // return array; |
| // } |
| |
| public void forEachSet(final Consumer<RElementVariable> action) { |
| for (int idx0= 0; idx0 < this.arrays.length; idx0++) { |
| final @Nullable RElementVariable[] segment= this.arrays[idx0]; |
| if (segment != null) { |
| for (int idx1= 0; idx1 < segment.length; idx1++) { |
| final RElementVariable value= segment[idx1]; |
| if (value != null) { |
| action.accept(value); |
| } |
| } |
| } |
| } |
| } |
| |
| } |