CN100511152C - Method for managing priority queue - Google Patents

Method for managing priority queue Download PDF

Info

Publication number
CN100511152C
CN100511152C CNB2005101243603A CN200510124360A CN100511152C CN 100511152 C CN100511152 C CN 100511152C CN B2005101243603 A CNB2005101243603 A CN B2005101243603A CN 200510124360 A CN200510124360 A CN 200510124360A CN 100511152 C CN100511152 C CN 100511152C
Authority
CN
China
Prior art keywords
queue
priority
queue unit
insertion position
unit
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Expired - Lifetime
Application number
CNB2005101243603A
Other languages
Chinese (zh)
Other versions
CN1979424A (en
Inventor
王鑫
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Tencent Technology Shenzhen Co Ltd
Original Assignee
Tencent Technology Shenzhen Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Tencent Technology Shenzhen Co Ltd filed Critical Tencent Technology Shenzhen Co Ltd
Priority to CNB2005101243603A priority Critical patent/CN100511152C/en
Publication of CN1979424A publication Critical patent/CN1979424A/en
Application granted granted Critical
Publication of CN100511152C publication Critical patent/CN100511152C/en
Anticipated expiration legal-status Critical
Expired - Lifetime legal-status Critical Current

Links

Images

Landscapes

  • Data Exchanges In Wide-Area Networks (AREA)

Abstract

本发明公开了一种优先级队列的管理方法,为每个优先级队列配置包括不同优先级队列单元插入位置的索引表,并将索引表中所有插入位置初始化为空地址,将新队列单元插入到优先级队列时,该方法还包括:步骤A,根据新队列单元的优先级以及索引表中的插入位置信息确定所述新队列单元的插入位置,并根据所确定的插入位置将新队列单元插入到优先级队列中;步骤B,刷新索引表中的插入位置。采用本发明所提供的技术方案,在将队列单元插入队列时,只需要以要插入的队列单元的优先级为索引号在索引表中进行查找即可确定插入位置,而不用从队列的一端开始,逐队列单元进行比较以确定插入位置,从而提高了插入队列单元时的处理速度,更便于优先级队列的管理。

Figure 200510124360

The invention discloses a management method of priority queues. An index table including insertion positions of different priority queue units is configured for each priority queue, and all insertion positions in the index table are initialized to empty addresses, and new queue units are inserted into the When arriving at the priority queue, the method also includes: step A, determining the insertion position of the new queue unit according to the priority of the new queue unit and the insertion position information in the index table, and inserting the new queue unit according to the determined insertion position Insert into the priority queue; step B, refresh the insertion position in the index table. Adopting the technical solution provided by the present invention, when inserting the queue unit into the queue, only need to search in the index table with the priority of the queue unit to be inserted as the index number to determine the insertion position, instead of starting from one end of the queue , compare each queue unit to determine the insertion position, thereby improving the processing speed when inserting queue units, and making it easier to manage the priority queue.

Figure 200510124360

Description

一种优先级队列的管理方法 A Management Method of Priority Queue

技术领域 technical field

本发明涉及计算机应用技术,特别是涉及一种优先级队列的管理方法。The invention relates to computer application technology, in particular to a management method of a priority queue.

背景技术 Background technique

在支持优先级的消息处理中,等候处理模块进行处理的消息是按照优先级的顺序排列,而不是按照请求处理的先后排列。也就是说,优先级高的消息,即使比优先级低的消息后请求处理,也应该比优先级低的消息先得到处理。In message processing that supports priority, the messages waiting to be processed by the processing module are arranged in the order of priority, not in the order of request processing. That is to say, even if a message with a higher priority is requested to be processed later than a message with a lower priority, it should be processed before a message with a lower priority.

优先级队列可以实现支持优先级的消息处理。优先级队列的每个队列单元封装了一条具有优先级的消息,队列单元的优先级就是消息的优先级。在优先级队列中,队列单元按照优先级由低到高的顺序从队列头排列到队列尾,即位于队列头的队列单元的优先级最低,位于队列尾的队列单元的优先级最高。具有同样优先级的队列单元按照插入队列的时间先后排列,先插入队列的队列单元排列在靠近队列尾的位置,后插入队列的队列单元排列在靠近队列头的位置。处理模块从队列尾开始,逐个处理每个队列单元所封装的消息,并将已处理的队列单元从优先级队列中删除。Priority queues enable priority-aware message processing. Each queue unit of the priority queue encapsulates a message with priority, and the priority of the queue unit is the priority of the message. In the priority queue, the queue units are arranged from the head of the queue to the tail of the queue in order of priority from low to high, that is, the queue unit at the head of the queue has the lowest priority, and the queue unit at the end of the queue has the highest priority. Queue units with the same priority are arranged in sequence according to the time when they are inserted into the queue. The queue units that are inserted into the queue first are arranged near the end of the queue, and the queue units that are inserted into the queue later are arranged near the head of the queue. The processing module processes the messages encapsulated by each queue unit one by one from the end of the queue, and deletes the processed queue units from the priority queue.

在现有技术中,如果有一个队列单元要插入到优先级队列中,需要从队列头或队列尾开始,逐个比较每个队列单元的优先级与当前要插入的队列单元的优先级的高低,以确定插入位置。以从队列头开始比较为例,如果经过比较发现前一个队列单元的优先级低于当前要插入的队列单元的优先级,且后一个队列单元的优先级不低于当前要插入的队列单元的优先级,则当前要插入的队列单元就应该插入到前一个队列单元和后一个队列单元之间。In the prior art, if there is a queue unit to be inserted into the priority queue, it is necessary to compare the priority of each queue unit with the priority of the current queue unit to be inserted one by one from the queue head or queue tail, to determine where to insert. Taking the comparison from the head of the queue as an example, if the comparison finds that the priority of the previous queue unit is lower than that of the current queue unit to be inserted, and the priority of the latter queue unit is not lower than that of the current queue unit to be inserted priority, the current queue unit to be inserted should be inserted between the previous queue unit and the next queue unit.

在实际应用中,优先级队列中队列单元的个数很多。在现有技术中,每次向优先级队列中插入队列单元都需要从队列的一端开始逐个比较,通常需要经过大量的比较操作才能确定插入位置,最坏的情况是需要遍历整个队列才能确定插入位置,然后再进行插入操作本身。这样,确定插入位置所需要的平均时间,远远大于进行插入操作本身所需要的时间,因此造成了处理能力的浪费,即大量的处理能力被用在确定插入位置时的比较上,从而降低了处理其他任务的能力。In practical applications, there are many queue units in the priority queue. In the prior art, every time a queue unit is inserted into a priority queue, it needs to be compared one by one from one end of the queue. Usually, a large number of comparison operations are required to determine the insertion position. In the worst case, the entire queue needs to be traversed to determine the insertion position. position before the insert operation itself. In this way, the average time required to determine the insertion position is far greater than the time required for the insertion operation itself, thus causing a waste of processing power, that is, a large amount of processing power is used for comparison when determining the insertion position, thereby reducing Ability to handle other tasks.

发明内容 Contents of the invention

有鉴于此,本发明的主要目的在于提供一种优先级队列的管理方法,以提高对优先级队列的处理速度。In view of this, the main purpose of the present invention is to provide a management method for priority queues, so as to improve the processing speed of priority queues.

为了达到上述目的,本发明提供了一种优先级队列的管理方法,为每个优先级队列配置包括不同优先级队列单元插入位置的索引表,并将索引表初始化为所有元素均为空地址,将新队列单元插入到优先级队列时,该方法还包括:In order to achieve the above object, the present invention provides a management method of priority queues, configuring index tables including insertion positions of different priority queue units for each priority queue, and initializing the index tables to be empty addresses for all elements, When inserting a new queue unit into the priority queue, the method also includes:

步骤A,根据新队列单元的优先级以及索引表中的插入位置信息,确定所述新队列单元的插入位置,并根据所确定的插入位置将新队列单元插入到优先级队列中;Step A, according to the priority of the new queue unit and the insertion position information in the index table, determine the insertion position of the new queue unit, and insert the new queue unit into the priority queue according to the determined insertion position;

步骤B,刷新索引表中的插入位置。Step B, refresh the insertion position in the index table.

其中,步骤B所述刷新插入位置包括:Wherein, the refresh insertion position described in step B includes:

步骤B1,判断新队列单元的优先级是否为最高优先级,如果是则结束当前刷新流程,否则执行步骤B2;Step B1, judge whether the priority of the new queue unit is the highest priority, if so, end the current refresh process, otherwise execute step B2;

步骤B2,以新队列单元的优先级加1为当前索引号;Step B2, taking the priority of the new queue unit plus 1 as the current index number;

步骤B3,读取索引表中当前索引号对应的插入位置,并判断是否需要修改所读取的插入位置,如果需要修改则执行步骤B4,否则结束当前刷新流程;Step B3, read the insertion position corresponding to the current index number in the index table, and judge whether the read insertion position needs to be modified, if it needs to be modified, execute step B4, otherwise end the current refresh process;

步骤B4,将当前索引号对应的插入位置修改为新队列单元的地址,并判断当前索引号是否等于最高优先级,如果是则结束当前刷新流程,否则执行步骤B5;Step B4, modify the insertion position corresponding to the current index number to the address of the new queue unit, and judge whether the current index number is equal to the highest priority, if so, end the current refresh process, otherwise execute step B5;

步骤B5,将当前索引号加1后作为新的当前索引号,返回执行步骤B3。In step B5, add 1 to the current index number as a new current index number, and return to step B3.

其中,步骤A中所述根据新队列单元的优先级以及索引表确定所述新队列单元的插入位置为:Wherein, the insertion position of the new queue unit determined according to the priority of the new queue unit and the index table in step A is:

以新队列单元的优先级为索引号,将索引表中该索引号对应的插入位置作为新队列单元在插入到优先级队列时,位于新队列单元之前的队列单元的存储地址。The priority of the new queue unit is used as the index number, and the insertion position corresponding to the index number in the index table is used as the storage address of the queue unit before the new queue unit when the new queue unit is inserted into the priority queue.

其中,步骤B3所述判断是否需要修改所读取的插入位置为:Wherein, the judgment in step B3 whether it is necessary to modify the read insertion position is:

判断当前索引所对应的插入位置是否等于空地址或所述新队列单元的插入地址两者中的任意一个,如果是则认为需要修改所读取的插入位置,否则认为不需要修改所读取的插入位置。Judging whether the insertion position corresponding to the current index is equal to any one of the empty address or the insertion address of the new queue unit, if it is, it is considered that the read insertion position needs to be modified, otherwise it is considered that the read insertion position does not need to be modified. insert position.

其中,该方法进一步包括:Wherein, the method further includes:

步骤C,将位于队列尾的队列单元从优先级队列中删除,并刷新索引表的插入位置。Step C, delete the queue unit at the end of the queue from the priority queue, and refresh the insertion position of the index table.

其中,所述步骤C包括:Wherein, the step C includes:

步骤C1,判断索引表中以最高优先级作为索引号所对应的插入位置是否为空地址,如果是则执行步骤C6,否则将位于队列尾的队列单元从优先级队列中删除,并执行步骤C2;Step C1, judge whether the insertion position corresponding to the highest priority as the index number in the index table is an empty address, if yes, execute step C6, otherwise delete the queue unit at the end of the queue from the priority queue, and execute step C2 ;

步骤C2,以步骤C1中删除的队列单元的优先级加1作为当前索引号;Step C2, using the priority of the queue unit deleted in step C1 plus 1 as the current index number;

步骤C3,将步骤C1中删除的队列单元中前向指针数据项的值,赋予索引表中当前索引号对应的插入位置,然后判断当前索引号的值是否等于最高优先级,如果是则执行步骤C5,否则执行步骤C4;Step C3, assign the value of the forward pointer data item in the queue unit deleted in step C1 to the insertion position corresponding to the current index number in the index table, and then judge whether the value of the current index number is equal to the highest priority, and if so, execute the step C5, otherwise execute step C4;

步骤C4,将当前索引号的值加1后作为新的当前索引号,返回执行步骤C3;Step C4, add 1 to the value of the current index number as the new current index number, return to step C3;

步骤C5,将位于队列尾的队列单元从优先级队列中删除,结束本次处理流程;Step C5, delete the queue unit at the end of the queue from the priority queue, and end this processing flow;

步骤C6,确定优先级队列为空队列,无法从其中删除任何队列单元。In step C6, it is determined that the priority queue is an empty queue, and no queue unit can be deleted therefrom.

其中,所述将位于队列尾的队列单元从优先级队列中删除为:Wherein, the queue unit positioned at the tail of the queue is deleted from the priority queue as:

将被删除队列单元的前向指针数据域的值,赋予与优先级队列对应的队列描述中队列尾指针数据域;以被删除队列单元的前向指针数据域的值为地址,将存储于该地址处的队列单元的后项指针数据域修改为空地址。The value of the forward pointer data field of the deleted queue unit is given to the queue tail pointer data field in the queue description corresponding to the priority queue; the value of the forward pointer data field of the deleted queue unit is the address, and will be stored in The next item pointer data field of the queue unit at the address is changed to a null address.

采用本发明所提供的技术方案,为每个优先级队列配置一张索引表,在将队列单元插入队列时,只需要以要插入的队列单元的优先级为索引号在索引表中进行查找即可确定插入位置,而不用从队列的一端开始,逐队列单元进行比较以确定插入位置,从而提高了插入队列单元时的处理速度,更便于优先级队列的管理。By adopting the technical scheme provided by the present invention, an index table is configured for each priority queue, and when the queue unit is inserted into the queue, it is only necessary to search the index table with the priority of the queue unit to be inserted as the index number. The insertion position can be determined instead of starting from one end of the queue and comparing each queue unit to determine the insertion position, thereby improving the processing speed when inserting a queue unit and making it easier to manage the priority queue.

附图说明 Description of drawings

图1是本发明所提供的优先级队列的示意图;Fig. 1 is the schematic diagram of the priority queue provided by the present invention;

图2是本发明所提供的优先级队列的管理方法中插入队列单元时的流程图;Fig. 2 is the flow chart when inserting queue unit in the management method of priority queue provided by the present invention;

图3是本发明所提供的优先级队列在插入队列单元后的示意图;Fig. 3 is the schematic diagram of the priority queue provided by the present invention after being inserted into the queue unit;

图4是本发明所提供的优先级队列的管理方法中删除队列单元时的流程图。Fig. 4 is a flow chart of deleting a queue unit in the priority queue management method provided by the present invention.

具体实施方式 Detailed ways

本发明的核心思想在于,为每个优先级队列配置一张索引表,在向优先级队列插入队列单元时,根据该索引表中记载的与队列单元优先级一一对应的插入位置信息确定插入位置,并且在向优先级队列插入队列单元和从优先级队列中删除队列单元时动态刷新该索引表。The core idea of the present invention is to configure an index table for each priority queue, and when inserting a queue unit into the priority queue, determine the insertion position information according to the insertion position information recorded in the index table corresponding to the priority of the queue unit one by one. position, and the index table is dynamically refreshed when queue units are inserted into and deleted from the priority queue.

为使本发明的目的、技术方案和优点更加清楚,下面结合附图及具体实施例对本发明作进一步地详细描述。In order to make the purpose, technical solution and advantages of the present invention clearer, the present invention will be further described in detail below in conjunction with the accompanying drawings and specific embodiments.

本发明中,每个优先级队列是由若干个队列单元和一个队列描述组成的。In the present invention, each priority queue is composed of several queue units and a queue description.

每个队列单元对应一条消息,队列单元中的消息指针数据项等于对应消息的存储地址;队列单元的优先级数据项等于对应消息的优先级;队列单元的前向指针数据项,等于优先级队列中比自身更靠近队列头的下一个队列单元的存储地址,位于队列头的队列单元的前向指针为空地址;队列单元的后向指针数据项,等于优先级队列中比自身更靠近队列尾的下一个队列单元的存储地址,位于队列尾的队列单元的后向指针为空地址。在本发明中,将相对于某个队列单元更靠近队列头的下一个队列单元称为前向队列单元,将相对于某个队列单元更靠近队列尾的下一个队列单元称为后向队列单元。空地址可以看作是一种特殊的队列单元,即:具有空地址的队列单元没有任何数据项,并且可以作为前向队列单元也可以作为后向队列单元。Each queue unit corresponds to a message, and the message pointer data item in the queue unit is equal to the storage address of the corresponding message; the priority data item of the queue unit is equal to the priority of the corresponding message; the forward pointer data item of the queue unit is equal to the priority queue The storage address of the next queue unit closer to the queue head than itself, the forward pointer of the queue unit at the queue head is an empty address; the backward pointer data item of the queue unit is equal to the queue unit closer to the queue end than itself in the priority queue The storage address of the next queue unit, the back pointer of the queue unit at the end of the queue is a null address. In the present invention, the next queue unit that is closer to the head of the queue relative to a certain queue unit is called a forward queue unit, and the next queue unit that is closer to the end of the queue relative to a certain queue unit is called a backward queue unit . An empty address can be regarded as a special queue unit, that is, a queue unit with an empty address does not have any data items, and can be used as a forward queue unit or as a backward queue unit.

队列描述有三个数据项:队列头指针、队列尾指针和索引表。其中队列头指针数据项等于位于队列头的队列单元的存储地址;队列尾指针数据项等于位于队列尾的队列单元的存储地址;索引表存储具有某一优先级的新队列单元在优先级队列中的插入位置。The queue description has three data items: queue head pointer, queue tail pointer and index table. Wherein the queue head pointer data item is equal to the storage address of the queue unit located at the head of the queue; the queue tail pointer data item is equal to the storage address of the queue unit located at the queue tail; the index table stores a new queue unit with a certain priority in the priority queue insertion position.

索引表中的索引号对应于优先级队列所能处理的优先级,例如当优先级队列能够处理20个优先级的时候,索引表中的索引号为1到20;索引表元素的数据类型为指针型。在建立一个新的优先级队列时,将索引表的所有元素初始化为空地址。The index number in the index table corresponds to the priority that the priority queue can handle. For example, when the priority queue can handle 20 priorities, the index number in the index table is 1 to 20; the data type of the index table element is pointer type. When building a new priority queue, initialize all elements of the index table to empty addresses.

索引表中每一个元素的值,表示消息优先级数据项等于该元素索引号的新队列单元,在插入到优先级队列时,应该插入到以该元素的值为地址的队列单元之后。如果某一元素的值为空指针,表示消息优先级数据项等于该元素索引号的新队列单元,在插入到优先级队列时,应该插入到队列头。The value of each element in the index table indicates that the new queue unit whose message priority data item is equal to the index number of the element, when inserted into the priority queue, should be inserted after the queue unit whose address is the value of the element. If the value of an element is a null pointer, it means that the new queue unit whose message priority data item is equal to the index number of this element should be inserted at the head of the queue when it is inserted into the priority queue.

请参考图1,图1是本发明所提供的优先级队列的示意图。Please refer to FIG. 1 , which is a schematic diagram of a priority queue provided by the present invention.

对于图1所示的优先级队列,假设该优先级队列所能处理的优先级个数为5,且队列单元1的地址为1000,队列单元2的地址为1008,队列单元3的地址为1006。则对应的索引表如表一所示。也就是说,对于优先级为1的队列单元,在插入到优先级队列时,应该插入到队列头;对于优先级为2的队列单元,在插入到优先级队列时,应该插入到地址为1000的队列单元,也就是队列单元1之后;对于优先级为3和4的队列单元,在插入到优先级队列时,应该插入到地址为1008的队列单元,也就是队列单元2之后;对于优先级为5的队列单元,在插入到优先级队列时,也应该插入到地址为1008的队列单元,也就是队列单元2之后,这是因为队列单元3具有最高的优先级,因此优先级为5的新队列单元都应该插入在队列单元3和队列单元2之间,也就是队列单元2之后。For the priority queue shown in Figure 1, suppose the number of priorities that the priority queue can handle is 5, and the address of queue unit 1 is 1000, the address of queue unit 2 is 1008, and the address of queue unit 3 is 1006 . The corresponding index table is shown in Table 1. That is to say, for a queue unit with a priority of 1, when inserted into the priority queue, it should be inserted at the head of the queue; for a queue unit with a priority of 2, when inserted into the priority queue, it should be inserted at the address 1000 The queue unit of , that is, after queue unit 1; for queue units with priority 3 and 4, when inserted into the priority queue, it should be inserted into the queue unit with address 1008, that is, after queue unit 2; for priority When the queue unit of 5 is inserted into the priority queue, it should also be inserted into the queue unit with address 1008, that is, after queue unit 2. This is because queue unit 3 has the highest priority, so the queue unit with priority 5 All new queue units should be inserted between queue unit 3 and queue unit 2, that is, after queue unit 2.

  1 2 3 4 5 空地址 1000 1008 1008 1008 1 2 3 4 5 empty address 1000 1008 1008 1008

表一Table I

请参考图2,图2是本发明所提供的优先级队列管理方法中插入队列单元时的流程图。Please refer to FIG. 2 . FIG. 2 is a flow chart of inserting queue units in the priority queue management method provided by the present invention.

步骤201,构造基于新消息的队列单元。Step 201, constructing a queue unit based on a new message.

由于优先级队列是用在处理有优先级的消息的处理中,因此当一条新的消息请求处理时,就要为这条消息构造队列单元,并将队列单元插入到优先级队列中。Since the priority queue is used in the processing of messages with priority, when a new message requests processing, it is necessary to construct a queue unit for this message and insert the queue unit into the priority queue.

在基于新消息构造队列单元时,队列单元中的消息指针数据项等于新消息的存储地址;队列单元的优先级数据项等于新消息的优先级;队列单元的前向指针和后向指针都为空地址。When constructing a queue unit based on a new message, the message pointer data item in the queue unit is equal to the storage address of the new message; the priority data item of the queue unit is equal to the priority of the new message; the forward pointer and the backward pointer of the queue unit are both empty address.

步骤202,判断新队列单元的优先级是否为最高优先级,如果是则执行步骤210,否则执行步骤203。Step 202, judging whether the priority of the new queue unit is the highest priority, if yes, execute step 210, otherwise execute step 203.

此处的最高优先级等于优先级队列所能处理的优先级个数。The highest priority here is equal to the number of priorities that the priority queue can handle.

步骤203,以新队列单元的优先级加1作为当前索引号。In step 203, the priority of the new queue unit plus 1 is used as the current index number.

步骤204,从队列描述的索引表中,读取索引号为当前索引号的元素,作为当前元素。Step 204, read the element whose index number is the current index number from the index table of the queue description, and use it as the current element.

步骤205,判断当前元素的值是否为空地址,如果是则执行步骤206,否则执行步骤209。Step 205, judging whether the value of the current element is an empty address, if yes, go to step 206, otherwise go to step 209.

步骤206,将当前元素的值修改为新队列单元的地址。也就是说,在完成本次插入操作以后,优先级等于当前索引号的队列单元,应该插入到本次插入的队列单元之后。Step 206, modify the value of the current element to the address of the new queue unit. That is to say, after this insertion operation is completed, the queue unit whose priority is equal to the current index number should be inserted after the queue unit inserted this time.

步骤207,判断当前索引号的值是否等于最高优先级,如果是则执行步骤210,否则执行步骤208。Step 207, judging whether the value of the current index number is equal to the highest priority, if yes, go to step 210, otherwise go to step 208.

步骤208,将当前索引号的值加1以后作为新的当前索引号,返回执行步骤204。In step 208, add 1 to the value of the current index number as a new current index number, and return to step 204 for execution.

这一步相当于准备处理索引表中的下一个元素。This step is equivalent to preparing to process the next element in the index table.

步骤209,判断当前元素的值是否等于以新队列单元的优先级为索引号的元素的值,如果是则执行步骤206,否则执行步骤210。Step 209 , judging whether the value of the current element is equal to the value of the element whose index number is the priority of the new queue unit, if yes, execute step 206 , otherwise execute step 210 .

步骤210,根据新队列单元的优先级,从索引表确定插入位置,将基于新消息的新队列单元插入到优先级队列中。Step 210, according to the priority of the new queue unit, determine the insertion position from the index table, and insert the new queue unit based on the new message into the priority queue.

根据对图1所示的优先级队列的描述,索引表中每一个元素的值,表示消息优先级数据项等于该元素索引号的新队列单元,在插入到优先级队列时,应该插入到以该元素的值为地址的队列单元之后。如果某一元素的值为空指针,表示消息优先级数据项等于该元素索引号的新队列单元,在插入到优先级队列时,应该插入到队列头。According to the description of the priority queue shown in Figure 1, the value of each element in the index table indicates that the new queue unit whose message priority data item is equal to the element index number, when inserted into the priority queue, should be inserted into the following The value of this element is the address of the queue unit following. If the value of an element is a null pointer, it means that the new queue unit whose message priority data item is equal to the index number of this element should be inserted at the head of the queue when it is inserted into the priority queue.

因此,以新队列单元的优先级为索引号,在索引表中读取对应于该索引号的元素的值,即为新队列单元插入到优先级队列时的前向队列单元的存储地址。进一步,由前向队列单元的后向指针数据项的值,可以得到新队列单元插入到优先级队列时的后向队列单元的存储地址;如果从索引表中得到的前向队列单元的地址值为空地址,那么新队列单元应该插入到优先级队列的队列头。Therefore, take the priority of the new queue unit as the index number, and read the value of the element corresponding to the index number in the index table, which is the storage address of the forward queue unit when the new queue unit is inserted into the priority queue. Further, by the value of the backward pointer data item of the forward queue unit, the storage address of the backward queue unit when the new queue unit is inserted into the priority queue can be obtained; if the address value of the forward queue unit obtained from the index table If it is an empty address, then the new queue unit should be inserted into the queue head of the priority queue.

将队列单元插入到优先级队列,实际上就是在双向链表中插入一个单元。本发明的发明目的在于快速定位插入位置,而对于如何插入的步骤,本发明和现有技术是一样的。Inserting a queue unit into a priority queue is actually inserting a unit into a doubly linked list. The purpose of the present invention is to quickly locate the insertion position, and for the steps of how to insert, the present invention is the same as the prior art.

步骤211,结束。Step 211, end.

以图1所示的优先级队列为例,如果一个新的队列单元4,其优先级为4,地址为1020,那么在这个队列单元插入优先级队列后,如表一所示的索引表相应的变为如表二所示。Taking the priority queue shown in Figure 1 as an example, if a new queue unit 4 has a priority of 4 and an address of 1020, then after this queue unit is inserted into the priority queue, the index table shown in Table 1 corresponds to changed as shown in Table 2.

  1 2 3 4 5 空地址 1000 1008 1008 1020 1 2 3 4 5 empty address 1000 1008 1008 1020

表二Table II

也就是说,在本次插入操作之前,优先级为5的队列单元应该插入到地址为1008的队列单元,即图1所示的队列单元2之后;在本次插入操作之后,优先级为5的队列单元就应该插入到优先级为4,地址为1020的队列单元4之后。That is to say, before this insertion operation, the queue unit with priority 5 should be inserted into the queue unit with address 1008, that is, after queue unit 2 shown in Figure 1; after this insertion operation, the priority is 5 The queue unit should be inserted after the queue unit 4 whose priority is 4 and whose address is 1020.

如果再有一个新的队列单元5,其优先级为1,地址为1030,那么在这个队列单元插入优先级队列后,索引表如表三所示。If there is a new queue unit 5 with a priority of 1 and an address of 1030, after this queue unit is inserted into the priority queue, the index table is shown in Table 3.

  1 2 3 4 5 空地址 1000 1008 1008 1020 1 2 3 4 5 empty address 1000 1008 1008 1020

表三Table three

表三相对于表二没有变化,这是因为新插入的队列单元被插入在队列头,在本次插入操作前后,优先级为2的队列单元都应该插入到地址为1000的队列单元,即图1所示的队列单元1之后。Table 3 is unchanged from Table 2. This is because the newly inserted queue unit is inserted at the head of the queue. Before and after this insertion operation, the queue unit with priority 2 should be inserted into the queue unit with address 1000, that is, 1 after queue unit 1 shown.

如果再有一个新的队列单元6,其优先级为3,地址为1028,那么在这个队列单元插入优先级队列后,如表三所示的索引表相应的变为如表四所示。If there is a new queue unit 6 with a priority of 3 and an address of 1028, then after this queue unit is inserted into the priority queue, the index table shown in Table 3 becomes correspondingly shown in Table 4.

  1 2 3 4 5 空地址 1000 1008 1028 1020 1 2 3 4 5 empty address 1000 1008 1028 1020

表四Table four

也就是说,在本次插入操作之前,优先级为4的队列单元应该插入到地址为1008的队列单元,即图1所示的队列单元2之后;在本次插入操作之后,优先级为4的队列单元就应该插入到优先级为3,地址为1028的队列单元4之后。That is to say, before this insertion operation, the queue unit with priority 4 should be inserted into the queue unit with address 1008, that is, after queue unit 2 shown in Figure 1; after this insertion operation, the priority is 4 The queue unit should be inserted after the queue unit 4 whose priority is 3 and whose address is 1028.

经过这几次插入操作以后的优先级队列如图3所示。The priority queue after these several insertion operations is shown in Figure 3.

请参考图4,图4是本发明所提供的优先级队列管理方法中删除队列单元时的流程图。Please refer to FIG. 4 , which is a flow chart of deleting a queue unit in the priority queue management method provided by the present invention.

对于优先级队列,在队列尾的队列单元具有最高的优先级,并且是相同优先级的队列单元中最先进入队列的队列单元。因此,消息处理模块每次都从优先级队列的队列尾开始,逐个处理队列单元所封装的消息,同时需要将该队列单元从队列中删除,然后相应的修改索引表。For a priority queue, the queue unit at the end of the queue has the highest priority and is the first to enter the queue among queue units of the same priority. Therefore, the message processing module starts from the tail of the priority queue each time, processes the messages encapsulated by the queue unit one by one, and needs to delete the queue unit from the queue, and then modify the index table accordingly.

步骤401,对位于队列尾的队列单元所封装的消息进行后续处理。Step 401, perform subsequent processing on the message encapsulated by the queue unit at the end of the queue.

步骤402,判断索引表中索引号等于最高优先级的元素的值是否为空地址,如果是则执行步骤408,否则执行步骤403。Step 402 , judging whether the value of the element whose index number is equal to the highest priority in the index table is an empty address, if yes, execute step 408 , otherwise execute step 403 .

步骤403,以位于队列尾的队列单元的优先级加1作为当前索引号。In step 403, the priority of the queue unit at the end of the queue plus 1 is used as the current index number.

步骤404,将位于队列尾的队列单元中前向指针数据项的值,赋予索引表中索引号为当前索引号的元素。Step 404, assigning the value of the forward pointer data item in the queue unit at the end of the queue to the element whose index number is the current index number in the index table.

步骤405,判断当前索引号是否等于最高优先级,如果是则执行步骤407,否则执行步骤406。Step 405, judge whether the current index number is equal to the highest priority, if yes, execute step 407, otherwise execute step 406.

步骤406,将当前索引号的值加1后作为当前索引号。Step 406, adding 1 to the value of the current index number as the current index number.

这一步相当于准备处理索引表中的下一个元素。This step is equivalent to preparing to process the next element in the index table.

步骤407,将位于队列尾的队列单元从优先级队列中删除,结束本次处理流程。Step 407, delete the queue unit located at the end of the queue from the priority queue, and end this processing flow.

将队列单元从优先级队列中删除,实际上就是从双向链表中的尾部删除一个单元。对于如何删除的步骤,本发明和现有技术是一样的。Deleting a queue unit from the priority queue is actually deleting a unit from the tail of the doubly linked list. For the steps of how to delete, the present invention is the same as the prior art.

步骤408,优先级队列为空队列,无法从其中删除任何队列单元,报错。Step 408, the priority queue is an empty queue, and no queue unit can be deleted from it, and an error is reported.

根据图3所示的优先级队列,在删除队列单元之前,其对应的索引表结构如表四所示。According to the priority queue shown in FIG. 3 , before the queue unit is deleted, its corresponding index table structure is shown in Table 4.

删除队列单元3以后,如表四所示的索引表相应的变为如表五所示。After the queue unit 3 is deleted, the index table shown in Table 4 becomes correspondingly shown in Table 5.

  1 2 3 4 5 空地址 1000 1008 1028 1020 1 2 3 4 5 empty address 1000 1008 1028 1020

表五Table five

删除队列单元4以后,如表五所示的索引表相应的变为如表六所示。After the queue unit 4 is deleted, the index table shown in Table 5 becomes correspondingly shown in Table 6.

  1 2 3 4 5 空地址 1000 1008 1028 1028 1 2 3 4 5 empty address 1000 1008 1028 1028

表六Table six

删除队列单元6以后,如表六所示的索引表相应的变为如表七所示。After the queue unit 6 is deleted, the index table shown in Table 6 becomes correspondingly shown in Table 7.

  1 2 3 4 5 空地址 1000 1008 1008 1008 1 2 3 4 5 empty address 1000 1008 1008 1008

表七Table seven

删除队列单元2以后,如表七所示的索引表相应的变为如表八所示。After the queue unit 2 is deleted, the index table shown in Table 7 becomes correspondingly shown in Table 8.

  1 2 3 4 5 空地址 1000 1000 1000 1000 1 2 3 4 5 empty address 1000 1000 1000 1000

表八table eight

删除队列单元1以后,如表八所示的索引表相应的变为如表九所示。After deleting queue unit 1, the index table shown in Table 8 becomes correspondingly shown in Table 9.

  1 2 3 4 5 空地址 1030 1030 1030 1030 1 2 3 4 5 empty address 1030 1030 1030 1030

表九Table nine

删除队列单元5以后,优先级队列为空队列,如表九所示的索引表相应的变为如表十所示。After the queue unit 5 is deleted, the priority queue is an empty queue, and the index table shown in Table 9 becomes correspondingly as shown in Table 10.

  1 2 3 4 5 空地址 空地址 空地址 空地址 空地址 1 2 3 4 5 empty address empty address empty address empty address empty address

表十table ten

需要说明的是,在实际应用中,队列单元的插入和删除往往是随机交替进行的。但是,这并不影响本发明所提供的优先级队列的管理方法,对于提高队列单元插入优先级队列时的处理速度的有效性。It should be noted that, in practical applications, insertion and deletion of queue units are often performed randomly and alternately. However, this does not affect the effectiveness of the priority queue management method provided by the present invention in improving the processing speed when the queue unit is inserted into the priority queue.

以上所述仅为本发明的较佳实施例而已,并非用于限定本发明的保护范围。凡在本发明的精神和原则之内,所作的任何修改、等同替换、改进等,均应包含在本发明的保护范围之内。The above descriptions are only preferred embodiments of the present invention, and are not intended to limit the protection scope of the present invention. Any modifications, equivalent replacements, improvements, etc. made within the spirit and principles of the present invention shall be included within the protection scope of the present invention.

Claims (6)

1, a kind of management method of priority query, it is characterized in that, the concordance list that comprises different priorities queue unit insertion position for each priority query's configuration, and all insertion positions in the concordance list are initialized as address blank, when new queue unit was inserted into priority query, this method also comprised:
Steps A according to the priority of new queue unit and the insertion position information in the concordance list, is determined the insertion position of described new queue unit, and according to determined insertion position new queue unit is inserted in the priority query;
Step B refreshes the insertion position in the concordance list;
Step B comprises:
Step B1 judges whether the priority of new queue unit is limit priority;
Step B2, if when the priority of new queue unit is not limit priority, adding 1 with the priority of new queue unit is the current cable quotation marks;
Step B3 reads the insertion position of current cable quotation marks correspondence in the concordance list, and judges whether to need to revise the insertion position of being read;
Step B4 when revising the insertion position of being read as if needs, is revised as the address of new queue unit with the insertion position of current cable quotation marks correspondence, and judges whether the current cable quotation marks equal limit priority;
Step B5, if the current cable quotation marks are not equal to limit priority, this adds 1 back as new current cable quotation marks with the current cable quotation marks, returns execution in step B3.
2, the management method of priority query according to claim 1 is characterized in that, described in the steps A according to the priority of new queue unit and the insertion position that concordance list is determined described new queue unit is:
Priority with new queue unit is call number, with the insertion position of this call number correspondence in the concordance list as new queue unit when being inserted into priority query, be positioned at the memory address of the queue unit before the new queue unit.
3, the management method of priority query according to claim 1 is characterized in that, step B3 is described to judge whether that needing to revise the insertion position of being read is:
Judge whether the pairing insertion position of current index equals insertion address any one among both of address blank or described new queue unit,, otherwise think and do not need to revise the insertion position of being read if then think and need to revise the insertion position of being read.
4, the management method of priority query according to claim 1 is characterized in that, this method further comprises:
Step C, the queue unit that will be arranged in rear of queue is deleted from priority query, and refreshes the insertion position of concordance list.
5, the management method of priority query according to claim 4 is characterized in that, described step C comprises:
Step C1 judges in the concordance list with limit priority whether be address blank as the pairing insertion position of call number, if execution in step C6 then, otherwise the queue unit that will be arranged in rear of queue deletes from priority query, and execution in step C2;
Step C2 adds 1 as the current cable quotation marks with the priority of the queue unit of deleting among the step C1;
Step C3, with the value of forwarding pointer data item in the queue unit of deleting among the step C1, give the insertion position of current cable quotation marks correspondence in the concordance list, judge then whether the value of current cable quotation marks equals limit priority, if execution in step C5 then, otherwise execution in step C4;
Step C4 adds 1 back as new current cable quotation marks with the value of current cable quotation marks, returns execution in step C3;
Step C5, the queue unit that will be arranged in rear of queue is deleted from priority query, finishes this treatment scheme;
Step C6 determines that priority query is empty queue, can't be from wherein deleting any queue unit.
According to the management method of claim 4 or 5 described priority queries, it is characterized in that 6, the described queue unit that will be arranged in rear of queue from priority query's deletion is:
With the value of the forwarding pointer data field of deleted queue unit, give describe with the priority query corresponding queues in formation tail pointer data field; Value with the forwarding pointer data field of deleted queue unit is the address, and address blank is revised as in the consequent pointer data territory that is stored in the queue unit at this place, address.
CNB2005101243603A 2005-11-29 2005-11-29 Method for managing priority queue Expired - Lifetime CN100511152C (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CNB2005101243603A CN100511152C (en) 2005-11-29 2005-11-29 Method for managing priority queue

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CNB2005101243603A CN100511152C (en) 2005-11-29 2005-11-29 Method for managing priority queue

Publications (2)

Publication Number Publication Date
CN1979424A CN1979424A (en) 2007-06-13
CN100511152C true CN100511152C (en) 2009-07-08

Family

ID=38130603

Family Applications (1)

Application Number Title Priority Date Filing Date
CNB2005101243603A Expired - Lifetime CN100511152C (en) 2005-11-29 2005-11-29 Method for managing priority queue

Country Status (1)

Country Link
CN (1) CN100511152C (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN107133115A (en) * 2017-05-15 2017-09-05 郑州云海信息技术有限公司 The method and blocker of the message insertion time of a kind of message queue

Families Citing this family (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN101902487B (en) 2009-05-26 2013-03-20 中兴通讯股份有限公司 Queue scheduling method and device based on linked list
CN103246653B (en) * 2012-02-03 2017-07-28 腾讯科技(深圳)有限公司 Data processing method and device
US9514170B1 (en) * 2013-05-15 2016-12-06 Amazon Technologies, Inc. Priority queue using two differently-indexed single-index tables
CN111124355B (en) * 2019-12-12 2023-04-07 东软集团股份有限公司 Information processing method and device, readable storage medium and electronic equipment
CN115348218B (en) * 2022-10-18 2022-12-27 井芯微电子技术(天津)有限公司 Queue scheduling method and device

Non-Patent Citations (1)

* Cited by examiner, † Cited by third party
Title
常量时间的优先队列算法. 刘晨亮,许家栋,杨少军.微型机与应用,第5期. 2004 *

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN107133115A (en) * 2017-05-15 2017-09-05 郑州云海信息技术有限公司 The method and blocker of the message insertion time of a kind of message queue

Also Published As

Publication number Publication date
CN1979424A (en) 2007-06-13

Similar Documents

Publication Publication Date Title
CN110674053B (en) SSD data storage node management method and device, computer equipment and storage medium
CN102867071B (en) Management method for massive network management historical data
CN103229164B (en) Data access method and device
US20230289343A1 (en) Allocating partitions for executing operations of a query
CN106095589A (en) Partition allocation method, device and system
CN103227778A (en) Method, device and system for accessing memory
CN102156717A (en) Method and device for mapping entity object into database
CN106156301A (en) A kind of processing method and processing device of big field data
CN114896215B (en) Metadata storage method and device
CN101963993A (en) Method for fast searching database sheet table record
TW583542B (en) NUMA page selection using coloring
CN100511152C (en) Method for managing priority queue
WO2024055571A1 (en) Namespace setting method and apparatus, and readable storage medium
JP5011311B2 (en) Method and mechanism for loading an XML document into memory
CN100433009C (en) Management and maintenance method of static range matching table
CN116996449A (en) Data message processing system, method, computer equipment, storage medium
CN107003932A (en) The CACHE DIRECTORY processing method and contents controller of multi-core processor system
CN112068948B (en) Data hashing method, readable storage medium and electronic device
CN103514185B (en) The database file access management method and device of the multiple update area of navigation map
CN110781101A (en) One-to-many mapping relation storage method and device, electronic equipment and medium
CN107085571B (en) A method and device for executing a verification rule
CN112486400A (en) Method, apparatus and computer program product for managing indexes of storage system
CN111984430A (en) Many-to-many process communication method and computer readable storage medium
CN117632433A (en) Method, device equipment and storage medium for improving interrupt response speed
CN115809125A (en) TCAM asynchronous moving method based on multitask

Legal Events

Date Code Title Description
C06 Publication
PB01 Publication
C10 Entry into substantive examination
SE01 Entry into force of request for substantive examination
C14 Grant of patent or utility model
GR01 Patent grant
CX01 Expiry of patent term
CX01 Expiry of patent term

Granted publication date: 20090708