~ubuntu-branches/ubuntu/wily/libtorrent/wily-proposed

« back to all changes in this revision

Viewing changes to src/dht/dht_hash_map.h

  • Committer: Bazaar Package Importer
  • Author(s): Rogério Brito
  • Date: 2011-03-20 01:06:18 UTC
  • mfrom: (1.1.13 upstream) (4.1.9 sid)
  • Revision ID: james.westby@ubuntu.com-20110320010618-g3wyylccqzqko73c
Tags: 0.12.7-5
* Use Steinar's "real" patch for IPv6. Addresses #490277, #618275,
  and Closes: #617791.
* Adapt libtorrent-0.12.6-ipv6-07.patch. It FTBFS otherwise.
* Add proper attibution to the IPv6 patch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// libTorrent - BitTorrent library
 
2
// Copyright (C) 2005-2007, Jari Sundell
 
3
//
 
4
// This program is free software; you can redistribute it and/or modify
 
5
// it under the terms of the GNU General Public License as published by
 
6
// the Free Software Foundation; either version 2 of the License, or
 
7
// (at your option) any later version.
 
8
// 
 
9
// This program is distributed in the hope that it will be useful,
 
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
// GNU General Public License for more details.
 
13
// 
 
14
// You should have received a copy of the GNU General Public License
 
15
// along with this program; if not, write to the Free Software
 
16
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
//
 
18
// In addition, as a special exception, the copyright holders give
 
19
// permission to link the code of portions of this program with the
 
20
// OpenSSL library under certain conditions as described in each
 
21
// individual source file, and distribute linked combinations
 
22
// including the two.
 
23
//
 
24
// You must obey the GNU General Public License in all respects for
 
25
// all of the code used other than OpenSSL.  If you modify file(s)
 
26
// with this exception, you may extend this exception to your version
 
27
// of the file(s), but you are not obligated to do so.  If you do not
 
28
// wish to do so, delete this exception statement from your version.
 
29
// If you delete this exception statement from all source files in the
 
30
// program, then also delete it here.
 
31
//
 
32
// Contact:  Jari Sundell <jaris@ifi.uio.no>
 
33
//
 
34
//           Skomakerveien 33
 
35
//           3185 Skoppum, NORWAY
 
36
 
 
37
#ifndef LIBTORRENT_DHT_HASH_MAP_H
 
38
#define LIBTORRENT_DHT_HASH_MAP_H
 
39
 
 
40
#include "config.h"
 
41
 
 
42
#if HAVE_TR1
 
43
#include <tr1/unordered_map>
 
44
#else
 
45
#include <map>
 
46
#endif
 
47
 
 
48
#include "torrent/hash_string.h"
 
49
 
 
50
#include "dht_node.h"
 
51
#include "dht_tracker.h"
 
52
 
 
53
namespace torrent {
 
54
 
 
55
#if HAVE_TR1
 
56
// Hash functions for HashString keys, and dereferencing HashString pointers.
 
57
 
 
58
// Since the first few bits are very similar if not identical (since the IDs
 
59
// will be close to our own node ID), we use an offset of 64 bits in the hash
 
60
// string. These bits will be uniformly distributed until the number of DHT
 
61
// nodes on the planet approaches 2^64 which is... unlikely.
 
62
// An offset of 64 bits provides 96 significant bits which is fine as long as
 
63
// the size of size_t does not exceed 12 bytes, while still having correctly
 
64
// aligned 64-bit access.
 
65
static const unsigned int hashstring_hash_ofs = 8;
 
66
 
 
67
struct hashstring_ptr_hash : public std::unary_function<const HashString*, size_t> {
 
68
  size_t operator () (const HashString* n) const 
 
69
  { return *(size_t*)(n->data() + hashstring_hash_ofs); }
 
70
};
 
71
 
 
72
struct hashstring_hash : public std::unary_function<HashString, size_t> {
 
73
  size_t operator () (const HashString& n) const 
 
74
  { return *(size_t*)(n.data() + hashstring_hash_ofs); }
 
75
};
 
76
 
 
77
// Compare HashString pointers by dereferencing them.
 
78
struct hashstring_ptr_equal : public std::binary_function<const HashString*, const HashString*, bool> {
 
79
  size_t operator () (const HashString* one, const HashString* two) const 
 
80
  { return *one == *two; }
 
81
};
 
82
 
 
83
class DhtNodeList : public std::tr1::unordered_map<const HashString*, DhtNode*, hashstring_ptr_hash, hashstring_ptr_equal> {
 
84
public:
 
85
  typedef std::tr1::unordered_map<const HashString*, DhtNode*, hashstring_ptr_hash, hashstring_ptr_equal> base_type;
 
86
 
 
87
  // Define accessor iterator with more convenient access to the key and
 
88
  // element values.  Allows changing the map definition more easily if needed.
 
89
  template<typename T>
 
90
  struct accessor_wrapper : public T {
 
91
    accessor_wrapper(const T& itr) : T(itr) { }
 
92
 
 
93
    const HashString&    id() const    { return *(**this).first; }
 
94
    DhtNode*             node() const  { return (**this).second; }
 
95
  };
 
96
 
 
97
  typedef accessor_wrapper<const_iterator>  const_accessor;
 
98
  typedef accessor_wrapper<iterator>        accessor;
 
99
 
 
100
  DhtNode*            add_node(DhtNode* n);
 
101
 
 
102
};
 
103
 
 
104
class DhtTrackerList : public std::tr1::unordered_map<HashString, DhtTracker*, hashstring_hash> {
 
105
public:
 
106
  typedef std::tr1::unordered_map<HashString, DhtTracker*, hashstring_hash> base_type;
 
107
 
 
108
  template<typename T>
 
109
  struct accessor_wrapper : public T {
 
110
    accessor_wrapper(const T& itr) : T(itr) { }
 
111
 
 
112
    const HashString&    id() const       { return (**this).first; }
 
113
    DhtTracker*          tracker() const  { return (**this).second; }
 
114
  };
 
115
 
 
116
  typedef accessor_wrapper<const_iterator>  const_accessor;
 
117
  typedef accessor_wrapper<iterator>        accessor;
 
118
 
 
119
};
 
120
 
 
121
#else
 
122
 
 
123
// Compare HashString pointers by dereferencing them.
 
124
struct hashstring_ptr_less : public std::binary_function<const HashString*, const HashString*, bool> {
 
125
  size_t operator () (const HashString* one, const HashString* two) const 
 
126
  { return *one < *two; }
 
127
};
 
128
 
 
129
class DhtNodeList : public std::map<const HashString*, DhtNode*, hashstring_ptr_less> {
 
130
public:
 
131
  typedef std::map<const HashString*, DhtNode*, hashstring_ptr_less> base_type;
 
132
 
 
133
  // Define accessor iterator with more convenient access to the key and
 
134
  // element values.  Allows changing the map definition more easily if needed.
 
135
  template<typename T>
 
136
  struct accessor_wrapper : public T {
 
137
    accessor_wrapper(const T& itr) : T(itr) { }
 
138
 
 
139
    const HashString&    id() const    { return *(**this).first; }
 
140
    DhtNode*             node() const  { return (**this).second; }
 
141
  };
 
142
 
 
143
  typedef accessor_wrapper<const_iterator>  const_accessor;
 
144
  typedef accessor_wrapper<iterator>        accessor;
 
145
 
 
146
  DhtNode*            add_node(DhtNode* n);
 
147
 
 
148
};
 
149
 
 
150
class DhtTrackerList : public std::map<HashString, DhtTracker*> {
 
151
public:
 
152
  typedef std::map<HashString, DhtTracker*> base_type;
 
153
 
 
154
  template<typename T>
 
155
  struct accessor_wrapper : public T {
 
156
    accessor_wrapper(const T& itr) : T(itr) { }
 
157
 
 
158
    const HashString&    id() const       { return (**this).first; }
 
159
    DhtTracker*          tracker() const  { return (**this).second; }
 
160
  };
 
161
 
 
162
  typedef accessor_wrapper<const_iterator>  const_accessor;
 
163
  typedef accessor_wrapper<iterator>        accessor;
 
164
 
 
165
};
 
166
#endif // HAVE_TR1
 
167
 
 
168
inline
 
169
DhtNode* DhtNodeList::add_node(DhtNode* n) {
 
170
  insert(std::make_pair<const HashString*, DhtNode*>(n, n));
 
171
  return n;
 
172
}
 
173
 
 
174
}
 
175
 
 
176
#endif