Пример #1
0
int searchElement(int *array,int left,int right,int element)
{
    int middle = (left + right) / 2;

    if(array[left] == element)
         return left;
    else if(array[middle] == element)             //if duplicate values are entered in array then minimum position of that value should be returned
         return middle;
    else if(array[right] == element)
        return right;
    else if(left - right == 1)
        return -1;

    if(array[left] <=array[middle])
    {
        if(element <= array[middle] && element >= array[left])
             return searchElement(array, left, middle, element);
        else
             return searchElement(array, middle, right, element);
    }
    else if(array[middle] <= array[right])
    {
        if(element >= array[middle] && element <= array[right])
            return searchElement(array, middle,right, element);
        else
            return searchElement(array, left, middle, element);
    }
    else
        return -1;
}
Пример #2
0
size_t searchElement (double const& val, size_t beg, size_t end, const std::vector<double*>& array)
{
	if (beg >= end)
		return std::numeric_limits<size_t>::max();
	size_t m ((end + beg) / 2);

	if (*(array[m]) - val <= 0 && *(array[m + 1]) - val > 0)
		return m;
	if (val < *(array[m]))
		return searchElement (val, beg, m, array);
	return searchElement (val, m + 1, end, array);
}
Пример #3
0
size_t searchElement (double const& val, size_t beg, size_t end, const std::vector<double>& array)
{
	if (beg >= end) return std::numeric_limits<size_t>::max();
	size_t m ((end+beg)/2);

	if (array[m] - val < 0 && array[m+1] - val > 0) {
		return m;
	}
	if (val < array[m]) {
		return searchElement (val, beg, m, array);
	}
	return searchElement (val, m+1, end, array);
}
Пример #4
0
int main(int argc, char *argv[]) {
    // if the user doesn't supply atleast 3 parameters then it displays an error message and stops execution
    if(argc < 3) {
        printf("Atleast 3 parameters are required!\n");
        exit(0);
        // if the user supplies more than 3 parameters and one of them is -c then it searches the search element in the files
    } else if(argc > 3 && strcmp(argv[1], "-c") == 0) {
        searchElement(argc, argv, true, false);
        // if the user supplies more than 3 parameters and one of them is -L then it searches the search element in the files and adds the line number
    } else if(argc > 3 && strcmp(argv[1], "-L") == 0) {
        searchElement(argc, argv, false, true);
        // if the user supplies more than 3 parameters and it does not consist of -c it searches the search element in the files
    } else {
        searchElement(argc, argv, false, false);
    }
}
Пример #5
0
int main()
{
  int array[100],i,direction,len,rot,element;
  printf("\nEnter size of array:");
  scanf_s("%d",&len);
  printf("\nEnter Array:\n");
  for(i=0;i<len;i++)
        scanf_s("%d",&array[i],sizeof(array));

  printf("Enter no of rotations:");
  scanf_s("%d",&rot);

  printf("\nEnter clockwise(0) or anticlockwise(1):");
  scanf_s("%d",&direction);

  printf("\nEnter search element:");
  scanf_s("%d",&element);

  if(rot%len==0){                        //if number of rotations is multiple of array length then array will be as it is given
       display(array,len);
       searchElement(array,0,len-1,element);
  }
  else
       rotateArray(array,len,rot,direction,element);
  _getch;
}
Пример #6
0
/************************************************
 The <Filename> element is the most basic matching rule.
 It matches a desktop entry if the desktop entry has the given desktop-file id
 ************************************************/
void XdgMenuLayoutProcessor::processFilenameTag(const QDomElement &element)
{
    QString id = element.text();

    QDomElement appLink = searchElement(QLatin1String("AppLink"), QLatin1String("id"), id);
    if (!appLink.isNull())
        mResult.appendChild(appLink);
}
Пример #7
0
void ModifyMeshProperties::substituteMaterialID(GEOLIB::Polygon const& polygon, size_t old_mat_id, size_t new_mat_id)
{
	MeshLib::ExtractMeshNodes mesh_node_extractor(_mesh);
	std::vector<size_t> mesh_node_ids;
	mesh_node_extractor.getMeshNodeIDsWithinPolygon(polygon, mesh_node_ids);
	const size_t n_mesh_node_ids(mesh_node_ids.size());
	std::vector<size_t> perm(n_mesh_node_ids);
	// init permutation for sorting
	for (size_t k(0); k < n_mesh_node_ids; k++)
		perm[k] = k;
	// sort - since we want to use binary search
	Quicksort<size_t>(mesh_node_ids, 0, n_mesh_node_ids, perm);

	//#ifndef NDEBUG
	//	std::ofstream test_out ("Points.gli");
	//	test_out << "#POINTS" << std::endl;
	//	FileIO::OGSMeshIO mesh_io;
	//	mesh_io.setMesh(_mesh);
	//	mesh_io.writeMeshNodesAsGLIPnts(mesh_node_ids, test_out);
	//	test_out << "#STOP" << std::endl;
	//#endif

	// get all nodes of the mesh
	const std::vector<MeshLib::CNode*>& mesh_nodes(_mesh->getNodeVector());
	// get all elements of the mesh
	const std::vector<MeshLib::CElem*>& mesh_elements(_mesh->getElementVector());

	for (size_t k(0); k < n_mesh_node_ids; k++)
	{
		std::vector<size_t> const& connected_element_ids(mesh_nodes[mesh_node_ids[k]]->getConnectedElementIDs());
		for (size_t j(0); j < connected_element_ids.size(); j++)
		{
			if (mesh_elements[connected_element_ids[j]]->GetPatchIndex() == old_mat_id)
			{
				std::vector<size_t> connected_nodes;
				// check if all nodes of element are in the mesh_node_ids vector
				mesh_elements[connected_element_ids[j]]->getNodeIndices(connected_nodes);
				bool all_found(true);
				const size_t n_connected_nodes(connected_nodes.size());
				for (size_t i(0); i < n_connected_nodes && all_found; i++)
				{
					if (searchElement(connected_nodes[i], 0, n_mesh_node_ids, mesh_node_ids)
					    == std::numeric_limits<size_t>::max())
					{
						all_found = false;
					}
				}
				if (all_found)
				{
					mesh_elements[connected_element_ids[j]]->setPatchIndex(new_mat_id);
				}
			}
		}
	}
}
Пример #8
0
// search in list
Student* searchElement(Node* element, char* name)
{
	if (element)
	{
		if (strcmp(element->info->name, name) == 0)
			return element->info;
		else
			return searchElement(element->next,name);
	}
	else
		return 0;
}
	List<T>& SortedList<T>::removeElement(T element)
	{
	    int index,i;
	    index = searchElement(element);
	    if(index != 0)
        {
            for(i = index ; i < this->numberOfElement() - 1 ; ++i)
                this->list[i] = this->list[i + 1];
                --this->used;

        }
       	 return *this;
	}
    bool myList<T>::removeElement(T data){
        int index=searchElement(data);
        if(index!=-1){
            T* tmpTPtr=new T(_numOfElements-1);
            int i,j;

            for(i=0,j=0;i<_numOfElements;++i){
                if(i!=index){
                    tmpTPtr[j]=_tPtr[i];
                    ++j;
                }
            }
            delete [] _tPtr;
            _tPtr=tmpTPtr;

            return true;
        }
        else
            return false;
    }
std::string SearchPayloadSerializer::serializePayload(boost::shared_ptr<SearchPayload> searchPayload)	const {
    XMLElement searchElement("query", "jabber:iq:search");

    if (searchPayload->getInstructions()) {
        searchElement.addNode(XMLElement::ref(new XMLElement("instructions", "", *searchPayload->getInstructions())));
    }

    if (searchPayload->getNick()) {
        searchElement.addNode(XMLElement::ref(new XMLElement("nick", "", *searchPayload->getNick())));
    }

    if (searchPayload->getFirst()) {
        searchElement.addNode(XMLElement::ref(new XMLElement("first", "", *searchPayload->getFirst())));
    }

    if (searchPayload->getLast()) {
        searchElement.addNode(XMLElement::ref(new XMLElement("last", "", *searchPayload->getLast())));
    }

    if (searchPayload->getEMail()) {
        searchElement.addNode(XMLElement::ref(new XMLElement("email", "", *searchPayload->getEMail())));
    }

    foreach(const SearchPayload::Item& item, searchPayload->getItems()) {
        XMLElement::ref itemElement(new XMLElement("item"));
        itemElement->setAttribute("jid", item.jid);
        itemElement->addNode(XMLElement::ref(new XMLElement("first", "", item.first)));
        itemElement->addNode(XMLElement::ref(new XMLElement("last", "", item.last)));
        itemElement->addNode(XMLElement::ref(new XMLElement("nick", "", item.nick)));
        itemElement->addNode(XMLElement::ref(new XMLElement("email", "", item.email)));

        searchElement.addNode(itemElement);
    }

    if (Form::ref form = searchPayload->getForm()) {
        searchElement.addNode(boost::make_shared<XMLRawTextNode>(FormSerializer().serialize(form)));
    }

    return searchElement.serialize();
}
Пример #12
0
  void GpsModeller::searchElement(xmlNode *node)
  {
    xmlNode *cur_node = NULL;

    for (cur_node = node; cur_node; cur_node = cur_node->next)
    {
      if (cur_node->type == XML_ELEMENT_NODE && strcmp((const char*)cur_node->name,"trkpt")==0)
      {
        xmlNode *time = NULL;
        for(xmlNode *s = cur_node->children; s; s = s->next)
        {
          if(s->type == XML_ELEMENT_NODE && strcmp((const char*)s->name,"time")==0)
          {
            time = s;
          }
        }

        if(!time)
        {
          // epic fail!!! We did not find time for point... Skep this point
          continue;
        }

        xmlChar *lon = xmlGetProp(cur_node, (xmlChar*)"lon");
        xmlChar *lat = xmlGetProp(cur_node, (xmlChar*)"lat");
        xmlChar *stime = xmlNodeGetContent(time);
        currentModellerTime = QDateTime::fromString((const char*)stime);
        if(!begin.isValid())
          begin=currentModellerTime;
        std::stringstream s;
        s << (const char *)lon << " " << (const char *) lat;
        setReady(true);
        s >> m_longitude >> m_latitude;
        // This code doesnt used by service
        //QThread::msleep(begin.msecsTo(currentModellerTime));
        QThread::msleep(begin.secsTo(currentModellerTime)*1000);
        begin=currentModellerTime;
      }
      searchElement(cur_node->children);
    }
Пример #13
0
void rotateArray(int *array, int len, int rot,int direction,int element)
{
  int i,temp,count=0,loc;
  int mid = len/2;
  rot = rot%len;
  if(mid<rot){                          //if rotations are more than mid of array then rotations will be minimized in opposite direction
       direction = ~direction+2;       //if direction is 0 then it becomes 1, if 1 ->0
       rot = len-rot;
  }
  while(count<rot) {
    if(direction)
    {
        temp = array[0];              //anticlockwise rotation
        for (i = 0; i < len-1; i++)
              array[i] = array[i+1];
        array[i] = temp;
    }
    else
    {
        temp = array[len-1];                     //Clockwise rotation
        for (i = len-1; i >0; i--)
              array[i] = array[i-1];
        array[0] = temp;
    }
    ++count;
  }

  if(count==rot) {
        display(array,len);
        loc=searchElement(array,0,len-1,element);
        if(loc == -1)
             printf("\nSearch Element not found");
        else
             printf("\nSearch Element Found at %d",loc);
   }
}
Пример #14
0
/************************************************
 Its contents references an immediate sub-menu of the current menu, as such it should never contain
 a slash. If no such sub-menu exists the element should be ignored.
 This element may have various attributes, the default values are taken from the DefaultLayout key.

 show_empty [ bool ]
    defines whether a menu that contains no desktop entries and no sub-menus
    should be shown at all.

 inline [ bool ]
    If the inline attribute is "true" the menu that is referenced may be copied into the current
    menu at the current point instead of being inserted as sub-menu of the current menu.

 inline_limit [ int ]
    defines the maximum number of entries that can be inlined. If the sub-menu has more entries
    than inline_limit, the sub-menu will not be inlined. If the inline_limit is 0 (zero) there
    is no limit.

 inline_header [ bool ]
    defines whether an inlined menu should be preceded with a header entry listing the caption of
    the sub-menu.

 inline_alias [ bool ]
    defines whether a single inlined entry should adopt the caption of the inlined menu. In such
    case no additional header entry will be added regardless of the value of the inline_header
    attribute.

 Example: if a menu has a sub-menu titled "WordProcessor" with a single entry "OpenOffice 4.2", and
 both inline="true" and inline_alias="true" are specified then this would result in the
 "OpenOffice 4.2" entry being inlined in the current menu but the "OpenOffice 4.2" caption of the
 entry would be replaced with "WordProcessor".
 ************************************************/
void XdgMenuLayoutProcessor::processMenunameTag(const QDomElement &element)
{
    QString id = element.text();
    QDomElement menu = searchElement(QLatin1String("Menu"), QLatin1String("name"), id);
    if (menu.isNull())
        return;

    LayoutParams params = mDefaultParams;
    setParams(element, &params);

    int count = childsCount(menu);

    if (count == 0)
    {
        if (params.mShowEmpty)
        {
            menu.setAttribute(QLatin1String("keep"), QLatin1String("true"));
            mResult.appendChild(menu);
        }
        return;
    }


    bool doInline = params.mInline &&
                    (!params.mInlineLimit || params.mInlineLimit > count);

    bool doAlias = params.mInlineAlias &&
                   doInline && (count == 1);

    bool doHeader = params.mInlineHeader &&
                    doInline && !doAlias;


    if (!doInline)
    {
        mResult.appendChild(menu);
        return;
    }


    // Header ....................................
    if (doHeader)
    {
        QDomElement header = mLayout.ownerDocument().createElement(QLatin1String("Header"));

        QDomNamedNodeMap attrs = menu.attributes();
        for (int i=0; i < attrs.count(); ++i)
        {
            header.setAttributeNode(attrs.item(i).toAttr());
        }

        mResult.appendChild(header);
    }

    // Alias .....................................
    if (doAlias)
    {
        menu.firstChild().toElement().setAttribute(QLatin1String("title"), menu.attribute(QLatin1String("title")));
    }

    // Inline ....................................
    MutableDomElementIterator it(menu);
    while (it.hasNext())
    {
        mResult.appendChild(it.next());
    }

}
Пример #15
0
/**
 * \brief Perform the search
 */
void UBLibActionBar::onActionSearch()
{
    emit searchElement(mSearchBar->text());
}
Пример #16
0
int main()
{
    int pil;
    infotype x, y;
    address P, Q;
    list L;
    createList(&L);

    while(pil != 10) {
        cout<<"1. Insert first"<<endl;
        cout<<"2. Insert after"<<endl;
        cout<<"3. Insert last"<<endl;
        cout<<"4. Delete first"<<endl;
        cout<<"5. Delete after"<<endl;
        cout<<"6. Delete last"<<endl;
        cout<<"7. View list"<<endl;
        cout<<"8. Search element"<<endl;
        cout<<"9. Search sentinel"<<endl;
        cout<<"Pilih: ";
        cin>>pil;

        switch(pil) {

        case 1:
            cout<<"ID           : ";
            cin>>x.id;
            cout<<"Nama         : ";
            cin>>x.nama;
            cout<<"Jabatan      : ";
            cin>>x.jabatan;
            cout<<"Departemen   : ";
            cin>>x.departemen;
            cout<<"Gaji         : ";
            cin>>x.gaji;

            P = allocate(x);
            insertFirst(&L, P);
            cout<<endl;
            cout<<"Insert complete.";
            break;

        case 2:
            cout<<"ID           : ";
            cin>>x.id;
            cout<<"Nama         : ";
            cin>>x.nama;
            cout<<"Jabatan      : ";
            cin>>x.jabatan;
            cout<<"Departemen   : ";
            cin>>x.departemen;
            cout<<"Gaji         : ";
            cin>>x.gaji;

            cout<<"Dimasukan setelah ID: ";
            cin>>y.id;
            if(searchElement(&L, y) != Nil) {
                insertAfter(&L, allocate(x), allocate(y));
                cout<<endl;
                cout<<"Insert complete.";
            } else {
                cout<<"Data tidak ada.";
            }
            break;

        case 3:
            cout<<"ID           : ";
            cin>>x.id;
            cout<<"Nama         : ";
            cin>>x.nama;
            cout<<"Jabatan      : ";
            cin>>x.jabatan;
            cout<<"Departemen   : ";
            cin>>x.departemen;
            cout<<"Gaji         : ";
            cin>>x.gaji;

            insertLast(&L, allocate(x));
            cout<<endl;
            cout<<"Insert complete.";

            break;

        case 4:
            cout<<"ID           : ";
            cin>>x.id;
            deleteFirst(&L, allocate(x));
            cout<<endl;
            cout<<"Delete complete.";

            break;

        case 5:
            cout<<"ID: ";
            cin>>x.id;
            P = allocate(x);
            cout<<"Dimasukan setelah ID: ";
            cin>>x.id;
            Q = allocate(x);

            deleteAfter(&L, P, Q);
            cout<<endl;
            cout<<"Delete complete.";

            break;

        case 6:
            cout<<"ID: ";
            cin>>x.id;
            deleteFirst(&L, allocate(x));
            cout<<endl;
            cout<<"Delete complete.";

            break;

        case 7:
            viewList(L);
            break;

        case 8:
            cout<<"ID: ";
            cin>>x.id;
            P = searchElement(&L, x);

            if(P != Nil) {
                cout<<"ID           = "<<info(P).id<<endl;
                cout<<"Nama         = "<<info(P).nama<<endl;
                cout<<"Jabatan      = "<<info(P).jabatan<<endl;
                cout<<"Departemen   = "<<info(P).departemen<<endl;
                cout<<"Gaji         = "<<info(P).gaji<<endl;
            } else {
                cout<<"Data tidak ditemukan.";
            }
            break;
        case 9:
            cout<<"ID: ";
            cin>>x.id;
            P = searchSentinel(&L, x);

            if(P != Nil) {
                cout<<"ID           = "<<info(P).id<<endl;
                cout<<"Nama         = "<<info(P).nama<<endl;
                cout<<"Jabatan      = "<<info(P).jabatan<<endl;
                cout<<"Departemen   = "<<info(P).departemen<<endl;
                cout<<"Gaji         = "<<info(P).gaji<<endl;
            }

            break;
        }
    }
    return 0;
}
Пример #17
0
void *Octree_Search(double *pt, Octree *myOctree)
{
  if(!myOctree) return 0;
  return searchElement(myOctree->root, pt, myOctree->info, 
                       myOctree->function_BB, myOctree->function_inElement);
}
Пример #18
0
void main()
{
	Node* list = 0;
	Node* secondList = 0;

	Student* stud = 0;

	FILE *f;
	f = fopen("Students.csv", "r");

	if (f)
	{
		stud = (Student*)malloc(sizeof(Student));

		// buffers
		char name[256];
		char birth[256];
		char faculty[256];

		// pattern: 1,John Smith,10-10-1990,Faculty of Computer Sciene,1,1008
		fscanf(f, "%d, %[^,], %[^,], %[^,], %hd, %hd",
			&(stud->id), name, birth, faculty, &stud->year, &stud->group);

		while (!feof(f))
		{
			stud->name = (char*)malloc((strlen(name) + 1) * sizeof(char));
			strcpy(stud->name, name);

			char* token;

			// solving date field
			char separator[2] = "-";
			token = strtok(birth, separator);
			stud->birthDay.day = atoi(token);

			token = strtok(0, separator);
			stud->birthDay.month = atoi(token);

			token = strtok(0, separator);
			stud->birthDay.year = atoi(token);


			// solving name field
			stud->name = (char*)malloc((strlen(name) + 1) * sizeof(char));
			strcpy(stud->name, name);

			// solving faculty
			stud->faculty = (char*)malloc((strlen(faculty) + 1) * sizeof(char));
			strcpy(stud->faculty, faculty);

			// INSERTION HERE:
			//list = frontInsertion(list, stud);
			list = rearInsertion(list, stud);
			secondList = frontInsertion(secondList, stud);

			// allocate new student && read next line from csv file
			stud = (Student*)malloc(sizeof(Student));
			fscanf(f, "%d, %[^,], %[^,], %[^,], %hd, %hd",
				&(stud->id), &name, &birth, &faculty, &stud->year, &stud->group);
		}
	}
	else
	{
		perror("[FILE ERROR]");
	}

	printList(list);
	searchStudent(list, "fate1");
	searchStudent(list, "fate2");
	searchStudent(list, "fate3");

	list = deleteStudent(list, "Max", 1);
	//list = deleteElement(list, "Philip Rafaello");
	printf("\nList after deletion of Max: ");

	printList(list);

	// search for a student in list
	Student* searched = searchElement(list, "John Smith");
	if (searched)
	{
		printf("\nStudent found: %s", searched->name);
	}
	else
	{
		printf("\nStudent not found!");
	}

	// interchange adjacent nodes in list
	list = interchangeNodesAdj(list, 3, 4);
	printf("\nList after adjacent interchange: ");
	printList(list);

	// interchange nodes in list
	list = interchangeNodes(list, 1, 5);
	printf("\nList after adjacent interchange: ");
	printList(list);

	// sort list by year
	printf("\nSorting the list: ");
	sortList(&list);
	printList(list);

	// list concatenation
	printf("\nFirst list: ");
	printList(list);
	printf("\nSecond list: ");
	printList(secondList);

	//Node* concatenatedList = concatenateList(list, secondList);
	//printf("\nResult: ");
	//printList(concatenatedList);

	deleteList(list);

	// testing some things
	int a = 0x5;

	a = 0xA;
	a = 0xB;
	a = 0xC;

	printf("\nHex: %x", a);


	// finally
	fclose(f);
	printf("\n");
}