Example #1
0
  // This is used to optimize the special case of randomSubset where
  // size == 2
  @Override
  Agent[] randomTwo(int precomputedCount, int random1, int random2) {
    Agent[] result = new Agent[2];

    // we know precomputedCount, or this method would not have been called.
    // see randomSubset().
    if (random2 >= random1) {
      // if random2 >= random1, we need to increment random2 to choose a
      // later agent.
      random2++;
    } else {
      // if random2 < random1, we swap them so our indices are in order.
      int tmp = random1;
      random1 = random2;
      random2 = tmp;
    }
    if ((size == capacity) && !((type == Turtle.class || type == Link.class) && !removableAgents)) {

      result[0] = agents[random1];
      result[1] = agents[random2];
    } else {
      AgentSet.Iterator iter = iterator();
      int i = 0;
      while (i++ < random1) {
        iter.next(); // skip to the first place
      }
      result[0] = iter.next();
      while (i++ < random2) {
        iter.next(); // skip to the next place
      }
      result[1] = iter.next();
    }
    return result;
  }
Example #2
0
 @Override
 public boolean contains(Agent agent) {
   for (AgentSet.Iterator iter = iterator(); iter.hasNext(); ) {
     if (iter.next() == agent) {
       return true;
     }
   }
   return false;
 }
Example #3
0
 @Override
 public LogoList toLogoList() {
   ArrayList<Agent> result = new ArrayList<Agent>();
   for (AgentSet.Iterator iter = iterator(); iter.hasNext(); ) {
     Agent agent = iter.next();
     result.add(agent);
   }
   Collections.sort(result);
   return LogoList.fromJava(result);
 }
Example #4
0
 // This assumes we've already checked that the counts
 // are equal. - ST 7/6/06
 @Override
 boolean equalAgentSetsHelper(org.nlogo.api.AgentSet otherSet) {
   HashSet<Agent> set = new HashSet<Agent>();
   for (AgentSet.Iterator iter = iterator(); iter.hasNext(); ) {
     set.add(iter.next());
   }
   for (org.nlogo.api.Agent a : otherSet.agents()) {
     if (!set.contains(a)) {
       return false;
     }
   }
   return true;
 }
Example #5
0
 @Override
 public String toString() {
   StringBuilder s = new StringBuilder("AgentSet");
   s = s.append("\n...... type: ");
   s = s.append(type == null ? "null" : type.toString());
   s = s.append("\n...... size: " + size);
   s = s.append("\n...... count(): " + count());
   s = s.append("\n...... capacity: " + capacity);
   s = s.append("\n...... agents: ");
   for (AgentSet.Iterator iter = iterator(); iter.hasNext(); ) {
     s = s.append("\n" + iter.next().toString());
   }
   return s.toString();
 }
Example #6
0
  @Override
  public Agent randomOne(int precomputedCount, int random) {
    // note: we can assume agentset is nonempty , since _randomoneof.java checks for that
    if ((size == capacity) && !((type == Turtle.class || type == Link.class) && !removableAgents)) {
      return agents[random];
    } else {
      AgentSet.Iterator iter = iterator();
      for (int i = 0; i < random; i++) {
        iter.next(); // skip to the right place
      }

      return iter.next();
    }
  }
Example #7
0
 @Override
 public int count() {
   if ((type == Turtle.class || type == Link.class) && !removableAgents) {
     // some of the turtles might be dead, so we need
     // to actually count them - ST 2/27/03
     int result = 0;
     for (AgentSet.Iterator iter = iterator(); iter.hasNext(); ) {
       iter.next();
       result++;
     }
     return result;
   } else {
     return size;
   }
 }
Example #8
0
  public int calcHashCode() {
    // IEEE 754 math involves two different zeros, positive zero and negative
    // zero. They are supposed to be indistinguishable from NetLogo code.
    // but Sun, in their infinite wisdom, made them have different hash codes
    // when stored in java.lang.Double objects. - ST 12/5/09
    if (sourceObject instanceof Double) {
      return ((Double) sourceObject).doubleValue() == 0.0 // true for both pos and neg zero
          ? ZERO_CODE
          : sourceObject.hashCode();
    }
    // these next two cases are sneaky -- NetLogo considers dead turtles to be
    // equal to each other, and to nobody.  Dead turtles have an id of minus one,
    // so that's what makes the next two cases work.  - ST 10/28/03*/
    else if (sourceObject instanceof Turtle) {
      return Long.valueOf(((Turtle) sourceObject).id).hashCode();
    } else if (sourceObject == Nobody$.MODULE$) {
      return NOBODY_CODE;
    } else if (sourceObject instanceof LogoList) {
      // Hash algor for List (from which ArrayList and therefore LogoList extend)
      // http://java.sun.com/j2se/1.4.1/docs/apiList.html#hashCode()
      // Instead of recursing on the original element types,
      // we use a LogoHashObject - JMD 10/28/03*/

      int hashCodeCalc = 1;
      Iterator<Object> listItr = ((LogoList) sourceObject).iterator();
      while (listItr.hasNext()) {
        LogoHashObject lhObj = new LogoHashObject(listItr.next());
        hashCodeCalc = 31 * hashCodeCalc + (lhObj.getSourceObject() == null ? 0 : lhObj.hashCode());
      }
      return hashCodeCalc;
    } else if (sourceObject instanceof AgentSet) {
      int code = 1;
      for (AgentSet.Iterator i = ((AgentSet) sourceObject).iterator(); i.hasNext(); ) {
        Agent agent = i.next();
        LogoHashObject obj = new LogoHashObject(agent);
        code = 31 * code + (agent != null ? 0 : obj.hashCode());
      }
      return code;
    } else {
      return sourceObject.hashCode();
    }
  }
Example #9
0
 @Override
 Agent[] randomSubsetGeneral(
     int resultSize, int precomputedCount, org.nlogo.util.MersenneTwisterFast random) {
   Agent result[] = new Agent[resultSize];
   if (precomputedCount == capacity) {
     for (int i = 0, j = 0; j < resultSize; i++) {
       if (random.nextInt(precomputedCount - i) < resultSize - j) {
         result[j] = agents[i];
         j++;
       }
     }
   } else {
     AgentSet.Iterator iter = iterator();
     for (int i = 0, j = 0; j < resultSize; i++) {
       Agent next = iter.next();
       if (random.nextInt(precomputedCount - i) < resultSize - j) {
         result[j] = next;
         j++;
       }
     }
   }
   return result;
 }