ROOT logo
// $Id: ZHashList.cxx 2256 2009-11-21 23:24:25Z matevz $

// Copyright (C) 1999-2008, Matevz Tadel. All rights reserved.
// This file is part of GLED, released under GNU General Public License version 2.
// For the licensing terms see $GLEDSYS/LICENSE or http://www.gnu.org/.

//__________________________________________________________________________
// ZHashList
//
//

#include "ZHashList.h"
#include "ZHashList.c7"

ClassImp(ZHashList);

/**************************************************************************/

void ZHashList::_init()
{
  bNerdyListOps = true;
}

/**************************************************************************/

void ZHashList::new_element_check(ZGlass* lens)
{
  static const Exc_t _eh("ZHashList::new_element_check ");

  hpLens2Iter_i i = mItHash.find(lens);
  if (i != mItHash.end())
    throw _eh + "lens " + lens->Identify() + " already in the list.";

  PARENT_GLASS::new_element_check(lens);
}

void ZHashList::clear_list()
{
  PARENT_GLASS::clear_list();
  mItHash.clear();
}

/**************************************************************************/

Int_t ZHashList::remove_references_to(ZGlass* lens)
{
  Int_t n = ZGlass::remove_references_to(lens);

  GMutexHolder llck(mListMutex);
  hpLens2Iter_i i = mItHash.find(lens);
  if (i != mItHash.end())
  {
    mElements.erase(i->second);
    mItHash.erase(i);
    --mSize;
    StampListRemove(lens);
    ++n;
  }
  return n;
}

/**************************************************************************/

void ZHashList::on_insert(ZList::iterator it)
{
  mItHash[it.lens()] = it;
}

void ZHashList::on_remove(ZList::iterator it)
{
  hpLens2Iter_i i = mItHash.find(it.lens());
  assert(i != mItHash.end());
  mItHash.erase(i);
}

void ZHashList::on_rebuild()
{
  mItHash.clear();
  for (ZList::iterator i=begin(); i!=end(); ++i)
    mItHash[i.lens()] = i;
}

/**************************************************************************/

Bool_t ZHashList::Has(ZGlass* lens)
{
  GMutexHolder llck(mListMutex);
  hpLens2Iter_i i = mItHash.find(lens);
  return (i != mItHash.end());
}

/**************************************************************************/

Int_t ZHashList::RemoveAll(ZGlass* lens)
{
  GMutexHolder llck(mListMutex);
  hpLens2Iter_i i = mItHash.find(lens);
  if (i != mItHash.end())
  {
    mElements.erase(i->second);
    mItHash.erase(i);
    --mSize;
    lens->DecRefCount(this);
    StampListRemove(lens);
    return 1;
  }
  else
  {
    return 0;
  }
}

/**************************************************************************/

void ZHashList::Insert(ZGlass* lens, ZGlass* before)
{
  static const Exc_t _eh("ZHashList::Insert ");

  GMutexHolder llck(mListMutex);
  new_element_check(lens);
  hpLens2Iter_i i = mItHash.find(before);
  if (i == mItHash.end())
    throw _eh + "before-lens " + before->Identify() + " not found in the list.";
  lens->IncRefCount(this);
  mItHash[lens] = mElements.insert(i->second, element(lens, mNextId));
  ++mSize;
  StampListInsert(lens, mNextId, before);
  ++mNextId;
}

void ZHashList::Remove(ZGlass* lens)
{
  static const Exc_t _eh("ZHashList::Remove ");

  GMutexHolder llck(mListMutex);
  hpLens2Iter_i i = mItHash.find(lens);
  if (i == mItHash.end())
    throw _eh + "lens " + lens->Identify() + " not found in the list.";
  mElements.erase(i->second);
  mItHash.erase(i);
  --mSize;
  lens->DecRefCount(this);
  StampListRemove(lens);
}

void ZHashList::MoveToFront(ZGlass* lens)
{
  static const Exc_t _eh("ZHashList::MoveToFront ");

  GMutexHolder llck(mListMutex);
  hpLens2Iter_i i = mItHash.find(lens);
  if(i == mItHash.end())
    throw _eh + "lens " + lens->Identify() + " not found in the list.";
  mElements.erase(i->second);
  mItHash.erase(i);
  StampListRemove(lens);
  mItHash[lens] = mElements.insert(begin(), element(lens, mNextId));
  StampListPushFront(lens, mNextId);
  ++mNextId;
}

void ZHashList::MoveToBack(ZGlass* lens)
{
  static const Exc_t _eh("ZHashList::MoveToBack ");

  GMutexHolder llck(mListMutex);
  hpLens2Iter_i i = mItHash.find(lens);
  if(i == mItHash.end())
    throw _eh + "lens " + lens->Identify() + " not found in the list.";
  ZList::iterator l = i->second;
  mItHash.erase(i);
  mElements.erase(l);
  StampListRemove(lens);
  mItHash[lens] = mElements.insert(end(), element(lens, mNextId));
  StampListPushBack(lens, mNextId);
  ++mNextId;
}

/**************************************************************************/
/**************************************************************************/

ZGlass* ZHashList::ElementAfter(ZGlass* lens)
{
  ZGlass* ret = 0;
  { GMutexHolder llck(mListMutex);
    hpLens2Iter_i i = mItHash.find(lens);
    if (i != mItHash.end())
    {
      ZList::iterator j(i->second);
      if(++j != end())
	ret = j.lens();
    }
  }
  ZMIR* mir = get_MIR();
  if (mir && mir->HasResultReq())
  {
    TBufferFile b(TBuffer::kWrite);
    GledNS::WriteLensID(b, ret);
    mSaturn->ShootMIRResult(b);
  }
  return ret;
}

ZGlass* ZHashList::ElementBefore(ZGlass* lens)
{
  ZGlass* ret = 0;
  { GMutexHolder llck(mListMutex);
    hpLens2Iter_i i = mItHash.find(lens);
    if(i != mItHash.end()) {
      ZList::iterator j = i->second;
      if(j != begin())
	ret = (--j).lens();
    }
  }
  ZMIR* mir = get_MIR();
  if (mir && mir->HasResultReq())
  {
    TBufferFile b(TBuffer::kWrite);
    GledNS::WriteLensID(b, ret);
    mSaturn->ShootMIRResult(b);
  }
  return ret;
}

/**************************************************************************/
 ZHashList.cxx:1
 ZHashList.cxx:2
 ZHashList.cxx:3
 ZHashList.cxx:4
 ZHashList.cxx:5
 ZHashList.cxx:6
 ZHashList.cxx:7
 ZHashList.cxx:8
 ZHashList.cxx:9
 ZHashList.cxx:10
 ZHashList.cxx:11
 ZHashList.cxx:12
 ZHashList.cxx:13
 ZHashList.cxx:14
 ZHashList.cxx:15
 ZHashList.cxx:16
 ZHashList.cxx:17
 ZHashList.cxx:18
 ZHashList.cxx:19
 ZHashList.cxx:20
 ZHashList.cxx:21
 ZHashList.cxx:22
 ZHashList.cxx:23
 ZHashList.cxx:24
 ZHashList.cxx:25
 ZHashList.cxx:26
 ZHashList.cxx:27
 ZHashList.cxx:28
 ZHashList.cxx:29
 ZHashList.cxx:30
 ZHashList.cxx:31
 ZHashList.cxx:32
 ZHashList.cxx:33
 ZHashList.cxx:34
 ZHashList.cxx:35
 ZHashList.cxx:36
 ZHashList.cxx:37
 ZHashList.cxx:38
 ZHashList.cxx:39
 ZHashList.cxx:40
 ZHashList.cxx:41
 ZHashList.cxx:42
 ZHashList.cxx:43
 ZHashList.cxx:44
 ZHashList.cxx:45
 ZHashList.cxx:46
 ZHashList.cxx:47
 ZHashList.cxx:48
 ZHashList.cxx:49
 ZHashList.cxx:50
 ZHashList.cxx:51
 ZHashList.cxx:52
 ZHashList.cxx:53
 ZHashList.cxx:54
 ZHashList.cxx:55
 ZHashList.cxx:56
 ZHashList.cxx:57
 ZHashList.cxx:58
 ZHashList.cxx:59
 ZHashList.cxx:60
 ZHashList.cxx:61
 ZHashList.cxx:62
 ZHashList.cxx:63
 ZHashList.cxx:64
 ZHashList.cxx:65
 ZHashList.cxx:66
 ZHashList.cxx:67
 ZHashList.cxx:68
 ZHashList.cxx:69
 ZHashList.cxx:70
 ZHashList.cxx:71
 ZHashList.cxx:72
 ZHashList.cxx:73
 ZHashList.cxx:74
 ZHashList.cxx:75
 ZHashList.cxx:76
 ZHashList.cxx:77
 ZHashList.cxx:78
 ZHashList.cxx:79
 ZHashList.cxx:80
 ZHashList.cxx:81
 ZHashList.cxx:82
 ZHashList.cxx:83
 ZHashList.cxx:84
 ZHashList.cxx:85
 ZHashList.cxx:86
 ZHashList.cxx:87
 ZHashList.cxx:88
 ZHashList.cxx:89
 ZHashList.cxx:90
 ZHashList.cxx:91
 ZHashList.cxx:92
 ZHashList.cxx:93
 ZHashList.cxx:94
 ZHashList.cxx:95
 ZHashList.cxx:96
 ZHashList.cxx:97
 ZHashList.cxx:98
 ZHashList.cxx:99
 ZHashList.cxx:100
 ZHashList.cxx:101
 ZHashList.cxx:102
 ZHashList.cxx:103
 ZHashList.cxx:104
 ZHashList.cxx:105
 ZHashList.cxx:106
 ZHashList.cxx:107
 ZHashList.cxx:108
 ZHashList.cxx:109
 ZHashList.cxx:110
 ZHashList.cxx:111
 ZHashList.cxx:112
 ZHashList.cxx:113
 ZHashList.cxx:114
 ZHashList.cxx:115
 ZHashList.cxx:116
 ZHashList.cxx:117
 ZHashList.cxx:118
 ZHashList.cxx:119
 ZHashList.cxx:120
 ZHashList.cxx:121
 ZHashList.cxx:122
 ZHashList.cxx:123
 ZHashList.cxx:124
 ZHashList.cxx:125
 ZHashList.cxx:126
 ZHashList.cxx:127
 ZHashList.cxx:128
 ZHashList.cxx:129
 ZHashList.cxx:130
 ZHashList.cxx:131
 ZHashList.cxx:132
 ZHashList.cxx:133
 ZHashList.cxx:134
 ZHashList.cxx:135
 ZHashList.cxx:136
 ZHashList.cxx:137
 ZHashList.cxx:138
 ZHashList.cxx:139
 ZHashList.cxx:140
 ZHashList.cxx:141
 ZHashList.cxx:142
 ZHashList.cxx:143
 ZHashList.cxx:144
 ZHashList.cxx:145
 ZHashList.cxx:146
 ZHashList.cxx:147
 ZHashList.cxx:148
 ZHashList.cxx:149
 ZHashList.cxx:150
 ZHashList.cxx:151
 ZHashList.cxx:152
 ZHashList.cxx:153
 ZHashList.cxx:154
 ZHashList.cxx:155
 ZHashList.cxx:156
 ZHashList.cxx:157
 ZHashList.cxx:158
 ZHashList.cxx:159
 ZHashList.cxx:160
 ZHashList.cxx:161
 ZHashList.cxx:162
 ZHashList.cxx:163
 ZHashList.cxx:164
 ZHashList.cxx:165
 ZHashList.cxx:166
 ZHashList.cxx:167
 ZHashList.cxx:168
 ZHashList.cxx:169
 ZHashList.cxx:170
 ZHashList.cxx:171
 ZHashList.cxx:172
 ZHashList.cxx:173
 ZHashList.cxx:174
 ZHashList.cxx:175
 ZHashList.cxx:176
 ZHashList.cxx:177
 ZHashList.cxx:178
 ZHashList.cxx:179
 ZHashList.cxx:180
 ZHashList.cxx:181
 ZHashList.cxx:182
 ZHashList.cxx:183
 ZHashList.cxx:184
 ZHashList.cxx:185
 ZHashList.cxx:186
 ZHashList.cxx:187
 ZHashList.cxx:188
 ZHashList.cxx:189
 ZHashList.cxx:190
 ZHashList.cxx:191
 ZHashList.cxx:192
 ZHashList.cxx:193
 ZHashList.cxx:194
 ZHashList.cxx:195
 ZHashList.cxx:196
 ZHashList.cxx:197
 ZHashList.cxx:198
 ZHashList.cxx:199
 ZHashList.cxx:200
 ZHashList.cxx:201
 ZHashList.cxx:202
 ZHashList.cxx:203
 ZHashList.cxx:204
 ZHashList.cxx:205
 ZHashList.cxx:206
 ZHashList.cxx:207
 ZHashList.cxx:208
 ZHashList.cxx:209
 ZHashList.cxx:210
 ZHashList.cxx:211
 ZHashList.cxx:212
 ZHashList.cxx:213
 ZHashList.cxx:214
 ZHashList.cxx:215
 ZHashList.cxx:216
 ZHashList.cxx:217
 ZHashList.cxx:218
 ZHashList.cxx:219
 ZHashList.cxx:220
 ZHashList.cxx:221
 ZHashList.cxx:222
 ZHashList.cxx:223
 ZHashList.cxx:224
 ZHashList.cxx:225