Esempio n. 1
0
shared_ptr<const ComponentLists> ComponentListsCache::get(int start,
                                                          int indexCount) {
  if (indexCount == 1) {
    shared_ptr<ComponentLists> result(new ComponentLists);
    findComponents(start, result->pointIndices, result->componentIndices,
                   result->arrayIndices);
    return result;
  }

  std::pair<int, int> key(start, indexCount);
  ComponentListsMap::const_iterator it = m_map.find(key);
  if (it != m_map.end()) {
    return make_shared_from_ref(*it->second);
  }

  // The relevant local DOF list doesn't exist yet and must be created.
  ComponentLists *newLists = new ComponentLists;
  findComponents(start, indexCount, newLists->pointIndices,
                 newLists->componentIndices, newLists->arrayIndices);

  // Attempt to insert the newly created DOF list into the map
  std::pair<ComponentListsMap::iterator, bool> result =
      m_map.insert(std::make_pair(key, newLists));
  if (result.second)
    // Insertion succeeded. The newly created DOF list will be deleted in
    // our own destructor
    ;
  else
    // Insertion failed -- another thread was faster. Delete the newly
    // created DOF list.
    delete newLists;

  // Return pointer to the DOF list that ended up in the map.
  return make_shared_from_ref(*result.first->second);
}
Esempio n. 2
0
int independentUpTo(LifeList *cells, LifeList *cumulative, 
                     LifeList *working, int ngens) {

int nClusters;

   copyLifeList(cells, working);
   cumulativeImage(working, cumulative, ngens);

   nClusters = findComponents(cumulative, working, 2);
  
   getLifeListValues(cells, working); 

   return nClusters;
}
Esempio n. 3
0
int main(int argc, char *argv[]) {
  // Usage message
  if (argc < 3) {
    usage_msg();
    return EXIT_FAILURE;
  }

  int mode; // mode of output
  char *a, *b;

  if (strcmp("components",argv[2]) == 0) {
    mode = MODE_COMPONENTS;
  } else if (strcmp("degrees",argv[2]) == 0) {
    mode = MODE_DEGREES;
  } else if (strcmp("path",argv[2]) == 0) {
    mode = MODE_PATH;
  } else if (strcmp("stats",argv[2]) == 0) {
     mode = MODE_STATS;
  } else if (strcmp("diameter",argv[2]) == 0) {
     mode = MODE_DIAMETER;
  } else {
    usage_msg();
  }

  if (mode & MODE_PATH) {
    if (argc < 5 || strlen(argv[3]) < 3 || strlen(argv[4]) < 3) {
      fprintf(stderr, "Please specify two 3-letter words\nExample: 3words path bus car\n");
      return EXIT_FAILURE;
    }
    a = argv[3];
    b = argv[4];
    if (a[0]<'a'||a[0]>'z'||a[1]<'a'||a[1]>'z'||a[2]<'a'||a[2]>'z'||b[0]<'a'||b[0]>'z'||b[1]<'a'||b[1]>'z'||b[2]<'a'||b[2]>'z') {
      fprintf(stderr, "Please only use lower case 3-letter words\nExample: 3words path bus car\n");
      return EXIT_FAILURE;
    }
  }

  // Open file
  FILE *f_input = fopen(argv[1], "r");
  if (!f_input) {
    fprintf(stderr, "File could not be opened: %s\n", argv[1]);
    return EXIT_FAILURE;
  }

  NODE nodes[LEN3];
  init(nodes);

  // Read lines
  INT wordCount = readWords(nodes, f_input);
  fclose(f_input);

  // Init navigation structure on the nodes
  initNav(nodes, 0);
  initNav(nodes, 1);
  initNav(nodes, 2);

  INT componentCount = findComponents(nodes, mode & MODE_COMPONENTLIST);

  if (mode & MODE_COMPONENTSTAT) printf("%d components\n", componentCount - 1);

  calcDegrees(nodes, mode & MODE_DEGREESTAT, mode & MODE_DEGREELIST);

  if (mode & MODE_PATH) shortestPath(nodes, a[0]-'a',a[1]-'a',a[2]-'a',b[0]-'a',b[1]-'a',b[2]-'a', TRUE);

  if (mode & MODE_DIAMETER) diameter(fopen(argv[1], "r"), wordCount, nodes);
}