org.apache.sysml.runtime.matrix.mapred.CachedMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of systemml Show documentation
Show all versions of systemml Show documentation
Declarative Machine Learning
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.sysml.runtime.matrix.mapred;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
public class CachedMap
{
protected HashMap> map=new HashMap>();
protected ArrayList cache=new ArrayList();
protected int numValid=0;
protected ArrayList returnListCache=new ArrayList(4);
public CachedMap()
{}
@SuppressWarnings("unchecked")
public T add(Byte index, T value)
{
if(numValid list=map.get(index);
if(list==null)
{
list=new ArrayList(4);
map.put(index, list);
}
list.add(numValid);
numValid++;
return cache.get(numValid-1);
}
public void reset()
{
numValid=0;
map.clear();
}
public void remove(byte index)
{
ArrayList list=map.remove(index);
if(list==null)
return;
for(Integer cacheIndex: list)
{
if(cacheIndex==numValid-1)
{
numValid--;
return;
}
//swap the last element and the element to remove
T lastElem=cache.get(numValid-1);
cache.set(numValid-1, cache.get(cacheIndex));
cache.set(cacheIndex, lastElem);
//remap the indexes
for(ArrayList lst: map.values())
for(int i=0; i get(byte index)
{
ArrayList list=map.get(index);
if(list==null)
return null;
returnListCache.clear();
for(Integer i: list)
returnListCache.add(cache.get(i));
return returnListCache;
}
public T getFirst(byte index)
{
ArrayList list=map.get(index);
if(list!=null && !list.isEmpty())
return cache.get(list.get(0));
else
return null;
}
public Set getIndexesOfAll()
{
return map.keySet();
}
/* public String toString()
{
String str="";
for(Entry e: map.entrySet())
str+=e.getKey()+" <--> "+cache.get(e.getValue())+"\n";
return str;
}*/
public String toString()
{
String str="numValid: "+numValid+"\n"+map.toString()+"\n"+cache.toString();
return str;
}
}