123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- #include "tokenset.h"
- /**
- * This file is part of uhferret.
- *
- * Author:: Peter Lane
- * Copyright:: Copyright 2011, Peter Lane.
- * License:: GPLv3
- *
- * uhferret 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 of the License, or
- * (at your option) any later version.
- *
- * uhferret 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 uhferret. If not, see <http://www.gnu.org/licenses/>.
- */
- Token::Token ()
- : _capacity (2), _top (0)
- {
- _token = new char [_capacity];
- }
- Token::~Token ()
- {
- delete[] _token;
- }
- void Token::Erase ()
- {
- _top = 0;
- }
- void Token::AddChar (char c)
- {
- if (_top == _capacity)
- Grow ();
- _token [_top] = c;
- ++_top;
- }
- std::string Token::GetString () const
- {
- return std::string (_token, _top);
- }
- int Token::GetLength () const
- {
- return _top;
- }
- void Token::Grow ()
- {
- char * newtoken = new char [2 * _capacity];
- for (int i = 0; i < _capacity; ++i)
- newtoken[i] = _token[i];
- _capacity = 2 * _capacity;
- delete _token;
- _token = newtoken;
- }
- // *** TokenSet
- TokenSet::TokenSet ()
- : _nextindex (0)
- {}
- std::size_t TokenSet::GetIndexFor (std::string token)
- {
- _tokens_it = _tokens.find (token);
- if (_tokens_it != _tokens.end()) // found it
- return _tokens_it->second;
- else // otherwise, make a new index
- {
- _tokens[token] = _nextindex;
- _strings[_nextindex] = token;
- _nextindex++;
- return _nextindex-1;
- }
- }
- std::string TokenSet::GetStringFor (std::size_t token)
- {
- _strings_it = _strings.find (token);
- assert (_strings_it != _strings.end ()); // it's an error if token not in token set
- return std::string (_strings_it->second.c_str ());
- }
- void TokenSet::Clear ()
- {
- _tokens.clear ();
- _strings.clear ();
- _nextindex = 0;
- }
- void TokenSet::SetNextIndex (int index)
- {
- _nextindex = index;
- }
- void TokenSet::SetIndexString (std::string token, int index)
- {
- _strings[index] = token;
- _tokens[token] = index;
- }
|