Esempio n. 1
0
int ancestor(BTNode *b,ElemType x)
{
	if (b==NULL)
		return(0);
	if (b->data==x)
		return(1);
	if (ancestor(b->lchild,x) || ancestor(b->rchild,x))
	{
		printf("%c ",b->data);
		return(1);
	}
}
void
ClientTiledPaintedLayer::GetAncestorLayers(LayerMetricsWrapper* aOutScrollAncestor,
                                          LayerMetricsWrapper* aOutDisplayPortAncestor)
{
  LayerMetricsWrapper scrollAncestor;
  LayerMetricsWrapper displayPortAncestor;
  for (LayerMetricsWrapper ancestor(this, LayerMetricsWrapper::StartAt::BOTTOM); ancestor; ancestor = ancestor.GetParent()) {
    const FrameMetrics& metrics = ancestor.Metrics();
    if (!scrollAncestor && metrics.GetScrollId() != FrameMetrics::NULL_SCROLL_ID) {
      scrollAncestor = ancestor;
    }
    if (!metrics.GetDisplayPort().IsEmpty()) {
      displayPortAncestor = ancestor;
      // Any layer that has a displayport must be scrollable, so we can break
      // here.
      break;
    }
  }
  if (aOutScrollAncestor) {
    *aOutScrollAncestor = scrollAncestor;
  }
  if (aOutDisplayPortAncestor) {
    *aOutDisplayPortAncestor = displayPortAncestor;
  }
}
int main(int argc,char** argv)
{
	//seeds random generator
	srand(time(0));

	//threads
	SYSTEM_INFO sysinfo;
	GetSystemInfo( &sysinfo );

	numCores= sysinfo.dwNumberOfProcessors;
	//numCores=1;
#ifdef _DEBUG
	printf("cores %d\n", numCores);
#endif

#ifdef  _DEBUG

	return debug(argc,argv);
	return WWD(argc,argv);
#else
	const int temp[] = {1387,38,23,2,1924};
	int size = sizeof( temp ) / sizeof ( *temp );
	std::vector<int> ancestor (temp, temp+size);
	//return pipeServerMain(numCores,populationSize,nrOfGenerations,ancestor);
	return pipeClientMain(argc,argv);
	//return WWD(argc,argv);

#endif

	return 0;
}
std::vector<int> eliminationTree::createParentVec(const int* colPtr,const int* row_ind,const int input_NumCols){

  int n = input_NumCols;
  std::vector<int> parent(n,0);
  std::vector<int> ancestor(n,0);
  
  for (int k = 0; k < n; k++){
    parent[k] = -1;
    ancestor[k] = -1;
    for (int p = colPtr[k]; p < colPtr[k+1]; p++){
      int i = row_ind[p];
      while ((i != -1) && (i < k)){
	int inext   = ancestor[i]; // Search for the root
	ancestor[i] = k;           // Update ancestor for efficiency
	if (inext == -1)           
	  parent[i] = k;           // Connect to k
	i = inext;
      }
    } 
  }

  // Correct for multiple roots
  std::vector<int> rootIdx;
  for (int i = 0; i < input_NumCols; i++)
    if (parent[i] < 0)
      rootIdx.push_back(i);
  
  for (int i = 0; i < (rootIdx.size()- 1); i++){
    parent[rootIdx[i]] = rootIdx[rootIdx.size() - 1];
  }
  //
  
  return parent;
}
Esempio n. 5
0
void union_find::do_union(INT a, INT b)
{
	INT A, B;

	A = ancestor(a);
	B = ancestor(b);
	if (A == B)
		return;
	if (A < B) {
		prev[a] = A;
		prev[b] = A;
		}
	else {
		prev[a] = B;
		prev[b] = B;
		}
}
Esempio n. 6
0
void
TiledContentHost::Composite(LayerComposite* aLayer,
                            EffectChain& aEffectChain,
                            float aOpacity,
                            const gfx::Matrix4x4& aTransform,
                            const gfx::Filter& aFilter,
                            const gfx::Rect& aClipRect,
                            const nsIntRegion* aVisibleRegion /* = nullptr */)
{
  MOZ_ASSERT(mCompositor);
  // Reduce the opacity of the low-precision buffer to make it a
  // little more subtle and less jarring. In particular, text
  // rendered at low-resolution and scaled tends to look pretty
  // heavy and this helps mitigate that. When we reduce the opacity
  // we also make sure to draw the background color behind the
  // reduced-opacity tile so that content underneath doesn't show
  // through.
  // However, in cases where the background is transparent, or the layer
  // already has some opacity, we want to skip this behaviour. Otherwise
  // we end up changing the expected overall transparency of the content,
  // and it just looks wrong.
  gfxRGBA backgroundColor(0);
  if (aOpacity == 1.0f && gfxPrefs::LowPrecisionOpacity() < 1.0f) {
    // Background colors are only stored on scrollable layers. Grab
    // the one from the nearest scrollable ancestor layer.
    for (LayerMetricsWrapper ancestor(GetLayer(), LayerMetricsWrapper::StartAt::BOTTOM); ancestor; ancestor = ancestor.GetParent()) {
      if (ancestor.Metrics().IsScrollable()) {
        backgroundColor = ancestor.Metrics().GetBackgroundColor();
        break;
      }
    }
  }
  float lowPrecisionOpacityReduction =
        (aOpacity == 1.0f && backgroundColor.a == 1.0f)
        ? gfxPrefs::LowPrecisionOpacity() : 1.0f;

  nsIntRegion tmpRegion;
  const nsIntRegion* renderRegion = aVisibleRegion;
#ifndef MOZ_IGNORE_PAINT_WILL_RESAMPLE
  if (PaintWillResample()) {
    // If we're resampling, then the texture image will contain exactly the
    // entire visible region's bounds, and we should draw it all in one quad
    // to avoid unexpected aliasing.
    tmpRegion = aVisibleRegion->GetBounds();
    renderRegion = &tmpRegion;
  }
#endif

  // Render the low and high precision buffers.
  RenderLayerBuffer(mLowPrecisionTiledBuffer,
                    lowPrecisionOpacityReduction < 1.0f ? &backgroundColor : nullptr,
                    aEffectChain, lowPrecisionOpacityReduction * aOpacity,
                    aFilter, aClipRect, *renderRegion, aTransform);
  RenderLayerBuffer(mTiledBuffer, nullptr, aEffectChain, aOpacity, aFilter,
                    aClipRect, *renderRegion, aTransform);
  mLowPrecisionTiledBuffer.ProcessDelayedUnlocks();
  mTiledBuffer.ProcessDelayedUnlocks();
}
Esempio n. 7
0
void main()
{
	BTNode *b;
	ElemType x='G';
	CreateBTNode(b,"A(B(D(,G)),C(E,F))");
	printf("b:");DispBTNode(b);printf("\n");
	printf("结点%c的所有祖先:",x);
	ancestor(b,'G');printf("\n");
}
Esempio n. 8
0
void union_find::print()
{
	INT i, j;
	
	cout << "i : ancestor(i) : prev[i]" << endl;
	for (i = 0; i < A->degree; i++) {
		j = ancestor(i);
		cout << setw(4) << i << " : " << setw(4) << j << " : " << setw(4) << i << endl;
		}
}
Esempio n. 9
0
static struct label* apportion(struct layout_ctx* ctx, struct label* node, 
                               struct label* default_ancestor) {
    if (!node->prev_sibling) {
        return default_ancestor;
    }
    struct label *inner_right_node = node;
    struct label *outer_right_node = node;
    struct label *inner_left_node = node->prev_sibling;
    struct label *outer_left_node = inner_right_node->parent->first_child;
    double shift_inner_right = inner_right_node->modifier;
    double shift_outer_right = outer_right_node->modifier;
    double shift_inner_left = inner_left_node->modifier;
    double shift_outer_left = outer_left_node->modifier;

    while (next_right(inner_left_node) && 
           next_left(inner_right_node)) {

        inner_left_node = next_right(inner_left_node);
        inner_right_node = next_left(inner_right_node);
        outer_left_node = next_left(outer_left_node);
        outer_right_node = next_right(outer_right_node);
        outer_right_node->ancestor = node;
        double shift = spacing(ctx, inner_left_node,inner_right_node,false)
            + inner_left_node->xcoord + shift_inner_left
            - (inner_right_node->xcoord + shift_inner_right);

        if (shift > 0) {
            struct label *a = ancestor(inner_left_node,node, default_ancestor);
            move_subtree(a, node, shift);
            shift_inner_right += shift;
            shift_outer_right += shift;
        }
        shift_inner_left += inner_left_node->modifier;
        shift_inner_right += inner_right_node->modifier;
        shift_outer_left += outer_left_node->modifier;
        shift_outer_right += outer_right_node->modifier;
    }
    if (next_right(inner_left_node) && 
        next_right(outer_right_node) == 0) {

        outer_right_node->thread = next_right(inner_left_node);
        outer_right_node->modifier += shift_inner_left - shift_outer_right;
    } else {
        if (next_left(inner_right_node) && 
            next_left(outer_left_node) == 0) {

            outer_left_node->thread = next_left(inner_right_node);
            outer_left_node->modifier += shift_inner_right - shift_outer_left;
        }
        default_ancestor = node;
    }

    return default_ancestor;
}
Esempio n. 10
0
		size_t TypeSystem::com(size_t l, size_t r, const impl::Value& op) {
			if (l == r) return l;																// Avoid memoizing ancestor(l, l) == l

			// Shortcut for table and Nil
			if (l == Traits<Table>::id || r == Traits<Table>::id) return Traits<Table>::id;		// com(Table, x, x) = Table
			if (l == Traits<Nil>::id || r == Traits<Nil>::id) return Traits<Nil>::id;			// com(Nil, x, x) = Nil (?)
			
			// Try for direct conversion
			auto idx = key(l, r);
			if (conv.count(idx) > 0)								// If there is a defined conversion, give the highest precedence
				return conv[idx][0];

			// Otherwise, give a common ancestor
			return siblings.count(idx) == 0 ? siblings[idx] = ancestor(l, r) : siblings[idx];
		}
int debug(int argc,char** argv){
	test();
	const int temp[] = {0, 7306, 77, 9859, 8996, 0, 7615, 0, 0, 1641, 8007, 0, 1666, 1098, 3253, 0, 5908, 6549, 7241, 8392, 2533, 8392, 7810, 150, 0, 8248, 1100, 0, 0, 0, 300, 350, 300
		, 2324, 4627, 1393, 457, 4736, 0, 819, 0, 289, 0, 0, 2872, 5034, 1, 539, 2160, 3399, 9902, 1094, 6143, 100, 0, 4660, 5670, 400, 1214, 0, 5202, 0, 6629, 5316, 0,
		8392, 7810, 1519, 4743, 0, 513, 3589, 1790, 25, 1433, 5552, 0, 8695, 3, 9352, 1413, 9324, 1, 25, 3278, 0, 0, 0, 6020, 0, 1071, 2400, 3678, 1641, 8007, 0, 4650,
		1098, 1801, 1211, 2465, 6549, 6435, 1471, 3032, 2598, 100, 3005, 9326, 150, 1,25, 3278, 0, 4710, 9276, 3266, 4386, 0, 0, 7340, 0, 0, 3559, 9407, 0, 4710, 6038
		, 3187, 9961, 7280, 1460, 7224, 7744, 200, 5723, 2289, 0, 3948, 9274, 0, 573, 0, 8815, 3264, 0, 3809, 0, 0, 0};
	int size = sizeof( temp ) / sizeof ( *temp );
	std::vector<int> ancestor (temp, temp+size);
	Physics* WWDPhysics = new Physics();
	//init creature
	readDNA(&ancestor,WWDPhysics);
	//WWDPhysics->testPhysics();
	//default glut doesn't return from mainloop
	WWDPhysics->solveGroundConflicts();
	return glutmain(argc, argv,1024,600,"Walking with dinosaurs",WWDPhysics);
}
Esempio n. 12
0
int Application::exec()
{
  bool done = false;
  while (!done)
  {
    SDL_Event e;
    if (SDL_WaitEvent(&e))
    {
      switch (e.type)
      {
      case SDL_WINDOWEVENT:
        {
          Widget *w = widgetByWindowId(e.window.windowID);
          switch (e.window.event)
          {
          case SDL_WINDOWEVENT_SHOWN:
            std::cout << "Window " << e.window.windowID << " shown" << std::endl;
            break;
          case SDL_WINDOWEVENT_HIDDEN:
            std::cout << "Window " << e.window.windowID << " hidden" << std::endl;
            break;
          case SDL_WINDOWEVENT_EXPOSED:
            {
              needUpdateWithoutRedraw_ = w;
              break;
            }
          case SDL_WINDOWEVENT_MOVED:
            break;
          case SDL_WINDOWEVENT_RESIZED:
            {
              w->resize(e.window.data1, e.window.data2);
#if __APPLE__==1
              SDL_RenderPresent(w->renderer_); // hack for MacOS X
#endif
              break;
            }
          case SDL_WINDOWEVENT_MINIMIZED:
            std::cout << "Window " << e.window.windowID << " minimized" << std::endl;
            break;
          case SDL_WINDOWEVENT_MAXIMIZED:
            std::cout << "Window " << e.window.windowID << " maximized" << std::endl;
            break;
          case SDL_WINDOWEVENT_RESTORED:
            std::cout << "Window " << e.window.windowID << " restored" << std::endl;
            break;
          case SDL_WINDOWEVENT_ENTER:
            std::cout << "Mouse entered window " << e.window.windowID << std::endl;
            break;
          case SDL_WINDOWEVENT_LEAVE:
            std::cout << "Mouse left window " << e.window.windowID << std::endl;
            break;
          case SDL_WINDOWEVENT_FOCUS_GAINED:
            std::cout << "Window " << e.window.windowID << " gained keyboard focus" << std::endl;
            break;
          case SDL_WINDOWEVENT_FOCUS_LOST:
            std::cout << "Window " << e.window.windowID << " lost keyboard focus" << std::endl;
            break;
          case SDL_WINDOWEVENT_CLOSE:
            std::cout << "Window " << e.window.windowID << " closed" << std::endl;
            break;
          default:
            std::cout << "Window " << e.window.windowID << " got unknown event " << static_cast<int>(e.window.event) << std::endl;
            break;
          }
          break;
        }
      case SDL_KEYDOWN:
        {
          KeyEvent ke { static_cast<KeyEvent::Key>(e.key.keysym.sym), SDL_GetModState(), static_cast<bool>(e.key.repeat) };
          auto w = focusWidget();
          if (!w)
            w = widgetByWindowId(e.key.windowID);
          else if (w->ancestor() != widgetByWindowId(e.key.windowID))
          {
            std::cerr << "Unknown windowID " << e.key.windowID << std::endl;
            break;
          }
          while (w)
          {
            if (w->keyPressEvent(ke))
              break;
            w = w->parent();
          }
          break;
        }
      case SDL_KEYUP:
        {
          KeyEvent ke { static_cast<KeyEvent::Key>(e.key.keysym.sym), SDL_GetModState(), static_cast<bool>(e.key.repeat) };
          auto w = focusWidget();
          if (!w)
            w = widgetByWindowId(e.key.windowID);
          else if (w->ancestor() != widgetByWindowId(e.key.windowID))
          {
            std::cerr << "Unknown windowID " << e.key.windowID << std::endl;
            break;
          }

          while (w)
          {
            if (w->keyReleaseEvent(ke))
              break;
            w = w->parent();
          }
          break;
        }
      case SDL_TEXTINPUT:
        {
          TextInputEvent tie { toUtf16(e.text.text) };
          auto w = focusWidget();
          if (!w)
            w = widgetByWindowId(e.key.windowID);
          else if (w->ancestor() != widgetByWindowId(e.key.windowID))
          {
            std::cerr << "Unknown windowID " << e.key.windowID << std::endl;
            break;
          }

          while (w)
          {
            if (w->textInputEvent(tie))
              break;
            w = w->parent();
          }
          break;
        }
      case SDL_QUIT:
        done = true;
        break;
      }
    }
    const auto isEmpty = (SDL_PeepEvents(&e, 1, SDL_PEEKEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT) == 0);
    if (isEmpty || SDL_GetTicks() > lastUpdate_ + 1000 / 60)
    {
      auto x = SDL_GetTicks();
      for (auto w: widgetList_)
        if (w->needRepaint())
        {
          PaintEvent e;
          w->internalPaint(e);
        }
      if (needUpdateWithoutRedraw_)
      {
        needUpdateWithoutRedraw_->updateWithoutRedraw();
        needUpdateWithoutRedraw_ = nullptr;
      }
      lastUpdate_ = SDL_GetTicks();
      std::cout << "Update time: " << lastUpdate_ - x << " " <<
        (lastUpdate_ - x > 0 ? 1000 / (lastUpdate_ - x) : 999)<< "fps" << std::endl;
    }
    for (auto obj: deletingObjects_)
      delete obj;
    deletingObjects_.clear();
  }
  return 0;
}
int WWD(int argc,char** argv){
	HANDLE *handles =new HANDLE[numCores];

	const int temp[] = {
		//Body
		295,195,95,	//main box: h,w,d
		1,		//1 box attached
		1,/*Joint type*/		50,50,5,/*preXYS*/		50,50,4,/*postXYS*/	45,45,45,/*DofXYZ*/ //Create joint
		295,195,95,	//attached box: h,w,d
		//NN-Effector0-layer 0
		1,	2,10,0,1,100,100,	//2-in-node: f=interpolate, in0=0,in1=1,w0=100,w1=100
		0,	2,0,0,0,0,100,		//2-in-node: f=sum,in0=0,in1=1,w0=0,w1=100 (just sends in1 through unchanged) - No more nodes
		1,0,1,	//NN-Effector: use outputs O0=1,O1=0,O2=1
		0,		//no attached boxes
		//NN-main-layer 0	(sensors not implemented yet)
		1,	0,30,			//constant node 30
		1,	0,62,			//constant node 62
		2,	0,125,			//constant node 125 - change layer
		//NN-main-layer 1
		1,	2,0,0,1,50,23,	//2-in-node: f=sum in0=0 in1=1 w0=50 w1=23
		2,	1,12,2,5,		//1-in node: f=cos in0=2 w0=5 - change layer
		//NN-main-layer 2
		1,	2,1,0,1,1,200,	//2-in node: f=product in0=0 in1=1, w0=1 w1=200
		0,	2,0,0,1,1,1		//2-in node: f=sum in0=0 in1=1, w0=1 w1=1 - No more nodes
	};
	int size = sizeof( temp ) / sizeof ( *temp );
	std::vector<int> ancestor (temp, temp+size);

	std::vector<creature>* creatures = new std::vector<creature>();

	//Creates one ancestor and the rest is mutation of the ancestor
	creatures->push_back(creature());
	creatures->at(0).dna=ancestor;
	creatures->at(0).fitness=0;
	for(int i=1; i<populationSize;i++){
		creatures->push_back(creature());
		creatures->at(i).dna=mutate(ancestor,2);
		creatures->at(i).fitness=0;
	}

	for(int i=0;i<nrOfGenerations;i++){
		//#pragma omp parallel
		{
			//run simulator
			//#pragma omp for schedule(dynamic)
			for(int j =0; j<populationSize; j++){
				//init world
				Physics* WWDPhysics = new Physics();

				//init creature
				readDNA(&creatures->at(j).dna,WWDPhysics);

				//run sim
				WWDPhysics->runSimulation();
				creatures->at(j).fitness = WWDPhysics->getFitness();
				creatures->at(j).treePointer = getMTree(&creatures->at(j).dna);
				delete WWDPhysics;
			}

			/*
			//threads
			for(int j =0; j<numCores; j++){
			handles[j] = (HANDLE)_beginthreadex(0, 0, &threadSim,(void*) j, 0, 0);
			}

			WaitForMultipleObjects(numCores, handles, true,INFINITE);

			for(int j =0; j<numCores; j++){
			CloseHandle(handles[j]);
			} */

			//print all unsorted
			/*for(int j=0;j< (int) worlds.size();j++){
			printf("nr %d %f\n",j,worlds.at(j)->getFitness());
			}*/

			//mutate
			//#pragma omp single nowait
			evolve(creatures); //Evolve cleans up the MTrees so no need for that here
			//print survivors sorted

			//#pragma omp for ordered
			for(int j=0;j< (int) (creatures->size()/5.f);j++){
#ifdef _DEBUG
				printf("nr %d %f\n",j,creatures->at(j).fitness);
#endif
			}
			//#pragma omp for ordered
#ifdef _DEBUG
			printf("round %d \n",i);
#endif
		}
	}

	for (int i = 0; i < (int) creatures->at(0).dna.size(); i++){
#ifdef _DEBUG
		printf("%d,", creatures->at(0).dna.at(i));
#endif
	}

	//Show end result if we want to...
	Physics* WWDPhysics = new Physics();
	//init creature
	readDNA(&creatures->at(0).dna,WWDPhysics);
	//default glut doesn't return from mainloop

	WWDPhysics->calcSize();
	WWDPhysics->solveGroundConflicts();
#ifdef _DEBUG
	printf("\nPress enter to continue\n");
#endif
	getchar();
	return glutmain(argc, argv,1024,600,"Walking with dinosaurs",WWDPhysics);
}
void loadDefault(){
	int temp[] = {300,350,300,0,		
		4,//nrAttachedToMain
		//leg1
		0,0,0,0,0,0,0,0,0,0, // nn
		0,0,0, //effector
		0,75,0,2,0,0,3,100,0,10, //joint
		100,400,100,0, //box+sensor
		0,//nrAttachedTo
		
		//leg2
		0,0,0,0,0,0,0,0,0,
		0,0,0,0,
		0,25,0,2,0,0,3,100,0,10,
		100,400,100,0,
		0, //nrAttachedTo
		//Tail1
		0,0,0,0,0,0,0,0,0,0,
		0,0,0,
		0,0,0,5,0,0,0,100,0,5,
		150,150,400,0,
		1, //nrAttachedTo
		//Tail2
		0,0,0,0,0,0,0,0,0,0,
		0,0,0,
		0,0,0,5,0,0,0,100,100,5,
		130,130,300,0,
		0, //nrAttachedTo
		//UpperBody
		0,0,0,0,0,0,0,0,0,0,
		0,0,0,
		0,0,0,3,0,0,2,0,0,0,
		300,350,300,0,
		3, //nrAttachedTo
		//Head
		0,0,0,0,0,0,0,0,0,0,
		0,0,0,
		0,0,0,3,0,0,2,20,20,20,
		230,230,230,0,
		0, //nrAttachedTo
		//arm1
		0,0,0,0,0,0,0,0,0,0,
		0,0,0,
		0,25,40,0,0,0,5,50,50,50,
		70,70,200,0,
		0, //nrAttachedTo
		//arm2
		0,0,0,0,0,0,0,0,0,0,
		0,0,0,
		0,75,40,0,0,0,5,50,50,50,
		70,70,200,0,
		0 //nrAttachedTo
	};
	int size = sizeof( temp ) / sizeof ( *temp );
	std::vector<int> ancestor (temp, temp+size);

	save* tmp = new save();
	tmp->dna=ancestor;
	tmp->name="DefDino";

	saves.push_back(tmp);

	

	int temp4[] = {300, 350, 300};
	int size4 = sizeof( temp4 ) / sizeof ( *temp4 );
	std::vector<int> ancestor4 (temp4, temp4+size4);

	save* tmp4 = new save();
	tmp4->dna=ancestor4;
	tmp4->name="block";

	saves.push_back(tmp4);
}