📄 gslist.imp
字号:
//// $Source: /home/gambit/CVS/gambit/sources/base/gslist.imp,v $// $Date: 2002/09/22 16:46:57 $// $Revision: 1.3.2.1 $//// DESCRIPTION:// Classes to sort/filter lists//// This file is part of Gambit// Copyright (c) 2002, The Gambit Project//// This program is free software; you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation; either version 2 of the License, or// (at your option) any later version.//// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU General Public License for more details.//// You should have received a copy of the GNU General Public License// along with this program; if not, write to the Free Software// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.//#include "base/gslist.h"//--------------------------------------------------------------------// gSortList<T>: Member functions//--------------------------------------------------------------------template <class T> gSortList<T>::gSortList(void) : vis_length(-1){ }template <class T> gSortList<T>::gSortList(const gList<T> &l) : gList<T>(l), vis_length(l.Length()){ }template <class T> gSortList<T>::~gSortList(){ }template <class T> void gSortList<T>::Swap(int a, int b){ if (a < 1 || a > length) throw BadIndex(); if (b < 1 || b > length) throw BadIndex(); if (a == b) return; typename gList<T>::gNode *na, *nb; int i; for (i = 1, na = head; i < a; i++, na = na->next); for (i = 1, nb = head; i < b; i++, nb = nb->next); typename gList<T>::gNode na1 = *na; if (na->next==nb || nb->next==na) { // adjacent if (nb->next==na) { typename gList<T>::gNode *t = nb; nb = na; na = t; } na->next = nb->next; na->prev = nb; nb->next = na; nb->prev = na1.prev; if (nb->prev) nb->prev->next = nb; else head=nb; if (na->next) na->next->prev = na; else tail = na; } else { na->next = nb->next; na->prev = nb->prev; if (na->next) na->next->prev = na; else tail = na; if (na->prev) na->prev->next = na; else head = na; nb->prev = na1.prev; nb->next = na1.next; if (nb->next) nb->next->prev = nb; else tail = nb; if (nb->prev) nb->prev->next = nb; else head = nb; } CurrIndex = 1; CurrNode = head;}template <class T> int gSortList<T>::VisibleLength(void) const{ return (vis_length == -1) ? Length() : vis_length;}template <class T> T gSortList<T>::Remove(int a){ if (a <= vis_length) vis_length--; try { return gList<T>::Remove(a); } catch (...) { if (a <= vis_length - 1) vis_length++; throw; }}template <class T> void gSortList<T>::Flush(void){ gList<T>::Flush(); vis_length = -1;}//--------------------------------------------------------------------// gListSorter<T>: Member functions//--------------------------------------------------------------------template <class T> gListSorter<T>::gListSorter(void){ }template <class T> gListSorter<T>::~gListSorter() { }template <class T> void gListSorter<T>::Sort(gSortList<T> &list){ // this uses a bubble sort algorithm bool changed; do { changed = false; for (int i = 1; i <= list.VisibleLength() - 1; i++) { if (LessThan(list[i], list[i+1])) { list.Swap(i, i+1); changed = true; } } } while (changed);}//--------------------------------------------------------------------// gListFilter<T>: Member functions//--------------------------------------------------------------------template <class T> gListFilter<T>::gListFilter(void){ }template <class T> gListFilter<T>::~gListFilter() { }template <class T> void gListFilter<T>::Filter(gSortList<T> &list){ list.vis_length = list.Length(); int nodes_rejected = 0, i = 1; while (nodes_rejected + i <= list.Length()) { if (!Passes(list[i])) { list.Swap(i, list.VisibleLength()); list.vis_length--; nodes_rejected++; } else i++; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -