Пример #1
0
 @Override
 public boolean contains(Agent agent) {
   for (AgentSet.Iterator iter = iterator(); iter.hasNext(); ) {
     if (iter.next() == agent) {
       return true;
     }
   }
   return false;
 }
Пример #2
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);
 }
Пример #3
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;
 }
Пример #4
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();
 }
Пример #5
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;
   }
 }
Пример #6
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();
    }
  }