aboutsummaryrefslogtreecommitdiffstats
path: root/test/ccured_olden/bisort/swap.c
blob: e1e98749e3d38961e35a226721a6b033a53d16d8 (plain)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* For copyright information, see olden_v1.0/COPYRIGHT */

#define COLLECT_SIZE 256
#define DFS_SIZE 20
#include "node.h"

#ifdef SS_PLAIN
#include "ssplain.h"
#endif SS_PLAIN


typedef struct {
  int top;
  HANDLE *handles[DFS_SIZE];
} stack;

#define push(s,v) (s)->handles[++((s)->top)] = v
#define pop(s) (s)->handles[((s)->top)--]
#define stackempty(s) ((s)->top < 0)  
void VisitCollect(HANDLE *t, local int *collect)
{
  register int val;
  val = t->value;
  *collect = val;
}

void VisitCollectReplace(HANDLE *t, local int *collect)
{
  register int temp = *collect;
  register int val = t->value;
  *collect = val;
  t->value = temp;
}

void VisitReplace(HANDLE *t, local int *collect)
{
  register int val = *collect;
  t->value = val;
}

void swapDFS(local stack *s, local int collection[], void visit())
{
  int num=0;
  
  while (!stackempty(s) && num < COLLECT_SIZE) 
    {
      HANDLE *v = pop(s);
      visit(v,&collection[num]);
      num++;
      if (v->left != NULL) 
	{
     HANDLE *child;
     child = v->right;
	  push(s,child);
     child = v->left;
	  push(s,child);
	}
    }
}

void NewSwapSubTree(HANDLE *t1, HANDLE *t2)
{
  local stack c1, r1, s2;
  int collection[COLLECT_SIZE];

  /*chatting("starting swapping\n");*/

  if (t1!=NULL) 
    {
      c1.top = -1;
      r1.top = -1;
      s2.top = -1;
      push(&c1,t1);
      push(&r1,t1);
      push(&s2,t2);
      while (!stackempty(&c1)) 
	{
     MLOCAL(t1);
	  swapDFS(&c1,collection,VisitCollect);
	  MLOCAL(t2);
	  swapDFS(&s2,collection,VisitCollectReplace);
	  MLOCAL(t1);
	  swapDFS(&r1,collection,VisitReplace);
	}
    }
  /*chatting("ending swapping\n");*/

}
  
  
int *Collect(HANDLE *t1, local int collection[])
{
  register int val;
  if (t1==NULL) return collection;
  MLOCAL(t1);
  val = t1->value;
  *collection = val;
  collection += 1;
  collection = Collect(t1->left,collection);
  collection = Collect(t1->right,collection);
  return collection;
}

int *Collect_Replace(HANDLE *t1, local int collection[])
{
  register int temp,val;
  if (t1==NULL) return collection;
  MLOCAL(t1);
  temp = *collection;
  val = t1->value;
  *collection = val;
  t1->value = temp;
  collection += 1;
  collection = Collect_Replace(t1->left,collection);
  collection = Collect_Replace(t1->right,collection);
  return collection;
}

int *Replace(HANDLE *t1, local int collection[])
{
  register int val;
  if (t1==NULL) return collection;
  MLOCAL(t1);
  val = *collection;
  t1->value = val;
  collection +=1;
  collection = Replace(t1->left,collection);
  collection = Replace(t1->right,collection);
  return collection;
}


void SwapSubTree(HANDLE *t1, HANDLE *t2)
{
  int collection[COLLECT_SIZE];
  HANDLE *t1loc, *t2loc;

  MLOCAL(t1);
  Collect(t1,collection);
  MLOCAL(t2);
  Collect_Replace(t2,collection);
  MLOCAL(t1);
  Replace(t1,collection);
}