MFEM v2.0
sets.cpp
Go to the documentation of this file.
00001 // Copyright (c) 2010, Lawrence Livermore National Security, LLC. Produced at
00002 // the Lawrence Livermore National Laboratory. LLNL-CODE-443211. All Rights
00003 // reserved. See file COPYRIGHT for details.
00004 //
00005 // This file is part of the MFEM library. For more information and source code
00006 // availability see http://mfem.googlecode.com.
00007 //
00008 // MFEM is free software; you can redistribute it and/or modify it under the
00009 // terms of the GNU Lesser General Public License (as published by the Free
00010 // Software Foundation) version 2.1 dated February 1999.
00011 
00012 #include "array.hpp"
00013 #include "table.hpp"
00014 #include "sets.hpp"
00015 
00016 
00017 IntegerSet::IntegerSet(IntegerSet &s)
00018    : me(s.me.Size())
00019 {
00020    for (int i = 0; i < me.Size(); i++)
00021       me[i] = s.me[i];
00022 }
00023 
00024 
00025 int IntegerSet::operator== (IntegerSet &s)
00026 {
00027    if (me.Size() != s.me.Size())
00028       return 0;
00029 
00030    for (int i = 0; i < me.Size(); i++)
00031       if (me[i] != s.me[i])
00032          return 0;
00033 
00034    return 1;
00035 }
00036 
00037 int IntegerSet::PickRandomElement()
00038 {
00039    int i, size = me.Size();
00040    unsigned int seed = 0;
00041 
00042    for (i = 0; i < size; i++)
00043       seed += me[i];
00044 
00045    srand(seed);
00046 
00047    return me[rand()/(RAND_MAX/size)];
00048 }
00049 
00050 void IntegerSet::Recreate(const int n, const int *p)
00051 {
00052    int i, j;
00053 
00054    me.SetSize(n);
00055 
00056    for (i = 0; i < n; i++)
00057       me[i] = p[i];
00058 
00059    me.Sort();
00060 
00061    for (j = 0, i = 1; i < n; i++)
00062       if (me[i] != me[j])
00063          me[++j] = me[i];
00064 
00065    me.SetSize(j+1);
00066 }
00067 
00068 
00069 int ListOfIntegerSets::Insert(IntegerSet &s)
00070 {
00071    for (int i = 0; i < TheList.Size(); i++)
00072       if (*TheList[i] == s)
00073          return i;
00074 
00075    TheList.Append(new IntegerSet(s));
00076 
00077    return TheList.Size()-1;
00078 }
00079 
00080 int ListOfIntegerSets::Lookup(IntegerSet &s)
00081 {
00082    for (int i = 0; i < TheList.Size(); i++)
00083       if (*TheList[i] == s)
00084          return i;
00085 
00086    mfem_error("ListOfIntegerSets::Lookup ()");
00087    return -1;
00088 }
00089 
00090 void ListOfIntegerSets::AsTable(Table & t)
00091 {
00092    int i;
00093 
00094    t.MakeI(Size());
00095 
00096    for (i = 0; i < Size(); i++)
00097       t.AddColumnsInRow(i, TheList[i] -> Size());
00098 
00099    t.MakeJ();
00100 
00101    for (i = 0; i < Size(); i++)
00102    {
00103       Array<int> &row = *TheList[i];
00104       t.AddConnections(i, row.GetData(), row.Size());
00105    }
00106 
00107    t.ShiftUpI();
00108 }
00109 
00110 ListOfIntegerSets::~ListOfIntegerSets()
00111 {
00112    for (int i = 0; i < TheList.Size(); i++)
00113       delete TheList[i];
00114 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines