"SfR Fresh" - the SfR Freeware/Shareware Archive 
Member "pan-0.133/pan/tasks/adaptable-set.h" of archive pan-0.133.tar.gz:
As a special service "SfR Fresh" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting with prefixed line numbers.
Alternatively you can here view or download the uninterpreted source code file.
That can be also achieved for any archive member file by clicking within an archive contents listing on the first character of the file(path) respectively on the according byte size field.
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*
3 * Pan - A Newsreader for Gtk+
4 * Copyright (C) 2002-2006 Charles Kerr <charles@rebelbase.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 #ifndef _AdaptableSet_h_
21 #define _AdaptableSet_h_
22
23 #include <set>
24 #include <vector>
25
26 namespace pan
27 {
28 /**
29 * A std::set-like container that allows items to be reordered..
30 *
31 * The intent is for newly-added elements to be inserted into the
32 * proper ordered place but to let callers rearrange elements at will.
33 * The subset of unmoved elements will remain ordered.
34 *
35 * This is used by the task queue so that users can rearrange tasks,
36 * but newly-added tasks will be added in a best-fit manner.
37 *
38 * @ingroup tasks
39 */
40 template <class X, class StrictWeakOrdering> class AdaptableSet
41 {
42 public:
43 typedef std::vector<X> items_t;
44 protected:
45 items_t _items;
46 private:
47 items_t _unmoved;
48 void remove_from_unmoved (const X& x);
49 const StrictWeakOrdering _comp;
50
51 public:
52 typedef typename items_t::iterator iterator;
53 typedef typename items_t::const_iterator const_iterator;
54 const_iterator begin() const { return _items.begin(); }
55 iterator begin() { return _items.begin(); }
56 const_iterator end() const { return _items.end(); }
57 iterator end() { return _items.end(); }
58
59 public:
60 AdaptableSet () {}
61 virtual ~AdaptableSet () {}
62
63 public:
64 bool empty() const { return _items.empty(); }
65 int size() const { return _items.size(); }
66 const X& operator[](int i) const { return *(_items.begin()+i); }
67
68 public:
69 int index_of (const X& x) const;
70 void remove (int index);
71 void move_up (int index);
72 void move_down (int index);
73 void move_top (int index);
74 void move_bottom (int index);
75 int add (X&);
76 void add (const std::vector<X>&);
77 void add_top (const std::vector<X>&);
78 void add_bottom (const std::vector<X>&);
79 void move (int new_index, int old_index);
80
81 public:
82 /**
83 * Interface class for objects that listen to an AdaptableSet's events.
84 */
85 struct Listener {
86 virtual ~Listener () {}
87 virtual void on_set_items_added (AdaptableSet&, items_t&, int index) = 0;
88 virtual void on_set_item_removed (AdaptableSet&, X&, int index) = 0;
89 virtual void on_set_item_moved (AdaptableSet&, X&, int index, int old_index) = 0;
90 };
91 void add_listener (Listener * l) { _listeners.insert (l); }
92 void remove_listener (Listener * l) { _listeners.erase (l); }
93
94 protected:
95 virtual void fire_items_added (items_t&, int index);
96 virtual void fire_item_removed (X&, int index);
97 virtual void fire_item_moved (X&, int index, int old_index);
98
99 private:
100 typedef std::set<Listener*> listeners_t;
101 listeners_t _listeners;
102 };
103 }
104
105 #include "adaptable-set.cc"
106
107 #endif