Simple Application Framework
1
|
00001 /* 00002 This file is part of Simple Application Framework (Saf) library. 00003 Copyright (C) 2010 - 2012 Ondrej Danek <ondrej.danek@gmail.com> 00004 00005 This library is free software: you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published 00007 by the Free Software Foundation, either version 3 of the License, or 00008 (at your option) any later version. 00009 00010 Saf is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with Simple Application Framework library. If not, 00017 see <http://www.gnu.org/licenses/>. 00018 */ 00019 00028 #ifndef SAF_ALGO_SORT_INSERTIONSORT_H 00029 #define SAF_ALGO_SORT_INSERTIONSORT_H 00030 00031 #include "../../Type.h" 00032 #include "../../Type/IteratorTraits.h" 00033 #include "../Swap.h" 00034 #include "../Predicate.h" 00035 00036 namespace Saf 00037 { 00039 namespace Internal 00040 { 00041 namespace AlgoSort 00042 { 00044 template <class FwdIter, class Pred> 00045 void InsertionSort(FwdIter begin, FwdIter end, Pred pred, 00046 Saf::Type::IteratorCategory::ForwardCategory) 00047 { 00048 if (begin != end) 00049 { 00050 FwdIter next = begin; 00051 00052 while (++next != end) 00053 { 00054 // Find new position for the next element 00055 FwdIter newPos = begin; 00056 00057 while (newPos != next && !pred(*next, *newPos)) 00058 { 00059 ++newPos; 00060 } 00061 00062 // Put the next element into its place and shift the rest 00063 while (newPos != next) 00064 { 00065 Saf::Algo::Swap(*newPos, *next); 00066 ++newPos; 00067 } 00068 } 00069 } 00070 } 00071 00078 template <class BidIter, class Pred> 00079 void InsertionSort(BidIter begin, BidIter end, Pred pred, 00080 Saf::Type::IteratorCategory::BidirectionalCategory) 00081 { 00082 if (begin != end) 00083 { 00084 BidIter next = begin; 00085 00086 while (++next != end) 00087 { 00088 // Shift the current element back to its position 00089 BidIter cur = next, prev = next; 00090 00091 while (cur != begin) 00092 { 00093 --prev; 00094 00095 if (!pred(*cur, *prev)) 00096 { 00097 break; 00098 } 00099 00100 Saf::Algo::Swap(*cur, *prev); 00101 --cur; 00102 } 00103 } 00104 } 00105 } 00106 } 00107 } 00110 namespace Algo 00111 { 00112 namespace Sort 00113 { 00128 template <class Iter, class Pred> 00129 void InsertionSort(Iter begin, Iter end, Pred pred) 00130 { 00131 Internal::AlgoSort::InsertionSort(begin, end, pred, Type::IteratorTraits<Iter>::Category()); 00132 } 00133 00147 template <class Iter> 00148 void InsertionSort(Iter begin, Iter end) 00149 { 00150 Predicate::Less<typename Type::IteratorTraits<Iter>::ValType> pred; 00151 InsertionSort(begin, end, pred); 00152 } 00153 } 00154 } 00155 } 00156 00157 #endif