1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
/*
* part of bidwatcher
* Author: Jan Starzynski
* use of this code is restricted to the terms
* of the GNU GPL, which should have been included in this
* distribution. If not, see www.gnu.org/copyleft/gpl.html.
* Here is the short version:
*
* 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.
*
* $Id: bidgroup.cpp,v 1.1.2.5 2004/10/25 12:11:29 sturnus Exp $
*
* Handling of Bidgroups
*/
#include "config.h"
#ifdef HAVE_STL
#include <algorithm>
#include <functional>
#include <gdk/gdk.h>
#include <gdk/gdkprivate.h>
#include <gtk/gtk.h>
#include "bidwatcher.h"
#include "bidgroup.h"
#include "portability.h"
using namespace std;
struct FindItem: public unary_function<BidGroup::ItemList, bool> {
BidGroup::IdType id;
FindItem(BidGroup::IdType id): id(id) {}
bool operator()(const BidGroup::ItemList &list) const { return list.find(id) != list.end(); }
};
struct UselessList: public unary_function<BidGroup::ItemList, bool> {
bool operator()(const BidGroup::ItemList &list) const { return list.size() <= 1; }
};
void BidGroup::clear()
{
m_list.clear();
}
int BidGroup::numGroup() const
{
return m_list.size();
}
BidGroup::ItemList *BidGroup::addItem(const IdType &id1, const IdType &id2)
{
ItemList *item;
item = findItem(id1);
if(item) {
item->insert(id2);
return item;
}
item = findItem(id2);
if(item) {
item->insert(id1);
return item;
}
if(id1 == id2) return 0;
ItemList add;
add.insert(id1);
add.insert(id2);
GroupList::iterator insert = find_if(m_list.begin(), m_list.end(), UselessList());
if(insert == m_list.end()) {
m_list.insert(insert, add);
} else {
*insert = add;
}
return &m_list.back();
}
int BidGroup::groupId(const IdType &id) const
{
GroupList::const_iterator it = find_if(m_list.begin(), m_list.end(), FindItem(id));
return it == m_list.end() ? -1 : it - m_list.begin();
}
BidGroup::ItemList *BidGroup::findItemP(const IdType &id) const
{
return getGroupP(groupId(id));
}
BidGroup::ItemList *BidGroup::getGroupP(int gid) const
{
return const_cast<BidGroup::ItemList*>(gid < 0 || gid >= numGroup() ? 0 : &m_list[gid]);
}
bool BidGroup::delItem(const IdType &id)
{
bool ok = false;
for(GroupList::iterator it = m_list.begin(); it != m_list.end(); ++it) {
ok |= it->erase(id);
}
return ok;
}
vector<BidGroup::IdType> &BidGroup::findItem(vector<IdType> &vec, const IdType &id)
{
ItemList *item = findItem(id);
if(id) vec.insert(vec.end(), item->begin(), item->end());
return vec;
}
bool BidGroup::read(const char *file)
{
FILE *fp = fopen(file, "r");
bool ret = read(fp);
if(fp) fclose(fp);
return ret;
}
bool BidGroup::write(const char *file) const
{
FopenW fp(file);
return write(fp);
}
bool BidGroup::read(FILE *fp)
{
if(!fp) return false;
bool ok = true;
char buffer[30];
ItemList group;
while(fgets(buffer, sizeof(buffer), fp)) {
IdType id;
if(sscanf(buffer, LLU_FMT, &id) == 1) {
group.insert(id);
} else {
if(!group.empty()) {
m_list.push_back(group);
group.clear();
}
}
}
if(!group.empty()) m_list.push_back(group);
return ok;
}
bool BidGroup::write(FILE *fp) const
{
if(!fp) return false;
bool ok = true;
for(GroupList::const_iterator it = m_list.begin(); it != m_list.end(); ++it) {
if(!it->empty()) {
for(ItemList::const_iterator jt = it->begin(); jt != it->end(); ++jt) {
fprintf(fp, LLU_FMT"\n", *jt);
}
fprintf(fp, "\n");
}
}
return ok;
}
void BidGroup::sweepList(bool at_end_only)
{
if(at_end_only) {
m_list.erase(find_if(m_list.rbegin(), m_list.rend(), not1<UselessList>(UselessList())).base(), m_list.end());
} else {
m_list.erase(remove_if(m_list.begin(), m_list.end(), UselessList()), m_list.end());
}
}
#endif
|