blob: c0064e5241d11f03f74a75b3c281476d5bf04574 [file] [log] [blame]
Prathmesh Prabhuba99b592013-04-17 15:13:14 -07001// Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "shill/ip_address_store.h"
6
7#include <iterator>
8
9#include <stdlib.h>
10#include <time.h>
11
12using std::advance;
13
14namespace shill {
15
16// This is a less than comparison so that IPAddress can be stored in a set.
17// We do not care about a semantically meaningful comparison. This is
18// deterministic, and that's all that matters.
19bool IPAddressLTIgnorePrefix::operator () (const IPAddress &lhs,
20 const IPAddress &rhs) const {
21 return lhs.ToString() < rhs.ToString();
22}
23
24IPAddressStore::IPAddressStore() {
25 srand(time(NULL));
26}
27
28IPAddressStore::~IPAddressStore() {}
29
30void IPAddressStore::AddUnique(const IPAddress &ip) {
31 ip_addresses_.insert(ip);
32}
33
34void IPAddressStore::Clear() {
35 ip_addresses_.clear();
36}
37
38size_t IPAddressStore::Count() const {
39 return ip_addresses_.size();
40}
41
42bool IPAddressStore::Empty() const {
43 return ip_addresses_.empty();
44}
45
46IPAddress IPAddressStore::GetRandomIP() {
47 if (ip_addresses_.empty())
48 return IPAddress(IPAddress::kFamilyUnknown);
49 int index = rand() % ip_addresses_.size();
50 IPAddresses::const_iterator cit = ip_addresses_.begin();
51 advance(cit, index);
52 return *cit;
53}
54
55} // namespace shill