summaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/ext/pb_ds/example/basic_multimap.cc
blob: 1a425f6c4766736e37e954a6cf78bb2f1572b3c5 (plain)
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
// -*- C++ -*-

// Copyright (C) 2005-2019 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library 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 3, or (at your option) any later
// version.

// This library 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.

// You should have received a copy of the GNU General Public License
// along with this library; see the file COPYING3.  If not see
// <http://www.gnu.org/licenses/>.


// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.

// Permission to use, copy, modify, sell, and distribute this software
// is hereby granted without fee, provided that the above copyright
// notice appears in all copies, and that both that copyright notice
// and this permission notice appear in supporting documentation. None
// of the above authors, nor IBM Haifa Research Laboratories, make any
// representation about the suitability of this software for any
// purpose. It is provided "as is" without express or implied
// warranty.

/**
 * @file basic_multimap_example.cpp
 * A basic example showing how to use multimaps.
 */

/**
 * This example shows how to use "multimaps" in the context of a simple
 * bank account application. Each customer holds a bank account
 * (or more than one) which holds some balance.
 */

#include <iostream>
#include <string>
#include <cassert>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

// A simple hash functor.
// hash could serve instead of this functor, but it is not yet
// standard everywhere.
struct string_hash : public unary_function<string, size_t>
{
  inline size_t
  operator()(const string& r_s) const
  {
    size_t ret = 0;
    string::const_iterator b = r_s.begin();
    string::const_iterator e = r_s.end();
    while (b != e)
      {
	ret *= 5;
	ret += static_cast<size_t>(*(b++));
      }
    return ret;
  }
};

int main()
{
  // Each customer is identified by a string.
  typedef string customer;

  // Each account is identified by an unsigned long.
  typedef unsigned long account_id;

  // The balance in the account is a floating point.
  typedef float balance_t;

  /*
   *  This is the data structure type used for storing information
   *  about accounts. In this case the primary key is the customer,
   *  and the secondary key is the account id.
   *
   *  A hash-based container maps each customer to a list-based
   *  container that maps each account to the balance it holds.
   *
   *  Note that we could use any combination of primary and secondary
   *  associative-containers. In this case we choose a hash-based
   *  container for the primary keys, since we do not need to store
   *  customers in a sorted order; we choos a list-based container for
   *  the secondary keys, since we expect that the average number of
   *  accounts per customer will be small.
   */
  typedef
    cc_hash_table<
    customer,
    list_update<
    account_id,
    balance_t>,
    string_hash>
    accounts_t;

  // This object will hold all information.
  accounts_t acc;

  // Customer "a" opens empty account 12.
  acc["a"][12] = 0;

  // Customer "a" deposits 45 into account 12.
  acc["a"][12] += 45;

  // Customer "b" opens account 13 with balance 12.3.
  acc["b"][13] = 12.3;

  // Customer "c" opens empty account 14.
  acc["c"][14] = 0;

  // Customer "a" opens account 160 with balance 142.
  // Note that "a" already holds account 12.
  acc["a"][160] = 142;

  // Verify the number of accounts that "a" holds.
  accounts_t::point_const_iterator it = acc.find("a");
  assert(it != acc.end());
  assert(it->second.size() == 2);

  // The beginning of the month has arrived. We need to give a 3%
  // interest to all accounts with a positive balance.

  // First we loop over all customers.
  accounts_t::iterator cust_it;
  for (cust_it = acc.begin(); cust_it != acc.end(); ++cust_it)
    {
      // For each customer, we loop over the customer's accounts.
      accounts_t::mapped_type::iterator it;
      for (it = cust_it->second.begin(); it != cust_it->second.end(); ++it)
	if (it->second > 0)
	  it->second *= 1.03;
    }

  // Customer "a" closes all accounts.
  acc.erase("a");

  // The bank now has only 2 customers.
  assert(acc.size() == 2);

  return 0;
}