001package org.clafer.ir.analysis.deduction; 002 003import java.util.ArrayList; 004import java.util.Arrays; 005import java.util.Collections; 006import java.util.HashMap; 007import java.util.List; 008import java.util.Map; 009import java.util.Map.Entry; 010import java.util.function.BiConsumer; 011import java.util.stream.Collectors; 012import org.clafer.ir.IrBoolVar; 013import org.clafer.ir.IrIntVar; 014import org.clafer.ir.IrRewriter; 015import org.clafer.ir.IrSetVar; 016import org.clafer.ir.IrStringVar; 017import static org.clafer.ir.Irs.string; 018 019/** 020 * 021 * @author jimmy 022 */ 023public class Coalesce extends IrRewriter<Void> { 024 025 private final Map<IrIntVar, IrIntVar> coalescedInts; 026 private final Map<IrSetVar, IrSetVar> coalescedSets; 027 private final Map<List<IrIntVar>, IrStringVar> stringVarCache = new HashMap<>(); 028 029 public Coalesce() { 030 this.coalescedInts = Collections.emptyMap(); 031 this.coalescedSets = Collections.emptyMap(); 032 } 033 034 public Coalesce( 035 Map<IrIntVar, IrIntVar> coalescedInts, 036 Map<IrSetVar, IrSetVar> coalescedSets) { 037 this.coalescedInts = coalescedInts; 038 this.coalescedSets = coalescedSets; 039 } 040 041 public IrBoolVar get(IrBoolVar var) { 042 return (IrBoolVar) coalescedInts.getOrDefault(var, var); 043 } 044 045 public IrIntVar get(IrIntVar var) { 046 return coalescedInts.getOrDefault(var, var); 047 } 048 049 public IrSetVar get(IrSetVar var) { 050 return coalescedSets.getOrDefault(var, var); 051 } 052 053 public void forEachIntVar(BiConsumer<? super IrIntVar, ? super IrIntVar> action) { 054 coalescedInts.forEach(action); 055 } 056 057 public void forEachSetVar(BiConsumer<? super IrSetVar, ? super IrSetVar> action) { 058 coalescedSets.forEach(action); 059 } 060 061 public boolean isEmpty() { 062 return coalescedInts.isEmpty() && coalescedSets.isEmpty(); 063 } 064 065 public Coalesce compose(Coalesce other) { 066 return new Coalesce( 067 compose(coalescedInts, other.coalescedInts), 068 compose(coalescedSets, other.coalescedSets)); 069 } 070 071 private static <T> Map<T, T> compose(Map<T, T> f1, Map<T, T> f2) { 072 if (f1.isEmpty()) { 073 return f2; 074 } 075 if (f2.isEmpty()) { 076 return f1; 077 } 078 Map<T, T> composed = new HashMap<>(f1.size() + f2.size()); 079 composed.putAll(f2); 080 for (Map.Entry<T, T> e : f1.entrySet()) { 081 T key = e.getKey(); 082 T value = f2.get(e.getValue()); 083 if (value == null) { 084 value = e.getValue(); 085 } 086 composed.put(key, value); 087 } 088 return composed; 089 } 090 091 @Override 092 public IrBoolVar visit(IrBoolVar ir, Void a) { 093 IrBoolVar var = (IrBoolVar) coalescedInts.get(ir); 094 return var == null ? ir : var; 095 } 096 097 @Override 098 public IrIntVar visit(IrIntVar ir, Void a) { 099 IrIntVar var = coalescedInts.get(ir); 100 return var == null ? ir : var; 101 } 102 103 @Override 104 public IrSetVar visit(IrSetVar ir, Void a) { 105 IrSetVar var = coalescedSets.get(ir); 106 return var == null ? ir : var; 107 } 108 109 @Override 110 public IrStringVar visit(IrStringVar ir, Void a) { 111 boolean changed = false; 112 IrIntVar[] chars = new IrIntVar[ir.getCharVars().length]; 113 for (int i = 0; i < chars.length; i++) { 114 chars[i] = coalescedInts.get(ir.getCharVars()[i]); 115 if (chars[i] == null) { 116 chars[i] = ir.getCharVars()[i]; 117 } else { 118 changed = true; 119 } 120 } 121 IrIntVar length = coalescedInts.get(ir.getLengthVar()); 122 changed |= length != null; 123 if (changed) { 124 length = length == null ? ir.getLengthVar() : length; 125 List<IrIntVar> key = new ArrayList<>(); 126 key.addAll(Arrays.asList(chars)); 127 key.add(length); 128 IrStringVar string = stringVarCache.get(key); 129 if (string == null) { 130 string = string(ir.getName(), chars, length); 131 stringVarCache.put(key, string); 132 } 133 return string; 134 } 135 return ir; 136 } 137 138 @Override 139 public String toString() { 140 StringBuilder builder = new StringBuilder(); 141 coalescedInts.entrySet().stream() 142 .collect(Collectors.groupingBy(Entry::getValue, Collectors.mapping(Entry::getKey, Collectors.toList()))) 143 .forEach((newVar, oldVars) -> builder.append(newVar).append(" <--- ").append(oldVars).append('\n')); 144 coalescedSets.entrySet().stream() 145 .collect(Collectors.groupingBy(Entry::getValue, Collectors.mapping(Entry::getKey, Collectors.toList()))) 146 .forEach((newVar, oldVars) -> builder.append(newVar).append(" <--- ").append(oldVars).append('\n')); 147 return builder.toString(); 148 } 149}