Author: baby-guest
Date: 2007-08-23 21:13:53 +0000 (Thu, 23 Aug 2007)
New Revision: 3797

Added:
   software/ui/src/taghandler.cpp
   software/ui/src/taghandler.h
   software/ui/src/taghandler_test.cpp
Log:
Started skeleton for tag filtering



Added: software/ui/src/taghandler.cpp
===================================================================
--- software/ui/src/taghandler.cpp                              (rev 0)
+++ software/ui/src/taghandler.cpp      2007-08-23 21:13:53 UTC (rev 3797)
@@ -0,0 +1,70 @@
+/*
+ * Copyright (C) 2007  Miriam Ruiz <[EMAIL PROTECTED]>
+ *
+ * This program 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 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#include "taghandler.h"
+
+#include <iostream>
+
+FilterTagHandler::FilterTagHandler() : current_position(0), current_flag(1)
+{
+}
+
+FilterTagHandler::~FilterTagHandler()
+{
+}
+
+const FilterTagHandler::Element *FilterTagHandler::GetTag(const std::string 
name)
+{
+       for (std::vector<Element>::const_iterator i = elements.begin();
+                       i != elements.end(); ++i)
+       { if (i->name == name) return &(*i); }
+       return NULL;
+}
+
+void FilterTagHandler::AddTag(const std::string name)
+{
+       for (std::vector<Element>::const_iterator i = elements.begin();
+                       i != elements.end(); ++i)
+       { if (i->name == name) return; }
+
+       Element tag(name, current_position, current_flag);
+       current_flag = current_flag << 1;
+       if (!current_flag)
+       { current_position++; current_flag=1; }
+       elements.push_back(tag);
+}
+
+void FilterTagHandler::PrintAll()
+{
+       for (std::vector<Element>::const_iterator i = elements.begin();
+                       i != elements.end(); ++i)
+       {
+               std::cerr << i->name << " (" << i->position << ":" << i->flag 
<< ")" << std::endl;
+       }
+}
+
+void FilterTagHandler::Print(Result *result)
+{
+       for (std::vector<Element>::const_iterator i = elements.begin();
+                       i != elements.end(); ++i)
+       {
+               if (result->CheckAny(i->position, i->flag))
+                       std::cerr << i->name << " ";
+       }
+       std::cerr << std::endl;
+}

Added: software/ui/src/taghandler.h
===================================================================
--- software/ui/src/taghandler.h                                (rev 0)
+++ software/ui/src/taghandler.h        2007-08-23 21:13:53 UTC (rev 3797)
@@ -0,0 +1,163 @@
+/*
+ * Copyright (C) 2007  Miriam Ruiz <[EMAIL PROTECTED]>
+ *
+ * This program 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 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#ifndef _games_taghandler_h
+#define _games_taghandler_h
+
+#include <string>
+#include <vector>
+#include <iostream>
+
+class FilterTagHandler
+{
+protected:
+       class Element
+       {
+       public:
+               inline Element(const std::string tag_name, unsigned int 
tag_position, unsigned int tag_flag)
+               { name = tag_name; position = tag_position; flag = tag_flag; }
+
+               std::string name;
+               unsigned int position;
+               unsigned int flag;
+       };
+
+public:
+       class Result
+       {
+       public:
+               inline Result()
+               { size = 1; data = new unsigned int[size]; for (unsigned int 
i=0; i<size; i++) data[i] = 0; }
+
+               ~Result()
+               { if (data) delete[] data; }
+
+               inline void Clear()
+               {
+                       for (unsigned int i=0; i<size; i++) data[i] = 0;
+               }
+
+               inline void SetTag(unsigned int position, unsigned int flag)
+               {
+                       Size(position+1);
+                       data[position] = data[position] | flag;
+               }
+
+               inline void ResetTag(unsigned int position, unsigned int flag)
+               {
+                       Size(position+1);
+                       data[position] = data[position] & ~flag;
+               }
+
+               inline bool CheckAny(unsigned int position, unsigned int flag)
+               {
+                       Size(position+1);
+                       if ((data[position] & flag) != 0) return true;
+                       return false;
+               }
+
+               inline bool CheckAll(unsigned int position, unsigned int flag)
+               {
+                       Size(position+1);
+                       if ((data[position] & flag) == flag) return true;
+                       return false;
+               }
+
+               bool CompareAny(const Result &red)
+               {
+                       unsigned int min_size = size;
+                       if (min_size > red.size) min_size = red.size;
+                       for (unsigned int i=0; i<min_size; i++)
+                               if ((data[i] & red.data[i]) != 0) return true;
+                       return false;
+               }
+
+               bool CompareAll(const Result &green)
+               {
+                       unsigned int min_size = size;
+                       if (min_size > green.size) min_size = green.size;
+                       for (unsigned int i=0; i<min_size; i++)
+                               if ((data[i] & green.data[i]) != green.data[i]) 
return false;
+                       if (size >= green.size) return true;
+                       for (unsigned int j=size; j<green.size; j++)
+                               if (green.data[j] != 0) return false;
+                       return true;
+               }
+
+               void Print()
+               {
+                       for (unsigned int i=0; i<size; i++)
+                               std::cerr << data[i] << " " ;
+                       std::cerr << std::endl;
+               }
+
+       protected:
+               void Size(unsigned int new_size)
+               {
+                       if (new_size > size)
+                       {
+                               unsigned int *new_data = new unsigned 
int[new_size];
+                               for (unsigned int i=0; i<new_size; i++) 
new_data[i] = 0;
+                               if (data)
+                               {
+                                       for (unsigned int i=0; i<size; i++) 
new_data[i] = data[i];
+                                       delete[] data;
+                               }
+                               data = new_data;
+                               size = new_size;
+                       }
+               }
+
+               unsigned int size;
+               unsigned int *data;
+       };
+
+public:
+       FilterTagHandler();
+       ~FilterTagHandler();
+
+       const Element *GetTag(const std::string name);
+       void AddTag(const std::string name);
+       void PrintAll();
+       void Print(Result *result);
+
+       inline void Clear()
+       { elements.clear(); current_position=0; current_flag=1; };
+
+       inline void SetTag(Result *result, const std::string name)
+       {
+               AddTag(name);
+               const Element *e = GetTag(name);
+               result->SetTag(e->position, e->flag);
+       }
+
+       inline void ResetTag(Result *result, const std::string name)
+       {
+               AddTag(name);
+               const Element *e = GetTag(name);
+               result->ResetTag(e->position, e->flag);
+       }
+
+protected:
+       std::vector<Element> elements;
+
+       unsigned int current_position;
+       unsigned int current_flag;
+};
+
+#endif

Added: software/ui/src/taghandler_test.cpp
===================================================================
--- software/ui/src/taghandler_test.cpp                         (rev 0)
+++ software/ui/src/taghandler_test.cpp 2007-08-23 21:13:53 UTC (rev 3797)
@@ -0,0 +1,70 @@
+#include "taghandler.h"
+
+#include <iostream>
+#include <sstream>
+#include <string>
+
+int main(int argc, const char* argv[])
+{
+       FilterTagHandler tags;
+       for (unsigned int i=0; i<70; i++)
+       {
+               tags.AddTag("A");
+               tags.AddTag("B");
+               tags.AddTag("C");
+               tags.AddTag("D");
+               tags.AddTag("E");
+               tags.AddTag("F");
+               tags.AddTag("G");
+               tags.AddTag("H");
+               tags.AddTag("I");
+               tags.AddTag("J");
+               tags.AddTag("K");
+               tags.AddTag("L");
+               tags.AddTag("LL");
+               tags.AddTag("M");
+               tags.AddTag("N");
+               tags.AddTag("O");
+               tags.AddTag("P");
+               tags.AddTag("Q");
+               tags.AddTag("R");
+               tags.AddTag("S");
+               tags.AddTag("T");
+               tags.AddTag("U");
+               tags.AddTag("V");
+               tags.AddTag("W");
+               tags.AddTag("X");
+               tags.AddTag("Y");
+               tags.AddTag("Z");
+               tags.AddTag("0");
+               tags.AddTag("1");
+               tags.AddTag("2");
+               tags.AddTag("3");
+               tags.AddTag("4");
+               tags.AddTag("5");
+               tags.AddTag("6");
+               tags.AddTag("7");
+               tags.AddTag("8");
+               tags.AddTag("9");
+               tags.AddTag("10");
+       }
+       tags.PrintAll();
+
+
+       FilterTagHandler::Result t1, t2, t3, t4, t5, t6, t7;
+       tags.SetTag(&t1, "A"); tags.SetTag(&t1, "7"); tags.SetTag(&t1, "MIRY");
+       t1.Print();
+       tags.Print(&t1);
+       t1.Print();
+       tags.SetTag(&t2, "A"); tags.SetTag(&t2, "7"); tags.SetTag(&t2, "MIRY");
+       tags.SetTag(&t3, "A"); tags.SetTag(&t3, "8"); tags.SetTag(&t3, "MIRY");
+       tags.SetTag(&t4, "B"); tags.SetTag(&t4, "C");
+       if (t1.CompareAny(t2)) { std::cerr << "Any: t1 == t2" << std::endl; } 
else { std::cerr << "Any: t1 != t2" << std::endl; }
+       if (t1.CompareAny(t3)) { std::cerr << "Any: t1 == t3" << std::endl; } 
else { std::cerr << "Any: t1 != t3" << std::endl; }
+       if (t1.CompareAny(t4)) { std::cerr << "Any: t1 == t4" << std::endl; } 
else { std::cerr << "Any: t1 != t4" << std::endl; }
+       if (t1.CompareAll(t2)) { std::cerr << "All: t1 == t2" << std::endl; } 
else { std::cerr << "All: t1 != t2" << std::endl; }
+       if (t1.CompareAll(t3)) { std::cerr << "All: t1 == t3" << std::endl; } 
else { std::cerr << "All: t1 != t3" << std::endl; }
+       if (t1.CompareAll(t4)) { std::cerr << "All: t1 == t4" << std::endl; } 
else { std::cerr << "All: t1 != t4" << std::endl; }
+
+       return 0;
+}


_______________________________________________
Pkg-games-commits mailing list
[email protected]
http://lists.alioth.debian.org/mailman/listinfo/pkg-games-commits

Reply via email to