001/* 002 * Copyright (c) 2014, 2014, Oracle and/or its affiliates. All rights reserved. 003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 004 * 005 * This code is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU General Public License version 2 only, as 007 * published by the Free Software Foundation. 008 * 009 * This code is distributed in the hope that it will be useful, but WITHOUT 010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 011 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 012 * version 2 for more details (a copy is included in the LICENSE file that 013 * accompanied this code). 014 * 015 * You should have received a copy of the GNU General Public License version 016 * 2 along with this work; if not, write to the Free Software Foundation, 017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 018 * 019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 020 * or visit www.oracle.com if you need additional information or have any 021 * questions. 022 */ 023package com.oracle.graal.lir.constopt; 024 025import java.util.*; 026import java.util.function.*; 027 028import com.oracle.graal.lir.*; 029 030/** 031 * Maps variables to a generic type. 032 * 033 * TODO (je) evaluate data structure 034 */ 035class VariableMap<T> { 036 037 private final ArrayList<T> content; 038 039 public VariableMap() { 040 content = new ArrayList<>(); 041 } 042 043 public T get(Variable key) { 044 if (key == null || key.index >= content.size()) { 045 return null; 046 } 047 return content.get(key.index); 048 } 049 050 public T put(Variable key, T value) { 051 assert key != null : "Key cannot be null"; 052 assert value != null : "Value cannot be null"; 053 while (key.index >= content.size()) { 054 content.add(null); 055 } 056 return content.set(key.index, value); 057 } 058 059 public T remove(Variable key) { 060 assert key != null : "Key cannot be null"; 061 if (key.index >= content.size()) { 062 return null; 063 } 064 return content.set(key.index, null); 065 } 066 067 public void forEach(Consumer<T> action) { 068 for (T e : content) { 069 if (e != null) { 070 action.accept(e); 071 } 072 } 073 } 074 075 /** 076 * Keeps only keys which match the given predicate. 077 */ 078 public void filter(Predicate<T> predicate) { 079 for (int i = 0; i < content.size(); i++) { 080 T e = content.get(i); 081 if (e != null && !predicate.test(e)) { 082 content.set(i, null); 083 } 084 } 085 } 086 087}