示例#1
0
  /*
   * (non-Javadoc)
   *
   * @see forge.card.cost.CostPart#toString()
   */
  @Override
  public final String toString() {
    final StringBuilder sb = new StringBuilder();
    sb.append("Return ");

    final Integer i = this.convertAmount();
    String pronoun = "its";

    if (this.payCostFromSource()) {
      sb.append(this.getType());
    } else {
      final String desc =
          this.getTypeDescription() == null ? this.getType() : this.getTypeDescription();
      if (i != null) {
        sb.append(Cost.convertIntAndTypeToWords(i, desc));
        if (i > 1) {
          pronoun = "their";
        }
      } else {
        sb.append(Cost.convertAmountTypeToWords(this.getAmount(), desc));
      }

      sb.append(" you control");
    }
    sb.append(" to ").append(pronoun).append(" owner's hand");
    return sb.toString();
  }
 @Override
 protected void saveEditor(Inventory inventory, Player player) {
   for (int i = 0; i < 8; i++) {
     ItemStack item = inventory.getItem(i);
     if (item != null && item.getType() != Material.AIR) {
       ItemStack cost1 = null, cost2 = null;
       ItemStack item1 = inventory.getItem(i + 9);
       ItemStack item2 = inventory.getItem(i + 18);
       if (item1 != null && item1.getType() != Material.AIR) {
         cost1 = item1;
         if (item2 != null && item2.getType() != Material.AIR) {
           cost2 = item2;
         }
       } else if (item2 != null && item2.getType() != Material.AIR) {
         cost1 = item2;
       }
       if (cost1 != null) {
         Cost cost = new Cost();
         cost.amount = item.getAmount();
         cost.item1 = cost1;
         cost.item2 = cost2;
         ItemStack saleItem = item.clone();
         saleItem.setAmount(1);
         ((TradingPlayerShopkeeper) shopkeeper).costs.put(saleItem, cost);
       } else {
         ItemStack saleItem = item.clone();
         saleItem.setAmount(1);
         ((TradingPlayerShopkeeper) shopkeeper).costs.remove(saleItem);
       }
     }
   }
   ((TradingPlayerShopkeeper) shopkeeper).clickedItem = null;
 }
示例#3
0
  private boolean sortByCost(Cost[] loopCostList, int[] loopIndex) {
    int maxlooptemp = loopCostList.length - 1;
    boolean exchanged = true;
    boolean permuted = false;

    while (exchanged) { // Bubble sort the LoopCosts.
      exchanged = false;

      for (int i = 0; i < maxlooptemp; i++) {
        int j = i + 1;
        Cost lci = loopCostList[i];
        Cost lci1 = loopCostList[j];
        if (lci1 == null) continue;

        if (lci.lessThan(lci1)) {
          int temp = loopIndex[i];
          loopIndex[i] = loopIndex[j];
          loopIndex[j] = temp;

          loopCostList[i] = lci1;
          loopCostList[j] = lci;
          exchanged = true;
        }
      }

      permuted |= exchanged;

      maxlooptemp--;
    }

    return permuted;
  }
  public AggregateImpl chooseAggregate(double maxCost, double minCostBenefitRatio, Cost cost) {

    AggregateImpl bestAggregate = null;
    // for all aggregates
    long max = 1 << schema.getAttributes().size();
    double bestBenefit = 0;
    int bestBenefitCount = 0;
    int[] benefitCount0 = {0};
    for (long i = 0; i < max; ++i) {
      BitSetPlus bits = toBits(i);
      AggregateImpl aggregate = getAggregate(bits);
      if (aggregate.materialized) {
        continue;
      }
      double benefit = getBenefit(aggregate, benefitCount0);
      if (benefit > bestBenefit) {
        bestBenefit = benefit;
        bestAggregate = aggregate;
        bestBenefitCount = benefitCount0[0];
      }
    }
    cost.benefit = bestBenefit;
    cost.benefitCount = bestBenefitCount;
    return bestAggregate;
  }
示例#5
0
 /**
  * Check if category exist in output data if no add new element if yes adds next amount to
  * category
  *
  * @param c Coast to add
  * @return BigDecimal
  */
 public BigDecimal calculateValue(Cost c) {
   BigDecimal value = null;
   String key = null;
   key = c.getCategory();
   if (outputData.containsKey(key)) {
     value = outputData.get(key).add(c.getValue());
   } else {
     value = c.getValue();
   }
   return value;
 }
示例#6
0
  public Cost computeRefCost(LoopHeaderChord loop, SubscriptExpr se) {
    Cost tripL = loop.getTripCount();
    InductionVar ivar = loop.getPrimaryInductionVar();
    if (ivar == null) return tripL;

    // Compute
    // tripL = (ubL - lbL + stepL) / stepL

    int nsubs = se.numSubscripts(); // number of subscripts in the subscript
    LoopHeaderChord tloop = loop.getTopLoop();
    VariableDecl iv = ivar.getVar();

    for (int i = 0; i < nsubs - 1; i++) { // for c code
      // If the loop index variable of this loop appears in any of the
      // subscripts other then the last, then return tripL.
      Expr sI = se.getSubscript(i);
      AffineExpr ae = tloop.isAffine(sI);

      if (ae == null) return null;

      if (ae.hasTerm(iv)) return tripL;
    }

    int fs = nsubs - 1;
    Expr s0 = se.getSubscript(fs);
    AffineExpr ae = tloop.isAffine(s0);
    if (ae == null) return null;

    int at = ae.getTermIndexOrig(iv);
    long coeff = 0;
    if (at >= 0) coeff = ae.getCoefficient(at);

    if (coeff == 0) // Invariant Reuse.
    return new Cost(1.0, 0);

    long stepL = loop.getStepValue();
    long stride = stepL * coeff;
    if (stride < 0) stride = -stride;

    // Unit Reuse.

    Type et = se.getCoreType().getPointedTo();
    int bs = et.memorySizeAsInt(Machine.currentMachine);
    int cs = Machine.currentMachine.getCacheSize(bs);
    if (stride <= cs) { // cache line or block size
      tripL.multiply(stride);
      tripL.divide(cs);
      return tripL;
    }

    // No Reuse.

    return tripL;
  }
示例#7
0
 /**
  * Group data by category and sum cost
  *
  * @return Grouped categories LinkedHashMap
  */
 public LinkedHashMap<String, BigDecimal> sumByCategory() {
   ListIterator<Cost> listIterator = inputData.listIterator();
   BigDecimal value = null;
   String key = null;
   while (listIterator.hasNext()) {
     Cost c = listIterator.next();
     key = c.getCategory();
     value = calculateValue(c);
     outputData.put(key, value);
   }
   return outputData;
 }
示例#8
0
  private Cost computeRefCostSum(LoopHeaderChord loop, Vector<RefGroup> refGroups) {
    Cost lc = new Cost();
    int l = refGroups.size();
    for (int i = 0; i < l; i++) {
      RefGroup rg = refGroups.elementAt(i);
      SubscriptExpr se = rg.getRepresentative();
      Cost cost = computeRefCost(loop, se);

      if (cost == null) continue;

      lc.add(cost);
    }
    return lc;
  }
 /*
  * (non-Javadoc)
  *
  * @see forge.card.cost.CostPart#toString()
  */
 @Override
 public final String toString() {
   final StringBuilder sb = new StringBuilder();
   final String desc =
       this.getTypeDescription() == null ? this.getType() : this.getTypeDescription();
   sb.append("Gain control of ").append(Cost.convertAmountTypeToWords(this.getAmount(), desc));
   return sb.toString();
 }
  /*
   * (non-Javadoc)
   *
   * @see forge.card.cost.CostPart#toString()
   */
  @Override
  public final String toString() {
    final StringBuilder sb = new StringBuilder();

    sb.append("Remove ");
    sb.append(Cost.convertIntAndTypeToWords(this.convertAmount(), "counter"));
    final String desc =
        this.getTypeDescription() == null ? this.getType() : this.getTypeDescription();
    sb.append(" from " + desc);

    return sb.toString();
  }
 @Override
 protected void load(ConfigurationSection config) {
   super.load(config);
   costs = new HashMap<ItemStack, Cost>();
   ConfigurationSection costsSection = config.getConfigurationSection("costs");
   if (costsSection != null) {
     for (String key : costsSection.getKeys(false)) {
       ConfigurationSection itemSection = costsSection.getConfigurationSection(key);
       ItemStack item = itemSection.getItemStack("item");
       if (itemSection.contains("attributes")) {
         String attr = itemSection.getString("attributes");
         if (attr != null && !attr.isEmpty()) {
           item = NMSManager.getProvider().loadItemAttributesFromString(item, attr);
         }
       }
       Cost cost = new Cost();
       cost.amount = itemSection.getInt("amount");
       cost.item1 = itemSection.getItemStack("item1");
       cost.item2 = itemSection.getItemStack("item2");
       costs.put(item, cost);
     }
   }
 }
示例#12
0
 public static void main(String[] args) throws Exception {
   Cost cost = new Cost();
   Thread.sleep(200);
   cost.updateEnd();
   System.out.println(cost);
 }
示例#13
0
  private void tryPermute(LoopHeaderChord topLoop) {
    Vector<LoopHeaderChord> loopNest = topLoop.getTightlyNestedLoops();
    if (loopNest == null) return;

    int loopDepth = loopNest.size();
    LoopHeaderChord bottom = loopNest.get(loopDepth - 1);
    if (!unsafe && !legalLoop(bottom)) return;

    // Set the loop costs for the loops in a loop nest. The cost for a loop is
    // the cost of executing the nest with that loop in the innermost nesting.

    Table<Declaration, SubscriptExpr> arrayRefs = new Table<Declaration, SubscriptExpr>();
    if (!topLoop.getSubscriptsRecursive(arrayRefs)) return;

    graph = topLoop.getDDGraph(false);
    if (graph == null) return;

    if (trace) System.out.println("     " + graph);

    int[] loopIndex = new int[loopDepth];
    Cost[] loopCostList = new Cost[loopDepth];
    Vector<RefGroup> refGroups = new Vector<RefGroup>(20);

    for (int i = 0; i < loopDepth; i++) {
      LoopHeaderChord loop = loopNest.elementAt(i);
      if (trace) System.out.println("   " + i + " " + loop);

      computeRefGroups(loop.getNestedLevel(), 2, 2, arrayRefs, refGroups);

      Cost lc = computeRefCostSum(loop, refGroups);
      if (trace) System.out.println("   " + i + " " + lc);

      loopCostList[i] = lc;
      loopIndex[i] = i; // the outtermost loop is at position 0

      Cost tp = tripProduct(loopNest, loop);
      if (trace) System.out.println("   " + i + " " + tp);
      lc.multiply(tp);
      if (trace) System.out.println("   " + i + " " + lc);
    }

    boolean permuted = sortByCost(loopCostList, loopIndex);

    if (!permuted) return;
    if (trace) {
      System.out.print("   permute " + loopDepth);
      System.out.print(":");
      for (int i = 0; i < loopIndex.length; i++) System.out.print(" " + loopIndex[i]);
      System.out.println("");
    }

    int[][] ddVec = getDDVec(arrayRefs, loopDepth);
    if (trace) printDDInfo(ddVec, loopDepth);

    if (!isLegal(loopIndex, ddVec)) return;

    if (trace) System.out.println("   permute " + loopDepth);

    int[] rank = new int[loopDepth];

    // We will do sorting on the rank vector, which corresponds to the interchange we need.

    for (int i = 0; i < loopDepth; i++) {
      int loopNum = loopIndex[i];
      rank[loopNum] = i;
    }

    if (trace) printOrder(rank);

    boolean changed = true;

    while (changed) {
      changed = false;

      for (int i = 0; i < loopDepth - 1; i++) {
        int j = i + 1;
        int outerRank = rank[i];
        int innerRank = rank[j];

        if (innerRank >= outerRank) continue;

        LoopHeaderChord innerLoop = loopNest.elementAt(j);
        LoopHeaderChord outerLoop = loopNest.elementAt(i);

        if (!outerLoop.isDDComplete() || outerLoop.inhibitLoopPermute()) continue;

        if (!innerLoop.isDDComplete() || innerLoop.inhibitLoopPermute()) continue;

        changed = true;

        rank[i] = innerRank;
        rank[j] = outerRank;

        loopNest.setElementAt(innerLoop, i);
        loopNest.setElementAt(outerLoop, j);

        performLoopInterchange(innerLoop, outerLoop);
      }
    }
  }