/**
 * Provide a function insert_sorted_array( ) that reads and
 * stores the 10 records from file in an a sorted array of
 * structs. The array should be sorted based on the car make.
 * Each new record from file should be inserted into its correct
 * sorted location by shifting other records if necessary.
 *
 * @param Car **, an array of car pointers.
 * @param Number, the amount of car pointers in the array of cars.
 * @return Return 0 on no error
 */
int insert_sorted_array(Car ** car_array, int * arr_length)
{
    /**
    * Open the file to read car records from.
    */
    FILE * file;
    file = fopen("./CarRecords.txt", "r");
    if (file == NULL) {
        perror("Error opening the file './CarRecords.txt'");
        return -1;
    }

    // Add car records from the file until no more can be added
    // or the limit of 10 is reached.
    while(true) {
        Car * car = (Car *) malloc(sizeof(Car));
        if (fscanf(file, "%49[^,], %49[^,], %d, %49[^,\r\n]\r\n", car->car_make,
                car->car_model, &car->year, car->color) == 4 && *arr_length < 10) {
                    ordered_insert(car_array, *arr_length, car);
                    (*arr_length)++; // Increase the count of cars in the array
                } else {
                    free(car); // Free the car not used and exit the loop
                    break;
                }
    }

    return 0;
}
Example #2
0
int
main (int   argc,
          char *argv[])
{
  node *t;

  init_list ();
  ordered_insert (10);
  ordered_insert (5);
  ordered_insert (8);
  ordered_insert (3);
  ordered_insert (1);
  ordered_insert (7);
  ordered_insert (8);

  printf ("\nInitial Linked list is ");
  print_list (head->next);

  printf ("\nFinding 5 is %ssuccessful", find_node (4) == tail ? "un" : "");

  t = find_node (5);
  printf ("\nFinding 5 is %ssuccessful", t == tail ? "un" : "");

  printf ("\nInserting 9 after 5");
  insert_after (9, t);
  print_list (head->next);

  t = find_node (10);
  printf ("\nDeleting next last node");
  delete_next (t);
  print_list (head->next);

  t = find_node (3);
  printf ("\nDeleting next 3");
  delete_next (t);
  print_list (head->next);

  printf ("\nInsert node 2 before 3");
  insert_node (2, 3);
  print_list (head->next);

  printf ("\nDeleting node 2");

  if (!delete_node (2))
    printf ("\n deleting 2 is unsuccessful");
  print_list (head->next);

  printf ("\nDeleting node 1");
  delete_node (1);
  print_list (head->next);

  printf ("\nDeleting all node");
  delete_all ();
  print_list (head->next);

  return 0;
}
Example #3
0
int
main (int   argc,
      char *argv[])
{
  dnode *t;
  init_dlist ();

  ordered_insert (10);
  ordered_insert (5);
  ordered_insert (8);
  ordered_insert (3);
  ordered_insert (1);
  ordered_insert (7);
  ordered_insert (8);

  printf ("\nInitial Linked list is ");
  print_dlist (head->next);

  printf ("\nFinding 4 is %ssuccessful", find_dnode (4) == tail ? "un" : "");

  t = find_dnode (5);
  printf ("\nFinding 5 is %ssuccessful", t == tail ? "un" : "");

  printf ("\nInserting 7 before 5");
  insert_dnode_ptr (7, t);
  print_dlist (head->next);

  t = find_dnode (3);
  printf ("\nDeleting 3 ");
  delete_dnode_ptr (t);
  print_dlist (head->next);

  printf ("\nInserting node 2 before 10");
  insert_dnode (2, 10);
  print_dlist (head->next);

  printf ("\nDeleting node 2");
  if (!delete_dnode (2))
    printf ("\n deleting 2 is unsuccessful");
  print_dlist (head->next);

  printf ("\nDeleting node 1");
  delete_dnode (1);
  print_dlist (head->next);

  printf ("\nInserting 15 at first");
  insert_dnode_ptr (15, head->next);
  print_dlist (head->next);

  printf ("\nDeleting all node");
  delete_all ();

  print_dlist(head->next);

  return 0;
}
CR_Matrix::CR_Matrix(
 ParallelMachine arg_comm ,
 const std::vector<unsigned> & arg_partition ,
       std::vector<unsigned> & arg_prefix ,
       std::vector<unsigned> & arg_coli ,
       std::vector<double>   & arg_coef )
  : m_comm( arg_comm ),
    m_comm_size( parallel_machine_size( arg_comm ) ),
    m_comm_rank( parallel_machine_rank( arg_comm ) ),
    m_sparse( false ),
    m_work_disp(),
    m_send_disp(),
    m_send_map(),
    m_row_size( 0 ),
    m_prefix(),
    m_coli(),
    m_coef()
{
  static const char method[] = "phdmesh::CR_Matrix::CR_Matrix" ;

  if ( arg_prefix.empty() ) { return ; }

  //------------------------------------

  if ( arg_coli.size() != arg_prefix.back() ||
       arg_coef.size() != arg_prefix.back() ) {
    std::ostringstream msg ;
    msg << method << " ERROR" ;
    msg << " arg_coli.size() = " << arg_coli.size() ;
    msg << " arg_coef.size() = " << arg_coef.size() ;
    msg << " !=  arg_prefix.back() = " << arg_prefix.back() ;
    throw std::invalid_argument( msg.str() );
  }

  swap( m_prefix , arg_prefix );
  swap( m_coli , arg_coli );
  swap( m_coef , arg_coef );

  m_row_size = m_prefix.size() - 1 ;

  if ( 1 == m_comm_size ) { return ; }

  //------------------------------------

  if ( arg_partition.size() != 1 + m_comm_size ) {
    std::ostringstream msg ;
    msg << method << " ERROR" ;
    msg << " comm_size = " << m_comm_size ;
    msg << " + 1  !=  arg_partition.size() = " << arg_partition.size() ;
    throw std::invalid_argument( msg.str() );
  }

  const unsigned row_first = arg_partition[ m_comm_rank ];
  const unsigned row_end   = arg_partition[ m_comm_rank + 1 ] ;

  if ( m_row_size != ( row_end - row_first ) ) {
    std::ostringstream msg ;
    msg << method << " ERROR" ;
    msg << " arg_prefix'row_size = " << m_row_size ;
    msg << " !=  arg_partition'row_size = " << ( row_end - row_first );
    throw std::invalid_argument( msg.str() );
  }

  //------------------------------------

  m_send_disp.resize( m_comm_size + 1 );
  m_work_disp.resize( m_comm_size + 1 );

  // Generate a vector of off-processor column identifiers

  std::vector<unsigned> work_col_ident ;

  {
    const std::vector<unsigned>::iterator j = m_coli.end();
          std::vector<unsigned>::iterator b = m_coli.begin();
          std::vector<unsigned>::iterator i ;

    for ( i = b ; j != i ; ++i ) { 
      const unsigned global_col = *i ;
      if ( global_col < row_first || row_end <= global_col ) {
        ordered_insert( work_col_ident , global_col );
      }
    }
  }

  //------------------------------------
  // Map column global identifiers to local work offsets

  {
    const std::vector<unsigned>::iterator b = work_col_ident.begin();
    const std::vector<unsigned>::iterator e = work_col_ident.end();
          std::vector<unsigned>::iterator j ;

    j = std::lower_bound( b , e , row_end );

    const unsigned local_row_end = j - b ;

    for ( std::vector<unsigned>::iterator
          i = m_coli.begin() ; i != m_coli.end() ; ++i ) {
      const unsigned global_col = *i ;

      j = std::lower_bound( b, e, global_col );

      unsigned local_col = j - b ;

      if ( row_end <= global_col ) { local_col += local_row_end ; }

      *i = local_col ;
    }
  }

  //------------------------------------
  // Displacement prefix for work vector

  {
    std::vector<unsigned>::const_iterator i = work_col_ident.begin() ;

    m_work_disp[0] = 0 ;

    for ( unsigned p = 0 ; p < m_comm_size ; ++p ) {
      const unsigned p_row_end = arg_partition[p+1] ;
      unsigned count = 0 ;
      for ( ; i != work_col_ident.end() && *i < p_row_end ; ++i ) {
        ++count ;
      }

      m_work_disp[p+1] = m_work_disp[p] + count ;
    }
  }

  //------------------------------------
  // Set up communications to gather work subvector

  {
    std::vector<unsigned> send_col_size( m_comm_size );
    std::vector<unsigned> recv_col_size( m_comm_size );

    for ( unsigned p = 0 ; p < m_comm_size ; ++p ) {
      send_col_size[p] = m_work_disp[p+1] - m_work_disp[p] ;
    }

    if ( send_col_size[ m_comm_rank ] ) {
      std::ostringstream msg ;
      msg << method << " ERROR with communication sizing logic" ;
      throw std::logic_error( msg.str() );
    }

    unsigned num_msg_maximum = 0 ;

    comm_sizes( m_comm , m_comm_size / 4 , num_msg_maximum ,
                & send_col_size[0] , & recv_col_size[0] );

    m_sparse = num_msg_maximum < ( m_comm_size / 4 );

    m_send_disp[0] = 0 ;
    for ( unsigned p = 0 ; p < m_comm_size ; ++p ) {
      m_send_disp[p+1] = m_send_disp[p] + recv_col_size[p] ;
    }
  }

  const unsigned send_map_size = m_send_disp[ m_comm_size ];

  m_send_map.resize( send_map_size );

  all_to_all( m_comm , PARALLEL_DATATYPE_UNSIGNED , m_sparse ,
              & work_col_ident[0] , & m_work_disp[0],
              & m_send_map[0] ,     & m_send_disp[0] );

  //------------------------------------
  // Remap the 'm_work_disp' for receiving coefficients into the
  // work vector: [ lower_row_recv , local_row , upper_row_recv ]

  for ( unsigned p = m_comm_rank ; p < m_comm_size ; ++p ) {
    m_work_disp[p+1] += m_row_size ;
  }

  //------------------------------------
  // Map the send_map from global to local indices,
  // also sanity check it.

  for ( unsigned i = 0 ; i < send_map_size ; ++i ) {

    if ( m_send_map[i] < (int) row_first ||
                         (int) row_end <= m_send_map[i] ) {
      std::ostringstream msg ;
      msg << method << " ERROR Received index " ;
      msg << m_send_map[i] ;
      msg << " out of range [ " ;
      msg << row_first ;
      msg << " : " ;
      msg << row_end ;
      msg << " )" ;
      throw std::runtime_error( msg.str() );
    }

    m_send_map[i] -= row_first ;
  }
}