blob: 5dd6c5df381a9ae91b45fb5dda78c09b72a28b7b [file] [log] [blame]
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07002/**
3 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -08005 *
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -08008 *
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07009 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
11 *
12 * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -080013 */
14
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080015#include "common.hpp"
16
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080017#include "exclude.hpp"
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -080018
Alexander Afanasyev3aeeaeb2014-04-22 23:34:23 -070019namespace ndn {
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -080020
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070021Exclude::Exclude()
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -080022{
23}
24
25// example: ANY /b /d ANY /f
26//
27// ordered in map as:
28//
29// /f (false); /d (true); /b (false); / (true)
30//
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070031// lower_bound(/) -> / (true) <-- excluded (equal)
32// lower_bound(/a) -> / (true) <-- excluded (any)
33// lower_bound(/b) -> /b (false) <--- excluded (equal)
34// lower_bound(/c) -> /b (false) <--- not excluded (not equal and no ANY)
35// lower_bound(/d) -> /d (true) <- excluded
36// lower_bound(/e) -> /d (true) <- excluded
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -080037bool
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070038Exclude::isExcluded(const name::Component& comp) const
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -080039{
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070040 const_iterator lowerBound = m_exclude.lower_bound(comp);
41 if (lowerBound == end())
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -080042 return false;
43
44 if (lowerBound->second)
45 return true;
46 else
47 return lowerBound->first == comp;
48
49 return false;
50}
51
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070052Exclude&
53Exclude::excludeOne(const name::Component& comp)
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -080054{
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070055 if (!isExcluded(comp))
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -080056 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070057 m_exclude.insert(std::make_pair(comp, false));
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -080058 }
59 return *this;
60}
61
62
63// example: ANY /b0 /d0 ANY /f0
64//
65// ordered in map as:
66//
67// /f0 (false); /d0 (true); /b0 (false); / (true)
68//
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070069// lower_bound(/) -> / (true) <-- excluded (equal)
70// lower_bound(/a0) -> / (true) <-- excluded (any)
71// lower_bound(/b0) -> /b0 (false) <--- excluded (equal)
72// lower_bound(/c0) -> /b0 (false) <--- not excluded (not equal and no ANY)
73// lower_bound(/d0) -> /d0 (true) <- excluded
74// lower_bound(/e0) -> /d0 (true) <- excluded
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -080075
76
77// examples with desired outcomes
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070078// excludeRange(/, /f0) -> ANY /f0
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -080079// /f0 (false); / (true)
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070080// excludeRange(/, /f1) -> ANY /f1
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -080081// /f1 (false); / (true)
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070082// excludeRange(/a0, /e0) -> ANY /f0
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -080083// /f0 (false); / (true)
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070084// excludeRange(/a0, /e0) -> ANY /f0
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -080085// /f0 (false); / (true)
86
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070087// excludeRange(/b1, /c0) -> ANY /b0 /b1 ANY /c0 /d0 ANY /f0
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -080088// /f0 (false); /d0 (true); /c0 (false); /b1 (true); /b0 (false); / (true)
89
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070090Exclude&
91Exclude::excludeRange(const name::Component& from, const name::Component& to)
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -080092{
Alexander Afanasyev3aeeaeb2014-04-22 23:34:23 -070093 if (from >= to) {
94 throw Error("Invalid exclude range [" +
95 from.toEscapedString() + ", " +
96 to.toEscapedString() +
97 "] (for single name exclude use Exclude::excludeOne)");
98 }
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -080099
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700100 iterator newFrom = m_exclude.lower_bound(from);
Alexander Afanasyev3aeeaeb2014-04-22 23:34:23 -0700101 if (newFrom == end() || !newFrom->second /*without ANY*/) {
102 std::pair<iterator, bool> fromResult = m_exclude.insert(std::make_pair(from, true));
103 newFrom = fromResult.first;
104 if (!fromResult.second) {
105 // this means that the lower bound is equal to the item itself. So, just update ANY flag
106 newFrom->second = true;
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800107 }
Alexander Afanasyev3aeeaeb2014-04-22 23:34:23 -0700108 }
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800109 // else
110 // nothing special if start of the range already exists with ANY flag set
111
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700112 iterator newTo = m_exclude.lower_bound(to); // !newTo cannot be end()
Alexander Afanasyev3aeeaeb2014-04-22 23:34:23 -0700113 if (newTo == newFrom || !newTo->second) {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700114 std::pair<iterator, bool> toResult = m_exclude.insert(std::make_pair(to, false));
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800115 newTo = toResult.first;
116 ++ newTo;
Alexander Afanasyev3aeeaeb2014-04-22 23:34:23 -0700117 }
118 // else
119 // nothing to do really
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800120
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700121 m_exclude.erase(newTo, newFrom); // remove any intermediate node, since all of the are excluded
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800122
123 return *this;
124}
125
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700126Exclude&
127Exclude::excludeAfter(const name::Component& from)
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800128{
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700129 iterator newFrom = m_exclude.lower_bound(from);
Alexander Afanasyev3aeeaeb2014-04-22 23:34:23 -0700130 if (newFrom == end() || !newFrom->second /*without ANY*/) {
131 std::pair<iterator, bool> fromResult = m_exclude.insert(std::make_pair(from, true));
132 newFrom = fromResult.first;
133 if (!fromResult.second) {
134 // this means that the lower bound is equal to the item itself. So, just update ANY flag
135 newFrom->second = true;
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800136 }
Alexander Afanasyev3aeeaeb2014-04-22 23:34:23 -0700137 }
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800138 // else
139 // nothing special if start of the range already exists with ANY flag set
140
Alexander Afanasyev3aeeaeb2014-04-22 23:34:23 -0700141 if (newFrom != m_exclude.begin()) {
142 // remove any intermediate node, since all of the are excluded
143 m_exclude.erase(m_exclude.begin(), newFrom);
144 }
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800145
146 return *this;
147}
148
149
150std::ostream&
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700151operator<<(std::ostream& os, const Exclude& exclude)
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800152{
153 bool empty = true;
Alexander Afanasyev3aeeaeb2014-04-22 23:34:23 -0700154 for (Exclude::const_reverse_iterator i = exclude.rbegin(); i != exclude.rend(); i++) {
155 if (!i->first.empty()) {
156 if (!empty) os << ",";
157 os << i->first.toEscapedString();
158 empty = false;
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800159 }
Alexander Afanasyev3aeeaeb2014-04-22 23:34:23 -0700160 if (i->second) {
161 if (!empty) os << ",";
162 os << "*";
163 empty = false;
164 }
165 }
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800166 return os;
167}
168
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800169
Alexander Afanasyev3aeeaeb2014-04-22 23:34:23 -0700170} // namespace ndn