Back to index

unity  6.0.0
FavoriteStorePrivate.cpp
Go to the documentation of this file.
00001 // -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
00002 /*
00003 * Copyright (C) 2011 Canonical Ltd
00004 *
00005 * This program is free software: you can redistribute it and/or modify
00006 * it under the terms of the GNU General Public License version 3 as
00007 * published by the Free Software Foundation.
00008 *
00009 * This program is distributed in the hope that it will be useful,
00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 * GNU General Public License for more details.
00013 *
00014 * You should have received a copy of the GNU General Public License
00015 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 *
00017 * Authored by: Andrea Azzaronea <azzaronea@gmail.com>
00018 */
00019 
00020 #include <algorithm>
00021 #include <boost/utility.hpp>
00022 
00023 #include "FavoriteStorePrivate.h"
00024 
00025 namespace unity
00026 {
00027 namespace internal
00028 {
00029 namespace impl
00030 {
00031 
00032 std::vector<std::string> GetNewbies(std::list<std::string> const& old, std::list<std::string> const& fresh)
00033 {
00034   auto sorted_old(old);
00035   auto sorted_fresh(fresh);
00036 
00037   sorted_old.sort();
00038   sorted_fresh.sort();
00039 
00040   std::vector<std::string> result;
00041   std::set_difference(sorted_fresh.begin(), sorted_fresh.end(), sorted_old.begin(), sorted_old.end(),
00042                       std::inserter(result, result.end()));
00043 
00044   return result;
00045 }
00046 
00047 void GetSignalAddedInfo(std::list<std::string> const& favs, std::vector<std::string> const& newbies,
00048                         std::string const& path, std::string& position, bool& before)
00049 {
00050   auto it = std::find(favs.begin(), favs.end(), path);
00051   before = (it == favs.begin());
00052   position = "";
00053 
00054   if (before and favs.size() > 1)
00055   {
00056     while (it != favs.end() && std::find(newbies.begin(), newbies.end(), *it) != newbies.end())
00057       ++it;
00058 
00059     if (it != favs.end())
00060       position = *it;
00061   }
00062   else if (!before)
00063   {
00064     position = *(boost::prior(it));
00065   }
00066 
00067 }
00068 
00069 std::vector<std::string> GetRemoved(std::list<std::string> const& old, std::list<std::string> const& fresh)
00070 {
00071   auto sorted_old(old);
00072   auto sorted_fresh(fresh);
00073 
00074   sorted_old.sort();
00075   sorted_fresh.sort();
00076 
00077   std::vector<std::string> result;
00078   std::set_difference(sorted_old.begin(), sorted_old.end(), sorted_fresh.begin(), sorted_fresh.end(),
00079                       std::inserter(result, result.end()));
00080 
00081   return result;
00082 }
00083 
00084 
00085 bool NeedToBeReordered(std::list<std::string> const& old, std::list<std::string> const& fresh)
00086 {
00087   auto sorted_old(old);
00088   auto sorted_fresh(fresh);
00089 
00090   sorted_old.sort();
00091   sorted_fresh.sort();
00092 
00093   std::vector<std::string> ignore_old, ignore_fresh;
00094 
00095   std::set_difference(sorted_old.begin(), sorted_old.end(), sorted_fresh.begin(), sorted_fresh.end(),
00096                       std::inserter(ignore_old, ignore_old.end()));
00097   std::set_difference(sorted_fresh.begin(), sorted_fresh.end(), sorted_old.begin(), sorted_old.end(),
00098                       std::inserter(ignore_fresh, ignore_fresh.end()));
00099 
00100   auto it_old = old.begin();
00101   auto it_fresh = fresh.begin();
00102 
00103   while (it_old != old.end() && it_fresh != fresh.end())
00104   {
00105 
00106     while (it_old != old.end() && std::find(ignore_old.begin(), ignore_old.end(), *it_old) != ignore_old.end())
00107       ++it_old;
00108 
00109     while (it_fresh != fresh.end() && std::find(ignore_fresh.begin(), ignore_fresh.end(), *it_fresh) != ignore_fresh.end())
00110       ++it_fresh;
00111 
00112     if (it_old == old.end() || it_fresh == fresh.end())
00113       break;
00114 
00115     if (*it_old != *it_fresh)
00116     {
00117       return true;
00118     }
00119 
00120     ++it_old;
00121     ++it_fresh;
00122   }
00123 
00124   return false;
00125 }
00126 
00127 
00128 } // namespace impl
00129 } // namespace internal
00130 } // namespace unity