File size: 3,478 Bytes
158b61b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// $Id$

/***********************************************************************
 Moses - factored phrase-based language decoder
 Copyright (C) 2006 University of Edinburgh

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, 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
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 ***********************************************************************/

#include <boost/version.hpp>
#ifdef WITH_THREADS
#include <boost/thread/locks.hpp>
#endif
#include <ostream>
#include <string>
#include "FactorCollection.h"
#include "util/pool.hh"
#include "util/exception.hh"
#include "../System.h"

using namespace std;

namespace Moses2
{

const Factor *FactorCollection::AddFactor(const StringPiece &factorString,
    const System &system, bool isNonTerminal)
{
  FactorFriend to_ins;
  to_ins.in.m_string = factorString;
  to_ins.in.m_id = (isNonTerminal) ? m_factorIdNonTerminal : m_factorId;
  Set & set = (isNonTerminal) ? m_set : m_setNonTerminal;
  // If we're threaded, hope a read-only lock is sufficient.
#ifdef WITH_THREADS
  {
    // read=lock scope
    boost::shared_lock<boost::shared_mutex> read_lock(m_accessLock);
    Set::const_iterator i = set.find(to_ins);
    if (i != set.end()) return &i->in;
  }
  boost::unique_lock<boost::shared_mutex> lock(m_accessLock);
#endif // WITH_THREADS
  std::pair<Set::iterator, bool> ret(set.insert(to_ins));
  if (ret.second) {
    ret.first->in.m_string.set(
      memcpy(m_string_backing.Allocate(factorString.size()),
             factorString.data(), factorString.size()), factorString.size());
    if (isNonTerminal) {
      m_factorIdNonTerminal++;
      UTIL_THROW_IF2(m_factorIdNonTerminal >= moses_MaxNumNonterminals,
                     "Number of non-terminals exceeds maximum size reserved. Adjust parameter moses_MaxNumNonterminals, then recompile");
    } else {
      m_factorId++;
    }
  }

  const Factor *factor = &ret.first->in;

  return factor;
}

const Factor *FactorCollection::GetFactor(const StringPiece &factorString,
    bool isNonTerminal)
{
  FactorFriend to_find;
  to_find.in.m_string = factorString;
  to_find.in.m_id = (isNonTerminal) ? m_factorIdNonTerminal : m_factorId;
  Set & set = (isNonTerminal) ? m_set : m_setNonTerminal;
  {
    // read=lock scope
#ifdef WITH_THREADS
    boost::shared_lock<boost::shared_mutex> read_lock(m_accessLock);
#endif // WITH_THREADS
    Set::const_iterator i = set.find(to_find);
    if (i != set.end()) return &i->in;
  }
  return NULL;
}

FactorCollection::~FactorCollection()
{
}

// friend
ostream& operator<<(ostream& out, const FactorCollection& factorCollection)
{
#ifdef WITH_THREADS
  boost::shared_lock<boost::shared_mutex> lock(factorCollection.m_accessLock);
#endif
  for (FactorCollection::Set::const_iterator i = factorCollection.m_set.begin();
       i != factorCollection.m_set.end(); ++i) {
    out << i->in;
  }
  return out;
}

}