Ejemplo n.º 1
0
void removeDupsNoBuffer(std::forward_list<int>& l) {
    if (l.empty())
        return;
    auto backtrack = l.before_begin();
    while (std::next(backtrack) != l.end()) {
        // Iterate and print values of the list
        for (int n : l)
            std::cout << n << '\t';
        std::cout << std::endl;

        auto prev = std::next(backtrack);
        auto iter = std::next(prev);
        //std::cout << " prev =" << *prev << ", iter=" << *iter << std::endl;
        while (iter != l.end()) {
            if (*iter == *std::next(backtrack))
                iter = l.erase_after(prev);
            else {
                ++prev;
                ++iter;
            }
                
        }
        ++backtrack;
    }
}
Ejemplo n.º 2
0
int midList(std::forward_list<int> l){

	int mid = std::distance(l.begin(), l.end()); //Size of Linked List
	
	
	if( mid % 2 == 0) //Even digits two mid
	{
		int k = 0;
		for(auto i = l.begin(); i != l.end(); ++i){

			if(++k == mid/2)
				return *i; 
		}

	}else{ //Odd Digits mid + 1

	int k = 0;
	for(auto i = l.begin(); i != l.end(); ++i){

		if(++k == mid/2 + 1)
				return *i; 

	}
        }
	return -1;
}
Ejemplo n.º 3
0
int iReturn(std::forward_list<int> l, int n){

	int k = 0;

	for(auto i = l.begin(); i != l.end(); ++i){
		if(++k == std::distance(l.begin(), l.end()) - n + 1)
			return *i;
	}
}
Ejemplo n.º 4
0
int detectLoop(std::forward_list<int> l){

	auto p1 = l.begin(), p2 = l.begin();	 //Initialise two pointers to head

	while(p1 != l.end() && p2 != l.end() && std::next(p2, 1) != l.end()){ //Move p1 by 1 and p2 by 2 positions
		
		if(*(++p1) == *(++(++p2))){ //Element p1 and p2 are equal -> loop
			return 1;
		}
	}
	return 0;
} 
Ejemplo n.º 5
0
void AudioStreamingServer::SendMediaList(const std::shared_ptr<ISocket>& clientSocket, const std::string& keyword, const std::string& hostName) const {
    
    const std::forward_list<const std::string*> files = audioLibrary->Search(keyword);
    std::vector<std::string> urls;
    urls.reserve(std::distance(files.begin(), files.end()));
    
    int responseSize = 0;
    for(const std::string* file : files) {
        std::string encodedFile = urlCodec->EncodeURL(*file);
        std::string url;
        url.reserve(hostName.length() + encodedFile.length() + 9); //account 2 for '/', '\n' and 7 for 'http://'
        url += "http://";
        url += hostName;
        url += '/';
        url += encodedFile;
        url += '\n';
        responseSize += url.length();
        urls.push_back(std::move(url));
    }

    HttpResponse response(HTTP_1_1, OK, M3U, responseSize, keyword + ".m3u");
    SendResponseHeader(clientSocket, response);
    for(const std::string& url : urls) {
        clientSocket->Send(url.c_str(), url.size());
    }
}
Ejemplo n.º 6
0
std::forward_list<B> map (std::function<B(A)> f, const std::forward_list<A>& L) 
{
  std::forward_list<B> H;
  std::transform(L.begin(), L.end(), std::front_inserter(H), f);
  H.reverse();
  return H;
}
Ejemplo n.º 7
0
void
TraialPool::return_traials(std::forward_list< Reference< Traial > >& list)
{
	for (auto it = list.begin() ; it != list.end() ; it = list.begin()) {
		available_traials_.push_front(*it);
		list.pop_front();  // 'it' got invalidated
	}
}
Ejemplo n.º 8
0
std::forward_list<std::tuple<A,B>> zip (const std::forward_list<A>& L, const std::forward_list<B>& M) 
{
  std::forward_list<std::tuple<A,B>> H;
  auto zipper = [] (const A& a, const B& b) {return std::make_tuple(a,b);};
  std::transform(L.begin(), L.end(), M.begin(), std::front_inserter(H), zipper);
  H.reverse();
  return H;
}
Ejemplo n.º 9
0
static int
fbsd_is_vfork_done_pending (pid_t pid)
{
  for (auto it = fbsd_pending_vfork_done.begin ();
       it != fbsd_pending_vfork_done.end (); it++)
    if (it->pid () == pid)
      return 1;
  return 0;
}
Ejemplo n.º 10
0
void Creature::startAutoWalk(const std::forward_list<Direction>& listDir)
{
	listWalkDir = listDir;

	size_t size = 0;
	for (auto it = listDir.begin(); it != listDir.end() && size <= 1; ++it) {
		size++;
	}
	addEventWalk(size == 1);
}
Ejemplo n.º 11
0
TEST(std_forward_list, clear) {
  std::forward_list<int> l1{0, 0, 0, 0};
  const std::forward_list<int> l2{1, 2, 3};

  l1.clear();
  ASSERT_TRUE(l1.begin() == l1.end());

  l1.insert_after(l1.before_begin(), l2.begin(), l2.end());
  ASSERT_TRUE(l1 == l2);
}
Ejemplo n.º 12
0
//Iterative
void iReverse(std::forward_list<int> &l){

	std::forward_list<int> s = l, r;
	
	for(auto i = l.begin(); i != l.end(); ++i) //Iterate, pop  and push into r
	{
		r.push_front(s.front());
		s.pop_front();	
	}
	l = r; //Assign s to l
}
Ejemplo n.º 13
0
void InsertString(std::forward_list<std::string> &s, const std::string &s1, const std::string &s2)
{
    std::forward_list<std::string>::iterator a, prev = s.before_begin();

    for (a = s.begin(); a != s.end(); ++a, ++prev) {
        if (*a == s1) {
            s.insert_after(a, s2);
            return;
        }
    }
    s.insert_after(prev, s2);
}
Ejemplo n.º 14
0
 inline std::string PrettyPrint(const std::forward_list<T, Allocator>& listtoprint, const bool add_delimiters=false, const std::string& separator=", ")
 {
     std::ostringstream strm;
     if (listtoprint.size() > 0)
     {
         if (add_delimiters)
         {
             strm << "[";
             typename std::forward_list<T, Allocator>::const_iterator itr;
             for (itr = listtoprint.begin(); itr != listtoprint.end(); ++itr)
             {
                 if (itr != listtoprint.begin())
                 {
                     strm << separator << PrettyPrint(*itr, add_delimiters, separator);
                 }
                 else
                 {
                     strm << PrettyPrint(*itr, add_delimiters, separator);
                 }
             }
             strm << "]";
         }
         else
         {
             typename std::forward_list<T, Allocator>::const_iterator itr;
             for (itr = listtoprint.begin(); itr != listtoprint.end(); ++itr)
             {
                 if (itr != listtoprint.begin())
                 {
                     strm << separator << PrettyPrint(*itr, add_delimiters, separator);
                 }
                 else
                 {
                     strm << PrettyPrint(*itr, add_delimiters, separator);
                 }
             }
         }
     }
     return strm.str();
 }
Ejemplo n.º 15
0
//Using two pointers
int pReturn(std::forward_list<int> l, int n){

	auto i1 = l.begin(), i2 = l.begin();

	std::advance(i1, n); //Move iterator to n places
	
	while(i1 != l.end()){
		++i1;
		++i2;
	}

	return *i2; 
}
Ejemplo n.º 16
0
    inline
    void CEREAL_SAVE_FUNCTION_NAME(Archive &ar, std::forward_list<T, A> const &forward_list) {
        // write the size - note that this is slow because we need to traverse
        // the entire list. there are ways we could avoid this but this was chosen
        // since it works in the most general fashion with any archive type
        size_type const size = std::distance(forward_list.begin(), forward_list.end());

        ar(make_size_tag(size));

        // write the list
        for (const auto &i : forward_list)
            ar(i);
    }
Ejemplo n.º 17
0
 void operator()(clmdep_msgpack::object::with_zone& o, const std::forward_list<T, Alloc>& v) const {
     o.type = clmdep_msgpack::type::ARRAY;
     if(v.empty()) {
         o.via.array.ptr = nullptr;
         o.via.array.size = 0;
     } else {
         uint32_t size = checked_get_container_size(std::distance(v.begin(), v.end()));
         o.via.array.size = size;
         clmdep_msgpack::object* p = static_cast<clmdep_msgpack::object*>(
             o.zone.allocate_align(sizeof(clmdep_msgpack::object)*size));
         o.via.array.ptr = p;
         for(auto const& e : v) *p++ = clmdep_msgpack::object(e, o.zone);
     }
 }
Ejemplo n.º 18
0
void removeDups2(std::forward_list<int>& l) {
    std::unordered_set<int> s;
    auto prev = l.before_begin();
    auto iter = l.begin();
    while (iter != l.end()) {
        if (s.find(*iter) != s.end()) {
            iter = l.erase_after(prev);
        } else {
            s.insert(*iter);
            ++iter;
            ++prev;
        }
    }
}
    MapPolarView PolarViewAggregator::aggregate_polarviews(
        const std::forward_list<
        std::pair<const r2d2::MapPolarView &,
        const r2d2::Coordinate &>> & polarviews_list){


        std::forward_list<MapPolarView> translated_polarviews;
        //constructs translated_polarviews list out of Sensors
        for (auto it = polarviews_list.begin(); it != polarviews_list.end();
         ++it ){
            translated_polarviews.push_front(translate_base_polarview(
                it->first,
                it->second));
        }
        return merge_translated_polarviews(translated_polarviews);
    }
Ejemplo n.º 20
0
void IEventsEx::addEventHandlers(std::forward_list<IEventSinkBase*> eventHandlers)
{
	if(eventHandlers.empty()) return;
	_eventHandlerMutex.lock();
	for(std::forward_list<IEventSinkBase*>::iterator i = eventHandlers.begin(); i != eventHandlers.end(); ++i)
	{
		bool exists = false;
		for(std::forward_list<IEventSinkBase*>::iterator j = _eventHandlers.begin(); j != _eventHandlers.end(); ++j)
		{
			if(*j == *i)
			{
				exists = true;
				break;
			}
		}
		if(exists) continue;
		_eventHandlers.push_front(*i);
	}
    _eventHandlerMutex.unlock();
}
Ejemplo n.º 21
0
 inline void operator () (Args... args)
 {
   if(slots.empty())
     return;
   auto it = slots.begin(), end = slots.end();
   auto prev = slots.before_begin();
   while(it != end)
   {
     try
     {
       (*it)(args...);
     }
     catch(slot_remove)
     {
       slots.erase_after(prev);
       it = prev;
     }
     prev = it;
     ++it;
   }
 }
Ejemplo n.º 22
0
void LinkedListExercises::RemoveDuplicates2(std::forward_list<int>& rList)
{
    std::unordered_map<int, int> my_table;
    std::unordered_map<int, int>::const_iterator found_pos;
    std::forward_list<int>::const_iterator it = rList.begin();
    std::forward_list<int>::const_iterator prev_it = rList.begin();
    while (it!=rList.end())
    {
        found_pos = my_table.find(*it);
        if (found_pos != my_table.end())
        {
            it++; //Note: could use it = std::next(it)
            rList.erase_after(prev_it);
        }
        else
        {
            my_table.insert(std::make_pair(*it, 1));
            prev_it = it;
            it++;
        }
    }
}
Ejemplo n.º 23
0
bool is_zero(std::forward_list<int> &l)
{
    auto it = l.begin();
    return (*it == 0 && ++it == l.end());
}