Example #1
0
 int singleNumber(vector<int>& nums) {
     return accumulate(nums.cbegin(), nums.cend(),
                       0, std::bit_xor<int>());
 }
Example #2
0
void draw_hull (const string& path,
                const vector<Point2D>& points,
                const vector<Point2D>& hull,
                const Point2D& apex) {
    string final_path = app_name + "_" + path;
    const int width = 640;
    const int height = 480;
    
    std::function <bool (const Point2D&, const Point2D&)> compare_points_farther =
        [](const Point2D& first, const Point2D& second) -> bool
    {   return std::max (std::abs (first.x), std::abs (first.y)) <
               std::max (std::abs (second.x), std::abs (second.y));
    };

    auto most_distant_point = std::max_element (points.cbegin (), points.cend (), compare_points_farther);
    const double scale = 200 / std::max (std::abs (most_distant_point->x), std::abs (most_distant_point->y));
    const double center_x = static_cast<double> (width) / 2.0;
    const double center_y = static_cast<double> (height) / 2.0;
    const int bits_per_pixel = 24;
    FreeImage_Initialise ();
    FIBITMAP* bitmap = FreeImage_Allocate (width, height, bits_per_pixel);
    
    if (nullptr == bitmap)
    {   cout << "bitmap creation failed" << endl;
        return;
    }
    
    std::function <Point2D (const Point2D&)> convert_to_frame_space =
        [scale, center_x, center_y] (const Point2D& point) -> Point2D
    {   Point2D frame_point;
        frame_point.x = point.x * scale + center_x;
        frame_point.y = point.y * scale + center_y;
        return frame_point;
    };

    RGBQUAD green_colour;
    green_colour.rgbRed = 0;
    green_colour.rgbGreen = 255;
    green_colour.rgbBlue = 0;
    RGBQUAD white_colour;
    white_colour.rgbRed = 255;
    white_colour.rgbGreen = 255;
    white_colour.rgbBlue = 255;
    RGBQUAD black_colour;
    black_colour.rgbRed = 0;
    black_colour.rgbGreen = 0;
    black_colour.rgbBlue = 0;

    FreeImage_FillBackground (bitmap, &white_colour);
    draw_line (bitmap, Point2D (center_x, 0), Point2D (center_x, height));
    draw_line (bitmap, Point2D (0, center_y), Point2D (width, center_y));

    for (vector<Point2D>::const_iterator point = hull.cbegin ();
         point != hull.cend (); ++point)
    {   auto prev_point = (point == hull.cbegin ())
                          ? std::prev (hull.cend ())
                          : std::prev (point);

        Point2D prev_frame_point = convert_to_frame_space (*prev_point);
        Point2D frame_point = convert_to_frame_space (*point);
        draw_line (bitmap, prev_frame_point, frame_point);
    }

    for (const Point2D& point : points)
    {   Point2D frame_point = convert_to_frame_space (point);
        RGBQUAD current_color (black_colour);
        FreeImage_SetPixelColor (bitmap, frame_point.x, frame_point.y, &current_color);
        FreeImage_SetPixelColor (bitmap, frame_point.x + 1, frame_point.y, &current_color);
        FreeImage_SetPixelColor (bitmap, frame_point.x - 1, frame_point.y, &current_color);
        FreeImage_SetPixelColor (bitmap, frame_point.x, frame_point.y - 1, &current_color);
        FreeImage_SetPixelColor (bitmap, frame_point.x, frame_point.y + 1, &current_color);
    }

    if (!is_point_invalid (apex))
    {
        Point2D frame_point = convert_to_frame_space (apex);
        FreeImage_SetPixelColor (bitmap, frame_point.x, frame_point.y, &green_colour);
        FreeImage_SetPixelColor (bitmap, frame_point.x + 1, frame_point.y, &green_colour);
        FreeImage_SetPixelColor (bitmap, frame_point.x - 1, frame_point.y, &green_colour);
        FreeImage_SetPixelColor (bitmap, frame_point.x, frame_point.y - 1, &green_colour);
        FreeImage_SetPixelColor (bitmap, frame_point.x, frame_point.y + 1, &green_colour);
    }

    if (0 == FreeImage_Save (FIF_PNG, bitmap, final_path.c_str ()))
    {   cout << "Saving failed" << endl;
    }
    FreeImage_DeInitialise ();
}
Example #3
0
void draw_enet_computation (const string& dst,
                            const vector<std::pair<Point2D,Point2D>>& lines,
                            const vector<Point2D>& points,
                            const Point2D& interior_point)
{
    const int width = 640;
    const int height = 480;

    std::function <bool (const Point2D&, const Point2D&)> compare_points_farther =
        [] (const Point2D& first, const Point2D& second) -> bool
    {   return std::max (std::abs (first.x), std::abs (first.y)) <
        std::max (std::abs (second.x), std::abs (second.y));
    };

    auto most_distant_point = std::max_element (points.cbegin (), points.cend (), compare_points_farther);
    const double scale = 200 / std::max (std::abs (most_distant_point->x), std::abs (most_distant_point->y));
    const double center_x = static_cast<double> (width) / 2.0;
    const double center_y = static_cast<double> (height) / 2.0;
    const int bits_per_pixel = 24;
    FreeImage_Initialise ();
    FIBITMAP* bitmap = FreeImage_Allocate (width, height, bits_per_pixel);

    if (nullptr == bitmap)
    {   cout << "bitmap creation failed" << endl;
        return;
    }

    std::function <Point2D (const Point2D&)> convert_to_frame_space =
        [scale, center_x, center_y] (const Point2D& point) -> Point2D
    {   Point2D frame_point;
        frame_point.x = point.x * scale + center_x;
        frame_point.y = point.y * scale + center_y;
        return frame_point;
    };

    RGBQUAD green_colour;
    green_colour.rgbRed = 0;
    green_colour.rgbGreen = 255;
    green_colour.rgbBlue = 0;
    RGBQUAD red_colour;
    red_colour.rgbRed = 255;
    red_colour.rgbGreen = 0;
    red_colour.rgbBlue = 0;
    RGBQUAD white_colour;
    white_colour.rgbRed = 255;
    white_colour.rgbGreen = 255;
    white_colour.rgbBlue = 255;
    RGBQUAD black_colour;
    black_colour.rgbRed = 0;
    black_colour.rgbGreen = 0;
    black_colour.rgbBlue = 0;

    FreeImage_FillBackground (bitmap, &white_colour);
    draw_line (bitmap, Point2D (center_x, 0), Point2D (center_x, height));
    draw_line (bitmap, Point2D (0, center_y), Point2D (width, center_y));

    for (const Point2D& point : points)
    {   Point2D frame_point = convert_to_frame_space (point);
        RGBQUAD current_color (black_colour);

        FreeImage_SetPixelColor (bitmap, frame_point.x, frame_point.y, &current_color);
        FreeImage_SetPixelColor (bitmap, frame_point.x + 1, frame_point.y, &current_color);
        FreeImage_SetPixelColor (bitmap, frame_point.x - 1, frame_point.y, &current_color);
        FreeImage_SetPixelColor (bitmap, frame_point.x, frame_point.y - 1, &current_color);
        FreeImage_SetPixelColor (bitmap, frame_point.x, frame_point.y + 1, &current_color);
    }

    assert (lines.size () > 0);
    for (const std::pair<Point2D,Point2D>& line : lines)
    {   Point2D src (convert_to_frame_space (line.first));
        Point2D dst (convert_to_frame_space (line.second));
        draw_line (bitmap, src, dst);
    }

    Point2D frame_interior_point = convert_to_frame_space (interior_point);
    FreeImage_SetPixelColor (bitmap, frame_interior_point.x, frame_interior_point.y, &green_colour);
    FreeImage_SetPixelColor (bitmap, frame_interior_point.x + 1, frame_interior_point.y, &green_colour);
    FreeImage_SetPixelColor (bitmap, frame_interior_point.x - 1, frame_interior_point.y, &green_colour);
    FreeImage_SetPixelColor (bitmap, frame_interior_point.x, frame_interior_point.y - 1, &green_colour);
    FreeImage_SetPixelColor (bitmap, frame_interior_point.x, frame_interior_point.y + 1, &green_colour);

    if (0 == FreeImage_Save (FIF_PNG, bitmap, dst.c_str ()))
    {   cout << "Saving failed" << endl;
    }
    FreeImage_DeInitialise ();
}
Example #4
0
void printv(vector<int> &vi) {
    for (auto i = vi.cbegin(); i != vi.cend(); i++)
        std::cout << *i << " ";
}
Example #5
0
void parallel_2d_hull::distribute_over_buckets () {
    int bucket_size = subset_cardinality_ * halfplane_coefficient_;
    LOG_LEAD ("Bucket size" << bucket_size);
    bucket_0_.resize (bucket_size * processors_amount_, {-1,-1});
    bucket_1_.resize (bucket_size * processors_amount_, {-1,-1});

    // iterate over local hull and send every point to the appropriate bucket
    vector<vector <Point2D>> bucket_entries;
    bucket_entries.resize (buckets_per_proc_ *
                           processors_amount_);
    for (const Point2D& local_hull_point : local_hull_)
    {   for (auto bucket_iter = splitters_.cbegin();
             bucket_iter != splitters_.cend ();
             ++bucket_iter)
        {   if (does_point_fall_into_bucket (local_hull_point, bucket_iter))
            {   int bucket_index =
                    std::distance<Point2D_const_iterator> (splitters_.cbegin (),
                                                           bucket_iter);
                bucket_entries.at (bucket_index).emplace_back (local_hull_point);
                break;
            }
        }
    }
    bsp_push_reg (bucket_0_.data (), bucket_0_.size () * sizeof (Point2D));
    bsp_push_reg (bucket_1_.data (), bucket_1_.size () * sizeof (Point2D));
    bsp_sync ();
    for (int bucket_index = 0;
         bucket_index < bucket_entries.size ();
         ++bucket_index)
    {   if (bucket_entries.at (bucket_index).empty ())
        {   continue;
        }
        int designated_proc = bucket_index / 2;
        void* destination = (0 == bucket_index % 2)
                            ? bucket_0_.data ()
                            : bucket_1_.data ();
        bsp_put (designated_proc,
                 bucket_entries.at (bucket_index).data (),
                 destination,
                 bucket_size * id_,
                 bucket_entries.at (bucket_index).size () * sizeof (Point2D));
    }
    bsp_pop_reg (bucket_0_.data ());
    bsp_pop_reg (bucket_1_.data ());
    bsp_sync ();
    ///////////////
    ///////////////
    bucket_0_.erase (std::remove_if (bucket_0_.begin (), bucket_0_.end (), is_point_invalid), bucket_0_.end ());
     bucket_1_.erase (std::remove_if (bucket_1_.begin (), bucket_1_.end (), is_point_invalid), bucket_1_.end ());
    assert (bucket_0_.size () <= subset_cardinality_ ||
            bucket_1_.size () <= subset_cardinality_);
    bsp_sync ();
    ///////////////
    //bsp_push_reg (whole_pointset_.data (), whole_pointset_.size () * sizeof (Point2D));
    // bsp_sync ();
    // if (1 ==id_)
    // {   bsp_get (LEAD_PROCESSOR_ID_,
    //              whole_pointset_.data (),
    //              0,
    //              whole_pointset_.data (),
    //              whole_pointset_.size () * sizeof (Point2D));
    // }
    // 
    // bsp_sync ();
    // bsp_pop_reg (whole_pointset_.data ());
    // bsp_sync ();
    /////////////
    // for (int i = 0; i < processors_amount_; ++i)
    // {   if (i == id_)
    //     {   if (0 == i)
    //         {   vector<std::pair<Point2D, Point2D>> lines =
    //             {generate_line_prev(splitters_.cbegin (), splitters_),
    //              generate_line_next(splitters_.cbegin (), splitters_)};
    //             draw_subset ("bucket_0_.png", whole_pointset_, bucket_0_,
    //                          interior_point_, lines);
    //         }
    //         assert (bucket_0_.size () <= bucket_size);
    //         assert (bucket_1_.size () <= bucket_size);
    //         LOG_ALL ("bucket 0");
    //         for (auto p : bucket_0_)
    //         {   if (!is_point_invalid (p)) LOG_ALL (p);
    //         }
    //         LOG_ALL ("bucket 1");
    //         for (auto p : bucket_1_)
    //         {   if (!is_point_invalid (p)) LOG_ALL (p);
    //         }
    //     }
    //     bsp_sync ();
    // }
}
 edge_const_iterator in_end() const {
     return incoming_edges_.cend();
 }
 edge_const_iterator out_end() const {
     return outgoing_edges_.cend();
 }
Example #8
0
double l2norm2(const pagerank_t& page, const vector<double> &p1, const vector<double> &p2) {
  double diff = 0;
  for (auto cit = p1.cbegin(), cjt = p2.cbegin(); diff <= page.eps*page.eps && cit != p1.cend(); ++cit, ++cjt)
    diff += (*cit - *cjt)*(*cit - *cjt);
  return diff;
}
int max_subarray_sum_in_circular(const vector<int>& A) {
  // Find the max in non-circular case and circular case.
  return max(find_optimum_subarray_using_comp(A, max),  // non-circular case.
             accumulate(A.cbegin(), A.cend(), 0) -
                 find_optimum_subarray_using_comp(A, min));  // circular case.
}
Example #10
0
void buildTests(vector<stlTestBase*>& tests, const vector<stlType*>& types) {
    for(vector<stlType*>::const_iterator it = types.cbegin();
        it != types.cend(); it++) {
        tests.push_back(new initTest(*it));
    }
}