1 /*
2 * Copyright (c) 2001-2014 Stephen Williams (steve@icarus.com)
3 *
4 * This source code is free software; you can redistribute it
5 * and/or modify it in source code form under the terms of the GNU
6 * General Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19
20 # include "config.h"
21 # include "HName.h"
22 # include <iostream>
23 # include <cstring>
24 # include <cstdlib>
25
26 using namespace std;
27
hname_t()28 hname_t::hname_t()
29 {
30 }
31
hname_t(perm_string text)32 hname_t::hname_t(perm_string text)
33 : name_(text)
34 {
35 }
36
hname_t(perm_string text,int num)37 hname_t::hname_t(perm_string text, int num)
38 : name_(text), number_(1)
39 {
40 number_[0] = num;
41 }
42
hname_t(perm_string text,const vector<int> & nums)43 hname_t::hname_t(perm_string text, const vector<int>&nums)
44 : name_(text), number_(nums)
45 {
46 }
47
hname_t(const hname_t & that)48 hname_t::hname_t(const hname_t&that)
49 : name_(that.name_), number_(that.number_)
50 {
51 }
52
operator =(const hname_t & that)53 hname_t& hname_t::operator = (const hname_t&that)
54 {
55 name_ = that.name_;
56 number_ = that.number_;
57 return *this;
58 }
59
operator <(const hname_t & r) const60 bool hname_t::operator < (const hname_t&r) const
61 {
62 int cmp = strcmp(name_, r.name_);
63 if (cmp < 0) return true;
64 if (cmp > 0) return false;
65
66 // The text parts are equal, so compare then number
67 // parts. Finish as soon as we find one to be less or more
68 // than the other.
69 size_t idx = 0;
70 while (number_.size() > idx || r.number_.size() > idx) {
71
72 // Ran out of l numbers, so less.
73 if (number_.size() <= idx)
74 return true;
75
76 // Ran out of r numbers, so greater.
77 if (r.number_.size() <= idx)
78 return false;
79
80 if (number_[idx] < r.number_[idx])
81 return true;
82
83 if (number_[idx] > r.number_[idx])
84 return false;
85
86 idx += 1;
87 }
88
89 // Fall-through means that we are equal, including all the
90 // number parts, so not less.
91 return false;
92 }
93
operator ==(const hname_t & r) const94 bool hname_t::operator == (const hname_t&r) const
95 {
96 if (name_ == r.name_) {
97 if (number_.size() != r.number_.size())
98 return false;
99
100 for (size_t idx = 0 ; idx < number_.size() ; idx += 1)
101 if (number_[idx] != r.number_[idx]) return false;
102
103 return true;
104 }
105
106 return false;
107 }
108
operator <<(ostream & out,const hname_t & that)109 ostream& operator<< (ostream&out, const hname_t&that)
110 {
111 if (that.peek_name() == 0) {
112 out << "";
113 return out;
114 }
115
116 out << that.peek_name();
117 for (size_t idx = 0 ; idx < that.number_.size() ; idx += 1)
118 out << "[" << that.number_[idx] << "]";
119
120 return out;
121 }
122