* replaced std::vector with std::deque.

pull/1/head
Tatsuhiro Tsujikawa 2006-03-22 16:21:11 +00:00
parent 26aa28acb3
commit 198b87fa3e
32 changed files with 96 additions and 84 deletions

View File

@ -11,6 +11,7 @@
* AbstractDiskWriter.{h,cc}: moved digest context initialization
to Constructor. Also, moved digest cleanup to Destructor.
* MetaFileUtil.cc: fixed memory leak
* replaced std::vector with std::deque.
2006-03-22 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>

View File

@ -185,8 +185,8 @@ int BitfieldMan::getFirstMissingUnusedIndex() const {
return -1;
}
vector<int> BitfieldMan::getAllMissingIndexes() const {
vector<int> missingIndexes;
BlockIndexes BitfieldMan::getAllMissingIndexes() const {
BlockIndexes missingIndexes;
for(int i = 0; i < bitfieldLength; i++) {
unsigned char bit = ~bitfield[i];
for(int bs = 7; bs >= 0 && i*8+7-bs < blocks; bs--) {

View File

@ -23,7 +23,9 @@
#define _D_BITFIELD_MAN_H_
#include "common.h"
#include <vector>
#include <deque>
typedef deque<int> BlockIndexes;
class BitfieldMan {
private:
@ -63,7 +65,7 @@ public:
int getFirstMissingUnusedIndex(const unsigned char* bitfield, int len) const;
int getFirstMissingUnusedIndex() const;
int getMissingUnusedIndex(const unsigned char* bitfield, int len) const;
vector<int> getAllMissingIndexes() const;
BlockIndexes getAllMissingIndexes() const;
int countMissingBlock() const;
bool setUseBit(int index);
bool unsetUseBit(int index);

View File

@ -54,18 +54,18 @@ void CookieBox::setField(Cookie& cookie, string name, string value) const {
void CookieBox::parse(Cookie& cookie, string cookieStr) const {
cookie.clear();
vector<string> terms;
Strings terms;
Util::slice(terms, cookieStr, ';');
for(vector<string>::iterator itr = terms.begin(); itr != terms.end(); itr++) {
for(Strings::iterator itr = terms.begin(); itr != terms.end(); itr++) {
pair<string, string> nv;
Util::split(nv, *itr, '=');
setField(cookie, nv.first, nv.second);
}
}
vector<Cookie> CookieBox::criteriaFind(string host, string dir, bool secure) const {
vector<Cookie> result;
for(vector<Cookie>::const_iterator itr = cookies.begin(); itr != cookies.end(); itr++) {
Cookies CookieBox::criteriaFind(string host, string dir, bool secure) const {
Cookies result;
for(Cookies::const_iterator itr = cookies.begin(); itr != cookies.end(); itr++) {
const Cookie& c = *itr;
if((secure || !c.secure && !secure) &&
Util::endsWith(host, c.domain) &&

View File

@ -24,7 +24,7 @@
#include "common.h"
#include <string>
#include <vector>
#include <deque>
using namespace std;
@ -49,9 +49,11 @@ public:
}
};
typedef deque<Cookie> Cookies;
class CookieBox {
private:
vector<Cookie> cookies;
Cookies cookies;
void setField(Cookie& cookie, string name, string value) const;
public:
CookieBox();
@ -60,7 +62,7 @@ public:
void add(const Cookie& cookie);
void add(string cookieStr);
void parse(Cookie& cookie, string cookieStr) const;
vector<Cookie> criteriaFind(string host, string dir, bool secure) const;
Cookies criteriaFind(string host, string dir, bool secure) const;
};
#endif // _D_COOKIE_BOX_H_

View File

@ -52,6 +52,6 @@ void Dictionary::accept(MetaEntryVisitor* v) const {
v->visit(this);
}
const vector<string>& Dictionary::getOrder() const {
const Order& Dictionary::getOrder() const {
return order;
}

View File

@ -24,17 +24,18 @@
#include "MetaEntry.h"
#include <map>
#include <vector>
#include <deque>
#include <string>
using namespace std;
typedef map<string, MetaEntry*> MetaTable;
typedef deque<string> Order;
class Dictionary : public MetaEntry {
private:
MetaTable table;
vector<string> order;
Order order;
void clearTable();
public:
Dictionary();
@ -44,7 +45,7 @@ public:
void put(string name, MetaEntry* entry);
void accept(MetaEntryVisitor* v) const;
const vector<string>& getOrder() const;
const Order& getOrder() const;
};

View File

@ -24,11 +24,11 @@
#include "common.h"
#include <string>
#include <vector>
#include <deque>
class Directory {
typedef vector<Directory*> Files;
typedef deque<Directory*> Files;
private:
string name;

View File

@ -75,14 +75,13 @@ void DownloadEngine::waitData() {
FD_ZERO(&rfds);
FD_ZERO(&wfds);
int max = 0;
for(vector<Socket*>::iterator itr = rsockets.begin(); itr != rsockets.end(); itr++) {
for(Sockets::iterator itr = rsockets.begin(); itr != rsockets.end(); itr++) {
FD_SET((*itr)->getSockfd(), &rfds);
if(max < (*itr)->getSockfd()) {
max = (*itr)->getSockfd();
}
}
for(vector<Socket*>::iterator itr = wsockets.begin(); itr != wsockets.end(); itr++) {
for(Sockets::iterator itr = wsockets.begin(); itr != wsockets.end(); itr++) {
FD_SET((*itr)->getSockfd(), &wfds);
if(max < (*itr)->getSockfd()) {
max = (*itr)->getSockfd();
@ -94,10 +93,10 @@ void DownloadEngine::waitData() {
retval = select(max+1, &rfds, /*&wfds*/NULL, NULL, &tv);
}
bool DownloadEngine::addSocket(vector<Socket*>& sockets, Socket* socket) {
vector<Socket*>::iterator itr = find(sockets.begin(),
sockets.end(),
socket);
bool DownloadEngine::addSocket(Sockets& sockets, Socket* socket) {
Sockets::iterator itr = find(sockets.begin(),
sockets.end(),
socket);
if(itr == sockets.end()) {
sockets.push_back(socket);
return true;
@ -106,10 +105,10 @@ bool DownloadEngine::addSocket(vector<Socket*>& sockets, Socket* socket) {
}
}
bool DownloadEngine::deleteSocket(vector<Socket*>& sockets, Socket* socket) {
vector<Socket*>::iterator itr = find(sockets.begin(),
sockets.end(),
socket);
bool DownloadEngine::deleteSocket(Sockets& sockets, Socket* socket) {
Sockets::iterator itr = find(sockets.begin(),
sockets.end(),
socket);
if(itr != sockets.end()) {
sockets.erase(itr);
return true;

View File

@ -23,7 +23,7 @@
#define _D_DOWNLOAD_ENGINE_H_
#include <queue>
#include <vector>
#include <deque>
#include "Command.h"
#include "Socket.h"
#include "SegmentMan.h"
@ -35,22 +35,25 @@
using namespace std;
typedef deque<Socket*> Sockets;
typedef queue<Command*> Commands;
class DownloadEngine {
private:
void waitData();
vector<Socket*> rsockets;
vector<Socket*> wsockets;
Sockets rsockets;
Sockets wsockets;
void shortSleep() const;
bool addSocket(vector<Socket*>& sockets, Socket* socket);
bool deleteSocket(vector<Socket*>& sockets, Socket* socket);
bool addSocket(Sockets& sockets, Socket* socket);
bool deleteSocket(Sockets& sockets, Socket* socket);
protected:
virtual void initStatistics() = 0;
virtual void calculateStatistics() = 0;
virtual void onEndOfRun() = 0;
public:
bool noWait;
queue<Command*> commands;
Commands commands;
SegmentMan* segmentMan;
DiskWriter* diskWriter;
const Logger* logger;

View File

@ -90,8 +90,8 @@ string HttpConnection::createRequest(const Segment& segment) const {
}
string cookiesValue;
vector<Cookie> cookies = req->cookieBox->criteriaFind(req->getHost(), req->getDir(), req->getProtocol() == "https" ? true : false);
for(vector<Cookie>::const_iterator itr = cookies.begin(); itr != cookies.end(); itr++) {
Cookies cookies = req->cookieBox->criteriaFind(req->getHost(), req->getDir(), req->getProtocol() == "https" ? true : false);
for(Cookies::const_iterator itr = cookies.begin(); itr != cookies.end(); itr++) {
cookiesValue += (*itr).toString()+";";
}
if(cookiesValue.size()) {

View File

@ -39,8 +39,8 @@ string HttpHeader::getFirst(const string& name) const {
}
}
vector<string> HttpHeader::get(const string& name) const {
vector<string> v;
Strings HttpHeader::get(const string& name) const {
Strings v;
for(multimap<string, string>::const_iterator itr = table.find(name); itr != table.end(); itr++) {
v.push_back((*itr).second);
}

View File

@ -24,7 +24,7 @@
#include "common.h"
#include <map>
#include <vector>
#include <deque>
#include <string>
using namespace std;
@ -39,7 +39,7 @@ public:
void put(const string& name, const string& value);
bool defined(const string& name) const;
string getFirst(const string& name) const;
vector<string> get(const string& name) const;
Strings get(const string& name) const;
int getFirstAsInt(const string& name) const;
long long int getFirstAsLLInt(const string& name) const;
};

View File

@ -159,8 +159,8 @@ void HttpResponseCommand::createHttpDownloadCommand(string transferEncoding) {
}
void HttpResponseCommand::retrieveCookie(const HttpHeader& headers) {
vector<string> v = headers.get("Set-Cookie");
for(vector<string>::const_iterator itr = v.begin(); itr != v.end(); itr++) {
Strings v = headers.get("Set-Cookie");
for(Strings::const_iterator itr = v.begin(); itr != v.end(); itr++) {
Cookie c;
req->cookieBox->parse(c, *itr);
req->cookieBox->add(c);

View File

@ -23,11 +23,11 @@
#define _D_LIST_H_
#include "MetaEntry.h"
#include <vector>
#include <deque>
using namespace std;
typedef vector<MetaEntry*> MetaList;
typedef deque<MetaEntry*> MetaList;
class List : public MetaEntry {
private:

View File

@ -317,9 +317,9 @@ void PeerInteractionCommand::createRequestPendingMessage(int blockIndex) {
void PeerInteractionCommand::sendMessages() {
if(!Piece::isNull(piece) && !peer->peerChoking) {
if(e->torrentMan->isEndGame()) {
vector<int> missingBlockIndexes = piece.getAllMissingBlockIndexes();
BlockIndexes missingBlockIndexes = piece.getAllMissingBlockIndexes();
if(requestSlotMan->isEmpty()) {
for(vector<int>::const_iterator itr = missingBlockIndexes.begin();
for(PieceIndexes::const_iterator itr = missingBlockIndexes.begin();
itr != missingBlockIndexes.end(); itr++) {
createRequestPendingMessage(*itr);
}
@ -374,17 +374,17 @@ void PeerInteractionCommand::beforeSocketCheck() {
if(sequence == WIRED) {
e->torrentMan->unadvertisePiece(cuid);
vector<int> indexes = e->torrentMan->getAdvertisedPieceIndexes(cuid);
PieceIndexes indexes = e->torrentMan->getAdvertisedPieceIndexes(cuid);
if(indexes.size() >= 20) {
PendingMessage pendingMessage(PeerMessage::BITFIELD, peerConnection);
pendingMessages.push_back(pendingMessage);
} else {
if(pendingMessages.size() == 0) {
for(vector<int>::iterator itr = indexes.begin(); itr != indexes.end(); itr++) {
for(PieceIndexes::iterator itr = indexes.begin(); itr != indexes.end(); itr++) {
peerConnection->sendHave(*itr);
}
} else {
for(vector<int>::iterator itr = indexes.begin(); itr != indexes.end(); itr++) {
for(PieceIndexes::iterator itr = indexes.begin(); itr != indexes.end(); itr++) {
PendingMessage pendingMessage = PendingMessage::createHaveMessage(*itr, peerConnection);
pendingMessages.push_back(pendingMessage);
}

View File

@ -26,7 +26,6 @@
#include "PeerConnection.h"
#include "PendingMessage.h"
#include "RequestSlotMan.h"
#include <vector>
using namespace std;

View File

@ -24,7 +24,7 @@
#include "common.h"
#include "PeerConnection.h"
#include <vector>
#include <deque>
class PendingMessage {
private:
@ -66,6 +66,6 @@ public:
static PendingMessage createHaveMessage(int index, PeerConnection* peerConnectioin);
};
typedef vector<PendingMessage> PendingMessages;
typedef deque<PendingMessage> PendingMessages;
#endif // _D_PENDING_MESSAGE_H_

View File

@ -79,6 +79,6 @@ int Piece::getMissingUnusedBlockIndex() const {
return blockIndex;
}
vector<int> Piece::getAllMissingBlockIndexes() const {
BlockIndexes Piece::getAllMissingBlockIndexes() const {
return bitfield->getAllMissingIndexes();
}

View File

@ -45,7 +45,7 @@ public:
Piece& operator=(const Piece& piece);
int getMissingUnusedBlockIndex() const;
vector<int> getAllMissingBlockIndexes() const;
BlockIndexes getAllMissingBlockIndexes() const;
void completeBlock(int blockIndex);
void cancelBlock(int blockIndex);
int countCompleteBlock() const {

View File

@ -29,11 +29,11 @@
#include "PeerConnection.h"
#include "PendingMessage.h"
#include "TorrentMan.h"
#include <vector>
#include <deque>
#define DEFAULT_TIME_OUT 120
typedef vector<RequestSlot> RequestSlots;
typedef deque<RequestSlot> RequestSlots;
class RequestSlotMan {
private:

View File

@ -23,7 +23,7 @@
#define _D_SEGMENT_H_
#include "common.h"
#include <vector>
#include <deque>
using namespace std;
@ -45,7 +45,7 @@ typedef struct {
bool finish;
} Segment;
typedef vector<Segment> Segments;
typedef deque<Segment> Segments;
#define SEGMENT_EQUAL(X, Y) (X.cuid == Y.cuid && X.sp == Y.sp && X.ep == Y.ep && X.ds == Y.ds && X.finish == Y.finish ? true : false)

View File

@ -35,7 +35,7 @@ SegmentMan::SegmentMan():totalSize(0),isSplittable(true),downloadStarted(false),
SegmentMan::~SegmentMan() {}
void SegmentMan::unregisterId(int cuid) {
for(vector<Segment>::iterator itr = segments.begin(); itr != segments.end(); itr++) {
for(Segments::iterator itr = segments.begin(); itr != segments.end(); itr++) {
if((*itr).cuid == cuid) {
cuid = 0;
}
@ -57,7 +57,7 @@ bool SegmentMan::getSegment(Segment& seg, int cuid) {
segments.push_back(seg);
return true;
}
for(vector<Segment>::iterator itr = segments.begin(); itr != segments.end(); itr++) {
for(Segments::iterator itr = segments.begin(); itr != segments.end(); itr++) {
if((*itr).cuid == cuid && !(*itr).finish) {
// logger->debug("return an existing segment { "
// "sp = "+Util::ulitos((*itr).sp)+", "+
@ -71,7 +71,7 @@ bool SegmentMan::getSegment(Segment& seg, int cuid) {
if(!isSplittable) {
return false;
}
for(vector<Segment>::iterator itr = segments.begin(); itr != segments.end(); itr++) {
for(Segments::iterator itr = segments.begin(); itr != segments.end(); itr++) {
Segment& s = *itr;
if(s.finish) {
continue;
@ -86,7 +86,7 @@ bool SegmentMan::getSegment(Segment& seg, int cuid) {
}
void SegmentMan::updateSegment(const Segment& segment) {
for(vector<Segment>::iterator itr = segments.begin(); itr != segments.end(); itr++) {
for(Segments::iterator itr = segments.begin(); itr != segments.end(); itr++) {
if((*itr).cuid == segment.cuid &&
(*itr).sp == segment.sp &&
(*itr).ep == segment.ep) {
@ -122,7 +122,7 @@ void SegmentMan::load() {
read(segFile);
fclose(segFile);
logger->info(MSG_LOADED_SEGMENT_FILE);
for(vector<Segment>::iterator itr = segments.begin(); itr != segments.end();
for(Segments::iterator itr = segments.begin(); itr != segments.end();
itr++) {
(*itr).cuid = 0;
}
@ -138,7 +138,7 @@ void SegmentMan::save() const {
if(fwrite(&totalSize, sizeof(totalSize), 1, segFile) < 1) {
throw new DlAbortEx(strerror(errno));
}
for(vector<Segment>::const_iterator itr = segments.begin(); itr != segments.end(); itr++) {
for(Segments::const_iterator itr = segments.begin(); itr != segments.end(); itr++) {
if(fwrite(&*itr, sizeof(Segment), 1, segFile) < 1) {
throw new DlAbortEx(strerror(errno));
}
@ -187,7 +187,7 @@ bool SegmentMan::finished() const {
if(!downloadStarted || segments.size() == 0) {
return false;
}
for(vector<Segment>::const_iterator itr = segments.begin(); itr != segments.end(); itr++) {
for(Segments::const_iterator itr = segments.begin(); itr != segments.end(); itr++) {
if(!(*itr).finish) {
return false;
}
@ -203,7 +203,7 @@ void SegmentMan::removeIfFinished() const {
long long int SegmentMan::getDownloadedSize() const {
long long int size = 0;
for(vector<Segment>::const_iterator itr = segments.begin(); itr != segments.end(); itr++) {
for(Segments::const_iterator itr = segments.begin(); itr != segments.end(); itr++) {
size += (*itr).ds;
}
return size;

View File

@ -22,7 +22,6 @@
#ifndef _D_SEGMENT_MAN_H_
#define _D_SEGMENT_MAN_H_
#include <vector>
#include "common.h"
#include "Logger.h"
#include "Segment.h"

View File

@ -54,8 +54,8 @@ void ShaVisitor::visit(const Data* d) {
void ShaVisitor::visit(const Dictionary* d) {
#ifdef HAVE_LIBSSL
EVP_DigestUpdate(&ctx, "d", 1);
const vector<string>& v = d->getOrder();
for(vector<string>::const_iterator itr = v.begin(); itr != v.end(); itr++) {
const Order& v = d->getOrder();
for(Order::const_iterator itr = v.begin(); itr != v.end(); itr++) {
string lenStr = Util::llitos(itr->size());
EVP_DigestUpdate(&ctx, lenStr.c_str(), lenStr.size());
EVP_DigestUpdate(&ctx, ":", 1);

View File

@ -22,7 +22,7 @@
#include "SplitFirstSegmentSplitter.h"
bool SplitFirstSegmentSplitter::splitSegment(Segment& seg, int cuid, Segments& segments) {
for(vector<Segment>::iterator itr = segments.begin(); itr != segments.end(); itr++) {
for(Segments::iterator itr = segments.begin(); itr != segments.end(); itr++) {
Segment& s = *itr;
if(s.finish) {
continue;

View File

@ -22,8 +22,8 @@
#include "SplitSlowestSegmentSplitter.h"
bool SplitSlowestSegmentSplitter::splitSegment(Segment& seg, int cuid, Segments& segments) {
vector<Segment>::iterator slowest = segments.end();;
for(vector<Segment>::iterator itr = segments.begin(); itr != segments.end(); itr++) {
Segments::iterator slowest = segments.end();;
for(Segments::iterator itr = segments.begin(); itr != segments.end(); itr++) {
Segment& s = *itr;
if(s.finish) {
continue;

View File

@ -30,7 +30,6 @@
#include "Piece.h"
#include "Directory.h"
#include <deque>
#include <vector>
#include <map>
#include <string>
@ -59,15 +58,16 @@ public:
typedef deque<Peer*> Peers;
typedef multimap<int, int> Haves;
typedef vector<FileEntry> MultiFileEntries;
typedef deque<FileEntry> MultiFileEntries;
typedef deque<Piece> UsedPieces;
typedef deque<int> PieceIndexes;
class TorrentMan {
private:
Peers peers;
BitfieldMan* bitfield;
unsigned char infoHash[INFO_HASH_LENGTH];
vector<string> pieceHashes;
deque<string> pieceHashes;
int peerEntryIdCounter;
int cuidCounter;
long long int downloadedSize;
@ -151,8 +151,8 @@ public:
haves.insert(vt);
}
vector<int> getAdvertisedPieceIndexes(int myCuid) const {
vector<int> indexes;
PieceIndexes getAdvertisedPieceIndexes(int myCuid) const {
PieceIndexes indexes;
for(Haves::const_iterator itr = haves.begin(); itr != haves.end(); itr++) {
const Haves::value_type& have = *itr;
if(have.first == myCuid) {

View File

@ -89,7 +89,7 @@ long long int Util::difftv(struct timeval tv1, struct timeval tv2) {
tv1.tv_usec-tv2.tv_usec);
}
void Util::slice(vector<string>& result, string src, char delim) {
void Util::slice(Strings& result, string src, char delim) {
string::size_type p = 0;
while(1) {
string::size_type np = src.find(delim, p);

View File

@ -25,7 +25,7 @@
#include "common.h"
#include <string>
#include <utility>
#include <vector>
#include <deque>
#include <sys/time.h>
using namespace std;
@ -47,7 +47,7 @@ public:
* Take a string src which is a deliminated list and add its elements
* into result. result is not cleared before conversion begins.
*/
static void slice(vector<string>& result, string src, char delim);
static void slice(Strings& result, string src, char delim);
static string trim(string src);

View File

@ -27,6 +27,8 @@
#include <iostream>
#include <assert.h>
#include <limits.h>
#include <string>
#include <deque>
#if ENABLE_NLS
# include <gettext.h>
# define _(String) gettext (String)
@ -45,4 +47,6 @@
using namespace std;
typedef deque<string> Strings;
#endif // _D_COMMON_H_

View File

@ -35,7 +35,7 @@
#include "PeerListenCommand.h"
#include "TorrentAutoSaveCommand.h"
#include "SleepCommand.h"
#include <vector>
#include <deque>
#include <algorithm>
#include <time.h>
#include <signal.h>
@ -54,6 +54,8 @@ extern int optind, opterr, optopt;
using namespace std;
typedef deque<Request*> Requests;
void printDownloadCompeleteMessage(string filename) {
printf(_("\nThe download was complete. <%s>\n"), filename.c_str());
}
@ -94,7 +96,7 @@ void torrentHandler(int signal) {
exit(0);
}
void addCommand(int cuid, const char* url, string referer, vector<Request*> requests) {
void addCommand(int cuid, const char* url, string referer, Requests requests) {
Request* req = new Request();
req->setReferer(referer);
if(req->setUrl(url)) {
@ -520,7 +522,7 @@ int main(int argc, char* argv[]) {
e->segmentMan->option = op;
e->segmentMan->splitter = splitter;
vector<Request*> requests;
Requests requests;
for(int i = 1; optind+i-1 < argc; i++) {
for(int s = 1; s <= split; s++) {
addCommand(split*(i-1)+s, argv[optind+i-1], referer, requests);